| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- package user
- import (
- "context"
- "time"
- authHelper "perms-system-server/internal/logic/auth"
- "perms-system-server/internal/middleware"
- "perms-system-server/internal/model/userperm"
- "perms-system-server/internal/response"
- "perms-system-server/internal/svc"
- "perms-system-server/internal/types"
- "github.com/zeromicro/go-zero/core/logx"
- "github.com/zeromicro/go-zero/core/stores/sqlx"
- )
- type SetUserPermsLogic struct {
- logx.Logger
- ctx context.Context
- svcCtx *svc.ServiceContext
- }
- func NewSetUserPermsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SetUserPermsLogic {
- return &SetUserPermsLogic{
- Logger: logx.WithContext(ctx),
- ctx: ctx,
- svcCtx: svcCtx,
- }
- }
- func (l *SetUserPermsLogic) SetUserPerms(req *types.SetPermsReq) error {
- if _, err := l.svcCtx.SysUserModel.FindOne(l.ctx, req.UserId); err != nil {
- return response.ErrNotFound("用户不存在")
- }
- productCode := middleware.GetProductCode(l.ctx)
- if err := authHelper.CheckManageAccess(l.ctx, l.svcCtx, req.UserId, productCode); err != nil {
- return err
- }
- if err := l.svcCtx.SysUserPermModel.TransactCtx(l.ctx, func(ctx context.Context, session sqlx.Session) error {
- if err := l.svcCtx.SysUserPermModel.DeleteByUserIdTx(ctx, session, req.UserId); err != nil {
- return err
- }
- if len(req.Perms) == 0 {
- return nil
- }
- now := time.Now().Unix()
- data := make([]*userperm.SysUserPerm, 0, len(req.Perms))
- for _, p := range req.Perms {
- data = append(data, &userperm.SysUserPerm{
- UserId: req.UserId,
- PermId: p.PermId,
- Effect: p.Effect,
- CreateTime: now,
- UpdateTime: now,
- })
- }
- return l.svcCtx.SysUserPermModel.BatchInsertWithTx(ctx, session, data)
- }); err != nil {
- return err
- }
- l.svcCtx.UserDetailsLoader.Del(l.ctx, req.UserId, productCode)
- return nil
- }
|