fastauth_plugin_organization/
model.rs1use fastauth_core::plugin_prelude::*;
4use serde_json::{Map, Value as JsonValue};
5
6#[derive(Model, Clone, Debug, PartialEq, Serialize)]
8#[model(table = "organization")]
9#[serde(rename_all = "camelCase")]
10pub struct OrganizationRow {
11 #[model(id)]
12 pub id: Id,
13 pub name: String,
14 #[model(unique)]
16 pub slug: String,
17 pub logo: Option<String>,
18 pub metadata: Option<Json<Map<String, JsonValue>>>,
20 #[model(default = now)]
21 #[serde(with = "time::serde::rfc3339")]
22 pub created_at: OffsetDateTime,
23}
24
25#[derive(Model, Clone, Debug, PartialEq, Serialize)]
28#[model(table = "member")]
29#[serde(rename_all = "camelCase")]
30pub struct Member {
31 #[model(id)]
32 pub id: Id,
33 #[model(references = OrganizationRow, on_delete = Cascade, index)]
34 pub organization_id: Id,
35 #[model(references = User, on_delete = Cascade, index)]
36 pub user_id: Id,
37 #[model(default = "member")]
39 pub role: String,
40 #[model(default = now)]
41 #[serde(with = "time::serde::rfc3339")]
42 pub created_at: OffsetDateTime,
43}
44
45impl Member {
46 pub fn roles(&self) -> impl Iterator<Item = &str> {
48 crate::access::role_list(&self.role)
49 }
50
51 pub fn has_role(&self, role: &str) -> bool {
53 self.roles().any(|r| r == role)
54 }
55}
56
57#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
60#[serde(rename_all = "lowercase")]
61pub enum InvitationStatus {
62 Pending,
63 Accepted,
64 Rejected,
65 Canceled,
66}
67
68impl InvitationStatus {
69 pub const fn as_str(self) -> &'static str {
71 match self {
72 Self::Pending => "pending",
73 Self::Accepted => "accepted",
74 Self::Rejected => "rejected",
75 Self::Canceled => "canceled",
76 }
77 }
78}
79
80impl ColumnValue for InvitationStatus {
81 const TYPE: ColumnType = ColumnType::Text;
82
83 fn into_value(self) -> Value {
84 Value::Text(self.as_str().to_owned())
85 }
86
87 fn from_value(value: Value) -> Result<Self, String> {
88 match value {
89 Value::Text(text) => match text.as_str() {
90 "pending" => Ok(Self::Pending),
91 "accepted" => Ok(Self::Accepted),
92 "rejected" => Ok(Self::Rejected),
93 "canceled" => Ok(Self::Canceled),
94 other => Err(format!("unknown invitation status `{other}`")),
95 },
96 other => Err(format!("expected Text, got {other:?}")),
97 }
98 }
99}
100
101#[derive(Model, Clone, Debug, PartialEq, Serialize)]
103#[model(table = "invitation")]
104#[serde(rename_all = "camelCase")]
105pub struct Invitation {
106 #[model(id)]
107 pub id: Id,
108 #[model(references = OrganizationRow, on_delete = Cascade, index)]
109 pub organization_id: Id,
110 #[model(index)]
112 pub email: String,
113 pub role: String,
115 #[model(default = "pending")]
116 pub status: InvitationStatus,
117 #[model(references = User, on_delete = Cascade)]
118 pub inviter_id: Id,
119 #[serde(with = "time::serde::rfc3339")]
120 pub expires_at: OffsetDateTime,
121 #[model(default = now)]
122 #[serde(with = "time::serde::rfc3339")]
123 pub created_at: OffsetDateTime,
124}
125
126#[derive(Fields, Clone, Debug, Default, PartialEq)]
129#[fields(table = Session)]
130pub struct OrganizationSession {
131 pub active_organization_id: Option<Id>,
134}
135
136#[derive(Clone, Debug, PartialEq, Serialize)]
138pub struct MemberUser {
139 pub id: Id,
140 pub name: String,
141 pub email: String,
142 pub image: Option<String>,
143}
144
145#[derive(Clone, Debug, PartialEq, Serialize)]
148pub struct MemberWithUser {
149 #[serde(flatten)]
150 pub member: Member,
151 pub user: MemberUser,
152}