2022-11-29 15:10:57 +08:00
|
|
|
package role
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/answerdev/answer/internal/base/handler"
|
|
|
|
"github.com/answerdev/answer/internal/base/translator"
|
|
|
|
"github.com/answerdev/answer/internal/entity"
|
|
|
|
"github.com/answerdev/answer/internal/schema"
|
|
|
|
"github.com/jinzhu/copier"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Since there is currently no need to edit roles to add roles and other operations,
|
|
|
|
// the current role information is translated directly.
|
|
|
|
// Later on, when the relevant ability is available, it can be adjusted by the user himself.
|
|
|
|
|
2022-12-02 15:08:18 +08:00
|
|
|
RoleUserID = 1
|
|
|
|
RoleAdminID = 2
|
|
|
|
RoleModeratorID = 3
|
|
|
|
|
2022-11-29 15:10:57 +08:00
|
|
|
roleUserName = "User"
|
|
|
|
roleAdminName = "Admin"
|
|
|
|
roleModeratorName = "Moderator"
|
|
|
|
|
|
|
|
trRoleNameUser = "role.name.user"
|
|
|
|
trRoleNameAdmin = "role.name.admin"
|
|
|
|
trRoleNameModerator = "role.name.moderator"
|
|
|
|
|
|
|
|
trRoleDescriptionUser = "role.description.user"
|
|
|
|
trRoleDescriptionAdmin = "role.description.admin"
|
|
|
|
trRoleDescriptionModerator = "role.description.moderator"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RoleRepo role repository
|
|
|
|
type RoleRepo interface {
|
|
|
|
GetRoleAllList(ctx context.Context) (roles []*entity.Role, err error)
|
|
|
|
GetRoleAllMapping(ctx context.Context) (roleMapping map[int]*entity.Role, err error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RoleService user service
|
|
|
|
type RoleService struct {
|
|
|
|
roleRepo RoleRepo
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRoleService(roleRepo RoleRepo) *RoleService {
|
|
|
|
return &RoleService{
|
|
|
|
roleRepo: roleRepo,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRoleList get role list all
|
|
|
|
func (rs *RoleService) GetRoleList(ctx context.Context) (resp []*schema.GetRoleResp, err error) {
|
|
|
|
roles, err := rs.roleRepo.GetRoleAllList(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, role := range roles {
|
|
|
|
rs.translateRole(ctx, role)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp = []*schema.GetRoleResp{}
|
|
|
|
_ = copier.Copy(&resp, roles)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *RoleService) GetRoleMapping(ctx context.Context) (roleMapping map[int]*entity.Role, err error) {
|
|
|
|
return rs.roleRepo.GetRoleAllMapping(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *RoleService) translateRole(ctx context.Context, role *entity.Role) {
|
|
|
|
switch role.Name {
|
|
|
|
case roleUserName:
|
2023-01-06 18:00:51 +08:00
|
|
|
role.Name = translator.Tr(handler.GetLangByCtx(ctx), trRoleNameUser)
|
|
|
|
role.Description = translator.Tr(handler.GetLangByCtx(ctx), trRoleDescriptionUser)
|
2022-11-29 15:10:57 +08:00
|
|
|
case roleAdminName:
|
2023-01-06 18:00:51 +08:00
|
|
|
role.Name = translator.Tr(handler.GetLangByCtx(ctx), trRoleNameAdmin)
|
|
|
|
role.Description = translator.Tr(handler.GetLangByCtx(ctx), trRoleDescriptionAdmin)
|
2022-11-29 15:10:57 +08:00
|
|
|
case roleModeratorName:
|
2023-01-06 18:00:51 +08:00
|
|
|
role.Name = translator.Tr(handler.GetLangByCtx(ctx), trRoleNameModerator)
|
|
|
|
role.Description = translator.Tr(handler.GetLangByCtx(ctx), trRoleDescriptionModerator)
|
2022-11-29 15:10:57 +08:00
|
|
|
}
|
|
|
|
}
|