chore: import upstream snapshot with attribution
CI / Check and lint (push) Failing after 1s
Lint / Lint (ubuntu-latest) (push) Failing after 1s

This commit is contained in:
wehub-resource-sync
2026-07-13 12:22:32 +08:00
commit 39dbe3a57d
1131 changed files with 272709 additions and 0 deletions
+31
View File
@@ -0,0 +1,31 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package role
import (
"context"
"github.com/apache/answer/internal/entity"
)
// PowerRepo power repository
type PowerRepo interface {
GetPowerList(ctx context.Context, power *entity.Power) (powers []*entity.Power, err error)
}
@@ -0,0 +1,58 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package role
import (
"context"
)
// RolePowerRelRepo rolePowerRel repository
type RolePowerRelRepo interface {
GetRolePowerTypeList(ctx context.Context, roleID int) (powers []string, err error)
}
// RolePowerRelService user service
type RolePowerRelService struct {
rolePowerRelRepo RolePowerRelRepo
userRoleRelService *UserRoleRelService
}
// NewRolePowerRelService new role power rel service
func NewRolePowerRelService(rolePowerRelRepo RolePowerRelRepo,
userRoleRelService *UserRoleRelService) *RolePowerRelService {
return &RolePowerRelService{
rolePowerRelRepo: rolePowerRelRepo,
userRoleRelService: userRoleRelService,
}
}
// GetRolePowerList get role power list
func (rs *RolePowerRelService) GetRolePowerList(ctx context.Context, roleID int) (powers []string, err error) {
return rs.rolePowerRelRepo.GetRolePowerTypeList(ctx, roleID)
}
// GetUserPowerList get list all
func (rs *RolePowerRelService) GetUserPowerList(ctx context.Context, userID string) (powers []string, err error) {
roleID, err := rs.userRoleRelService.GetUserRole(ctx, userID)
if err != nil {
return nil, err
}
return rs.rolePowerRelRepo.GetRolePowerTypeList(ctx, roleID)
}
+103
View File
@@ -0,0 +1,103 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package role
import (
"context"
"github.com/apache/answer/internal/base/handler"
"github.com/apache/answer/internal/base/translator"
"github.com/apache/answer/internal/entity"
"github.com/apache/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.
RoleUserID = 1
RoleAdminID = 2
RoleModeratorID = 3
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:
role.Name = translator.Tr(handler.GetLangByCtx(ctx), trRoleNameUser)
role.Description = translator.Tr(handler.GetLangByCtx(ctx), trRoleDescriptionUser)
case roleAdminName:
role.Name = translator.Tr(handler.GetLangByCtx(ctx), trRoleNameAdmin)
role.Description = translator.Tr(handler.GetLangByCtx(ctx), trRoleDescriptionAdmin)
case roleModeratorName:
role.Name = translator.Tr(handler.GetLangByCtx(ctx), trRoleNameModerator)
role.Description = translator.Tr(handler.GetLangByCtx(ctx), trRoleDescriptionModerator)
}
}
@@ -0,0 +1,125 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package role
import (
"context"
"github.com/apache/answer/internal/entity"
)
// UserRoleRelRepo userRoleRel repository
type UserRoleRelRepo interface {
SaveUserRoleRel(ctx context.Context, userID string, roleID int) (err error)
GetUserRoleRelList(ctx context.Context, userIDs []string) (userRoleRelList []*entity.UserRoleRel, err error)
GetUserRoleRelListByRoleID(ctx context.Context, roleIDs []int) (
userRoleRelList []*entity.UserRoleRel, err error)
GetUserRoleRel(ctx context.Context, userID string) (rolePowerRel *entity.UserRoleRel, exist bool, err error)
}
// UserRoleRelService user service
type UserRoleRelService struct {
userRoleRelRepo UserRoleRelRepo
roleService *RoleService
}
// NewUserRoleRelService new user role rel service
func NewUserRoleRelService(userRoleRelRepo UserRoleRelRepo, roleService *RoleService) *UserRoleRelService {
return &UserRoleRelService{
userRoleRelRepo: userRoleRelRepo,
roleService: roleService,
}
}
// SaveUserRole save user role
func (us *UserRoleRelService) SaveUserRole(ctx context.Context, userID string, roleID int) (err error) {
return us.userRoleRelRepo.SaveUserRoleRel(ctx, userID, roleID)
}
// GetUserRoleMapping get user role mapping
func (us *UserRoleRelService) GetUserRoleMapping(ctx context.Context, userIDs []string) (
userRoleMapping map[string]*entity.Role, err error) {
userRoleMapping = make(map[string]*entity.Role, 0)
roleMapping, err := us.roleService.GetRoleMapping(ctx)
if err != nil {
return userRoleMapping, err
}
if len(roleMapping) == 0 {
return userRoleMapping, nil
}
relMapping, err := us.GetUserRoleRelMapping(ctx, userIDs)
if err != nil {
return userRoleMapping, err
}
// default role is user
defaultRole := roleMapping[1]
for _, userID := range userIDs {
roleID, ok := relMapping[userID]
if !ok {
userRoleMapping[userID] = defaultRole
continue
}
userRoleMapping[userID] = roleMapping[roleID]
if userRoleMapping[userID] == nil {
userRoleMapping[userID] = defaultRole
}
}
return userRoleMapping, nil
}
// GetUserRoleRelMapping get user role rel mapping
func (us *UserRoleRelService) GetUserRoleRelMapping(ctx context.Context, userIDs []string) (
userRoleRelMapping map[string]int, err error) {
userRoleRelMapping = make(map[string]int, 0)
relList, err := us.userRoleRelRepo.GetUserRoleRelList(ctx, userIDs)
if err != nil {
return userRoleRelMapping, err
}
for _, rel := range relList {
userRoleRelMapping[rel.UserID] = rel.RoleID
}
return userRoleRelMapping, nil
}
// GetUserRole get user role
func (us *UserRoleRelService) GetUserRole(ctx context.Context, userID string) (roleID int, err error) {
rolePowerRel, exist, err := us.userRoleRelRepo.GetUserRoleRel(ctx, userID)
if err != nil {
return 0, err
}
if !exist {
// set default role
return 1, nil
}
return rolePowerRel.RoleID, nil
}
// GetUserByRoleID get user by role id
func (us *UserRoleRelService) GetUserByRoleID(ctx context.Context, roleIDs []int) (rel []*entity.UserRoleRel, err error) {
rolePowerRels, err := us.userRoleRelRepo.GetUserRoleRelListByRoleID(ctx, roleIDs)
if err != nil {
return nil, err
}
return rolePowerRels, nil
}