Adds global constant strings for Context custom variables

This commit is contained in:
smavros 2019-06-05 10:03:12 +02:00
parent 83444505b0
commit 1b39f2fd83
3 changed files with 8 additions and 4 deletions

View file

@ -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 {

View file

@ -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,

View file

@ -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 {