Managers invite people into their organization and workspace admins into their workspace; the email links to a new /invite page that creates the account (or attaches an existing one without an organization) and adds the workspace membership. Invites bypass the signup rules, store only a token hash, expire after seven days, and return the link so it can be passed on when mail is not configured. Inviting someone already in the organization adds them to the workspace directly. Magic links reuse PocketBase's email OTP: the email now carries a link to /magic-link next to the code, and the admin settings gain a switch for it. Managers can also send a user a password-reset email. The dev proxies read DOCWELL_DEV_API so a dev server can target another docwell-server. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
65 lines
2.2 KiB
Go
65 lines
2.2 KiB
Go
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
package access
|
|
|
|
import "docwell/server/internal/apperr"
|
|
|
|
// ---- Invites ---------------------------------------------------------------
|
|
//
|
|
// An invite is a promise to create an account (or attach an existing one
|
|
// without an organization) later. Inviting follows the same rules as doing the
|
|
// thing directly: creating a user for organization invites, adding a member
|
|
// for workspace invites.
|
|
|
|
// InviteToOrg checks inviting someone with instance role into organization org
|
|
// and returns the organization the invite is for (see CreateUser).
|
|
func InviteToOrg(c Caller, role, org string) (string, error) {
|
|
return CreateUser(c, role, org)
|
|
}
|
|
|
|
// InviteToWorkspace checks a workspace invite: the inviter (effective role
|
|
// actorRole) may grant role up to their own rank.
|
|
func InviteToWorkspace(actorRole, role string) error {
|
|
if !ValidWorkspaceRole(role) {
|
|
return apperr.Invalid("role must be owner, admin, member or guest")
|
|
}
|
|
if !AtLeast(actorRole, WorkspaceAdmin) {
|
|
return apperr.Forbidden("workspace admin role required")
|
|
}
|
|
if workspaceRank[role] > workspaceRank[actorRole] {
|
|
return apperr.Forbidden("you cannot grant a role above your own")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ManageInvite checks revoking or resending an invite for organization org.
|
|
// The inviter may always; otherwise a manager of that organization, or
|
|
// (wsRole, for workspace invites) a workspace admin.
|
|
func ManageInvite(c Caller, invitedBy, org, wsRole string) error {
|
|
switch {
|
|
case c.ID == invitedBy, c.IsSuperadmin():
|
|
return nil
|
|
case c.Role == RoleAdmin && c.OrgID != "" && c.OrgID == org:
|
|
return nil
|
|
case AtLeast(wsRole, WorkspaceAdmin):
|
|
return nil
|
|
}
|
|
return apperr.Forbidden("you cannot manage this invite")
|
|
}
|
|
|
|
// AcceptInvite checks attaching an existing account (instance role role,
|
|
// organization userOrg) to the invite's organization org.
|
|
func AcceptInvite(role, userOrg, org string) error {
|
|
if userOrg == org {
|
|
return nil
|
|
}
|
|
if userOrg != "" {
|
|
return apperr.Conflict("your account already belongs to another organization")
|
|
}
|
|
if role == RoleSuperadmin {
|
|
// Superadmins span every organization; attaching one would demote them.
|
|
return apperr.Conflict("a superadmin account cannot join an organization")
|
|
}
|
|
return nil
|
|
}
|