89 lines
1.6 KiB
Go
Raw Normal View History

2023-10-08 15:59:42 +07:00
package mycontext
import (
"context"
2025-03-04 20:36:17 +07:00
"enaklo-pos-be/internal/constants/role"
"enaklo-pos-be/internal/entity"
2023-10-08 15:59:42 +07:00
)
type ContextKey string
type Context interface {
context.Context
RequestedBy() int64
IsSuperAdmin() bool
2024-07-26 22:32:24 +07:00
IsAdmin() bool
2024-07-30 23:51:53 +07:00
IsPartnerAdmin() bool
2024-06-04 02:59:31 +07:00
IsCasheer() bool
2024-06-03 14:40:50 +07:00
GetPartnerID() *int64
2024-06-05 00:24:53 +07:00
GetSiteID() *int64
2024-08-21 15:48:50 +07:00
GetName() string
2023-10-08 15:59:42 +07:00
}
type MyContextImpl struct {
context.Context
requestedBy int64
requestID string
2024-06-03 14:40:50 +07:00
partnerID int64
2023-10-08 15:59:42 +07:00
roleID int
2024-06-05 00:24:53 +07:00
siteID int64
2024-08-21 15:48:50 +07:00
name string
2023-10-08 15:59:42 +07:00
}
func (m *MyContextImpl) RequestedBy() int64 {
return m.requestedBy
}
func (m *MyContextImpl) IsSuperAdmin() bool {
return m.roleID == int(role.SuperAdmin)
}
2024-07-26 22:32:24 +07:00
func (m *MyContextImpl) IsAdmin() bool {
return m.roleID == int(role.SuperAdmin) || m.roleID == int(role.Admin)
}
2024-07-30 23:51:53 +07:00
func (m *MyContextImpl) IsPartnerAdmin() bool {
return m.roleID == int(role.PartnerAdmin)
}
2024-06-04 02:59:31 +07:00
func (m *MyContextImpl) IsCasheer() bool {
return m.roleID == int(role.Casheer)
}
2024-06-03 14:40:50 +07:00
func (m *MyContextImpl) GetPartnerID() *int64 {
if m.partnerID != 0 {
return &m.partnerID
}
return nil
}
2024-06-05 00:24:53 +07:00
func (m *MyContextImpl) GetSiteID() *int64 {
if m.siteID != 0 {
return &m.siteID
}
return nil
}
2024-08-21 15:48:50 +07:00
func (m *MyContextImpl) GetName() string {
return m.name
}
2023-10-08 15:59:42 +07:00
func NewMyContext(parent context.Context, claims *entity.JWTAuthClaims) (*MyContextImpl, error) {
return &MyContextImpl{
Context: parent,
requestedBy: claims.UserID,
2024-06-03 14:40:50 +07:00
partnerID: claims.PartnerID,
2023-10-08 15:59:42 +07:00
roleID: claims.Role,
2024-06-05 00:24:53 +07:00
siteID: claims.SiteID,
2024-08-21 15:48:50 +07:00
name: claims.Name,
2023-10-08 15:59:42 +07:00
}, nil
}
func NewContext(parent context.Context) *MyContextImpl {
return &MyContextImpl{
Context: parent,
}
}