|
@@ -31,6 +31,10 @@ type IRule interface {
|
|
|
GetIsButtonList(ctx context.Context) ([]*model.SysAuthRuleInfoRes, error)
|
|
GetIsButtonList(ctx context.Context) ([]*model.SysAuthRuleInfoRes, error)
|
|
|
Add(ctx context.Context, req *system.RuleAddReq) (err error)
|
|
Add(ctx context.Context, req *system.RuleAddReq) (err error)
|
|
|
Get(ctx context.Context, id uint) (rule *entity.SysAuthRule, err error)
|
|
Get(ctx context.Context, id uint) (rule *entity.SysAuthRule, err error)
|
|
|
|
|
+ GetMenuRoles(ctx context.Context, id uint) (roleIds []uint, err error)
|
|
|
|
|
+ Update(ctx context.Context, req *system.RuleUpdateReq) (err error)
|
|
|
|
|
+ GetMenuListSearch(ctx context.Context, req *system.RuleSearchReq) (res []*model.SysAuthRuleInfoRes, err error)
|
|
|
|
|
+ GetMenuListTree(pid uint, list []*model.SysAuthRuleInfoRes) []*model.SysAuthRuleTreeRes
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
type ruleImpl struct {
|
|
type ruleImpl struct {
|
|
@@ -42,6 +46,21 @@ func Rule() IRule {
|
|
|
return IRule(&rule)
|
|
return IRule(&rule)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+func (s *ruleImpl) GetMenuListSearch(ctx context.Context, req *system.RuleSearchReq) (res []*model.SysAuthRuleInfoRes, err error) {
|
|
|
|
|
+ err = g.Try(func() {
|
|
|
|
|
+ m := dao.SysAuthRule.Ctx(ctx)
|
|
|
|
|
+ if req.Title != "" {
|
|
|
|
|
+ m = m.Where("title like ?", "%"+req.Title+"%")
|
|
|
|
|
+ }
|
|
|
|
|
+ if req.Component != "" {
|
|
|
|
|
+ m = m.Where("component like ?", "%"+req.Component+"%")
|
|
|
|
|
+ }
|
|
|
|
|
+ err = m.Fields(model.SysAuthRuleInfoRes{}).Order("weigh desc,id asc").Scan(&res)
|
|
|
|
|
+ liberr.ErrIsNil(ctx, err, "获取菜单失败")
|
|
|
|
|
+ })
|
|
|
|
|
+ return
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
// GetIsMenuList 获取isMenu=0|1
|
|
// GetIsMenuList 获取isMenu=0|1
|
|
|
func (s *ruleImpl) GetIsMenuList(ctx context.Context) ([]*model.SysAuthRuleInfoRes, error) {
|
|
func (s *ruleImpl) GetIsMenuList(ctx context.Context) ([]*model.SysAuthRuleInfoRes, error) {
|
|
|
list, err := s.GetMenuList(ctx)
|
|
list, err := s.GetMenuList(ctx)
|
|
@@ -139,7 +158,7 @@ func (s *ruleImpl) Add(ctx context.Context, req *system.RuleAddReq) (err error)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//检查菜单规则是否存在
|
|
//检查菜单规则是否存在
|
|
|
-func (s *ruleImpl) menuNameExists(ctx context.Context, name string, id int) bool {
|
|
|
|
|
|
|
+func (s *ruleImpl) menuNameExists(ctx context.Context, name string, id uint) bool {
|
|
|
m := dao.SysAuthRule.Ctx(ctx).Where("name=?", name)
|
|
m := dao.SysAuthRule.Ctx(ctx).Where("name=?", name)
|
|
|
if id != 0 {
|
|
if id != 0 {
|
|
|
m = m.Where("id!=?", id)
|
|
m = m.Where("id!=?", id)
|
|
@@ -172,3 +191,90 @@ func (s *ruleImpl) Get(ctx context.Context, id uint) (rule *entity.SysAuthRule,
|
|
|
})
|
|
})
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+func (s *ruleImpl) GetMenuRoles(ctx context.Context, id uint) (roleIds []uint, err error) {
|
|
|
|
|
+ err = g.Try(func() {
|
|
|
|
|
+ enforcer, e := commonService.CasbinEnforcer(ctx)
|
|
|
|
|
+ liberr.ErrIsNil(ctx, e)
|
|
|
|
|
+ policies := enforcer.GetFilteredNamedPolicy("p", 1, gconv.String(id))
|
|
|
|
|
+ for _, policy := range policies {
|
|
|
|
|
+ roleIds = append(roleIds, gconv.Uint(policy[0]))
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ return
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func (s *ruleImpl) Update(ctx context.Context, req *system.RuleUpdateReq) (err error) {
|
|
|
|
|
+ if s.menuNameExists(ctx, req.Name, req.Id) {
|
|
|
|
|
+ err = gerror.New("接口规则已经存在")
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+ err = g.DB().Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
|
|
|
|
|
+ err = g.Try(func() {
|
|
|
|
|
+ //菜单数据
|
|
|
|
|
+ data := do.SysAuthRule{
|
|
|
|
|
+ Pid: req.Pid,
|
|
|
|
|
+ Name: req.Name,
|
|
|
|
|
+ Title: req.Title,
|
|
|
|
|
+ Icon: req.Icon,
|
|
|
|
|
+ Condition: req.Condition,
|
|
|
|
|
+ Remark: req.Remark,
|
|
|
|
|
+ MenuType: req.MenuType,
|
|
|
|
|
+ Weigh: req.Weigh,
|
|
|
|
|
+ IsHide: req.IsHide,
|
|
|
|
|
+ Path: req.Path,
|
|
|
|
|
+ Component: req.Component,
|
|
|
|
|
+ IsLink: req.IsLink,
|
|
|
|
|
+ IsIframe: req.IsIframe,
|
|
|
|
|
+ IsCached: req.IsCached,
|
|
|
|
|
+ Redirect: req.Redirect,
|
|
|
|
|
+ IsAffix: req.IsAffix,
|
|
|
|
|
+ LinkUrl: req.LinkUrl,
|
|
|
|
|
+ }
|
|
|
|
|
+ _, e := dao.SysAuthRule.Ctx(ctx).TX(tx).WherePri(req.Id).Update(data)
|
|
|
|
|
+ liberr.ErrIsNil(ctx, e, "添加菜单失败")
|
|
|
|
|
+ e = s.UpdateRoleRule(ctx, req.Id, req.Roles)
|
|
|
|
|
+ liberr.ErrIsNil(ctx, e, "添加菜单失败")
|
|
|
|
|
+ })
|
|
|
|
|
+ return err
|
|
|
|
|
+ })
|
|
|
|
|
+ if err == nil {
|
|
|
|
|
+ // 删除相关缓存
|
|
|
|
|
+ commonService.Cache(ctx).RemoveByTag(ctx, consts.CacheSysAuthTag)
|
|
|
|
|
+ }
|
|
|
|
|
+ return
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func (s *ruleImpl) UpdateRoleRule(ctx context.Context, ruleId uint, roleIds []uint) (err error) {
|
|
|
|
|
+ err = g.Try(func() {
|
|
|
|
|
+ enforcer, e := service.CasbinEnforcer(ctx)
|
|
|
|
|
+ liberr.ErrIsNil(ctx, e)
|
|
|
|
|
+ //删除旧权限
|
|
|
|
|
+ _, e = enforcer.RemoveFilteredPolicy(1, gconv.String(ruleId))
|
|
|
|
|
+ liberr.ErrIsNil(ctx, e)
|
|
|
|
|
+ // 添加新权限
|
|
|
|
|
+ roleIdsStrArr := gconv.Strings(roleIds)
|
|
|
|
|
+ for _, v := range roleIdsStrArr {
|
|
|
|
|
+ _, e = enforcer.AddPolicy(v, gconv.String(ruleId), "All")
|
|
|
|
|
+ liberr.ErrIsNil(ctx, e)
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ return
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func (s *ruleImpl) GetMenuListTree(pid uint, list []*model.SysAuthRuleInfoRes) []*model.SysAuthRuleTreeRes {
|
|
|
|
|
+ tree := make([]*model.SysAuthRuleTreeRes, 0, len(list))
|
|
|
|
|
+ for _, menu := range list {
|
|
|
|
|
+ if menu.Pid == pid {
|
|
|
|
|
+ t := &model.SysAuthRuleTreeRes{
|
|
|
|
|
+ SysAuthRuleInfoRes: menu,
|
|
|
|
|
+ }
|
|
|
|
|
+ child := s.GetMenuListTree(menu.Id, list)
|
|
|
|
|
+ if child != nil {
|
|
|
|
|
+ t.Children = child
|
|
|
|
|
+ }
|
|
|
|
|
+ tree = append(tree, t)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return tree
|
|
|
|
|
+}
|