Skip to main content

coil_auth/types/
entity.rs

1use super::*;
2
3#[derive(Debug, Clone, PartialEq, Eq, Hash)]
4pub enum Entity {
5    Tenant(String),
6    Site(String),
7    Brand(String),
8    Storefront(String),
9    User(String),
10    Group(String),
11    Team(String),
12    ServiceAccount(String),
13    Page(String),
14    Navigation(String),
15    Product(String),
16    Collection(String),
17    Order(String),
18    Subscription(String),
19    MembershipTier(String),
20    Event(String),
21    EventSlot(String),
22    Booking(String),
23    Media(String),
24    MediaLibrary(String),
25    Asset(String),
26    AssetFolder(String),
27    ThemeAssetBundle(String),
28    AdminModule(String),
29}
30
31impl Entity {
32    pub fn tenant(id: impl Into<String>) -> Self {
33        Self::Tenant(id.into())
34    }
35
36    pub fn site(id: impl Into<String>) -> Self {
37        Self::Site(id.into())
38    }
39
40    pub fn brand(id: impl Into<String>) -> Self {
41        Self::Brand(id.into())
42    }
43
44    pub fn storefront(id: impl Into<String>) -> Self {
45        Self::Storefront(id.into())
46    }
47
48    pub fn user(id: impl Into<String>) -> Self {
49        Self::User(id.into())
50    }
51
52    pub fn any_user() -> Self {
53        Self::User("*".into())
54    }
55
56    pub fn group(id: impl Into<String>) -> Self {
57        Self::Group(id.into())
58    }
59
60    pub fn team(id: impl Into<String>) -> Self {
61        Self::Team(id.into())
62    }
63
64    pub fn service_account(id: impl Into<String>) -> Self {
65        Self::ServiceAccount(id.into())
66    }
67
68    pub fn page(id: impl Into<String>) -> Self {
69        Self::Page(id.into())
70    }
71
72    pub fn navigation(id: impl Into<String>) -> Self {
73        Self::Navigation(id.into())
74    }
75
76    pub fn product(id: impl Into<String>) -> Self {
77        Self::Product(id.into())
78    }
79
80    pub fn collection(id: impl Into<String>) -> Self {
81        Self::Collection(id.into())
82    }
83
84    pub fn order(id: impl Into<String>) -> Self {
85        Self::Order(id.into())
86    }
87
88    pub fn subscription(id: impl Into<String>) -> Self {
89        Self::Subscription(id.into())
90    }
91
92    pub fn membership_tier(id: impl Into<String>) -> Self {
93        Self::MembershipTier(id.into())
94    }
95
96    pub fn event(id: impl Into<String>) -> Self {
97        Self::Event(id.into())
98    }
99
100    pub fn event_slot(id: impl Into<String>) -> Self {
101        Self::EventSlot(id.into())
102    }
103
104    pub fn booking(id: impl Into<String>) -> Self {
105        Self::Booking(id.into())
106    }
107
108    pub fn media(id: impl Into<String>) -> Self {
109        Self::Media(id.into())
110    }
111
112    pub fn media_library(id: impl Into<String>) -> Self {
113        Self::MediaLibrary(id.into())
114    }
115
116    pub fn asset(id: impl Into<String>) -> Self {
117        Self::Asset(id.into())
118    }
119
120    pub fn asset_folder(id: impl Into<String>) -> Self {
121        Self::AssetFolder(id.into())
122    }
123
124    pub fn theme_asset_bundle(id: impl Into<String>) -> Self {
125        Self::ThemeAssetBundle(id.into())
126    }
127
128    pub fn admin_module(id: impl Into<String>) -> Self {
129        Self::AdminModule(id.into())
130    }
131
132    pub const fn namespace(&self) -> Namespace {
133        match self {
134            Self::Tenant(_) => Namespace::Tenant,
135            Self::Site(_) => Namespace::Site,
136            Self::Brand(_) => Namespace::Brand,
137            Self::Storefront(_) => Namespace::Storefront,
138            Self::User(_) => Namespace::User,
139            Self::Group(_) => Namespace::Group,
140            Self::Team(_) => Namespace::Team,
141            Self::ServiceAccount(_) => Namespace::ServiceAccount,
142            Self::Page(_) => Namespace::Page,
143            Self::Navigation(_) => Namespace::Navigation,
144            Self::Product(_) => Namespace::Product,
145            Self::Collection(_) => Namespace::Collection,
146            Self::Order(_) => Namespace::Order,
147            Self::Subscription(_) => Namespace::Subscription,
148            Self::MembershipTier(_) => Namespace::MembershipTier,
149            Self::Event(_) => Namespace::Event,
150            Self::EventSlot(_) => Namespace::EventSlot,
151            Self::Booking(_) => Namespace::Booking,
152            Self::Media(_) => Namespace::Media,
153            Self::MediaLibrary(_) => Namespace::MediaLibrary,
154            Self::Asset(_) => Namespace::Asset,
155            Self::AssetFolder(_) => Namespace::AssetFolder,
156            Self::ThemeAssetBundle(_) => Namespace::ThemeAssetBundle,
157            Self::AdminModule(_) => Namespace::AdminModule,
158        }
159    }
160
161    pub fn id(&self) -> &str {
162        match self {
163            Self::Tenant(id)
164            | Self::Site(id)
165            | Self::Brand(id)
166            | Self::Storefront(id)
167            | Self::User(id)
168            | Self::Group(id)
169            | Self::Team(id)
170            | Self::ServiceAccount(id)
171            | Self::Page(id)
172            | Self::Navigation(id)
173            | Self::Product(id)
174            | Self::Collection(id)
175            | Self::Order(id)
176            | Self::Subscription(id)
177            | Self::MembershipTier(id)
178            | Self::Event(id)
179            | Self::EventSlot(id)
180            | Self::Booking(id)
181            | Self::Media(id)
182            | Self::MediaLibrary(id)
183            | Self::Asset(id)
184            | Self::AssetFolder(id)
185            | Self::ThemeAssetBundle(id)
186            | Self::AdminModule(id) => id,
187        }
188    }
189
190    pub fn to_object(&self) -> Object {
191        Object {
192            namespace: self.namespace().to_string(),
193            id: self.id().to_owned(),
194        }
195    }
196
197    pub fn from_object(object: &Object) -> Option<Self> {
198        let id = object.id.clone();
199
200        match Namespace::from_str(&object.namespace)? {
201            Namespace::Tenant => Some(Self::Tenant(id)),
202            Namespace::Site => Some(Self::Site(id)),
203            Namespace::Brand => Some(Self::Brand(id)),
204            Namespace::Storefront => Some(Self::Storefront(id)),
205            Namespace::User => Some(Self::User(id)),
206            Namespace::Group => Some(Self::Group(id)),
207            Namespace::Team => Some(Self::Team(id)),
208            Namespace::ServiceAccount => Some(Self::ServiceAccount(id)),
209            Namespace::Page => Some(Self::Page(id)),
210            Namespace::Navigation => Some(Self::Navigation(id)),
211            Namespace::Product => Some(Self::Product(id)),
212            Namespace::Collection => Some(Self::Collection(id)),
213            Namespace::Order => Some(Self::Order(id)),
214            Namespace::Subscription => Some(Self::Subscription(id)),
215            Namespace::MembershipTier => Some(Self::MembershipTier(id)),
216            Namespace::Event => Some(Self::Event(id)),
217            Namespace::EventSlot => Some(Self::EventSlot(id)),
218            Namespace::Booking => Some(Self::Booking(id)),
219            Namespace::Media => Some(Self::Media(id)),
220            Namespace::MediaLibrary => Some(Self::MediaLibrary(id)),
221            Namespace::Asset => Some(Self::Asset(id)),
222            Namespace::AssetFolder => Some(Self::AssetFolder(id)),
223            Namespace::ThemeAssetBundle => Some(Self::ThemeAssetBundle(id)),
224            Namespace::AdminModule => Some(Self::AdminModule(id)),
225        }
226    }
227
228    pub fn as_subject(&self) -> DefaultSubject {
229        DefaultSubject::Entity(self.clone())
230    }
231}
232
233impl fmt::Display for Entity {
234    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
235        write!(f, "{}:{}", self.namespace(), self.id())
236    }
237}
238
239impl From<&Entity> for Object {
240    fn from(value: &Entity) -> Self {
241        value.to_object()
242    }
243}
244
245impl From<Entity> for Object {
246    fn from(value: Entity) -> Self {
247        value.to_object()
248    }
249}