diff --git a/internal/handlers/api/v2/endpoints.go b/internal/handlers/api/v2/endpoints.go index 66f3036..dfbe821 100644 --- a/internal/handlers/api/v2/endpoints.go +++ b/internal/handlers/api/v2/endpoints.go @@ -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, "") +} diff --git a/internal/handlers/api/v2/handler.go b/internal/handlers/api/v2/handler.go index d76f448..bbc3317 100644 --- a/internal/handlers/api/v2/handler.go +++ b/internal/handlers/api/v2/handler.go @@ -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) } diff --git a/internal/handlers/api/v2/requests.go b/internal/handlers/api/v2/requests.go new file mode 100644 index 0000000..6427868 --- /dev/null +++ b/internal/handlers/api/v2/requests.go @@ -0,0 +1,6 @@ +package v2 + +type UserRoleQuery struct { + Username string `query:"username"` + Role string `query:"role"` +} diff --git a/internal/handlers/api/v2/responses.go b/internal/handlers/api/v2/responses.go index a5be188..c4120a8 100644 --- a/internal/handlers/api/v2/responses.go +++ b/internal/handlers/api/v2/responses.go @@ -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"` }