diff --git a/cmd/laas/docs/docs.go b/cmd/laas/docs/docs.go index f8db8af..8beb288 100644 --- a/cmd/laas/docs/docs.go +++ b/cmd/laas/docs/docs.go @@ -2030,6 +2030,47 @@ const docTemplate = `{ } } }, + "/users/profile": { + "get": { + "security": [ + { + "ApiKeyAuth": [] + } + ], + "description": "Get user's own profile", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Users" + ], + "summary": "Get user's own profile", + "operationId": "GetUserProfile", + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/models.UserResponse" + } + }, + "400": { + "description": "Invalid user", + "schema": { + "$ref": "#/definitions/models.LicenseError" + } + }, + "404": { + "description": "User not found", + "schema": { + "$ref": "#/definitions/models.LicenseError" + } + } + } + } + }, "/users/{username}": { "get": { "security": [ diff --git a/cmd/laas/docs/swagger.json b/cmd/laas/docs/swagger.json index 70bd241..720f8b0 100644 --- a/cmd/laas/docs/swagger.json +++ b/cmd/laas/docs/swagger.json @@ -2023,6 +2023,47 @@ } } }, + "/users/profile": { + "get": { + "security": [ + { + "ApiKeyAuth": [] + } + ], + "description": "Get user's own profile", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Users" + ], + "summary": "Get user's own profile", + "operationId": "GetUserProfile", + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/models.UserResponse" + } + }, + "400": { + "description": "Invalid user", + "schema": { + "$ref": "#/definitions/models.LicenseError" + } + }, + "404": { + "description": "User not found", + "schema": { + "$ref": "#/definitions/models.LicenseError" + } + } + } + } + }, "/users/{username}": { "get": { "security": [ diff --git a/cmd/laas/docs/swagger.yaml b/cmd/laas/docs/swagger.yaml index 08e961a..f630f6c 100644 --- a/cmd/laas/docs/swagger.yaml +++ b/cmd/laas/docs/swagger.yaml @@ -2092,6 +2092,32 @@ paths: summary: Create new user via oidc id token tags: - Users + /users/profile: + get: + consumes: + - application/json + description: Get user's own profile + operationId: GetUserProfile + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/models.UserResponse' + "400": + description: Invalid user + schema: + $ref: '#/definitions/models.LicenseError' + "404": + description: User not found + schema: + $ref: '#/definitions/models.LicenseError' + security: + - ApiKeyAuth: [] + summary: Get user's own profile + tags: + - Users securityDefinitions: ApiKeyAuth: description: Token from /login endpoint. Enter the token with the `Bearer ` prefix, diff --git a/pkg/api/api.go b/pkg/api/api.go index fc2288b..1ea1190 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -123,6 +123,7 @@ func Router() *gin.Engine { users := authorizedv1.Group("/users") { users.GET("", middleware.RoleBasedAccessMiddleware([]string{"ADMIN"}), auth.GetAllUser) + users.GET("/profile", auth.GetUserProfile) users.GET(":username", middleware.RoleBasedAccessMiddleware([]string{"ADMIN"}), auth.GetUser) users.POST("", middleware.RoleBasedAccessMiddleware([]string{"ADMIN"}), auth.CreateUser) users.PATCH("", auth.UpdateProfile) @@ -226,6 +227,7 @@ func Router() *gin.Engine { users := authorizedv1.Group("/users") { users.GET("", middleware.RoleBasedAccessMiddleware([]string{"ADMIN"}), auth.GetAllUser) + users.GET("/profile", auth.GetUserProfile) users.GET(":username", middleware.RoleBasedAccessMiddleware([]string{"ADMIN"}), auth.GetUser) users.POST("", middleware.RoleBasedAccessMiddleware([]string{"ADMIN"}), auth.CreateUser) users.PATCH(":username", middleware.RoleBasedAccessMiddleware([]string{"ADMIN"}), auth.UpdateUser) diff --git a/pkg/auth/auth.go b/pkg/auth/auth.go index c9a9a62..e9f0e54 100644 --- a/pkg/auth/auth.go +++ b/pkg/auth/auth.go @@ -803,6 +803,47 @@ func Login(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"token": token}) } +// GetUserProfile retrieves the user's own profile. +// +// @Summary Get user's own profile +// @Description Get user's own profile +// @Id GetUserProfile +// @Tags Users +// @Accept json +// @Produce json +// @Success 200 {object} models.UserResponse +// @Failure 400 {object} models.LicenseError "Invalid user" +// @Failure 404 {object} models.LicenseError "User not found" +// @Security ApiKeyAuth +// @Router /users/profile [get] +func GetUserProfile(c *gin.Context) { + var user models.User + username := c.GetString("username") + + active := true + if err := db.DB.Where(models.User{Username: &username, Active: &active}).First(&user).Error; err != nil { + er := models.LicenseError{ + Status: http.StatusNotFound, + Message: "no user with such username exists", + Error: err.Error(), + Path: c.Request.URL.Path, + Timestamp: time.Now().Format(time.RFC3339), + } + c.JSON(http.StatusNotFound, er) + return + } + + res := models.UserResponse{ + Data: []models.User{user}, + Status: http.StatusOK, + Meta: &models.PaginationMeta{ + ResourceCount: 1, + }, + } + + c.JSON(http.StatusOK, res) +} + // encryptUserPassword checks if the password is already encrypted or not. If // not, it encrypts the password. func encryptUserPassword(user *models.User) error {