2024-02-27 14:14:30 +01:00
|
|
|
package user
|
2023-03-07 23:10:42 +01:00
|
|
|
|
2024-02-26 18:20:40 +01:00
|
|
|
import (
|
2024-03-03 18:40:56 +01:00
|
|
|
"encoding/json"
|
|
|
|
|
2024-02-26 18:20:40 +01:00
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/auth"
|
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/datastore"
|
|
|
|
)
|
2023-03-07 23:10:42 +01:00
|
|
|
|
|
|
|
type Role string
|
|
|
|
|
|
|
|
const (
|
|
|
|
RoleWriter Role = "writer"
|
|
|
|
RoleReader Role = "reader"
|
2024-02-27 14:14:30 +01:00
|
|
|
RoleAdmin Role = "admin"
|
2023-03-07 23:10:42 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func isValidRole(r string) bool {
|
|
|
|
rr := Role(r)
|
|
|
|
|
2024-02-27 14:14:30 +01:00
|
|
|
return rr == RoleWriter || rr == RoleReader || rr == RoleAdmin
|
2023-03-07 23:10:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type User struct {
|
2024-02-26 18:20:40 +01:00
|
|
|
subject string
|
|
|
|
tenantID datastore.TenantID
|
|
|
|
role Role
|
2023-03-07 23:10:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Subject implements auth.User
|
|
|
|
func (u *User) Subject() string {
|
|
|
|
return u.subject
|
|
|
|
}
|
|
|
|
|
2024-02-26 18:20:40 +01:00
|
|
|
// Tenant implements auth.User
|
|
|
|
func (u *User) Tenant() datastore.TenantID {
|
|
|
|
return u.tenantID
|
|
|
|
}
|
|
|
|
|
2023-03-07 23:10:42 +01:00
|
|
|
func (u *User) Role() Role {
|
|
|
|
return u.role
|
|
|
|
}
|
|
|
|
|
2024-03-03 18:40:56 +01:00
|
|
|
func (u *User) MarshalJSON() ([]byte, error) {
|
|
|
|
type user struct {
|
|
|
|
Subject string `json:"subject"`
|
|
|
|
Tenant string `json:"tenant"`
|
|
|
|
Role string `json:"role"`
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonUser := user{
|
|
|
|
Subject: u.Subject(),
|
|
|
|
Tenant: string(u.Tenant()),
|
|
|
|
Role: string(u.Role()),
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.Marshal(jsonUser)
|
|
|
|
}
|
|
|
|
|
2023-03-07 23:10:42 +01:00
|
|
|
var _ auth.User = &User{}
|