mirror of
https://github.com/go-admin-team/go-admin.git
synced 2026-09-21 02:04:09 +00:00
Control role_name and tole_key cannot be duplicate
This commit is contained in:
+27
-13
@@ -1,25 +1,26 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
orm "go-admin/database"
|
||||
"go-admin/tools"
|
||||
)
|
||||
|
||||
type SysRole struct {
|
||||
RoleId int `json:"roleId" gorm:"primary_key;AUTO_INCREMENT"` // 角色编码
|
||||
RoleName string `json:"roleName" gorm:"type:varchar(128);"` // 角色名称
|
||||
Status string `json:"status" gorm:"type:int(1);"` //
|
||||
RoleKey string `json:"roleKey" gorm:"type:varchar(128);"` //角色代码
|
||||
RoleSort int `json:"roleSort" gorm:"type:int(4);"` //角色排序
|
||||
Flag string `json:"flag" gorm:"type:varchar(128);"` //
|
||||
CreateBy string `json:"createBy" gorm:"type:varchar(128);"` //
|
||||
UpdateBy string `json:"updateBy" gorm:"type:varchar(128);"` //
|
||||
Remark string `json:"remark" gorm:"type:varchar(255);"` //备注
|
||||
Admin bool `json:"admin" gorm:"type:char(1);"`
|
||||
RoleId int `json:"roleId" gorm:"primary_key;AUTO_INCREMENT"` // 角色编码
|
||||
RoleName string `json:"roleName" gorm:"type:varchar(128);"` // 角色名称
|
||||
Status string `json:"status" gorm:"type:int(1);"` //
|
||||
RoleKey string `json:"roleKey" gorm:"type:varchar(128);"` //角色代码
|
||||
RoleSort int `json:"roleSort" gorm:"type:int(4);"` //角色排序
|
||||
Flag string `json:"flag" gorm:"type:varchar(128);"` //
|
||||
CreateBy string `json:"createBy" gorm:"type:varchar(128);"` //
|
||||
UpdateBy string `json:"updateBy" gorm:"type:varchar(128);"` //
|
||||
Remark string `json:"remark" gorm:"type:varchar(255);"` //备注
|
||||
Admin bool `json:"admin" gorm:"type:char(1);"`
|
||||
DataScope string `json:"dataScope" gorm:"type:varchar(128);"`
|
||||
Params string `json:"params" gorm:"-"`
|
||||
MenuIds []int `json:"menuIds" gorm:"-"`
|
||||
DeptIds []int `json:"deptIds" gorm:"-"`
|
||||
Params string `json:"params" gorm:"-"`
|
||||
MenuIds []int `json:"menuIds" gorm:"-"`
|
||||
DeptIds []int `json:"deptIds" gorm:"-"`
|
||||
BaseModel
|
||||
}
|
||||
|
||||
@@ -107,6 +108,11 @@ func (role *SysRole) GetRoleMeunId() ([]int, error) {
|
||||
}
|
||||
|
||||
func (role *SysRole) Insert() (id int, err error) {
|
||||
i := 0
|
||||
orm.Eloquent.Table("sys_role").Where("role_name=? or role_key = ?", role.RoleName, role.RoleKey).Count(&i)
|
||||
if i > 0 {
|
||||
return 0, errors.New("角色名称或者角色标识已经存在!")
|
||||
}
|
||||
role.UpdateBy = ""
|
||||
result := orm.Eloquent.Table("sys_role").Create(&role)
|
||||
if result.Error != nil {
|
||||
@@ -141,6 +147,14 @@ func (role *SysRole) Update(id int) (update SysRole, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
if role.RoleName != "" && role.RoleName != update.RoleName {
|
||||
return update, errors.New("角色名称不允许修改!")
|
||||
}
|
||||
|
||||
if role.RoleKey != "" && role.RoleKey != update.RoleKey {
|
||||
return update, errors.New("角色标识不允许修改!")
|
||||
}
|
||||
|
||||
//参数1:是要修改的数据
|
||||
//参数2:是修改的数据
|
||||
if err = orm.Eloquent.Table("sys_role").Model(&update).Updates(&role).Error; err != nil {
|
||||
|
||||
Reference in New Issue
Block a user