[系统用户]实现系统用户的增删改

This commit is contained in:
刘祥超
2020-12-02 23:11:48 +08:00
parent 73e5d60326
commit ee296ad1c9
3 changed files with 244 additions and 7 deletions

View File

@@ -121,13 +121,19 @@ func (this *AdminDAO) UpdateAdminPassword(adminId int64, password string) error
}
// 创建管理员
func (this *AdminDAO) CreateAdmin(username string, password string, fullname string) (int64, error) {
func (this *AdminDAO) CreateAdmin(username string, password string, fullname string, isSuper bool, modulesJSON []byte) (int64, error) {
op := NewAdminOperator()
op.IsOn = true
op.State = AdminStateEnabled
op.Username = username
op.Password = stringutil.Md5(password)
op.Fullname = fullname
op.IsSuper = isSuper
if len(modulesJSON) > 0 {
op.Modules = modulesJSON
} else {
op.Modules = "[]"
}
_, err := this.Save(op)
if err != nil {
return 0, err
@@ -136,7 +142,7 @@ func (this *AdminDAO) CreateAdmin(username string, password string, fullname str
}
// 修改管理员个人资料
func (this *AdminDAO) UpdateAdmin(adminId int64, fullname string) error {
func (this *AdminDAO) UpdateAdminInfo(adminId int64, fullname string) error {
if adminId <= 0 {
return errors.New("invalid adminId")
}
@@ -147,6 +153,28 @@ func (this *AdminDAO) UpdateAdmin(adminId int64, fullname string) error {
return err
}
// 修改管理员详细信息
func (this *AdminDAO) UpdateAdmin(adminId int64, username string, password string, fullname string, isSuper bool, modulesJSON []byte) error {
if adminId <= 0 {
return errors.New("invalid adminId")
}
op := NewAdminOperator()
op.Id = adminId
op.Fullname = fullname
op.Username = username
if len(password) > 0 {
op.Password = stringutil.Md5(password)
}
op.IsSuper = isSuper
if len(modulesJSON) > 0 {
op.Modules = modulesJSON
} else {
op.Modules = "[]"
}
_, err := this.Save(op)
return err
}
// 检查用户名是否存在
func (this *AdminDAO) CheckAdminUsername(adminId int64, username string) (bool, error) {
query := this.Query().
@@ -172,3 +200,49 @@ func (this *AdminDAO) UpdateAdminLogin(adminId int64, username string, password
_, err := this.Save(op)
return err
}
// 修改管理员可以管理的模块
func (this *AdminDAO) UpdateAdminModules(adminId int64, allowModulesJSON []byte) error {
if adminId <= 0 {
return errors.New("invalid adminId")
}
op := NewAdminOperator()
op.Id = adminId
op.Modules = allowModulesJSON
_, err := this.Save(op)
if err != nil {
return err
}
return nil
}
// 查询所有管理的权限
func (this *AdminDAO) FindAllAdminModules() (result []*Admin, err error) {
_, err = this.Query().
State(AdminStateEnabled).
Attr("isOn", true).
Result("id", "modules", "isSuper").
Slice(&result).
FindAll()
return
}
// 计算所有管理员数量
func (this *AdminDAO) CountAllEnabledAdmins() (int64, error) {
return this.Query().
State(AdminStateEnabled).
Count()
}
// 列出单页的管理员
func (this *AdminDAO) ListEnabledAdmins(offset int64, size int64) (result []*Admin, err error) {
_, err = this.Query().
State(AdminStateEnabled).
Result("id", "isOn", "username", "fullname", "isSuper", "createdAt").
Offset(offset).
Limit(size).
DescPk().
Slice(&result).
FindAll()
return
}

View File

@@ -3,26 +3,28 @@ package models
// 管理员
type Admin struct {
Id uint32 `field:"id"` // ID
IsOn uint8 `field:"isOn"` // 是否启用
Username string `field:"username"` // 用户名
Password string `field:"password"` // 密码
Fullname string `field:"fullname"` // 全名
IsSuper uint8 `field:"isSuper"` // 是否为超级管理员
CreatedAt uint64 `field:"createdAt"` // 创建时间
UpdatedAt uint64 `field:"updatedAt"` // 修改时间
IsOn uint8 `field:"isOn"` // 是否启用
State uint8 `field:"state"` // 状态
Modules string `field:"modules"` // 允许的模块
}
type AdminOperator struct {
Id interface{} // ID
IsOn interface{} // 是否启用
Username interface{} // 用户名
Password interface{} // 密码
Fullname interface{} // 全名
IsSuper interface{} // 是否为超级管理员
CreatedAt interface{} // 创建时间
UpdatedAt interface{} // 修改时间
IsOn interface{} // 是否启用
State interface{} // 状态
Modules interface{} // 允许的模块
}
func NewAdminOperator() *AdminOperator {