mirror of
https://git.rwth-aachen.de/acs/public/villas/web-backend-go/
synced 2025-03-30 00:00:12 +01:00
Adds global constant strings for Context custom variables
This commit is contained in:
parent
83444505b0
commit
1b39f2fd83
3 changed files with 8 additions and 4 deletions
|
@ -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 {
|
||||||
|
|
|
@ -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,
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
Loading…
Add table
Reference in a new issue