fastauth_plugin_organization/
lib.rs1pub mod access;
47mod api;
48mod endpoints;
49mod error;
50#[cfg(feature = "axum")]
51pub mod extract;
52mod model;
53
54use fastauth_core::config::CoreConfig;
55use fastauth_core::plugin_prelude::*;
56
57pub use access::{Permission, Roles};
58pub use api::{OrganizationApi, OrganizationExt};
59pub use endpoints::server::{
60 AddMember, CheckPermission, EmailQuery, NewMember, PermissionCheck, UserInvitations,
61};
62pub use endpoints::*;
63pub use error::OrganizationError;
64pub use model::{
65 Invitation, InvitationStatus, Member, MemberUser, MemberWithUser, OrganizationRow,
66 OrganizationSession,
67};
68
69#[derive(Clone, Debug)]
71pub struct Organization {
72 pub(crate) roles: Roles,
73 pub(crate) allow_user_to_create_organization: bool,
74 pub(crate) organization_limit: Option<u64>,
75 pub(crate) invitation_ttl: Duration,
76 pub(crate) invitation_url: Option<String>,
77 pub(crate) require_email_verification_on_invitation: bool,
78}
79
80impl Default for Organization {
81 fn default() -> Self {
82 Self {
83 roles: Roles::new(),
84 allow_user_to_create_organization: true,
85 organization_limit: None,
86 invitation_ttl: Duration::hours(48),
87 invitation_url: None,
88 require_email_verification_on_invitation: false,
89 }
90 }
91}
92
93impl Organization {
94 pub fn roles(mut self, roles: Roles) -> Self {
96 self.roles = roles;
97 self
98 }
99
100 pub fn allow_user_to_create_organization(mut self, allow: bool) -> Self {
103 self.allow_user_to_create_organization = allow;
104 self
105 }
106
107 pub fn organization_limit(mut self, limit: u64) -> Self {
110 self.organization_limit = Some(limit);
111 self
112 }
113
114 pub fn invitation_ttl(mut self, ttl: Duration) -> Self {
116 self.invitation_ttl = ttl;
117 self
118 }
119
120 pub fn invitation_url(mut self, template: impl Into<String>) -> Self {
124 self.invitation_url = Some(template.into());
125 self
126 }
127
128 pub fn require_email_verification_on_invitation(mut self, require: bool) -> Self {
131 self.require_email_verification_on_invitation = require;
132 self
133 }
134
135 pub(crate) fn invitation_url_for(&self, config: &CoreConfig, id: &Id) -> String {
136 match &self.invitation_url {
137 Some(template) => template.replace("{id}", id.as_str()),
138 None => format!(
139 "{}/accept-invitation/{id}",
140 config.base_url.as_str().trim_end_matches('/')
141 ),
142 }
143 }
144}
145
146impl Plugin for Organization {
147 const ID: &'static str = "organization";
148 type Requires = ();
149 type UserFields = ();
150 type SessionFields = OrganizationSession;
151
152 fn schema(&self, schema: &mut SchemaBuilder) {
153 schema
154 .model::<OrganizationRow>()
155 .model::<Member>()
156 .model::<Invitation>();
157 }
158
159 fn init<D: Adapter>(self: &Arc<Self>, app: &mut PluginBuilder<'_, D>) {
160 app.endpoint(CreateOrganization::client())
161 .endpoint(UpdateOrganization)
162 .endpoint(DeleteOrganization)
163 .endpoint(ListOrganizations)
164 .endpoint(GetFullOrganization)
165 .endpoint(SetActiveOrganization)
166 .endpoint(CheckSlug)
167 .endpoint(InviteMember)
168 .endpoint(AcceptInvitation)
169 .endpoint(RejectInvitation)
170 .endpoint(CancelInvitation)
171 .endpoint(GetInvitation)
172 .endpoint(ListInvitations)
173 .endpoint(ListUserInvitations)
174 .endpoint(RemoveMember)
175 .endpoint(UpdateMemberRole)
176 .endpoint(LeaveOrganization)
177 .endpoint(GetActiveMember)
178 .endpoint(ListMembers)
179 .endpoint(HasPermission)
180 .server_endpoint::<AddMember>()
181 .server_endpoint::<CheckPermission>()
182 .server_endpoint::<UserInvitations>()
183 .errors::<OrganizationError>();
184 }
185}
186
187#[derive(Clone, Debug, PartialEq, Eq)]
189pub struct OrganizationCreated {
190 pub organization_id: Id,
191 pub user_id: Id,
193}
194
195#[derive(Clone, Debug, PartialEq, Eq)]
198pub struct MemberAdded {
199 pub organization_id: Id,
200 pub user_id: Id,
201 pub member_id: Id,
202 pub role: String,
203}
204
205#[derive(Clone, Debug, PartialEq, Eq)]
208pub struct MemberRemoved {
209 pub organization_id: Id,
210 pub user_id: Id,
211 pub member_id: Id,
212}
213
214#[derive(Clone, Debug, PartialEq, Eq)]
216pub struct InvitationAccepted {
217 pub invitation_id: Id,
218 pub organization_id: Id,
219 pub user_id: Id,
220 pub member_id: Id,
221}
222
223impl Event for OrganizationCreated {}
224impl Event for MemberAdded {}
225impl Event for MemberRemoved {}
226impl Event for InvitationAccepted {}