175 lines
5.0 KiB
Go
175 lines
5.0 KiB
Go
package services
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/dusiburg/kotosoft-id/internal/models"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func (s *Service) GetAllPermissions(ctx context.Context) ([]models.Permission, error) {
|
|
return s.positions.GetAllPermissions(ctx, "en")
|
|
}
|
|
|
|
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 == "" {
|
|
locale = "en"
|
|
}
|
|
return s.positions.GetUserPositions(ctx, userID, locale)
|
|
}
|
|
|
|
func (s *Service) GetAllPositions(ctx context.Context, locale string) ([]models.PositionLocalized, error) {
|
|
if locale == "" {
|
|
locale = "en"
|
|
}
|
|
return s.positions.GetAllPositions(ctx, locale)
|
|
}
|
|
|
|
func (s *Service) GetUsersWithPositions(ctx context.Context, locale string) ([]models.UserWithPositions, error) {
|
|
if locale == "" {
|
|
locale = "en"
|
|
}
|
|
return s.positions.GetUsersWithPositions(ctx, locale)
|
|
}
|
|
|
|
func (s *Service) UpsertPositionForLocale(ctx context.Context, code, locale, name string, description *string) error {
|
|
if locale == "" {
|
|
locale = "en"
|
|
}
|
|
return s.positions.UpsertPositionForLocale(ctx, code, locale, name, description)
|
|
}
|
|
|
|
func (s *Service) DeletePosition(ctx context.Context, code string) error {
|
|
return s.positions.DeletePositionByCode(ctx, code)
|
|
}
|
|
|
|
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, input.ValidTo)
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
if position.Code == "kotosoft_id_staff" {
|
|
return models.ErrForbidden
|
|
}
|
|
|
|
return s.positions.RevokePosition(ctx, targetUserID, position.ID)
|
|
}
|
|
|
|
func (s *Service) GetAllPermissionsForLocale(ctx context.Context, locale string) ([]models.Permission, error) {
|
|
if locale == "" {
|
|
locale = "en"
|
|
}
|
|
return s.positions.GetAllPermissions(ctx, locale)
|
|
}
|
|
|
|
func (s *Service) CreatePermission(ctx context.Context, code, locale string, description *string) error {
|
|
if locale == "" {
|
|
locale = "en"
|
|
}
|
|
return s.positions.CreatePermission(ctx, code, locale, description)
|
|
}
|
|
|
|
func (s *Service) UpdatePermission(ctx context.Context, code, locale string, description *string) error {
|
|
if locale == "" {
|
|
locale = "en"
|
|
}
|
|
return s.positions.UpdatePermission(ctx, code, locale, description)
|
|
}
|
|
|
|
func (s *Service) DeletePermission(ctx context.Context, code string) error {
|
|
return s.positions.DeletePermission(ctx, code)
|
|
}
|
|
|
|
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, "en")
|
|
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
|
|
}
|