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" "github.com/jinzhu/gorm"
) )
const UserIDCtx = "user_id"
const UserRoleCtx = "user_role"
func ProvideErrorResponse(c *gin.Context, err error) bool { func ProvideErrorResponse(c *gin.Context, err error) bool {
if err != nil { if err != nil {
if err == gorm.ErrRecordNotFound { 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 // 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 // going to be updated AND the role is NOT admin (is already saved
// in the context from the Authentication middleware) // in the context from the Authentication middleware)
userID, _ := c.Get("user_id") userID, _ := c.Get(common.UserIDCtx)
userRole, _ := c.Get("user_role") userRole, _ := c.Get(common.UserRoleCtx)
if toBeUpdatedID != userID && userRole != "Admin" { if toBeUpdatedID != userID && userRole != "Admin" {
c.JSON(http.StatusForbidden, gin.H{ c.JSON(http.StatusForbidden, gin.H{
"success": false, "success": false,

View file

@ -2,6 +2,7 @@ package user
import ( import (
"fmt" "fmt"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/common"
"github.com/dgrijalva/jwt-go" "github.com/dgrijalva/jwt-go"
"github.com/dgrijalva/jwt-go/request" "github.com/dgrijalva/jwt-go/request"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -21,8 +22,8 @@ func userToContext(ctx *gin.Context, user_id uint) {
return return
} }
ctx.Set("user_role", user.Role) ctx.Set(common.UserRoleCtx, user.Role)
ctx.Set("user_id", user_id) ctx.Set(common.UserIDCtx, user_id)
} }
func Authentication(unauthorized bool) gin.HandlerFunc { func Authentication(unauthorized bool) gin.HandlerFunc {