2024-07-26 22:32:24 +07:00

77 lines
1.4 KiB
Go

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
IsAdmin() bool
IsCasheer() bool
GetPartnerID() *int64
GetSiteID() *int64
}
type MyContextImpl struct {
context.Context
requestedBy int64
requestID string
partnerID int64
roleID int
siteID int64
}
func (m *MyContextImpl) RequestedBy() int64 {
return m.requestedBy
}
func (m *MyContextImpl) IsSuperAdmin() bool {
return m.roleID == int(role.SuperAdmin)
}
func (m *MyContextImpl) IsAdmin() bool {
return m.roleID == int(role.SuperAdmin) || m.roleID == int(role.Admin)
}
func (m *MyContextImpl) IsCasheer() bool {
return m.roleID == int(role.Casheer)
}
func (m *MyContextImpl) GetPartnerID() *int64 {
if m.partnerID != 0 {
return &m.partnerID
}
return nil
}
func (m *MyContextImpl) GetSiteID() *int64 {
if m.siteID != 0 {
return &m.siteID
}
return nil
}
func NewMyContext(parent context.Context, claims *entity.JWTAuthClaims) (*MyContextImpl, error) {
return &MyContextImpl{
Context: parent,
requestedBy: claims.UserID,
partnerID: claims.PartnerID,
roleID: claims.Role,
siteID: claims.SiteID,
}, nil
}
func NewContext(parent context.Context) *MyContextImpl {
return &MyContextImpl{
Context: parent,
}
}