Skip to content

Commit

Permalink
feat: roles
Browse files Browse the repository at this point in the history
  • Loading branch information
amirhnajafiz committed Jun 24, 2024
1 parent 9c55ed8 commit 75125bd
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
56 changes: 56 additions & 0 deletions internal/handlers/api/v2/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,66 @@ func (h Handler) getUser(c echo.Context) error {
return echo.ErrInternalServerError
}

// fetch user's roles
roles, err := h.Ctl.GetUserRoles(user.Username)
if err != nil {
h.Logger.Error("failed to get user's roles", zap.String("username", username), zap.Error(err))

return echo.ErrInternalServerError
}

return c.JSON(http.StatusOK, UserResponse{
Username: user.Username,
Groups: groups,
Roles: roles,
CreatedAt: user.CreatedAt,
UpdatedAt: user.UpdatedAt,
})
}

func (h Handler) addRoleToUser(c echo.Context) error {
// fetch query params
req := new(UserRoleQuery)
if err := c.Bind(req); err != nil {
return echo.ErrBadRequest
}

// add role to a user
if err := h.Ctl.NewUserRole(req.Username, req.Role); err != nil {
h.Logger.Error("failed to add role to a user", zap.String("username", req.Username), zap.String("role", req.Role), zap.Error(err))

return echo.ErrInternalServerError
}

return c.String(http.StatusOK, "")
}

func (h Handler) removeRoleFromUser(c echo.Context) error {
// fetch query params
req := new(UserRoleQuery)
if err := c.Bind(req); err != nil {
return echo.ErrBadRequest
}

// remove role from a user
if err := h.Ctl.RemoveUserGroup(req.Username, req.Role); err != nil {
h.Logger.Error("failed to remove role from a user", zap.String("username", req.Username), zap.String("role", req.Role), zap.Error(err))

return echo.ErrInternalServerError
}

return c.String(http.StatusOK, "")
}

func (h Handler) removeRole(c echo.Context) error {
role := c.QueryParam("role")

// remove a group
if err := h.Ctl.RemoveRole(role); err != nil {
h.Logger.Error("failed to remove a role", zap.String("role", role), zap.Error(err))

return echo.ErrInternalServerError
}

return c.String(http.StatusOK, "")
}
6 changes: 6 additions & 0 deletions internal/handlers/api/v2/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ type Handler struct {

func (h Handler) New(v2 *echo.Group) {
users := v2.Group("/users")
roles := v2.Group("/roles")

// users methods
users.GET("/", h.getUser)

// roles methods
roles.POST("/", h.addRoleToUser)
roles.PATCH("/", h.removeRoleFromUser)
roles.DELETE("/", h.removeRole)
}
6 changes: 6 additions & 0 deletions internal/handlers/api/v2/requests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package v2

type UserRoleQuery struct {
Username string `query:"username"`
Role string `query:"role"`
}
1 change: 1 addition & 0 deletions internal/handlers/api/v2/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "time"
type UserResponse struct {
Username string `json:"username"`
Groups []string `json:"groups"`
Roles []string `json:"roles"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}

0 comments on commit 75125bd

Please sign in to comment.