template-monorepo/internal/logic/permission/get_me_permissions_logic.go

54 lines
1.4 KiB
Go
Raw Normal View History

package permission
import (
"context"
"gateway/internal/svc"
"gateway/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type GetMePermissionsLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// NewGetMePermissionsLogic returns the "what can I see" reader.
func NewGetMePermissionsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetMePermissionsLogic {
return &GetMePermissionsLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// GetMePermissions returns the role + permission map for the current user.
func (l *GetMePermissionsLogic) GetMePermissions(req *types.MePermissionsQuery) (*types.MePermissionsData, error) {
if l.svcCtx.PermissionAuthQuery == nil {
return nil, errb.SysNotImplemented("permission module not configured")
}
actor, err := ActorFromContext(l.ctx)
if err != nil {
return nil, errb.AuthUnauthorized(err.Error()).WithCause(err)
}
resp, err := l.svcCtx.PermissionAuthQuery.Me(l.ctx, actor.TenantID, actor.UID, req.IncludeTree)
if err != nil {
return nil, err
}
out := &types.MePermissionsData{
UID: resp.UID,
TenantID: resp.TenantID,
Roles: resp.Roles,
Permissions: make(map[string]string, len(resp.Permissions)),
}
for name, status := range resp.Permissions {
out.Permissions[name] = status.String()
}
if req.IncludeTree {
out.Tree = mapNodes(resp.Tree)
}
return out, nil
}