作者 yangfu

用户更新当前组织验证、菜单路径返回

@@ -5,11 +5,13 @@ import ( @@ -5,11 +5,13 @@ import (
5 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain" 5 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
6 ) 6 )
7 7
  8 +/***** 1.快速模块 *****/
  9 +
8 // FastPgUser 快速返回领域用户 10 // FastPgUser 快速返回领域用户
9 // 11 //
10 // transactionContext 事务 12 // transactionContext 事务
11 // userId 用户ID 13 // userId 用户ID
12 -func FastPgUser(transactionContext application.TransactionContext, userId int64) (domain.UserRepository, *domain.User, error) { 14 +func FastPgUser(transactionContext application.TransactionContext, userId int64, options ...option) (domain.UserRepository, *domain.User, error) {
13 var rep domain.UserRepository 15 var rep domain.UserRepository
14 var mod *domain.User 16 var mod *domain.User
15 var err error 17 var err error
@@ -28,6 +30,9 @@ func FastPgUser(transactionContext application.TransactionContext, userId int64) @@ -28,6 +30,9 @@ func FastPgUser(transactionContext application.TransactionContext, userId int64)
28 return nil, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 30 return nil, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
29 } 31 }
30 } 32 }
  33 + if err = fastPgDataAuth(transactionContext, mod, options...); err != nil {
  34 + return nil, nil, err
  35 + }
31 return rep, mod, err 36 return rep, mod, err
32 } 37 }
33 38
@@ -61,7 +66,7 @@ func FastPgUserBase(transactionContext application.TransactionContext, userBaseI @@ -61,7 +66,7 @@ func FastPgUserBase(transactionContext application.TransactionContext, userBaseI
61 // 66 //
62 // transactionContext 事务 67 // transactionContext 事务
63 // roleId 角色Id 68 // roleId 角色Id
64 -func FastPgRole(transactionContext application.TransactionContext, roleId int64) (domain.RoleRepository, *domain.Role, error) { 69 +func FastPgRole(transactionContext application.TransactionContext, roleId int64, options ...option) (domain.RoleRepository, *domain.Role, error) {
65 var rep domain.RoleRepository 70 var rep domain.RoleRepository
66 var mod *domain.Role 71 var mod *domain.Role
67 var err error 72 var err error
@@ -80,6 +85,9 @@ func FastPgRole(transactionContext application.TransactionContext, roleId int64) @@ -80,6 +85,9 @@ func FastPgRole(transactionContext application.TransactionContext, roleId int64)
80 return nil, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 85 return nil, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
81 } 86 }
82 } 87 }
  88 + if err = fastPgDataAuth(transactionContext, mod, options...); err != nil {
  89 + return nil, nil, err
  90 + }
83 return rep, mod, err 91 return rep, mod, err
84 } 92 }
85 93
@@ -213,6 +221,49 @@ func FastPgAccountDestroyRecord(transactionContext application.TransactionContex @@ -213,6 +221,49 @@ func FastPgAccountDestroyRecord(transactionContext application.TransactionContex
213 return rep, mod, err 221 return rep, mod, err
214 } 222 }
215 223
216 -func FastPgDataAuth(transactionContext application.TransactionContext, operateInfo *domain.OperateInfo) error { 224 +// fastPgDataAuth 快速数据权限验证
  225 +//
  226 +// data 待认证的数据
  227 +// options 配置项
  228 +func fastPgDataAuth(transactionContext application.TransactionContext, data domain.AuthedData, options ...option) error {
  229 + option := NewFastOptions(options...)
  230 + if option.DataAuthRequired && data != nil {
  231 + if data.BelongOrg() != option.OperateInfo.OrgId {
  232 + return application.ThrowError(application.BUSINESS_ERROR, "当前登录的组织机构与操作数据组织机构不一致")
  233 + }
  234 + }
217 return nil 235 return nil
218 } 236 }
  237 +
  238 +/***** 2.配置 *****/
  239 +
  240 +type FastOptions struct {
  241 + DataAuthRequired bool
  242 + OperateInfo *domain.OperateInfo
  243 +}
  244 +
  245 +func NewFastOptions(options ...option) *FastOptions {
  246 + o := &FastOptions{
  247 + DataAuthRequired: false,
  248 + }
  249 + for i := 0; i < len(options); i++ {
  250 + options[i](o)
  251 + }
  252 + return o
  253 +}
  254 +
  255 +type option func(options *FastOptions)
  256 +
  257 +// 需要数据权限
  258 +func WithDataAuthRequired() option {
  259 + return func(options *FastOptions) {
  260 + options.DataAuthRequired = true
  261 + }
  262 +}
  263 +
  264 +// WithOperator 操作人
  265 +func WithOperator(op *domain.OperateInfo) option {
  266 + return func(options *FastOptions) {
  267 + options.OperateInfo = op
  268 + }
  269 +}
@@ -144,7 +144,9 @@ func (orgService *OrgService) GetOrgSubDepartment(getOrgSubDepartmentQuery *quer @@ -144,7 +144,9 @@ func (orgService *OrgService) GetOrgSubDepartment(getOrgSubDepartmentQuery *quer
144 }() 144 }()
145 145
146 orgRepository, org, err := factory.FastPgOrg(transactionContext, getOrgSubDepartmentQuery.OrgId) 146 orgRepository, org, err := factory.FastPgOrg(transactionContext, getOrgSubDepartmentQuery.OrgId)
147 - 147 + if err != nil {
  148 + return nil, err
  149 + }
148 _, orgs, err := orgRepository.Find(map[string]interface{}{"companyId": org.CompanyId}) 150 _, orgs, err := orgRepository.Find(map[string]interface{}{"companyId": org.CompanyId})
149 if err != nil { 151 if err != nil {
150 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 152 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
@@ -29,7 +29,7 @@ type Menu struct { @@ -29,7 +29,7 @@ type Menu struct {
29 // 菜单类别 (web:1、app:2) 29 // 菜单类别 (web:1、app:2)
30 //Category string `json:"category,omitempty"` 30 //Category string `json:"category,omitempty"`
31 // 路径节点路径("0,11,12,") 31 // 路径节点路径("0,11,12,")
32 - //ParentPath string `json:"parentPath,omitempty"` 32 + ParentPath string `json:"parentPath,omitempty"`
33 // 菜单是否公开状态,[1:显示],[2:隐藏],默认显示 33 // 菜单是否公开状态,[1:显示],[2:隐藏],默认显示
34 //IsPublish int `json:"isPublish,omitempty"` 34 //IsPublish int `json:"isPublish,omitempty"`
35 // 启用状态(启用:1 禁用:2),默认启用 35 // 启用状态(启用:1 禁用:2),默认启用
@@ -48,6 +48,7 @@ func (dto *UserAccessMenuDto) LoadDto(menus []*domain.Menu) error { @@ -48,6 +48,7 @@ func (dto *UserAccessMenuDto) LoadDto(menus []*domain.Menu) error {
48 Icon: v.Icon, 48 Icon: v.Icon,
49 Sort: v.Sort, 49 Sort: v.Sort,
50 EnableStatus: v.EnableStatus, 50 EnableStatus: v.EnableStatus,
  51 + ParentPath: v.ParentPath,
51 }) 52 })
52 } 53 }
53 54
@@ -623,7 +623,7 @@ func (userService *UserService) UpdateUser(updateUserCommand *command.UpdateUser @@ -623,7 +623,7 @@ func (userService *UserService) UpdateUser(updateUserCommand *command.UpdateUser
623 defer func() { 623 defer func() {
624 transactionContext.RollbackTransaction() 624 transactionContext.RollbackTransaction()
625 }() 625 }()
626 - _, user, err := factory.FastPgUser(transactionContext, updateUserCommand.UserId) 626 + _, user, err := factory.FastPgUser(transactionContext, updateUserCommand.UserId, factory.WithDataAuthRequired(), factory.WithOperator(updateUserCommand.OperateInfo))
627 if err != nil { 627 if err != nil {
628 return nil, err 628 return nil, err
629 } 629 }
@@ -141,3 +141,8 @@ func (m *Role) CacheKeyFunc() string { @@ -141,3 +141,8 @@ func (m *Role) CacheKeyFunc() string {
141 } 141 }
142 return fmt.Sprintf("%v:cache:role:id:%v", constant.CACHE_PREFIX, m.RoleId) 142 return fmt.Sprintf("%v:cache:role:id:%v", constant.CACHE_PREFIX, m.RoleId)
143 } 143 }
  144 +
  145 +/***** 3.实现接口 AuthedData *****/
  146 +func (m *Role) BelongOrg() int64 {
  147 + return m.OrgId
  148 +}
@@ -240,3 +240,8 @@ func (user *User) CacheKeyFunc() string { @@ -240,3 +240,8 @@ func (user *User) CacheKeyFunc() string {
240 } 240 }
241 return fmt.Sprintf("%v:cache:users:id:%v", constant.CACHE_PREFIX, user.UserId) 241 return fmt.Sprintf("%v:cache:users:id:%v", constant.CACHE_PREFIX, user.UserId)
242 } 242 }
  243 +
  244 +/***** 3.实现接口 AuthedData *****/
  245 +func (user *User) BelongOrg() int64 {
  246 + return user.OrganizationId
  247 +}
@@ -17,6 +17,9 @@ type PgDataAuthService struct { @@ -17,6 +17,9 @@ type PgDataAuthService struct {
17 // options 数据参数 17 // options 数据参数
18 // data 需要验证权限的数据 18 // data 需要验证权限的数据
19 func (ptr *PgDataAuthService) DataAuth(options domain.OperateInfo, data domain.AuthedData) error { 19 func (ptr *PgDataAuthService) DataAuth(options domain.OperateInfo, data domain.AuthedData) error {
  20 + if data.BelongOrg() != options.OrgId {
  21 + return fmt.Errorf("当前登录的组织机构与操作数据组织机构不一致")
  22 + }
20 return nil 23 return nil
21 } 24 }
22 25