fix
This commit is contained in:
@@ -2,12 +2,43 @@ package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/dusiburg/kotosoft-id/internal/models"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const adminPositionCode = "PRESIDENT"
|
||||
func (s *Service) GetAllPermissions(ctx context.Context) ([]models.Permission, error) {
|
||||
return s.positions.GetAllPermissions(ctx)
|
||||
}
|
||||
|
||||
func (s *Service) AddPermissionToPosition(ctx context.Context, positionCode, permissionCode string) error {
|
||||
position, err := s.positions.GetPositionByCode(ctx, positionCode)
|
||||
if err != nil {
|
||||
return models.ErrNotFound
|
||||
}
|
||||
|
||||
permission, err := s.positions.GetPermissionByCode(ctx, permissionCode)
|
||||
if err != nil {
|
||||
return models.ErrNotFound
|
||||
}
|
||||
|
||||
return s.positions.AddPermissionToPosition(ctx, position.ID, permission.ID)
|
||||
}
|
||||
|
||||
func (s *Service) RemovePermissionFromPosition(ctx context.Context, positionCode, permissionCode string) error {
|
||||
position, err := s.positions.GetPositionByCode(ctx, positionCode)
|
||||
if err != nil {
|
||||
return models.ErrNotFound
|
||||
}
|
||||
|
||||
permission, err := s.positions.GetPermissionByCode(ctx, permissionCode)
|
||||
if err != nil {
|
||||
return models.ErrNotFound
|
||||
}
|
||||
|
||||
return s.positions.RemovePermissionFromPosition(ctx, position.ID, permission.ID)
|
||||
}
|
||||
|
||||
func (s *Service) GetUserPositions(ctx context.Context, userID uuid.UUID, locale string) ([]models.PositionLocalized, error) {
|
||||
if locale == "" {
|
||||
@@ -30,6 +61,74 @@ func (s *Service) UpsertPositionForLocale(ctx context.Context, code, locale, nam
|
||||
return s.positions.UpsertPositionForLocale(ctx, code, locale, name, description)
|
||||
}
|
||||
|
||||
func (s *Service) UserIsPositionsAdmin(ctx context.Context, userID uuid.UUID) (bool, error) {
|
||||
return s.positions.UserHasPosition(ctx, userID, adminPositionCode)
|
||||
func (s *Service) AssignPosition(ctx context.Context, adminID, targetUserID uuid.UUID, input models.AssignPositionInput) error {
|
||||
position, err := s.positions.GetPositionByCode(ctx, input.PositionCode)
|
||||
if err != nil {
|
||||
return models.ErrNotFound
|
||||
}
|
||||
|
||||
validFrom := input.ValidFrom
|
||||
if validFrom.IsZero() {
|
||||
validFrom = time.Now()
|
||||
}
|
||||
|
||||
return s.positions.AssignPosition(ctx, targetUserID, position.ID, &adminID, validFrom, (*interface{})(nil))
|
||||
}
|
||||
|
||||
func (s *Service) RevokePosition(ctx context.Context, targetUserID uuid.UUID, positionCode string) error {
|
||||
position, err := s.positions.GetPositionByCode(ctx, positionCode)
|
||||
if err != nil {
|
||||
return models.ErrNotFound
|
||||
}
|
||||
|
||||
return s.positions.RevokePosition(ctx, targetUserID, position.ID)
|
||||
}
|
||||
|
||||
func (s *Service) UserHasPermission(ctx context.Context, userID uuid.UUID, permissionCode string) (bool, error) {
|
||||
allowed, _, err := s.positions.UserHasPermission(ctx, userID, permissionCode)
|
||||
return allowed, err
|
||||
}
|
||||
|
||||
type IntrospectResponse struct {
|
||||
Active bool `json:"active"`
|
||||
Sub string `json:"sub"`
|
||||
IsAdmin bool `json:"is_admin"`
|
||||
Positions []models.PositionLocalized `json:"positions"`
|
||||
Permissions []models.Permission `json:"permissions"`
|
||||
}
|
||||
|
||||
func (s *Service) IntrospectToken(ctx context.Context, token string) (*IntrospectResponse, error) {
|
||||
info, err := s.ValidateAccessToken(ctx, token)
|
||||
if err != nil {
|
||||
return &IntrospectResponse{Active: false}, nil
|
||||
}
|
||||
|
||||
user, err := s.users.GetUserByID(ctx, info.UserID)
|
||||
if err != nil {
|
||||
return &IntrospectResponse{Active: false}, nil
|
||||
}
|
||||
|
||||
positions, err := s.positions.GetUserPositions(ctx, user.ID, "en")
|
||||
if err != nil {
|
||||
return &IntrospectResponse{Active: false}, nil
|
||||
}
|
||||
if positions == nil {
|
||||
positions = []models.PositionLocalized{}
|
||||
}
|
||||
|
||||
allPerms, err := s.positions.GetUserPermissions(ctx, user.ID)
|
||||
if err != nil {
|
||||
return &IntrospectResponse{Active: false}, nil
|
||||
}
|
||||
if allPerms == nil {
|
||||
allPerms = []models.Permission{}
|
||||
}
|
||||
|
||||
return &IntrospectResponse{
|
||||
Active: true,
|
||||
Sub: user.ID.String(),
|
||||
IsAdmin: user.IsAdmin,
|
||||
Positions: positions,
|
||||
Permissions: allPerms,
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user