Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix. Myprofile API 수정 및 Permission 조회 시 순서 보장 #426

Merged
merged 5 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 38 additions & 6 deletions internal/delivery/http/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/http"
"sort"
"strings"

"github.com/gorilla/mux"
Expand Down Expand Up @@ -540,6 +541,7 @@ func (u UserHandler) UpdateMyProfile(w http.ResponseWriter, r *http.Request) {
ErrorJSON(w, r, err)
return
}
user.ID = requestUserInfo.GetUserId()
user.Name = input.Name
user.Email = input.Email
user.Department = input.Department
Expand Down Expand Up @@ -781,19 +783,20 @@ func (u UserHandler) GetPermissionsByAccountId(w http.ResponseWriter, r *http.Re
organizationId = v
}

// ToDo: admin portal에 대해서는 현재 permission 관련 로직을 적용하지 않아 임시로 빈 값을 리턴하도록 함
if organizationId == "master" {
ResponseJSON(w, r, http.StatusOK, domain.GetUsersPermissionsResponse{})
return
}

user, err := u.usecase.GetByAccountId(r.Context(), accountId, organizationId)
if err != nil {
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
return
}

var roles []*model.Role
for _, role := range user.Roles {
roles = append(roles, &role)
}

var permissionSets []*model.PermissionSet
for _, role := range roles {
for _, role := range user.Roles {
permissionSet, err := u.permissionUsecase.GetPermissionSetByRoleId(r.Context(), role.ID)
if err != nil {
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
Expand All @@ -819,11 +822,40 @@ func (u UserHandler) GetPermissionsByAccountId(w http.ResponseWriter, r *http.Re
func convertModelToMergedPermissionSetResponse(ctx context.Context, permission *model.Permission) *domain.MergePermissionResponse {
var permissionResponse domain.MergePermissionResponse

var sortOrder = map[string]int{
"READ": 0,
"CREATE": 1,
"UPDATE": 2,
"DELETE": 3,
}

permissionResponse.Key = permission.Key
if permission.IsAllowed != nil {
permissionResponse.IsAllowed = permission.IsAllowed
}

if len(permission.Children) > 0 {
if permission.Children[0].IsAllowed != nil {
sort.Slice(permission.Children, func(i, j int) bool {
key1 := permission.Children[i].Key
key2 := permission.Children[j].Key

order1, exists1 := sortOrder[key1]
order2, exists2 := sortOrder[key2]

if exists1 && exists2 {
return order1 < order2
} else if exists1 {
return true
} else if exists2 {
return false
}

return key1 < key2
})
}
}

for _, child := range permission.Children {
permissionResponse.Children = append(permissionResponse.Children, convertModelToMergedPermissionSetResponse(ctx, child))
}
Expand Down
2 changes: 1 addition & 1 deletion internal/model/permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func NewAdminPermissionSet() *PermissionSet {
}

func GetEdgePermission(root *Permission, edgePermissions []*Permission, f *func(permission Permission) bool) []*Permission {
if root.Children == nil {
if root.Children == nil || len(root.Children) == 0 {
return append(edgePermissions, root)
}

Expand Down
3 changes: 2 additions & 1 deletion internal/repository/permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ func (r PermissionRepository) Create(ctx context.Context, p *model.Permission) e
func (r PermissionRepository) List(ctx context.Context, roleId string) ([]*model.Permission, error) {
var permissions []*model.Permission

err := r.db.WithContext(ctx).Preload("Children.Children.Children.Children").Where("parent_id IS NULL AND role_id = ?", roleId).Find(&permissions).Error
err := r.db.WithContext(ctx).Preload("Children.Children.Children.Children").
Where("parent_id IS NULL AND role_id = ?", roleId).Find(&permissions).Error
if err != nil {
return nil, err
}
Expand Down
14 changes: 7 additions & 7 deletions internal/usecase/permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,18 +125,18 @@ func (p PermissionUsecase) MergePermissionWithOrOperator(ctx context.Context, pe
continue
}

out.Dashboard = p.mergePermission(out.Dashboard, ps.Dashboard)
out.Stack = p.mergePermission(out.Stack, ps.Stack)
out.Policy = p.mergePermission(out.Policy, ps.Policy)
out.ProjectManagement = p.mergePermission(out.ProjectManagement, ps.ProjectManagement)
out.Notification = p.mergePermission(out.Notification, ps.Notification)
out.Configuration = p.mergePermission(out.Configuration, ps.Configuration)
out.Dashboard = p.mergePermission(ctx, out.Dashboard, ps.Dashboard)
out.Stack = p.mergePermission(ctx, out.Stack, ps.Stack)
out.Policy = p.mergePermission(ctx, out.Policy, ps.Policy)
out.ProjectManagement = p.mergePermission(ctx, out.ProjectManagement, ps.ProjectManagement)
out.Notification = p.mergePermission(ctx, out.Notification, ps.Notification)
out.Configuration = p.mergePermission(ctx, out.Configuration, ps.Configuration)
}

return out
}

func (p PermissionUsecase) mergePermission(mergedPermission, permission *model.Permission) *model.Permission {
func (p PermissionUsecase) mergePermission(ctx context.Context, mergedPermission, permission *model.Permission) *model.Permission {
var mergedEdgePermissions []*model.Permission
mergedEdgePermissions = model.GetEdgePermission(mergedPermission, mergedEdgePermissions, nil)

Expand Down
1 change: 1 addition & 0 deletions internal/usecase/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ func (u *UserUsecase) UpdateByAccountId(ctx context.Context, user *model.User) (

if ((*users)[0].Email != user.Email) || ((*users)[0].Name != user.Name) {
err = u.kc.UpdateUser(ctx, user.Organization.ID, &gocloak.User{
ID: gocloak.StringP(user.ID.String()),
Email: gocloak.StringP(user.Email),
FirstName: gocloak.StringP(user.Name),
})
Expand Down
Loading