162 lines
4.0 KiB
Go
162 lines
4.0 KiB
Go
|
|
package usecase
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"permission/reborn/domain/entity"
|
||
|
|
"permission/reborn/domain/errors"
|
||
|
|
"permission/reborn/domain/repository"
|
||
|
|
"permission/reborn/domain/usecase"
|
||
|
|
"time"
|
||
|
|
)
|
||
|
|
|
||
|
|
type userRoleUseCase struct {
|
||
|
|
userRoleRepo repository.UserRoleRepository
|
||
|
|
roleRepo repository.RoleRepository
|
||
|
|
cache repository.CacheRepository
|
||
|
|
}
|
||
|
|
|
||
|
|
// NewUserRoleUseCase 建立使用者角色 UseCase
|
||
|
|
func NewUserRoleUseCase(
|
||
|
|
userRoleRepo repository.UserRoleRepository,
|
||
|
|
roleRepo repository.RoleRepository,
|
||
|
|
cache repository.CacheRepository,
|
||
|
|
) usecase.UserRoleUseCase {
|
||
|
|
return &userRoleUseCase{
|
||
|
|
userRoleRepo: userRoleRepo,
|
||
|
|
roleRepo: roleRepo,
|
||
|
|
cache: cache,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (uc *userRoleUseCase) Assign(ctx context.Context, req usecase.AssignRoleRequest) (*usecase.UserRoleResponse, error) {
|
||
|
|
// 檢查角色是否存在
|
||
|
|
role, err := uc.roleRepo.GetByUID(ctx, req.RoleUID)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
if !role.IsActive() {
|
||
|
|
return nil, errors.Wrap(errors.ErrCodeInvalidInput, "role is not active", nil)
|
||
|
|
}
|
||
|
|
|
||
|
|
// 檢查使用者是否已有角色
|
||
|
|
exists, err := uc.userRoleRepo.Exists(ctx, req.UserUID)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
if exists {
|
||
|
|
return nil, errors.ErrUserRoleAlreadyExists
|
||
|
|
}
|
||
|
|
|
||
|
|
// 建立使用者角色
|
||
|
|
userRole := &entity.UserRole{
|
||
|
|
UID: req.UserUID,
|
||
|
|
RoleID: req.RoleUID,
|
||
|
|
Brand: req.Brand,
|
||
|
|
Status: entity.StatusActive,
|
||
|
|
}
|
||
|
|
|
||
|
|
if err := uc.userRoleRepo.Create(ctx, userRole); err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
return uc.toResponse(userRole), nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (uc *userRoleUseCase) Update(ctx context.Context, userUID, roleUID string) (*usecase.UserRoleResponse, error) {
|
||
|
|
// 檢查角色是否存在
|
||
|
|
role, err := uc.roleRepo.GetByUID(ctx, roleUID)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
if !role.IsActive() {
|
||
|
|
return nil, errors.Wrap(errors.ErrCodeInvalidInput, "role is not active", nil)
|
||
|
|
}
|
||
|
|
|
||
|
|
// 更新使用者角色
|
||
|
|
userRole, err := uc.userRoleRepo.Update(ctx, userUID, roleUID)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
// 清除使用者權限快取
|
||
|
|
if uc.cache != nil {
|
||
|
|
_ = uc.cache.Delete(ctx, repository.CacheKeyUserPermission(userUID))
|
||
|
|
}
|
||
|
|
|
||
|
|
return uc.toResponse(userRole), nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (uc *userRoleUseCase) Remove(ctx context.Context, userUID string) error {
|
||
|
|
if err := uc.userRoleRepo.Delete(ctx, userUID); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
// 清除使用者權限快取
|
||
|
|
if uc.cache != nil {
|
||
|
|
_ = uc.cache.Delete(ctx, repository.CacheKeyUserPermission(userUID))
|
||
|
|
}
|
||
|
|
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (uc *userRoleUseCase) Get(ctx context.Context, userUID string) (*usecase.UserRoleResponse, error) {
|
||
|
|
userRole, err := uc.userRoleRepo.Get(ctx, userUID)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
return uc.toResponse(userRole), nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (uc *userRoleUseCase) GetByRole(ctx context.Context, roleUID string) ([]*usecase.UserRoleResponse, error) {
|
||
|
|
// 檢查角色是否存在
|
||
|
|
if _, err := uc.roleRepo.GetByUID(ctx, roleUID); err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
userRoles, err := uc.userRoleRepo.GetByRoleID(ctx, roleUID)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
result := make([]*usecase.UserRoleResponse, 0, len(userRoles))
|
||
|
|
for _, ur := range userRoles {
|
||
|
|
result = append(result, uc.toResponse(ur))
|
||
|
|
}
|
||
|
|
|
||
|
|
return result, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (uc *userRoleUseCase) List(ctx context.Context, filter usecase.UserRoleFilterRequest) ([]*usecase.UserRoleResponse, error) {
|
||
|
|
repoFilter := repository.UserRoleFilter{
|
||
|
|
Brand: filter.Brand,
|
||
|
|
RoleID: filter.RoleID,
|
||
|
|
Status: filter.Status,
|
||
|
|
}
|
||
|
|
|
||
|
|
userRoles, err := uc.userRoleRepo.List(ctx, repoFilter)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
result := make([]*usecase.UserRoleResponse, 0, len(userRoles))
|
||
|
|
for _, ur := range userRoles {
|
||
|
|
result = append(result, uc.toResponse(ur))
|
||
|
|
}
|
||
|
|
|
||
|
|
return result, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (uc *userRoleUseCase) toResponse(userRole *entity.UserRole) *usecase.UserRoleResponse {
|
||
|
|
return &usecase.UserRoleResponse{
|
||
|
|
UserUID: userRole.UID,
|
||
|
|
RoleUID: userRole.RoleID,
|
||
|
|
Brand: userRole.Brand,
|
||
|
|
CreateTime: userRole.CreateTime.UTC().Format(time.RFC3339),
|
||
|
|
UpdateTime: userRole.UpdateTime.UTC().Format(time.RFC3339),
|
||
|
|
}
|
||
|
|
}
|