common response transforms

This commit is contained in:
Pedro Costa
2025-04-16 21:58:54 +01:00
parent c69d4e40c4
commit 1a145eda47
2 changed files with 58 additions and 53 deletions

View File

@@ -26,7 +26,7 @@ type handler struct {
}
func AddEndpoints(accountManager account.Manager, usersManager users.Manager, router *mux.Router) {
userHandler := newHandler(accountManager)
userHandler := newHandler(accountManager, usersManager)
router.HandleFunc("/users", userHandler.getAllUsers).Methods("GET", "OPTIONS")
router.HandleFunc("/users/current", userHandler.getCurrentUser).Methods("GET", "OPTIONS")
router.HandleFunc("/users/{userId}", userHandler.updateUser).Methods("PUT", "OPTIONS")
@@ -38,9 +38,10 @@ func AddEndpoints(accountManager account.Manager, usersManager users.Manager, ro
}
// newHandler creates a new UsersHandler HTTP handler
func newHandler(accountManager account.Manager) *handler {
func newHandler(accountManager account.Manager, usersManager users.Manager) *handler {
return &handler{
accountManager: accountManager,
usersManager: usersManager,
}
}
@@ -312,34 +313,13 @@ func (h *handler) getRoles(w http.ResponseWriter, r *http.Request) {
func toRolesResponse(roles map[types.UserRole]roles.RolePermissions) []api.RolePermissions {
result := make([]api.RolePermissions, 0, len(roles))
for _, permissions := range roles {
rolePermissions := api.RolePermissions{
Role: string(permissions.Role),
Role: string(permissions.Role),
Default: toOperationsMapResponse(permissions.AutoAllowNew),
Modules: toModulesMapResponse(permissions.Permissions),
}
if len(permissions.AutoAllowNew) > 0 {
rolePermissions.Default = make(map[string]bool)
for k, v := range permissions.AutoAllowNew {
rolePermissions.Default[string(k)] = v
}
}
if len(permissions.Permissions) > 0 {
modules := make(map[string]map[string]bool)
for module, operations := range permissions.Permissions {
if len(operations) == 0 {
continue
}
access := make(map[string]bool)
for k, v := range operations {
access[string(k)] = v
}
modules[string(module)] = access
}
rolePermissions.Modules = &modules
}
result = append(result, rolePermissions)
}
return result
@@ -348,38 +328,47 @@ func toRolesResponse(roles map[types.UserRole]roles.RolePermissions) []api.RoleP
func toUserWithPermissionsResponse(user *users.UserInfoWithPermissions, userID string) *api.User {
response := toUserResponse(user.UserInfo, userID)
permissions := api.UserPermissions{
response.Permissions = api.UserPermissions{
IsRestricted: user.Restricted,
Default: toOperationsMapResponse(user.Permissions.AutoAllowNew),
Modules: toModulesMapResponse(user.Permissions.Permissions),
}
if len(user.Permissions.AutoAllowNew) > 0 {
permissions.Default = make(map[string]bool)
for k, v := range user.Permissions.AutoAllowNew {
permissions.Default[string(k)] = v
}
}
if len(user.Permissions.Permissions) > 0 {
modules := make(map[string]map[string]bool)
for module, operations := range user.Permissions.Permissions {
if len(operations) == 0 {
continue
}
access := make(map[string]bool)
for k, v := range operations {
access[string(k)] = v
}
modules[string(module)] = access
}
permissions.Modules = &modules
}
response.Permissions = permissions
return response
}
func toOperationsMapResponse[K ~string, V any](input map[K]V) map[string]V {
if len(input) == 0 {
return nil
}
result := make(map[string]V, len(input))
for k, v := range input {
result[string(k)] = v
}
return result
}
func toModulesMapResponse[K, L ~string](input map[K]map[L]bool) *map[string]map[string]bool {
if len(input) == 0 {
return nil
}
modules := make(map[string]map[string]bool)
for module, operations := range input {
if converted := toOperationsMapResponse(operations); len(converted) > 0 {
modules[string(module)] = converted
}
}
if len(modules) == 0 {
return nil
}
return &modules
}
func toUserResponse(user *types.UserInfo, currenUserID string) *api.User {
autoGroups := user.AutoGroups
if autoGroups == nil {

View File

@@ -9,6 +9,7 @@ import (
reflect "reflect"
gomock "github.com/golang/mock/gomock"
roles "github.com/netbirdio/netbird/management/server/permissions/roles"
types "github.com/netbirdio/netbird/management/server/types"
)
@@ -35,6 +36,21 @@ func (m *MockManager) EXPECT() *MockManagerMockRecorder {
return m.recorder
}
// GetRoles mocks base method.
func (m *MockManager) GetRoles(ctx context.Context, accountId, userId string) (map[types.UserRole]roles.RolePermissions, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetRoles", ctx, accountId, userId)
ret0, _ := ret[0].(map[types.UserRole]roles.RolePermissions)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// GetRoles indicates an expected call of GetRoles.
func (mr *MockManagerMockRecorder) GetRoles(ctx, accountId, userId interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetRoles", reflect.TypeOf((*MockManager)(nil).GetRoles), ctx, accountId, userId)
}
// GetUser mocks base method.
func (m *MockManager) GetUser(ctx context.Context, userID string) (*types.User, error) {
m.ctrl.T.Helper()