Skip to content

Commit

Permalink
Merge pull request #429 from cho4036/user
Browse files Browse the repository at this point in the history
[RE] bugfix. fix permission ordering error
  • Loading branch information
ktkfree authored Apr 25, 2024
2 parents a4fc290 + fac8202 commit 90ea598
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 34 deletions.
3 changes: 1 addition & 2 deletions internal/delivery/http/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package http

import (
"context"
"net/http"

"github.com/gorilla/mux"
"github.com/openinfradev/tks-api/internal/model"
"github.com/openinfradev/tks-api/internal/pagination"
Expand All @@ -12,6 +10,7 @@ import (
"github.com/openinfradev/tks-api/pkg/domain"
"github.com/openinfradev/tks-api/pkg/httpErrors"
"github.com/openinfradev/tks-api/pkg/log"
"net/http"
)

type IRoleHandler interface {
Expand Down
30 changes: 0 additions & 30 deletions internal/delivery/http/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"net/http"
"sort"
"strings"

"github.com/gorilla/mux"
Expand Down Expand Up @@ -825,40 +824,11 @@ 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
8 changes: 8 additions & 0 deletions internal/model/permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ import (

type PermissionKind string

var SortOrder = map[string]int{
OperationRead: 0,
OperationCreate: 1,
OperationUpdate: 2,
OperationDelete: 3,
OperationDownload: 4,
}

const (
DashBoardPermission PermissionKind = "대시보드"
StackPermission PermissionKind = "스택"
Expand Down
47 changes: 45 additions & 2 deletions internal/usecase/permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/google/uuid"
"github.com/openinfradev/tks-api/internal/model"
"github.com/openinfradev/tks-api/internal/repository"
"sort"
)

type IPermissionUsecase interface {
Expand Down Expand Up @@ -80,17 +81,32 @@ func (p PermissionUsecase) GetPermissionSetByRoleId(ctx context.Context, roleId
case string(model.ConfigurationPermission):
permissionSet.Configuration = permission
}

p.sortPermissionRecursive(permission)
}

return permissionSet, nil
}

func (p PermissionUsecase) ListPermissions(ctx context.Context, roleId string) ([]*model.Permission, error) {
return p.repo.List(ctx, roleId)
permissions, err := p.repo.List(ctx, roleId)
if err != nil {
return nil, err
}
for _, permission := range permissions {
p.sortPermissionRecursive(permission)
}

return permissions, nil
}

func (p PermissionUsecase) GetPermission(ctx context.Context, id uuid.UUID) (*model.Permission, error) {
return p.repo.Get(ctx, id)
permission, err := p.repo.Get(ctx, id)
if err != nil {
return nil, err
}
p.sortPermissionRecursive(permission)
return permission, nil
}

func (p PermissionUsecase) DeletePermission(ctx context.Context, id uuid.UUID) error {
Expand Down Expand Up @@ -149,3 +165,30 @@ func (p PermissionUsecase) mergePermission(ctx context.Context, mergedPermission

return mergedPermission
}

func (p PermissionUsecase) sortPermissionRecursive(permission *model.Permission) {
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 := model.SortOrder[key1]
order2, exists2 := model.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 {
p.sortPermissionRecursive(child)
}
}
}

0 comments on commit 90ea598

Please sign in to comment.