Skip to content

Commit

Permalink
fix: admin field on permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandrak committed Feb 26, 2024
1 parent 907bd76 commit 7b5bda9
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 24 deletions.
16 changes: 9 additions & 7 deletions backend/internal/controller/http/v1/role_permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
{
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion backend/internal/controller/http/v1/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions backend/internal/usecase/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ type (
GetProfile(token string) (entity.UserResponse, error)
GetSuperAdminUsers() ([]entity.UserResponse, error)
UpdateName(id string, name string) error
IsUserSuperAdmin(id string) (bool, error)
}

// User -.
User interface {
Detail(email string) (entity.User, error)
CreateUser(user entity.User) error
Autentication(name string, password string) (User, error)

}

// Wallet -.
Expand Down
17 changes: 16 additions & 1 deletion backend/internal/usecase/mocks/mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions backend/internal/usecase/repo/role_postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,6 @@ func (r RoleRepo) GetSuperAdminRole() (entity.Role, error) {

return role, nil
}



26 changes: 25 additions & 1 deletion backend/internal/usecase/repo/user_postgres.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package repo

import (
"database/sql"
"errors"
"fmt"

Expand Down Expand Up @@ -180,4 +181,27 @@ func (r UserRepo) UpdateName(id string, name string) error {
return fmt.Errorf("UserRepo - UpdateName - db.Exec: %w", err)
}
return nil
}
}

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
}
14 changes: 1 addition & 13 deletions backend/internal/usecase/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
11 changes: 10 additions & 1 deletion backend/internal/usecase/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,13 @@ func (uc *UserUseCase) UpdateName(userID string, name string) error {
return err
}
return nil
}
}

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
}

0 comments on commit 7b5bda9

Please sign in to comment.