pub struct Tenancy { /* private fields */ }Expand description
Organizations, memberships and invitations over one database.
Implementations§
Source§impl Tenancy
impl Tenancy
Sourcepub fn new(db: DatabaseConnection) -> Self
pub fn new(db: DatabaseConnection) -> Self
New store over db.
Sourcepub async fn ensure_tables(&self) -> Result<(), DbErr>
pub async fn ensure_tables(&self) -> Result<(), DbErr>
Create the three tables if missing.
Sourcepub async fn create_org(
&self,
owner_id: i64,
name: &str,
) -> Result<Model, TenancyError>
pub async fn create_org( &self, owner_id: i64, name: &str, ) -> Result<Model, TenancyError>
Create an org with owner_id as its first owner.
Sourcepub async fn role(
&self,
org_id: i64,
user_id: i64,
) -> Result<Option<Role>, DbErr>
pub async fn role( &self, org_id: i64, user_id: i64, ) -> Result<Option<Role>, DbErr>
user_id’s role in org_id, None when not a member (or the org does not exist).
Sourcepub async fn orgs_of(&self, user_id: i64) -> Result<Vec<(Model, Role)>, DbErr>
pub async fn orgs_of(&self, user_id: i64) -> Result<Vec<(Model, Role)>, DbErr>
Every org user_id belongs to, with their role, oldest org first.
Sourcepub async fn members(&self, org_id: i64) -> Result<Vec<Model>, DbErr>
pub async fn members(&self, org_id: i64) -> Result<Vec<Model>, DbErr>
The members of org_id, in join order.
Sourcepub async fn add_member(
&self,
org_id: i64,
user_id: i64,
role: Role,
) -> Result<(), TenancyError>
pub async fn add_member( &self, org_id: i64, user_id: i64, role: Role, ) -> Result<(), TenancyError>
Add user_id to org_id. Errors if already a member.
Sourcepub async fn set_role(
&self,
org_id: i64,
user_id: i64,
role: Role,
) -> Result<(), TenancyError>
pub async fn set_role( &self, org_id: i64, user_id: i64, role: Role, ) -> Result<(), TenancyError>
Change a member’s role. Refuses to demote the last owner. Demoting below admin also revokes the invitations that member sent.
Sourcepub async fn change_role(
&self,
org_id: i64,
user_id: i64,
from: Role,
to: Role,
) -> Result<bool, TenancyError>
pub async fn change_role( &self, org_id: i64, user_id: i64, from: Role, to: Role, ) -> Result<bool, TenancyError>
Like Self::set_role, but only if the member still has role from: false when it changed in the
meantime. Use it after checking permissions on a role you read earlier, so a concurrent change
(an owner promoted, a member removed) cannot be overwritten on the strength of a stale read.
Sourcepub async fn remove_member(
&self,
org_id: i64,
user_id: i64,
) -> Result<(), TenancyError>
pub async fn remove_member( &self, org_id: i64, user_id: i64, ) -> Result<(), TenancyError>
Remove a member (and the invitations they sent). Refuses to remove the last owner.
Sourcepub async fn delete_org(&self, org_id: i64) -> Result<(), DbErr>
pub async fn delete_org(&self, org_id: i64) -> Result<(), DbErr>
Delete an org with its memberships and invitations. Your own tenant-owned rows
(org_id columns) are yours to delete first.
Sourcepub async fn invite(
&self,
org_id: i64,
invited_by: i64,
email: &str,
role: Role,
ttl: Duration,
) -> Result<String, TenancyError>
pub async fn invite( &self, org_id: i64, invited_by: i64, email: &str, role: Role, ttl: Duration, ) -> Result<String, TenancyError>
Invite email to org_id (not as owner). Returns the plaintext token to embed in the
emailed link; only its hash is stored. A new invite for the same org and email replaces
the earlier one.
Sourcepub async fn accept(
&self,
token: &str,
user_id: i64,
user_email: &str,
) -> Result<Option<i64>, TenancyError>
pub async fn accept( &self, token: &str, user_id: i64, user_email: &str, ) -> Result<Option<i64>, TenancyError>
Redeem an invitation for the signed-in user. Some(org_id) exactly once; None if the
token is unknown, expired or used, or user_email is not the invited address (that case
does not burn the token, so a forwarded link is useless to anyone else). Joining an org
you already belong to consumes the invite and keeps your current role.
Sourcepub async fn pending_invites(&self, org_id: i64) -> Result<Vec<Model>, DbErr>
pub async fn pending_invites(&self, org_id: i64) -> Result<Vec<Model>, DbErr>
Pending (unexpired) invitations of org_id, newest first. The token hash is not a usable credential.
Sourcepub async fn revoke_invite(
&self,
org_id: i64,
invite_id: i64,
) -> Result<bool, DbErr>
pub async fn revoke_invite( &self, org_id: i64, invite_id: i64, ) -> Result<bool, DbErr>
Revoke an invitation; false when it is not one of org_id’s (never touches another org’s).
Sourcepub async fn prune_invites(&self) -> Result<u64, DbErr>
pub async fn prune_invites(&self) -> Result<u64, DbErr>
Delete expired invitations; returns how many.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Tenancy
impl !UnwindSafe for Tenancy
impl Freeze for Tenancy
impl Send for Tenancy
impl Sync for Tenancy
impl Unpin for Tenancy
impl UnsafeUnpin for Tenancy
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more