89 lines
1.6 KiB
Go
89 lines
1.6 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
|
|
IsPartnerAdmin() bool
|
|
IsCasheer() bool
|
|
GetPartnerID() *int64
|
|
GetSiteID() *int64
|
|
GetName() string
|
|
}
|
|
|
|
type MyContextImpl struct {
|
|
context.Context
|
|
|
|
requestedBy int64
|
|
requestID string
|
|
partnerID int64
|
|
roleID int
|
|
siteID int64
|
|
name string
|
|
}
|
|
|
|
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) IsPartnerAdmin() bool {
|
|
return m.roleID == int(role.PartnerAdmin)
|
|
}
|
|
|
|
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 (m *MyContextImpl) GetName() string {
|
|
return m.name
|
|
}
|
|
|
|
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,
|
|
name: claims.Name,
|
|
}, nil
|
|
}
|
|
|
|
func NewContext(parent context.Context) *MyContextImpl {
|
|
return &MyContextImpl{
|
|
Context: parent,
|
|
}
|
|
}
|