fastauth-plugin-organization 0.5.0

Organizations for fastauth: members, invitations, roles and permissions, wire-compatible with Better Auth.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
//! Creating, updating, deleting, listing and activating organizations.

use fastauth_core::adapter::Transaction;
use fastauth_core::plugin_prelude::*;
use fastauth_core::routes::Status;
use serde_json::{Map, Value as JsonValue};

use super::{
    active_organization, find_member, find_organization, find_organization_by_slug, nullable,
    organization, plugin, requested_organization, require_text, set_active, slug_taken,
    user_not_found, with_users,
};
use crate::access::OWNER;
use crate::model::{Invitation, Member, MemberWithUser, OrganizationRow, OrganizationSession};
use crate::{MemberAdded, Organization, OrganizationCreated, OrganizationError};

/// Body of `/organization/create`.
#[derive(Clone, Debug, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateOrganizationInput {
    pub name: String,
    pub slug: String,
    pub logo: Option<String>,
    pub metadata: Option<Map<String, JsonValue>>,
    /// Leave the session's active organization as it is instead of switching to the new one.
    #[serde(default)]
    pub keep_current_active_organization: bool,
    /// The creator, who becomes the owner, when the server API creates it. Never read from a
    /// request body: over HTTP (and `create_as`) the session's user is the creator.
    #[serde(skip)]
    pub user_id: Option<Id>,
}

/// An organization with its members: what `/organization/create` answers.
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct OrganizationWithMembers {
    #[serde(flatten)]
    pub organization: OrganizationRow,
    pub members: Vec<Member>,
}

/// `POST /organization/create`: creates an organization with the caller as its owner and
/// makes it the session's active organization. The server API's `create` runs the same
/// endpoint, so hooks on `CreateOrganization` see both.
#[derive(Clone, Copy, Debug)]
pub struct CreateOrganization {
    trusted: bool,
}

impl CreateOrganization {
    /// The HTTP route: the session's user creates it, if `allow_user_to_create_organization`.
    pub fn client() -> Self {
        Self { trusted: false }
    }

    /// The server API: `user_id` names the creator, no session is needed, and
    /// `allow_user_to_create_organization` doesn't apply.
    pub fn server() -> Self {
        Self { trusted: true }
    }
}

impl<D: Adapter> Endpoint<D> for CreateOrganization {
    const METHOD: Method = Method::POST;
    const PATH: &'static str = "/organization/create";
    type Access = MaybeAuthenticated;
    type Input = CreateOrganizationInput;
    type Output = OrganizationWithMembers;

    async fn call(
        &self,
        ctx: Ctx<D, MaybeAuthenticated>,
        mut input: CreateOrganizationInput,
    ) -> Result<OrganizationWithMembers, AuthError> {
        if self.trusted {
            let user_id = input.user_id.take().ok_or_else(AuthError::unauthorized)?;
            let user = ctx
                .internal()
                .find_user_by_id(&user_id)
                .await?
                .ok_or_else(user_not_found)?;
            return create(&ctx, &user.id, input).await;
        }
        let session = ctx.session().ok_or_else(AuthError::unauthorized)?;
        if !plugin(&ctx).allow_user_to_create_organization {
            return Err(OrganizationError::YouAreNotAllowedToCreateANewOrganization.into());
        }
        let keep_active = input.keep_current_active_organization;
        let created = create(&ctx, &session.user.id, input).await?;
        if !keep_active {
            set_active(&ctx, session, Some(&created.organization.id)).await?;
        }
        Ok(created)
    }
}

/// Creates the organization and its owner membership in one transaction.
async fn create<D: Adapter, A>(
    ctx: &Ctx<D, A>,
    user_id: &Id,
    input: CreateOrganizationInput,
) -> Result<OrganizationWithMembers, AuthError> {
    require_text(&input.name, "name")?;
    require_text(&input.slug, "slug")?;
    if let Some(limit) = plugin(ctx).organization_limit {
        let owned = ctx
            .db()
            .count::<Member>(Member::USER_ID.eq(user_id))
            .await?;
        if owned >= limit {
            return Err(OrganizationError::YouHaveReachedTheMaximumNumberOfOrganizations.into());
        }
    }
    if find_organization_by_slug(ctx, &input.slug).await?.is_some() {
        return Err(OrganizationError::OrganizationAlreadyExists.into());
    }
    let now = ctx.now();
    let organization = OrganizationRow {
        id: ctx.new_id()?,
        name: input.name,
        slug: input.slug,
        logo: input.logo,
        metadata: input.metadata.map(Json),
        created_at: now,
    };
    let member = Member {
        id: ctx.new_id()?,
        organization_id: organization.id.clone(),
        user_id: user_id.clone(),
        role: OWNER.to_owned(),
        created_at: now,
    };
    let tx = ctx.adapter().begin().await?;
    let db = ctx.db_on(&tx);
    let organization = db.insert(&organization).await.map_err(slug_taken)?;
    let member = db.insert(&member).await?;
    tx.commit().await?;
    ctx.emit(OrganizationCreated {
        organization_id: organization.id.clone(),
        user_id: user_id.clone(),
    });
    ctx.emit(MemberAdded {
        organization_id: organization.id.clone(),
        user_id: user_id.clone(),
        member_id: member.id.clone(),
        role: member.role.clone(),
    });
    Ok(OrganizationWithMembers {
        organization,
        members: vec![member],
    })
}

/// The fields `/organization/update` changes; absent fields stay as they are.
#[derive(Clone, Debug, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UpdateOrganizationData {
    pub name: Option<String>,
    pub slug: Option<String>,
    /// `null` removes the logo.
    #[serde(default, deserialize_with = "nullable")]
    pub logo: Option<Option<String>>,
    pub metadata: Option<Map<String, JsonValue>>,
}

/// Body of `/organization/update`.
#[derive(Clone, Debug, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UpdateOrganizationInput {
    pub data: UpdateOrganizationData,
    /// Defaults to the active organization.
    pub organization_id: Option<String>,
}

/// `POST /organization/update`: needs `organization: [update]`.
#[derive(Clone, Copy, Debug, Default)]
pub struct UpdateOrganization;

impl<D: Adapter> Endpoint<D> for UpdateOrganization {
    const METHOD: Method = Method::POST;
    const PATH: &'static str = "/organization/update";
    type Access = Authenticated;
    type Input = UpdateOrganizationInput;
    type Output = OrganizationRow;

    async fn call(
        &self,
        ctx: Ctx<D, Authenticated>,
        input: UpdateOrganizationInput,
    ) -> Result<OrganizationRow, AuthError> {
        let session = ctx.session();
        let organization_id = requested_organization(session, input.organization_id.as_deref())?
            .ok_or(OrganizationError::OrganizationNotFound)?;
        let member = find_member(&ctx, &organization_id, &session.user.id)
            .await?
            .ok_or(OrganizationError::UserIsNotAMemberOfTheOrganization)?;
        if !plugin(&ctx)
            .roles
            .allows(&member.role, &[("organization", &["update"])])
        {
            return Err(OrganizationError::YouAreNotAllowedToUpdateThisOrganization.into());
        }
        let data = input.data;
        if let Some(slug) = &data.slug {
            require_text(slug, "slug")?;
            if find_organization_by_slug(&ctx, slug)
                .await?
                .is_some_and(|o| o.id != organization_id)
            {
                return Err(OrganizationError::OrganizationSlugAlreadyTaken.into());
            }
        }
        let mut update = ctx
            .db()
            .update::<OrganizationRow>(OrganizationRow::ID.eq(&organization_id));
        let mut changed = false;
        if let Some(name) = data.name {
            require_text(&name, "name")?;
            update = update.set(OrganizationRow::NAME, name);
            changed = true;
        }
        if let Some(slug) = data.slug {
            update = update.set(OrganizationRow::SLUG, slug);
            changed = true;
        }
        if let Some(logo) = data.logo {
            update = update.set(OrganizationRow::LOGO, logo);
            changed = true;
        }
        if let Some(metadata) = data.metadata {
            update = update.set(OrganizationRow::METADATA, Some(Json(metadata)));
            changed = true;
        }
        if changed {
            update.exec().await.map_err(slug_taken)?;
        }
        organization(&ctx, &organization_id).await
    }
}

/// Body of `/organization/delete`.
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DeleteOrganizationInput {
    pub organization_id: String,
}

/// `POST /organization/delete`: needs `organization: [delete]`. Deletes the organization
/// with its members and invitations, and clears it as any session's active organization.
/// Answers the deleted organization.
#[derive(Clone, Copy, Debug, Default)]
pub struct DeleteOrganization;

impl<D: Adapter> Endpoint<D> for DeleteOrganization {
    const METHOD: Method = Method::POST;
    const PATH: &'static str = "/organization/delete";
    type Access = Authenticated;
    type Input = DeleteOrganizationInput;
    type Output = OrganizationRow;

    async fn call(
        &self,
        ctx: Ctx<D, Authenticated>,
        input: DeleteOrganizationInput,
    ) -> Result<OrganizationRow, AuthError> {
        let session = ctx.session();
        let organization_id = Id::from(input.organization_id);
        let member = find_member(&ctx, &organization_id, &session.user.id)
            .await?
            .ok_or(OrganizationError::UserIsNotAMemberOfTheOrganization)?;
        if !plugin(&ctx)
            .roles
            .allows(&member.role, &[("organization", &["delete"])])
        {
            return Err(OrganizationError::YouAreNotAllowedToDeleteThisOrganization.into());
        }
        let deleted = organization(&ctx, &organization_id).await?;
        delete(&ctx, &organization_id).await?;
        if active_organization(session)?.as_ref() == Some(&organization_id) {
            ctx.delete_cookie(super::SESSION_CACHE_COOKIE);
        }
        Ok(deleted)
    }
}

/// Deletes the organization, its members and invitations in one transaction, without
/// relying on the database's cascades, and clears it from sessions.
pub(crate) async fn delete<D: Adapter, A>(ctx: &Ctx<D, A>, id: &Id) -> Result<(), AuthError> {
    let tx = ctx.adapter().begin().await?;
    let db = ctx.db_on(&tx);
    db.delete::<Invitation>(Invitation::ORGANIZATION_ID.eq(id))
        .await?;
    db.delete::<Member>(Member::ORGANIZATION_ID.eq(id)).await?;
    db.delete::<OrganizationRow>(OrganizationRow::ID.eq(id))
        .await?;
    db.update::<Session>(OrganizationSession::ACTIVE_ORGANIZATION_ID.eq(Some(id.clone())))
        .set(OrganizationSession::ACTIVE_ORGANIZATION_ID, None::<Id>)
        .exec()
        .await?;
    tx.commit().await?;
    Ok(())
}

/// `GET /organization/list`: the organizations the caller is a member of.
#[derive(Clone, Copy, Debug, Default)]
pub struct ListOrganizations;

impl<D: Adapter> Endpoint<D> for ListOrganizations {
    const METHOD: Method = Method::GET;
    const PATH: &'static str = "/organization/list";
    type Access = Authenticated;
    type Input = Empty;
    type Output = Vec<OrganizationRow>;

    async fn call(
        &self,
        ctx: Ctx<D, Authenticated>,
        _input: Empty,
    ) -> Result<Vec<OrganizationRow>, AuthError> {
        let memberships = ctx
            .db()
            .find::<Member>(Member::USER_ID.eq(&ctx.session().user.id))
            .order_by(Member::CREATED_AT.asc())
            .all()
            .await?;
        if memberships.is_empty() {
            return Ok(Vec::new());
        }
        let organizations = ctx
            .db()
            .find::<OrganizationRow>(
                OrganizationRow::ID.in_(memberships.iter().map(|m| m.organization_id.clone())),
            )
            .all()
            .await?;
        Ok(memberships
            .iter()
            .filter_map(|m| {
                organizations
                    .iter()
                    .find(|o| o.id == m.organization_id)
                    .cloned()
            })
            .collect())
    }
}

/// Query of `/organization/get-full-organization`.
#[derive(Clone, Debug, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GetFullOrganizationQuery {
    pub organization_id: Option<String>,
    pub organization_slug: Option<String>,
    pub members_limit: Option<u64>,
}

/// An organization with its members (and their users) and invitations.
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct FullOrganization {
    #[serde(flatten)]
    pub organization: OrganizationRow,
    pub members: Vec<MemberWithUser>,
    /// Empty unless the caller may create invitations; invitations granting `owner` only
    /// show to owners.
    pub invitations: Vec<Invitation>,
}

/// `GET /organization/get-full-organization`: the requested organization (by id or slug,
/// else the active one) with members and invitations. `null` when there is no active
/// organization.
#[derive(Clone, Copy, Debug, Default)]
pub struct GetFullOrganization;

impl<D: Adapter> Endpoint<D> for GetFullOrganization {
    const METHOD: Method = Method::GET;
    const PATH: &'static str = "/organization/get-full-organization";
    type Access = Authenticated;
    type Input = GetFullOrganizationQuery;
    type Output = Option<FullOrganization>;

    async fn call(
        &self,
        ctx: Ctx<D, Authenticated>,
        input: GetFullOrganizationQuery,
    ) -> Result<Option<FullOrganization>, AuthError> {
        let session = ctx.session();
        let active = active_organization(session)?;
        let found = match (&input.organization_slug, &input.organization_id, &active) {
            (Some(slug), _, _) => find_organization_by_slug(&ctx, slug).await?,
            (None, Some(id), _) => find_organization(&ctx, &Id::from(id.as_str())).await?,
            (None, None, Some(id)) => find_organization(&ctx, id).await?,
            (None, None, None) => return Ok(None),
        };
        let organization = found.ok_or(OrganizationError::OrganizationNotFound)?;
        let Some(member) = find_member(&ctx, &organization.id, &session.user.id).await? else {
            if active.as_ref() == Some(&organization.id) {
                set_active(&ctx, session, None).await?;
            }
            return Err(OrganizationError::UserIsNotAMemberOfTheOrganization
                .with_status(StatusCode::FORBIDDEN));
        };
        let mut members = ctx
            .db()
            .find::<Member>(Member::ORGANIZATION_ID.eq(&organization.id))
            .order_by(Member::CREATED_AT.asc());
        if let Some(limit) = input.members_limit {
            members = members.limit(limit);
        }
        let members = with_users(&ctx, members.all().await?).await?;
        let invitations = visible_invitations(&ctx, plugin(&ctx), &member).await?;
        Ok(Some(FullOrganization {
            organization,
            members,
            invitations,
        }))
    }
}

/// The member's organization's invitations, as far as the member may see them: none
/// without `invitation: [create]` (an invitation id works as a link), and none granting
/// `owner` unless the member is an owner.
pub(crate) async fn visible_invitations<D: Adapter, A>(
    ctx: &Ctx<D, A>,
    plugin: &Organization,
    member: &Member,
) -> Result<Vec<Invitation>, AuthError> {
    if !plugin
        .roles
        .allows(&member.role, &[("invitation", &["create"])])
    {
        return Ok(Vec::new());
    }
    let invitations = ctx
        .db()
        .find::<Invitation>(Invitation::ORGANIZATION_ID.eq(&member.organization_id))
        .order_by(Invitation::CREATED_AT.asc())
        .all()
        .await?;
    let is_owner = member.has_role(OWNER);
    Ok(invitations
        .into_iter()
        .filter(|i| is_owner || !crate::access::role_list(&i.role).any(|r| r == OWNER))
        .collect())
}

/// Body of `/organization/set-active`.
#[derive(Clone, Debug, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SetActiveOrganizationInput {
    /// `null` clears the active organization.
    #[serde(default, deserialize_with = "nullable")]
    pub organization_id: Option<Option<String>>,
    pub organization_slug: Option<String>,
}

/// `POST /organization/set-active`: makes an organization the caller is a member of the
/// session's active one, or clears it with `organizationId: null`. Answers the organization,
/// or `null`.
#[derive(Clone, Copy, Debug, Default)]
pub struct SetActiveOrganization;

impl<D: Adapter> Endpoint<D> for SetActiveOrganization {
    const METHOD: Method = Method::POST;
    const PATH: &'static str = "/organization/set-active";
    type Access = Authenticated;
    type Input = SetActiveOrganizationInput;
    type Output = Option<OrganizationRow>;

    async fn call(
        &self,
        ctx: Ctx<D, Authenticated>,
        input: SetActiveOrganizationInput,
    ) -> Result<Option<OrganizationRow>, AuthError> {
        let session = ctx.session();
        let active = active_organization(session)?;
        let organization_id = match (input.organization_id, input.organization_slug) {
            (Some(None), _) => {
                if active.is_some() {
                    set_active(&ctx, session, None).await?;
                }
                return Ok(None);
            }
            (Some(Some(id)), _) => Id::from(id),
            (None, Some(slug)) => {
                find_organization_by_slug(&ctx, &slug)
                    .await?
                    .ok_or(OrganizationError::OrganizationNotFound)?
                    .id
            }
            (None, None) => match &active {
                Some(id) => id.clone(),
                None => return Ok(None),
            },
        };
        if find_member(&ctx, &organization_id, &session.user.id)
            .await?
            .is_none()
        {
            if active.is_some() {
                set_active(&ctx, session, None).await?;
            }
            return Err(OrganizationError::UserIsNotAMemberOfTheOrganization
                .with_status(StatusCode::FORBIDDEN));
        }
        let organization = organization(&ctx, &organization_id).await?;
        set_active(&ctx, session, Some(&organization.id)).await?;
        Ok(Some(organization))
    }
}

/// Body of `/organization/check-slug`.
#[derive(Clone, Debug, Deserialize)]
pub struct CheckSlugInput {
    pub slug: String,
}

/// `POST /organization/check-slug`: `{ "status": true }` when the slug is free, else 400
/// `ORGANIZATION_SLUG_ALREADY_TAKEN`.
#[derive(Clone, Copy, Debug, Default)]
pub struct CheckSlug;

impl<D: Adapter> Endpoint<D> for CheckSlug {
    const METHOD: Method = Method::POST;
    const PATH: &'static str = "/organization/check-slug";
    type Access = Authenticated;
    type Input = CheckSlugInput;
    type Output = Status;

    async fn call(
        &self,
        ctx: Ctx<D, Authenticated>,
        input: CheckSlugInput,
    ) -> Result<Status, AuthError> {
        if find_organization_by_slug(&ctx, &input.slug)
            .await?
            .is_some()
        {
            return Err(OrganizationError::OrganizationSlugAlreadyTaken.into());
        }
        Ok(Status { status: true })
    }
}