54 lines
1.4 KiB
Go
54 lines
1.4 KiB
Go
package authz
|
|
|
|
import (
|
|
"git.itzana.me/StrafesNET/dev-service/pkg/model"
|
|
"git.itzana.me/strafesnet/go-grpc/auth"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// Context keys
|
|
const (
|
|
ContextKeyUser = "user"
|
|
ContextKeyIsAdmin = "is_admin"
|
|
ContextKeyRoles = "roles"
|
|
ContextKeyProfile = "profile"
|
|
)
|
|
|
|
// GetAuthProfileFromContext retrieves the auth profile from the session context
|
|
func (s *Service) GetAuthProfileFromContext(ctx *gin.Context) (*auth.SessionUserResponse, bool) {
|
|
profile, exists := ctx.Get(ContextKeyProfile)
|
|
if !exists {
|
|
return nil, false
|
|
}
|
|
return profile.(*auth.SessionUserResponse), true
|
|
}
|
|
|
|
// GetRolesFromContext retrieves the roles of the user from the Gin context
|
|
func (s *Service) GetRolesFromContext(ctx *gin.Context) ([]model.UserRole, bool) {
|
|
roles, exists := ctx.Get(ContextKeyRoles)
|
|
if !exists {
|
|
return nil, false
|
|
}
|
|
return roles.([]model.UserRole), true
|
|
}
|
|
|
|
// GetUserAuthProfileFromContext retreives the r
|
|
|
|
// GetUserFromContext retrieves the User object from the Gin context
|
|
func (s *Service) GetUserFromContext(ctx *gin.Context) (*model.User, bool) {
|
|
user, exists := ctx.Get(ContextKeyUser)
|
|
if !exists {
|
|
return nil, false
|
|
}
|
|
return user.(*model.User), true
|
|
}
|
|
|
|
// ContextIsAdmin checks if the current user is an admin from the context
|
|
func (s *Service) ContextIsAdmin(ctx *gin.Context) bool {
|
|
isAdmin, exists := ctx.Get(ContextKeyIsAdmin)
|
|
if !exists {
|
|
return false
|
|
}
|
|
return isAdmin.(bool)
|
|
}
|