pub mod access;
mod api;
mod endpoints;
mod error;
#[cfg(feature = "axum")]
pub mod extract;
mod model;
use fastauth_core::config::CoreConfig;
use fastauth_core::plugin_prelude::*;
pub use access::{Permission, Roles};
pub use api::{OrganizationApi, OrganizationExt};
pub use endpoints::server::{
AddMember, CheckPermission, EmailQuery, NewMember, PermissionCheck, UserInvitations,
};
pub use endpoints::*;
pub use error::OrganizationError;
pub use model::{
Invitation, InvitationStatus, Member, MemberUser, MemberWithUser, OrganizationRow,
OrganizationSession,
};
#[derive(Clone, Debug)]
pub struct Organization {
pub(crate) roles: Roles,
pub(crate) allow_user_to_create_organization: bool,
pub(crate) organization_limit: Option<u64>,
pub(crate) invitation_ttl: Duration,
pub(crate) invitation_url: Option<String>,
pub(crate) require_email_verification_on_invitation: bool,
}
impl Default for Organization {
fn default() -> Self {
Self {
roles: Roles::new(),
allow_user_to_create_organization: true,
organization_limit: None,
invitation_ttl: Duration::hours(48),
invitation_url: None,
require_email_verification_on_invitation: true,
}
}
}
impl Organization {
pub fn roles(mut self, roles: Roles) -> Self {
self.roles = roles;
self
}
pub fn allow_user_to_create_organization(mut self, allow: bool) -> Self {
self.allow_user_to_create_organization = allow;
self
}
pub fn organization_limit(mut self, limit: u64) -> Self {
self.organization_limit = Some(limit);
self
}
pub fn invitation_ttl(mut self, ttl: Duration) -> Self {
self.invitation_ttl = ttl;
self
}
pub fn invitation_url(mut self, template: impl Into<String>) -> Self {
self.invitation_url = Some(template.into());
self
}
pub fn require_email_verification_on_invitation(mut self, require: bool) -> Self {
self.require_email_verification_on_invitation = require;
self
}
pub(crate) fn invitation_url_for(&self, config: &CoreConfig, id: &Id) -> String {
match &self.invitation_url {
Some(template) => template.replace("{id}", id.as_str()),
None => format!(
"{}/accept-invitation/{id}",
config.base_url.as_str().trim_end_matches('/')
),
}
}
}
impl Plugin for Organization {
const ID: &'static str = "organization";
type Requires = ();
type UserFields = ();
type SessionFields = OrganizationSession;
fn schema(&self, schema: &mut SchemaBuilder) {
schema
.model::<OrganizationRow>()
.model::<Member>()
.model::<Invitation>();
}
fn init<D: Adapter>(self: &Arc<Self>, app: &mut PluginBuilder<'_, D>) {
app.endpoint(CreateOrganization::client())
.endpoint(UpdateOrganization)
.endpoint(DeleteOrganization)
.endpoint(ListOrganizations)
.endpoint(GetFullOrganization)
.endpoint(SetActiveOrganization)
.endpoint(CheckSlug)
.endpoint(InviteMember)
.endpoint(AcceptInvitation)
.endpoint(RejectInvitation)
.endpoint(CancelInvitation)
.endpoint(GetInvitation)
.endpoint(ListInvitations)
.endpoint(ListUserInvitations)
.endpoint(RemoveMember)
.endpoint(UpdateMemberRole)
.endpoint(LeaveOrganization)
.endpoint(GetActiveMember)
.endpoint(ListMembers)
.endpoint(HasPermission)
.server_endpoint::<AddMember>()
.server_endpoint::<CheckPermission>()
.server_endpoint::<UserInvitations>()
.errors::<OrganizationError>();
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct OrganizationCreated {
pub organization_id: Id,
pub user_id: Id,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MemberAdded {
pub organization_id: Id,
pub user_id: Id,
pub member_id: Id,
pub role: String,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MemberRemoved {
pub organization_id: Id,
pub user_id: Id,
pub member_id: Id,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct InvitationAccepted {
pub invitation_id: Id,
pub organization_id: Id,
pub user_id: Id,
pub member_id: Id,
}
impl Event for OrganizationCreated {}
impl Event for MemberAdded {}
impl Event for MemberRemoved {}
impl Event for InvitationAccepted {}