diff --git a/backend/internal/controller/http/v1/role_permissions.go b/backend/internal/controller/http/v1/role_permissions.go index e6011dc..bd1c270 100644 --- a/backend/internal/controller/http/v1/role_permissions.go +++ b/backend/internal/controller/http/v1/role_permissions.go @@ -11,14 +11,15 @@ import ( ) type rolePermissions struct { - rolePermissionUseCase usecase.RolePermissionUseCase - roleUseCase usecase.RoleUseCase - messengerController HTTPControllerMessenger - l *logger.Logger + rolePermissionUseCase usecase.RolePermissionUseCase + roleUseCase usecase.RoleUseCase + userUseCase usecase.UserUseCase + messengerController HTTPControllerMessenger + l *logger.Logger } -func newRolePermissionsRoutes(handler *gin.RouterGroup, rolePermissionUseCase usecase.RolePermissionUseCase, roleUseCase usecase.RoleUseCase, messengerController HTTPControllerMessenger, l *logger.Logger) { - r := &rolePermissions{rolePermissionUseCase, roleUseCase, messengerController, l} +func newRolePermissionsRoutes(handler *gin.RouterGroup, rolePermissionUseCase usecase.RolePermissionUseCase, roleUseCase usecase.RoleUseCase, userUseCase usecase.UserUseCase, messengerController HTTPControllerMessenger, l *logger.Logger) { + r := &rolePermissions{rolePermissionUseCase, roleUseCase, userUseCase, messengerController, l} h := handler.Group("/role-permissions") { @@ -58,7 +59,8 @@ func (r *rolePermissions) userPermissions(c *gin.Context) { } res.Permissions = rolePermissions - res.Admin, err = r.roleUseCase.IsUserSuperAdmin(token) + + res.Admin, err = r.userUseCase.IsUserSuperAdmin(token) if err != nil { r.l.Error(err, "http - v1 - list user permissions - IsUserSuperAdmin") errorResponse(c, http.StatusInternalServerError, "database problems", err) diff --git a/backend/internal/controller/http/v1/router.go b/backend/internal/controller/http/v1/router.go index e83ee5d..0597f68 100644 --- a/backend/internal/controller/http/v1/router.go +++ b/backend/internal/controller/http/v1/router.go @@ -91,7 +91,7 @@ func NewRouter( newWalletsRoutes(groupV1, walletUseCase, messengerController, authUseCase, logger) newAssetsRoutes(groupV1, walletUseCase, assetUseCase, messengerController, authUseCase, logUc, rolePermissionUc, logger, profanityF) newRoleRoutes(groupV1, roleUseCase, messengerController, logger, profanityF) - newRolePermissionsRoutes(groupV1, rolePermissionUc, roleUseCase, messengerController, logger) + newRolePermissionsRoutes(groupV1, rolePermissionUc, roleUseCase, userUseCase, messengerController, logger) newVaultCategoryRoutes(groupV1, messengerController, authUseCase, vaultCategoryUc, logger, profanityF) newVaultRoutes(groupV1, messengerController, authUseCase, vaultUc, vaultCategoryUc, walletUseCase, assetUseCase, logger, profanityF) newContractRoutes(groupV1, messengerController, authUseCase, contractUc, vaultUc, assetUseCase, userUseCase, logger) diff --git a/backend/internal/usecase/interfaces.go b/backend/internal/usecase/interfaces.go index 58bf1d8..5b4c8b3 100644 --- a/backend/internal/usecase/interfaces.go +++ b/backend/internal/usecase/interfaces.go @@ -22,6 +22,7 @@ type ( GetProfile(token string) (entity.UserResponse, error) GetSuperAdminUsers() ([]entity.UserResponse, error) UpdateName(id string, name string) error + IsUserSuperAdmin(id string) (bool, error) } // User -. @@ -29,6 +30,7 @@ type ( Detail(email string) (entity.User, error) CreateUser(user entity.User) error Autentication(name string, password string) (User, error) + } // Wallet -. diff --git a/backend/internal/usecase/mocks/mocks.go b/backend/internal/usecase/mocks/mocks.go index 5902f39..8faf58b 100644 --- a/backend/internal/usecase/mocks/mocks.go +++ b/backend/internal/usecase/mocks/mocks.go @@ -1,5 +1,5 @@ // Code generated by MockGen. DO NOT EDIT. -// Source: internal/usecase/interfaces.go +// Source: usecase/interfaces.go // Package mocks is a generated GoMock package. package mocks @@ -140,6 +140,21 @@ func (mr *MockUserRepoMockRecorder) GetUserByToken(token interface{}) *gomock.Ca return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetUserByToken", reflect.TypeOf((*MockUserRepo)(nil).GetUserByToken), token) } +// IsUserSuperAdmin mocks base method. +func (m *MockUserRepo) IsUserSuperAdmin(id string) (bool, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "IsUserSuperAdmin", id) + ret0, _ := ret[0].(bool) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// IsUserSuperAdmin indicates an expected call of IsUserSuperAdmin. +func (mr *MockUserRepoMockRecorder) IsUserSuperAdmin(id interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "IsUserSuperAdmin", reflect.TypeOf((*MockUserRepo)(nil).IsUserSuperAdmin), id) +} + // UpdateName mocks base method. func (m *MockUserRepo) UpdateName(id, name string) error { m.ctrl.T.Helper() diff --git a/backend/internal/usecase/repo/role_postgres.go b/backend/internal/usecase/repo/role_postgres.go index 112ca5c..49c7d45 100644 --- a/backend/internal/usecase/repo/role_postgres.go +++ b/backend/internal/usecase/repo/role_postgres.go @@ -129,3 +129,6 @@ func (r RoleRepo) GetSuperAdminRole() (entity.Role, error) { return role, nil } + + + diff --git a/backend/internal/usecase/repo/user_postgres.go b/backend/internal/usecase/repo/user_postgres.go index 8f6568c..2cd4860 100644 --- a/backend/internal/usecase/repo/user_postgres.go +++ b/backend/internal/usecase/repo/user_postgres.go @@ -1,6 +1,7 @@ package repo import ( + "database/sql" "errors" "fmt" @@ -180,4 +181,27 @@ func (r UserRepo) UpdateName(id string, name string) error { return fmt.Errorf("UserRepo - UpdateName - db.Exec: %w", err) } return nil -} \ No newline at end of file +} + +func (r UserRepo) IsUserSuperAdmin(token string) (bool, error) { + query := `SELECT EXISTS ( + SELECT * + FROM UserAccount u + JOIN role r ON u.role_id = r.id + WHERE r.admin = $1 AND u.token = $2 + ) AS is_super_admin;` + + row := r.Db.QueryRow(query, 1, token) + + var isSuperAdmin bool + + err := row.Scan(&isSuperAdmin) + if err != nil { + if err == sql.ErrNoRows { + return false, fmt.Errorf("UserRepo - IsUserSuperAdmin - not found") + } + return false, fmt.Errorf("UserRepo - IsUserSuperAdmin - row.Scan: %w", err) + } + + return isSuperAdmin, nil +} diff --git a/backend/internal/usecase/role.go b/backend/internal/usecase/role.go index 0b67f25..92dedce 100644 --- a/backend/internal/usecase/role.go +++ b/backend/internal/usecase/role.go @@ -62,16 +62,4 @@ func (useCase *RoleUseCase) GetSuperAdminRole() (entity.Role, error) { return entity.Role{}, fmt.Errorf("RoleUseCase - GetSuperAdminRole - uc.repo.GetSuperAdminRole: %w", err) } return role, nil -} - - -func (useCase *RoleUseCase) IsUserSuperAdmin(token string) (bool, error) { - role, err := useCase.repo.GetSuperAdminRole() - if err != nil { - return false, fmt.Errorf("RoleUseCase - GetSuperAdminRole - uc.repo.GetSuperAdminRole: %w", err) - } - if (role == entity.Role{}) { - return false, nil - } - return true, nil -} +} \ No newline at end of file diff --git a/backend/internal/usecase/users.go b/backend/internal/usecase/users.go index 80208c6..e7afb34 100644 --- a/backend/internal/usecase/users.go +++ b/backend/internal/usecase/users.go @@ -126,4 +126,13 @@ func (uc *UserUseCase) UpdateName(userID string, name string) error { return err } return nil -} \ No newline at end of file +} + +func (useCase *UserUseCase) IsUserSuperAdmin(token string) (bool, error) { + value, err := useCase.repo.IsUserSuperAdmin(token) + if err != nil { + return false, fmt.Errorf("UserUseCase - IsUserSuperAdmin - uc.repo.IsUserSuperAdmin: %w", err) + } + + return value, nil +}