Skip to main content

fastauth_plugin_organization/endpoints/
server.rs

1//! Server-only endpoints: no HTTP route, called through `auth.organization()`. `init`
2//! declares them with `server_endpoint`, so hooks can target them.
3
4use std::collections::BTreeMap;
5
6use fastauth_core::plugin_prelude::*;
7
8use super::members::allows_map;
9use super::{
10    RoleInput, checked_roles, find_member, insert_member, organization, plugin, user_not_found,
11};
12use crate::endpoints::invitations::{InvitationWithOrganization, invitations_for};
13use crate::model::Member;
14
15/// A membership to add without an invitation.
16#[derive(Clone, Debug, Deserialize)]
17pub struct NewMember {
18    pub organization_id: Id,
19    pub user_id: Id,
20    pub role: RoleInput,
21}
22
23/// Adds a user to an organization directly, Better Auth's server-only `addMember`.
24#[derive(Clone, Copy, Debug, Default)]
25pub struct AddMember;
26
27impl<D: Adapter> Endpoint<D> for AddMember {
28    const METHOD: Method = Method::POST;
29    const PATH: &'static str = "/organization/add-member";
30    type Access = Public;
31    type Input = NewMember;
32    type Output = Member;
33
34    async fn call(&self, ctx: Ctx<D, Public>, input: NewMember) -> Result<Member, AuthError> {
35        let role = checked_roles(plugin(&ctx), &input.role)?;
36        ctx.internal()
37            .find_user_by_id(&input.user_id)
38            .await?
39            .ok_or_else(user_not_found)?;
40        organization(&ctx, &input.organization_id).await?;
41        insert_member(&ctx, &input.organization_id, &input.user_id, role).await
42    }
43}
44
45/// A permission question about a user, without a session.
46#[derive(Clone, Debug, Deserialize)]
47pub struct PermissionCheck {
48    pub organization_id: Id,
49    pub user_id: Id,
50    pub permissions: BTreeMap<String, Vec<String>>,
51}
52
53/// Whether the user's role in the organization grants the permissions; `false` for
54/// non-members.
55#[derive(Clone, Copy, Debug, Default)]
56pub struct CheckPermission;
57
58impl<D: Adapter> Endpoint<D> for CheckPermission {
59    const METHOD: Method = Method::POST;
60    const PATH: &'static str = "/organization/has-permission";
61    type Access = Public;
62    type Input = PermissionCheck;
63    type Output = bool;
64
65    async fn call(&self, ctx: Ctx<D, Public>, input: PermissionCheck) -> Result<bool, AuthError> {
66        Ok(find_member(&ctx, &input.organization_id, &input.user_id)
67            .await?
68            .is_some_and(|m| allows_map(&plugin(&ctx).roles, &m.role, &input.permissions)))
69    }
70}
71
72/// Input of [`UserInvitations`].
73#[derive(Clone, Debug, Deserialize)]
74pub struct EmailQuery {
75    pub email: String,
76}
77
78/// Pending invitations addressed to any email.
79#[derive(Clone, Copy, Debug, Default)]
80pub struct UserInvitations;
81
82impl<D: Adapter> Endpoint<D> for UserInvitations {
83    const METHOD: Method = Method::GET;
84    const PATH: &'static str = "/organization/list-user-invitations";
85    type Access = Public;
86    type Input = EmailQuery;
87    type Output = Vec<InvitationWithOrganization>;
88
89    async fn call(
90        &self,
91        ctx: Ctx<D, Public>,
92        input: EmailQuery,
93    ) -> Result<Vec<InvitationWithOrganization>, AuthError> {
94        invitations_for(&ctx, &input.email).await
95    }
96}