mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-01-19 08:19:15 +08:00
fix: 解决数据库用户名超长导致用户创建失败的问题 (#2011)
This commit is contained in:
parent
cb1b6742d4
commit
7fac0fcbe8
@ -1,5 +1,11 @@
|
|||||||
package client
|
package client
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/1Panel-dev/1Panel/backend/utils/common"
|
||||||
|
)
|
||||||
|
|
||||||
type DBInfo struct {
|
type DBInfo struct {
|
||||||
From string `json:"from"`
|
From string `json:"from"`
|
||||||
Address string `json:"address"`
|
Address string `json:"address"`
|
||||||
@ -85,3 +91,16 @@ var formatMap = map[string]string{
|
|||||||
"gbk": "gbk_chinese_ci",
|
"gbk": "gbk_chinese_ci",
|
||||||
"big5": "big5_chinese_ci",
|
"big5": "big5_chinese_ci",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func loadNameByDB(name, version string) string {
|
||||||
|
if strings.HasPrefix(version, "5.6") {
|
||||||
|
if len(name) <= 16 {
|
||||||
|
return name
|
||||||
|
}
|
||||||
|
return strings.TrimSuffix(name[:10], "_") + "_" + common.RandStr(5)
|
||||||
|
}
|
||||||
|
if len(name) <= 32 {
|
||||||
|
return name
|
||||||
|
}
|
||||||
|
return strings.TrimSuffix(name[:25], "_") + "_" + common.RandStr(5)
|
||||||
|
}
|
||||||
|
@ -38,7 +38,7 @@ func (r *Local) Create(info CreateInfo) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := r.CreateUser(info); err != nil {
|
if err := r.CreateUser(info, true); err != nil {
|
||||||
_ = r.ExecSQL(fmt.Sprintf("drop database if exists `%s`", info.Name), info.Timeout)
|
_ = r.ExecSQL(fmt.Sprintf("drop database if exists `%s`", info.Name), info.Timeout)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -46,7 +46,7 @@ func (r *Local) Create(info CreateInfo) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Local) CreateUser(info CreateInfo) error {
|
func (r *Local) CreateUser(info CreateInfo, withDeleteDB bool) error {
|
||||||
var userlist []string
|
var userlist []string
|
||||||
if strings.Contains(info.Permission, ",") {
|
if strings.Contains(info.Permission, ",") {
|
||||||
ips := strings.Split(info.Permission, ",")
|
ips := strings.Split(info.Permission, ",")
|
||||||
@ -201,7 +201,7 @@ func (r *Local) ChangeAccess(info AccessChangeInfo) error {
|
|||||||
Password: info.Password,
|
Password: info.Password,
|
||||||
Permission: info.Permission,
|
Permission: info.Permission,
|
||||||
Timeout: info.Timeout,
|
Timeout: info.Timeout,
|
||||||
}); err != nil {
|
}, false); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := r.ExecSQL("flush privileges", 300); err != nil {
|
if err := r.ExecSQL("flush privileges", 300); err != nil {
|
||||||
@ -305,15 +305,16 @@ func (r *Local) SyncDB(version string) ([]SyncDBInfo, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(dataItem.Username) == 0 {
|
if len(dataItem.Username) == 0 {
|
||||||
|
username := loadNameByDB(parts[0], version)
|
||||||
if err := r.CreateUser(CreateInfo{
|
if err := r.CreateUser(CreateInfo{
|
||||||
Name: parts[0],
|
Name: parts[0],
|
||||||
Format: parts[1],
|
Format: parts[1],
|
||||||
Version: version,
|
Version: version,
|
||||||
Username: parts[0],
|
Username: username,
|
||||||
Password: common.RandStr(16),
|
Password: common.RandStr(16),
|
||||||
Permission: "%",
|
Permission: "%",
|
||||||
Timeout: 300,
|
Timeout: 300,
|
||||||
}); err != nil {
|
}, false); err != nil {
|
||||||
global.LOG.Errorf("sync from remote server failed, err: create user failed %v", err)
|
global.LOG.Errorf("sync from remote server failed, err: create user failed %v", err)
|
||||||
}
|
}
|
||||||
dataItem.Username = parts[0]
|
dataItem.Username = parts[0]
|
||||||
|
@ -39,7 +39,7 @@ func (r *Remote) Create(info CreateInfo) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := r.CreateUser(info); err != nil {
|
if err := r.CreateUser(info, true); err != nil {
|
||||||
_ = r.ExecSQL(fmt.Sprintf("drop database if exists `%s`", info.Name), info.Timeout)
|
_ = r.ExecSQL(fmt.Sprintf("drop database if exists `%s`", info.Name), info.Timeout)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -47,7 +47,7 @@ func (r *Remote) Create(info CreateInfo) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Remote) CreateUser(info CreateInfo) error {
|
func (r *Remote) CreateUser(info CreateInfo, withDeleteDB bool) error {
|
||||||
var userlist []string
|
var userlist []string
|
||||||
if strings.Contains(info.Permission, ",") {
|
if strings.Contains(info.Permission, ",") {
|
||||||
ips := strings.Split(info.Permission, ",")
|
ips := strings.Split(info.Permission, ",")
|
||||||
@ -62,15 +62,17 @@ func (r *Remote) CreateUser(info CreateInfo) error {
|
|||||||
|
|
||||||
for _, user := range userlist {
|
for _, user := range userlist {
|
||||||
if err := r.ExecSQL(fmt.Sprintf("create user %s identified by '%s';", user, info.Password), info.Timeout); err != nil {
|
if err := r.ExecSQL(fmt.Sprintf("create user %s identified by '%s';", user, info.Password), info.Timeout); err != nil {
|
||||||
_ = r.Delete(DeleteInfo{
|
if withDeleteDB {
|
||||||
Name: info.Name,
|
_ = r.Delete(DeleteInfo{
|
||||||
Version: info.Version,
|
Name: info.Name,
|
||||||
Username: info.Username,
|
Version: info.Version,
|
||||||
Permission: info.Permission,
|
Username: info.Username,
|
||||||
ForceDelete: true,
|
Permission: info.Permission,
|
||||||
Timeout: 300})
|
ForceDelete: true,
|
||||||
if strings.Contains(err.Error(), "ERROR 1396") {
|
Timeout: 300})
|
||||||
return buserr.New(constant.ErrUserIsExist)
|
if strings.Contains(err.Error(), "ERROR 1396") {
|
||||||
|
return buserr.New(constant.ErrUserIsExist)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -82,13 +84,15 @@ func (r *Remote) CreateUser(info CreateInfo) error {
|
|||||||
grantStr = fmt.Sprintf("%s identified by '%s' with grant option;", grantStr, info.Password)
|
grantStr = fmt.Sprintf("%s identified by '%s' with grant option;", grantStr, info.Password)
|
||||||
}
|
}
|
||||||
if err := r.ExecSQL(grantStr, info.Timeout); err != nil {
|
if err := r.ExecSQL(grantStr, info.Timeout); err != nil {
|
||||||
_ = r.Delete(DeleteInfo{
|
if withDeleteDB {
|
||||||
Name: info.Name,
|
_ = r.Delete(DeleteInfo{
|
||||||
Version: info.Version,
|
Name: info.Name,
|
||||||
Username: info.Username,
|
Version: info.Version,
|
||||||
Permission: info.Permission,
|
Username: info.Username,
|
||||||
ForceDelete: true,
|
Permission: info.Permission,
|
||||||
Timeout: 300})
|
ForceDelete: true,
|
||||||
|
Timeout: 300})
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -202,7 +206,7 @@ func (r *Remote) ChangeAccess(info AccessChangeInfo) error {
|
|||||||
Password: info.Password,
|
Password: info.Password,
|
||||||
Permission: info.Permission,
|
Permission: info.Permission,
|
||||||
Timeout: info.Timeout,
|
Timeout: info.Timeout,
|
||||||
}); err != nil {
|
}, false); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := r.ExecSQL("flush privileges", 300); err != nil {
|
if err := r.ExecSQL("flush privileges", 300); err != nil {
|
||||||
@ -312,16 +316,17 @@ func (r *Remote) SyncDB(version string) ([]SyncDBInfo, error) {
|
|||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
if len(dataItem.Username) == 0 {
|
if len(dataItem.Username) == 0 {
|
||||||
|
userName := loadNameByDB(dbName, version)
|
||||||
if err := r.CreateUser(CreateInfo{
|
if err := r.CreateUser(CreateInfo{
|
||||||
Name: dbName,
|
Name: dbName,
|
||||||
Format: charsetName,
|
Format: charsetName,
|
||||||
Version: version,
|
Version: version,
|
||||||
Username: dbName,
|
Username: userName,
|
||||||
Password: common.RandStr(16),
|
Password: common.RandStr(16),
|
||||||
Permission: "%",
|
Permission: "%",
|
||||||
Timeout: 300,
|
Timeout: 300,
|
||||||
}); err != nil {
|
}, false); err != nil {
|
||||||
global.LOG.Errorf("sync from remote server failed, err: create user failed %v", err)
|
return datas, fmt.Errorf("sync db from remote server failed, err: create user failed %v", err)
|
||||||
}
|
}
|
||||||
dataItem.Username = dbName
|
dataItem.Username = dbName
|
||||||
dataItem.Permission = "%"
|
dataItem.Permission = "%"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user