Files
outline/server/policies/utils.ts
Tom Moorandcodegen-sh[bot] 617504d8bb feat: Add group admin role (#10030)
* Add admin role to GroupUser

This change adds an admin role to GroupUser that allows group admins to:
1. Administer other users in the group
2. Change the group name

Changes include:
- Database migration to add isAdmin field to group_users table
- Updated GroupUser model to include isAdmin field
- Added isGroupAdmin policy utility function
- Updated group policy to allow group admins to update groups
- Added API endpoints for managing admin status
- Updated GroupUsersStore to handle admin functionality
- Added tests for the new functionality

* Replace isAdmin with role-based approach for GroupUser

- Added role field to GroupUser model using UserRole.Admin and UserRole.Member
- Created migration to convert isAdmin boolean to role enum
- Updated policies to be synchronous and require pre-loaded relationships
- Updated API endpoints to support both role and legacy isAdmin parameters
- Updated GroupUsersStore to handle role-based functionality
- Updated tests to use role instead of isAdmin
- Maintained backward compatibility with isAdmin in presenters

* Remove isAdmin logic from GroupUser implementation

- Removed isAdmin parameter from GroupUsersStore methods
- Removed isAdmin field from presenter output
- Removed isAdmin from API schemas
- Removed isAdmin parameter handling in API endpoints
- Updated tests to use role instead of isAdmin
- Simplified role handling throughout the codebase

* lint

* tests

* role -> permission

* fe

* test

* Change permission label from 'Admin' to 'Manage'

---------

Co-authored-by: codegen-sh[bot] <131295404+codegen-sh[bot]@users.noreply.github.com>
2025-09-01 05:10:32 -04:00

138 lines
3.2 KiB
TypeScript

import env from "@server/env";
import { User, Team, type Group } from "@server/models";
import Model from "@server/models/base/Model";
import { GroupPermission } from "@shared/types";
import invariant from "invariant";
type Args = boolean | string | Args[];
export function and(...args: Args[]) {
const filtered = args.filter(Boolean);
return filtered.length === args.length ? filtered : false;
}
export function or(...args: Args[]) {
return args.find(Boolean) || false;
}
/**
* Check if the actor is present in the same team as the model.
*
* @param actor The actor to check
* @param model The model to check
* @returns True if the actor is in the same team as the model
*/
export function isTeamModel(
actor: User,
model: Model | null | undefined
): model is Model {
if (!model) {
return false;
}
if (model instanceof Team) {
return actor.teamId === model.id;
}
if ("teamId" in model) {
return actor.teamId === model.teamId;
}
return false;
}
/**
* Check if the actor is the owner of the model.
*
* @param actor The actor to check
* @param model The model to check
* @returns True if the actor is the owner of the model
*/
export function isOwner(
actor: User,
model: Model | null | undefined
): model is Model {
if (!model) {
return false;
}
if ("userId" in model) {
return actor.id === model.userId;
}
return false;
}
/**
* Check if the actor is an admin of the team.
*
* @param actor The actor to check
* @param mode The model to check
* @returns True if the actor is an admin of the team the model belongs to
*/
export function isTeamAdmin(
actor: User,
model: Model | null | undefined
): model is Model {
return !!and(isTeamModel(actor, model), actor.isAdmin);
}
/**
* Check if the actor is a member of the team.
*
* @param actor The actor to check
* @param model The model to check
* @returns True if the actor is a member of the team the model belongs to
*/
export function isTeamMember(actor: User, model: Model | null | undefined) {
return !!and(isTeamModel(actor, model), actor.isMember);
}
/**
* Check the actors team is mutable, meaning the team models can be modified.
*
* @param actor The actor to check
* @returns True if the actor's team is mutable
*/
export function isTeamMutable(_actor: User, _model?: Model | null) {
return true;
}
/**
* Check if this instance is running in the cloud-hosted environment.
*/
export function isCloudHosted() {
if (!env.isCloudHosted) {
return false;
}
return true;
}
/**
* Check if the actor is an admin of the group.
*
* @param actor The actor to check
* @param model The group model to check
* @returns True if the actor is an admin of the group
*/
export function isGroupAdmin(actor: User, model: Group | null): boolean {
if (!model || !("id" in model)) {
return false;
}
invariant(
model.groupUsers,
"Group users relationship not loaded, ensure to include groupUsers when fetching the group"
);
// Check if the user is a group admin
const membership = model.groupUsers.find(
(gu) => gu.userId === actor.id && gu.permission === GroupPermission.Admin
);
if (membership) {
return true;
}
// Team admins are always group admins
if (isTeamAdmin(actor, model)) {
return true;
}
return false;
}