diff --git a/common/utilities.go b/common/utilities.go index 9ed6aa7..a4eff28 100644 --- a/common/utilities.go +++ b/common/utilities.go @@ -9,6 +9,9 @@ import ( "github.com/jinzhu/gorm" ) +const UserIDCtx = "user_id" +const UserRoleCtx = "user_role" + func ProvideErrorResponse(c *gin.Context, err error) bool { if err != nil { if err == gorm.ErrRecordNotFound { diff --git a/routes/user/userEndpoints.go b/routes/user/userEndpoints.go index 75aeb25..4607dbe 100644 --- a/routes/user/userEndpoints.go +++ b/routes/user/userEndpoints.go @@ -251,8 +251,8 @@ func updateUser(c *gin.Context) { // If the logged in user has NOT the same id as the user that is // going to be updated AND the role is NOT admin (is already saved // in the context from the Authentication middleware) - userID, _ := c.Get("user_id") - userRole, _ := c.Get("user_role") + userID, _ := c.Get(common.UserIDCtx) + userRole, _ := c.Get(common.UserRoleCtx) if toBeUpdatedID != userID && userRole != "Admin" { c.JSON(http.StatusForbidden, gin.H{ "success": false, diff --git a/routes/user/userMiddleware.go b/routes/user/userMiddleware.go index beb0470..40b83ff 100644 --- a/routes/user/userMiddleware.go +++ b/routes/user/userMiddleware.go @@ -2,6 +2,7 @@ package user import ( "fmt" + "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/common" "github.com/dgrijalva/jwt-go" "github.com/dgrijalva/jwt-go/request" "github.com/gin-gonic/gin" @@ -21,8 +22,8 @@ func userToContext(ctx *gin.Context, user_id uint) { return } - ctx.Set("user_role", user.Role) - ctx.Set("user_id", user_id) + ctx.Set(common.UserRoleCtx, user.Role) + ctx.Set(common.UserIDCtx, user_id) } func Authentication(unauthorized bool) gin.HandlerFunc {