Skip to content

Commit

Permalink
expose list of a user's roles to them
Browse files Browse the repository at this point in the history
  • Loading branch information
khanzadimahdi committed Jul 24, 2024
1 parent 28c12a4 commit 6f73622
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 11 deletions.
27 changes: 27 additions & 0 deletions backend/application/dashboard/profile/getRoles/response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package getRoles

import "github.com/khanzadimahdi/testproject/domain/role"

type roleResponse struct {
UUID string `json:"uuid"`
Name string `json:"name"`
Description string `json:"description"`
}

type Response struct {
Items []roleResponse `json:"items"`
}

func NewResponse(r []role.Role) *Response {
items := make([]roleResponse, len(r))

for i := range r {
items[i].UUID = r[i].UUID
items[i].Name = r[i].Name
items[i].Description = r[i].Description
}

return &Response{
Items: items,
}
}
26 changes: 26 additions & 0 deletions backend/application/dashboard/profile/getRoles/usecase.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package getRoles

import (
"github.com/khanzadimahdi/testproject/domain/role"
)

const limit = 10

type UseCase struct {
roleRepository role.Repository
}

func NewUseCase(roleRepository role.Repository) *UseCase {
return &UseCase{
roleRepository: roleRepository,
}
}

func (uc *UseCase) GetRoles(UUID string) (*Response, error) {
roles, err := uc.roleRepository.GetByUserUUID(UUID)
if err != nil {
return nil, err
}

return NewResponse(roles), nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package getRoles
1 change: 1 addition & 0 deletions backend/domain/role/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ type Repository interface {
Delete(UUID string) error
Count() (uint, error)
UserHasPermission(userUUID string, permission string) (bool, error)
GetByUserUUID(userUUID string) ([]Role, error)
}
38 changes: 37 additions & 1 deletion backend/infrastructure/repository/mongodb/roles/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (r *RolesRepository) GetAll(offset uint, limit uint) ([]role.Role, error) {
UUID: r.UUID,
Name: r.Name,
Description: r.Description,
Permissions: r.UserUUIDs,
Permissions: r.Permissions,
})
}

Expand Down Expand Up @@ -171,3 +171,39 @@ func (r *RolesRepository) UserHasPermission(userUUID string, permission string)

return c > 0, nil
}

func (r *RolesRepository) GetByUserUUID(userUUID string) ([]role.Role, error) {
ctx, cancel := context.WithTimeout(context.Background(), queryTimeout)
defer cancel()

filter := bson.D{{Key: "user_uuids", Value: userUUID}}

cur, err := r.collection.Find(ctx, filter, nil)

if err != nil {
return nil, err
}

defer cur.Close(ctx)

items := make([]role.Role, 0, 2)
for cur.Next(ctx) {
var r RoleBson

if err := cur.Decode(&r); err != nil {
return nil, err
}
items = append(items, role.Role{
UUID: r.UUID,
Name: r.Name,
Description: r.Description,
Permissions: r.Permissions,
})
}

if err := cur.Err(); err != nil {
return nil, err
}

return items, nil
}
15 changes: 9 additions & 6 deletions backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
dashboardUploadFile "github.com/khanzadimahdi/testproject/application/dashboard/file/uploadFile"
dashboardGetPermissions "github.com/khanzadimahdi/testproject/application/dashboard/permission/getPermissions"
"github.com/khanzadimahdi/testproject/application/dashboard/profile/changepassword"
"github.com/khanzadimahdi/testproject/application/dashboard/profile/getRoles"
"github.com/khanzadimahdi/testproject/application/dashboard/profile/getprofile"
"github.com/khanzadimahdi/testproject/application/dashboard/profile/updateprofile"
dashboardCreateRole "github.com/khanzadimahdi/testproject/application/dashboard/role/createRole"
Expand Down Expand Up @@ -212,9 +213,10 @@ func httpHandler() http.Handler {
router.Handler(http.MethodGet, "/files/:uuid", fileAPI.NewShowHandler(getFileUseCase))

// -------------------- dashboard -------------------- //
getProfile := getprofile.NewUseCase(userRepository)
updateProfile := updateprofile.NewUseCase(userRepository)
dashboardChangePassword := changepassword.NewUseCase(userRepository, hasher)
getProfileUseCase := getprofile.NewUseCase(userRepository)
updateProfileUseCase := updateprofile.NewUseCase(userRepository)
dashboardProfileChangePasswordUseCase := changepassword.NewUseCase(userRepository, hasher)
dashboardProfileGetRolesUseCase := getRoles.NewUseCase(roleRepository)

dashboardCreateArticleUsecase := dashboardCreateArticle.NewUseCase(articlesRepository)
dashboardDeleteArticleUsecase := dashboardDeleteArticle.NewUseCase(articlesRepository)
Expand Down Expand Up @@ -255,9 +257,10 @@ func httpHandler() http.Handler {
dashboardUpdateElementUsecase := dashboardUpdateElement.NewUseCase(elementsRepository)

// profile
router.Handler(http.MethodGet, "/api/dashboard/profile", middleware.NewAuthoriseMiddleware(profile.NewGetProfileHandler(getProfile), j, userRepository))
router.Handler(http.MethodPut, "/api/dashboard/profile", middleware.NewAuthoriseMiddleware(profile.NewUpdateProfileHandler(updateProfile), j, userRepository))
router.Handler(http.MethodPut, "/api/dashboard/password", middleware.NewAuthoriseMiddleware(profile.NewChangePasswordHandler(dashboardChangePassword), j, userRepository))
router.Handler(http.MethodGet, "/api/dashboard/profile", middleware.NewAuthoriseMiddleware(profile.NewGetProfileHandler(getProfileUseCase), j, userRepository))
router.Handler(http.MethodPut, "/api/dashboard/profile", middleware.NewAuthoriseMiddleware(profile.NewUpdateProfileHandler(updateProfileUseCase), j, userRepository))
router.Handler(http.MethodPut, "/api/dashboard/password", middleware.NewAuthoriseMiddleware(profile.NewChangePasswordHandler(dashboardProfileChangePasswordUseCase), j, userRepository))
router.Handler(http.MethodGet, "/api/dashboard/profile/roles", middleware.NewAuthoriseMiddleware(profile.NewGetRolesHandler(dashboardProfileGetRolesUseCase), j, userRepository))

// user
router.Handler(http.MethodPost, "/api/dashboard/users", middleware.NewAuthoriseMiddleware(dashboardUserAPI.NewCreateHandler(dashboardCreateUserUsecase, authorization), j, userRepository))
Expand Down
33 changes: 33 additions & 0 deletions backend/presentation/http/api/dashboard/profile/getroles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package profile

import (
"encoding/json"
"net/http"

"github.com/khanzadimahdi/testproject/application/auth"
getroles "github.com/khanzadimahdi/testproject/application/dashboard/profile/getRoles"
)

type getRolesHandler struct {
getRolesUseCase *getroles.UseCase
}

func NewGetRolesHandler(getRolesUseCase *getroles.UseCase) *getRolesHandler {
return &getRolesHandler{
getRolesUseCase: getRolesUseCase,
}
}

func (h *getRolesHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
userUUID := auth.FromContext(r.Context()).UUID

response, err := h.getRolesUseCase.GetRoles(userUUID)
switch true {
case err != nil:
rw.WriteHeader(http.StatusInternalServerError)
default:
rw.Header().Add("Content-Type", "application/json")
rw.WriteHeader(http.StatusOK)
json.NewEncoder(rw).Encode(response)
}
}
8 changes: 4 additions & 4 deletions backend/presentation/http/api/dashboard/role/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import (
)

type indexHandler struct {
getRolesUsaCase *getroles.UseCase
getRolesUseCase *getroles.UseCase
authorizer domain.Authorizer
}

func NewIndexHandler(getRolesUsaCase *getroles.UseCase, a domain.Authorizer) *indexHandler {
func NewIndexHandler(getRolesUseCase *getroles.UseCase, a domain.Authorizer) *indexHandler {
return &indexHandler{
getRolesUsaCase: getRolesUsaCase,
getRolesUseCase: getRolesUseCase,
authorizer: a,
}
}
Expand All @@ -46,7 +46,7 @@ func (h *indexHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
Page: page,
}

response, err := h.getRolesUsaCase.GetRoles(request)
response, err := h.getRolesUseCase.GetRoles(request)
switch true {
case err != nil:
rw.WriteHeader(http.StatusInternalServerError)
Expand Down

0 comments on commit 6f73622

Please sign in to comment.