You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
5.0 KiB

6 months ago
package service
import (
"bkb-seller/global"
"bkb-seller/model"
"bkb-seller/model/request"
"bkb-seller/model/response"
"errors"
"strconv"
"gorm.io/gorm"
)
//@function: CreateAuthority
//@description: 创建一个角色
//@param: auth model.SysAuthority
//@return: err error, authority model.SysAuthority
func CreateAuthority(auth model.SysAuthority) (err error, authority model.SysAuthority) {
err = global.MG_DB.Create(&auth).Error
return err, auth
}
//@function: CopyAuthority
//@description: 复制一个角色
//@param: copyInfo response.SysAuthorityCopyResponse
//@return: err error, authority model.SysAuthority
func CopyAuthority(copyInfo response.SysAuthorityCopyResponse) (err error, authority model.SysAuthority) {
var authorityBox model.SysAuthority
if !errors.Is(global.MG_DB.Where("authority_id = ?", copyInfo.Authority.AuthorityId).First(&authorityBox).Error, gorm.ErrRecordNotFound) {
return errors.New("存在相同角色id"), authority
}
copyInfo.Authority.Children = []model.SysAuthority{}
err, menus := GetMenuAuthority(copyInfo.OldAuthorityId, copyInfo.Authority.Appid)
var baseMenu []model.SysBaseMenu
for _, v := range menus {
//intNum, _ := strconv.Atoi(v.ID)
intNum := int(v.ID)
v.SysBaseMenu.ID = uint(intNum)
baseMenu = append(baseMenu, v.SysBaseMenu)
}
copyInfo.Authority.SysBaseMenus = baseMenu
err = global.MG_DB.Create(&copyInfo.Authority).Error
paths := GetPolicyPathByAuthorityId(copyInfo.OldAuthorityId)
err = UpdateCasbin(copyInfo.Authority.AuthorityId, copyInfo.Authority.Appid, paths)
if err != nil {
_ = DeleteAuthority(&copyInfo.Authority)
}
return err, copyInfo.Authority
}
//@function: UpdateAuthority
//@description: 更改一个角色
//@param: auth model.SysAuthority
//@return: err error, authority model.SysAuthority
func UpdateAuthority(auth model.SysAuthority) (err error, authority model.SysAuthority) {
err = global.MG_DB.Where("authority_id = ?", auth.AuthorityId).First(&model.SysAuthority{}).Updates(&auth).Error
return err, auth
}
//@function: DeleteAuthority
//@description: 删除角色
//@param: auth *model.SysAuthority
//@return: err error
func DeleteAuthority(auth *model.SysAuthority) (err error) {
// if !errors.Is(global.MG_DB.Where("sys_authority_id = ?", auth.AuthorityId).First(&model.SysUserAuthority{}).Error, gorm.ErrRecordNotFound) {
// return errors.New("此角色有用户正在使用禁止删除")
// }
if !errors.Is(global.MG_DB.Where("parent_id = ?", auth.AuthorityId).First(&model.SysAuthority{}).Error, gorm.ErrRecordNotFound) {
return errors.New("此角色存在子角色不允许删除")
}
db := global.MG_DB.Preload("SysBaseMenus").Where("authority_id = ?", auth.AuthorityId).First(auth)
err = db.Unscoped().Delete(auth).Error
if len(auth.SysBaseMenus) > 0 {
err = global.MG_DB.Model(auth).Association("SysBaseMenus").Delete(auth.SysBaseMenus)
//err = db.Association("SysBaseMenus").Delete(&auth)
} else {
err = db.Error
}
ClearCasbin(1, auth.Appid, strconv.Itoa(int(auth.AuthorityId)))
return err
}
//@function: GetAuthorityInfoList
//@description: 分页获取数据
//@param: info request.PageInfo
//@return: err error, list interface{}, total int64
func GetAuthorityInfoList(info request.PageInfo, appid string) (err error, list interface{}, total int64) {
limit := info.PageSize
offset := info.PageSize * (info.Page - 1)
db := global.MG_DB
var authority []model.SysAuthority
err = db.Limit(limit).Offset(offset).Where("parent_id = 0 and appid=? and type=?", appid, "seller").Find(&authority).Error
if len(authority) > 0 {
for k := range authority {
err = findChildrenAuthority(&authority[k])
}
}
return err, authority, total
}
//@function: GetAuthorityInfo
//@description: 获取所有角色信息
//@param: auth model.SysAuthority
//@return: err error, sa model.SysAuthority
func GetAuthorityInfo(auth model.SysAuthority) (err error, sa model.SysAuthority) {
err = global.MG_DB.Preload("DataAuthorityId").Where("authority_id = ?", auth.AuthorityId).First(&sa).Error
return err, sa
}
//@function: SetMenuAuthority
//@description: 菜单与角色绑定
//@param: auth *model.SysAuthority
//@return: error
func SetMenuAuthority(auth *model.SysAuthority) error {
//清除原有角色与菜单
ClearCasbin(0, "menu", auth.Appid, strconv.Itoa(int(auth.AuthorityId)))
//更新casbin
rules := [][]string{}
for _, v := range auth.SysBaseMenus {
rules = append(rules, []string{"menu", auth.Appid, strconv.Itoa(int(auth.AuthorityId)), v.Path, "*", "*"})
}
e := Casbin()
success, _ := e.AddPolicies(rules)
if success == false {
return errors.New("存在相同menu,添加失败,请联系管理员")
}
return nil
}
//@function: findChildrenAuthority
//@description: 查询子角色
//@param: authority *model.SysAuthority
//@return: err error
func findChildrenAuthority(authority *model.SysAuthority) (err error) {
err = global.MG_DB.Preload("DataAuthorityId").Where("parent_id = ?", authority.AuthorityId).Find(&authority.Children).Error
if len(authority.Children) > 0 {
for k := range authority.Children {
err = findChildrenAuthority(&authority.Children[k])
}
}
return err
}