49 lines
858 B
Go
Raw Normal View History

2023-10-08 15:59:42 +07:00
package mycontext
import (
"context"
"furtuna-be/internal/constants/role"
"furtuna-be/internal/entity"
)
type ContextKey string
type Context interface {
context.Context
RequestedBy() int64
IsSuperAdmin() bool
}
type MyContextImpl struct {
context.Context
requestedBy int64
requestID string
branchID int64
roleID int
}
func (m *MyContextImpl) RequestedBy() int64 {
return m.requestedBy
}
func (m *MyContextImpl) IsSuperAdmin() bool {
return m.roleID == int(role.SuperAdmin)
}
func NewMyContext(parent context.Context, claims *entity.JWTAuthClaims) (*MyContextImpl, error) {
return &MyContextImpl{
Context: parent,
requestedBy: claims.UserID,
branchID: claims.BranchID,
roleID: claims.Role,
}, nil
}
func NewContext(parent context.Context) *MyContextImpl {
return &MyContextImpl{
Context: parent,
}
}