package service import ( "aps_crm/model" "aps_crm/pkg/ecode" "github.com/flipped-aurora/gin-vue-admin/server/global" "github.com/flipped-aurora/gin-vue-admin/server/model/system" "strconv" ) type AuthorityService struct{} var AuthorityServiceApp = new(AuthorityService) // CreateAuthority create authority and return authority id and error code func (authorityService *AuthorityService) CreateAuthority(auth model.Authority) (int, uint) { // check if authority name exists err := model.NewSysAuthoritySearch().Create(&auth) if err != nil { return ecode.RoleExist, 0 } return ecode.OK, auth.Id } func (authorityService *AuthorityService) UpdateAuthority(auth model.Authority) int { _, err := model.NewSysAuthoritySearch().SetAuthorityId(auth.Id).Find() if err != nil { return ecode.RoleNotExist } err = model.NewSysAuthoritySearch().SetAuthorityId(auth.Id).Update(&auth) return ecode.OK } func (authorityService *AuthorityService) DeleteAuthority(auth *model.Authority) int { _, err := model.NewSysAuthoritySearch().SetAuthorityId(auth.Id).Find() if err != nil { return ecode.RoleNotExist } if len(auth.Users) != 0 { return ecode.RoleDeleteErr1 } authorityId := strconv.Itoa(int(auth.Id)) CasbinServiceApp.ClearCasbin(0, authorityId) return ecode.OK } //@author: [piexlmax](https://github.com/piexlmax) //@function: GetAuthorityInfo //@description: 获取所有角色信息 //@param: auth model.Authority //@return: sa system.Authority, err error func (authorityService *AuthorityService) GetAuthorityInfo(auth system.SysAuthority) (sa system.SysAuthority, err error) { err = global.GVA_DB.Preload("DataAuthorityId").Where("authority_id = ?", auth.AuthorityId).First(&sa).Error return sa, err } //@author: [piexlmax](https://github.com/piexlmax) //@function: SetMenuAuthority //@description: 设置角色资源权限 //@param: auth model.Authority //@return: error func (authorityService *AuthorityService) SetDataAuthority(auth system.SysAuthority) error { var s system.SysAuthority global.GVA_DB.Preload("DataAuthorityId").First(&s, "authority_id = ?", auth.AuthorityId) err := global.GVA_DB.Model(&s).Association("DataAuthorityId").Replace(&auth.DataAuthorityId) return err } //@author: [piexlmax](https://github.com/piexlmax) //@function: SetMenuAuthority //@description: 菜单与角色绑定 //@param: auth *model.Authority //@return: error func (authorityService *AuthorityService) SetMenuAuthority(auth *model.Authority) error { return model.NewSysAuthoritySearch().SetMenuAuthority(auth) }