feat: add permission for editUsers + implement UI (#25402)

## What does this PR do?
This PR implements attributes PBAC - router checks + UI

## Visual Demo (For contributors especially)

A visual demonstration is strongly recommended, for both the original and new change **(video / image - any one)**.

#### Video Demo (if applicable):

- Show screen recordings of the issue or feature.
- Demonstrate how to reproduce the issue, the behavior before and after the change.

#### Image Demo (if applicable):

- Add side-by-side screenshots of the original and updated change.
- Highlight any significant change(s).

## Mandatory Tasks (DO NOT REMOVE)

- [ ] I have self-reviewed the code (A decent size PR without self-review might be rejected).
- [ ] I have updated the developer docs in /docs if this PR makes changes that would require a [documentation change](https://cal.com/docs). If N/A, write N/A here and check the checkbox.
- [ ] I confirm automated tests are in place that prove my fix is effective or that my feature works.

## How should this be tested?
Enable PBAC on an org 
Create a custom role -> advanced -> organizations -> "editUser" 
Assign it to a user
impersonate user
test they have access to all things attributes
remove permissions
check they dont have permissions. 

## Checklist

<!-- Remove bullet points below that don't apply to you -->

- I haven't read the [contributing guide](https://github.com/calcom/cal.com/blob/main/CONTRIBUTING.md)
- My code doesn't follow the style guidelines of this project
- I haven't commented my code, particularly in hard-to-understand areas
- I haven't checked if my changes generate no new warnings










<!-- This is an auto-generated description by cubic. -->
---
## Summary by cubic
Adds a new PBAC “editUsers” permission and read gating for Attributes, updating the UI and backend so users can view and edit attributes only when allowed.

- **New Features**
  - Added CustomAction.EditUsers to the permission registry for organization-scoped attribute editing.
  - Settings computes canViewAttributes and shows the Attributes tab only when allowed.
  - Members page fetches Attributes permissions and exposes canViewAttributes and canEditAttributesForUser to the UI.
  - User Edit Sheet hides attributes without read permission and shows attribute editing and the bulk “Mass Assign Attributes” action only with editUsers; other user edits depend on changeMemberRole.
  - Attributes TRPC router gates create/edit/delete/toggle via PBAC and requires organization.attributes.editUsers for assign/bulk-assign; added a helper to create PBAC-aware procedures.

- **Migration**
  - Run the new Prisma migration to seed the admin role with editUsers.
  - If using custom roles, grant Edit Users under Attributes as needed.

<sup>Written for commit 856aa2e8e1521fc22cd71cb0fa6d720036efe8a2. Summary will update automatically on new commits.</sup>

<!-- End of auto-generated description by cubic. -->
This commit is contained in:
sean-brydon authored and GitHub committed 2025-11-26 13:58:29 +00:00
1 parent 7d2a06ddb6
commit 2a7c6590a3
13 files changed
+266 -99

No files matched your search

@@ -71,11 +71,13 @@ export function EditForm({
avatarUrl,
domainUrl,
dispatch,
canEditAttributesForUser,
}: {
selectedUser: RouterOutputs["viewer"]["organizations"]["getUser"];
avatarUrl: string;
domainUrl: string;
dispatch: Dispatch<UserTableAction>;
canEditAttributesForUser?: boolean;
}) {
const setEditMode = useEditMode((state) => state.setEditMode);
const [mutationLoading, setMutationLoading] = useState(false);
@@ -249,8 +251,12 @@ export function EditForm({
<Label>{t("timezone")}</Label>
<TimezoneSelect value={watchTimezone ?? "America/Los_Angeles"} />
</div>
<Divider />
<AttributesList selectedUserId={selectedUser?.id} />
{canEditAttributesForUser && (
<>
<Divider />
<AttributesList selectedUserId={selectedUser?.id} />
</>
)}
</SheetBody>
<SheetFooter>
<Button
@@ -23,9 +23,15 @@ function removeProtocol(url: string) {
export function EditUserSheet({
state,
dispatch,
canViewAttributes,
canEditAttributesForUser,
canChangeMemberRole,
}: {
state: UserTableState;
dispatch: Dispatch<UserTableAction>;
canViewAttributes?: boolean;
canEditAttributesForUser?: boolean;
canChangeMemberRole?: boolean;
}) {
const { t } = useLocale();
const { user: selectedUser } = state.editSheet;
@@ -47,7 +53,7 @@ export function EditUserSheet({
userId: selectedUser?.id,
},
{
enabled: !!selectedUser?.id,
enabled: !!selectedUser?.id && !!canViewAttributes,
}
);
@@ -83,8 +89,8 @@ export function EditUserSheet({
</p>
</div>
</SheetHeader>
<SheetBody className="flex flex-col stack-y-4 p-4">
<div className="mb-4 flex flex-col stack-y-4">
<SheetBody className="stack-y-4 flex flex-col p-4">
<div className="stack-y-4 mb-4 flex flex-col">
<h3 className="text-emphasis mb-1 text-base font-semibold">{t("profile")}</h3>
<DisplayInfo
label="Cal"
@@ -108,10 +114,10 @@ export function EditUserSheet({
icon="calendar"
/>
</div>
{usersAttributes && usersAttributes?.length > 0 && (
{canViewAttributes && usersAttributes && usersAttributes?.length > 0 && (
<div className="mt-4 flex flex-col">
<h3 className="text-emphasis mb-5 text-base font-semibold">{t("attributes")}</h3>
<div className="flex flex-col stack-y-4">
<div className="stack-y-4 flex flex-col">
{usersAttributes.map((attribute, index) => (
<>
<DisplayInfo
@@ -130,7 +136,10 @@ export function EditUserSheet({
)}
</SheetBody>
<SheetFooter>
<SheetFooterControls />
<SheetFooterControls
canEditAttributesForUser={canEditAttributesForUser}
canChangeMemberRole={canChangeMemberRole}
/>
</SheetFooter>
</>
) : (
@@ -140,6 +149,7 @@ export function EditUserSheet({
avatarUrl={loadedUser.avatarUrl ?? avatarURL}
domainUrl={orgBranding?.fullDomain ?? WEBAPP_URL}
dispatch={dispatch}
canEditAttributesForUser={canEditAttributesForUser}
/>
</>
)}
@@ -28,10 +28,19 @@ function EditModeFooter() {
);
}
function MoreInfoFooter() {
function MoreInfoFooter({
canEditAttributesForUser,
canChangeMemberRole,
}: {
canEditAttributesForUser?: boolean;
canChangeMemberRole?: boolean;
}) {
const { t } = useLocale();
const setEditMode = useEditMode((state) => state.setEditMode);
// Show edit button if user can change member role (edit user info) or edit attributes
const canEdit = canChangeMemberRole || canEditAttributesForUser;
return (
<>
<SheetClose asChild>
@@ -39,21 +48,40 @@ function MoreInfoFooter() {
{t("close")}
</Button>
</SheetClose>
<Button
type="button"
className="justify-center"
onClick={() => {
setEditMode(true);
}}
key="EDIT_BUTTON"
StartIcon="pencil">
{t("edit")}
</Button>
{canEdit && (
<Button
type="button"
className="justify-center"
onClick={() => {
setEditMode(true);
}}
key="EDIT_BUTTON"
StartIcon="pencil">
{t("edit")}
</Button>
)}
</>
);
}
export function SheetFooterControls() {
export function SheetFooterControls({
canEditAttributesForUser,
canChangeMemberRole,
}: {
canEditAttributesForUser?: boolean;
canChangeMemberRole?: boolean;
}) {
const editMode = useEditMode((state) => state.editMode);
return <>{editMode ? <EditModeFooter /> : <MoreInfoFooter />}</>;
return (
<>
{editMode ? (
<EditModeFooter />
) : (
<MoreInfoFooter
canEditAttributesForUser={canEditAttributesForUser}
canChangeMemberRole={canChangeMemberRole}
/>
)}
</>
);
}
@@ -449,16 +449,19 @@ function UserListTableContent({
const isSelf = user.id === session?.user.id;
const permissionsForUser = {
canEdit: permissionsRaw.canEdit && user.accepted && !isSelf,
canRemove: permissionsRaw.canRemove && !isSelf,
canEdit:
((permissionsRaw.canEdit ?? false) || (permissions?.canEditAttributesForUser ?? false)) &&
user.accepted &&
!isSelf,
canRemove: (permissionsRaw.canRemove ?? false) && !isSelf,
canImpersonate:
user.accepted &&
!user.disableImpersonation &&
!isSelf &&
!!org?.canAdminImpersonate &&
permissionsRaw.canImpersonate,
(permissionsRaw.canImpersonate ?? false),
canLeave: user.accepted && isSelf,
canResendInvitation: permissionsRaw.canResendInvitation && !user.accepted,
canResendInvitation: (permissionsRaw.canResendInvitation ?? false) && !user.accepted,
};
return (
@@ -633,7 +636,7 @@ function UserListTableContent({
{t("group_meeting")}
</DataTableSelectionBar.Button>
)}
{(permissions?.canChangeMemberRole ?? adminOrOwner) && (
{(permissions?.canEditAttributesForUser ?? adminOrOwner) && (
<MassAssignAttributesBulkAction table={table} filters={columnFilters} />
)}
{(permissions?.canChangeMemberRole ?? adminOrOwner) && (
@@ -655,7 +658,15 @@ function UserListTableContent({
{state.inviteMember.showModal && <InviteMemberModal dispatch={dispatch} />}
{state.impersonateMember.showModal && <ImpersonationMemberModal dispatch={dispatch} state={state} />}
{state.changeMemberRole.showModal && <ChangeUserRoleModal dispatch={dispatch} state={state} />}
{state.editSheet.showModal && <EditUserSheet dispatch={dispatch} state={state} />}
{state.editSheet.showModal && (
<EditUserSheet
dispatch={dispatch}
state={state}
canViewAttributes={permissions?.canViewAttributes}
canEditAttributesForUser={permissions?.canEditAttributesForUser}
canChangeMemberRole={permissions?.canChangeMemberRole ?? adminOrOwner}
/>
)}
{ctaContainerRef.current &&
createPortal(
@@ -59,4 +59,6 @@ export interface MemberPermissions {
canChangeMemberRole: boolean;
canRemove: boolean;
canImpersonate: boolean;
canEditAttributesForUser?: boolean;
canViewAttributes?: boolean;
}