Skip to content

Commit aaf8d7e

Browse files
committed
feat(organization): allow setting a default role for new members
1 parent 1ccf15f commit aaf8d7e

10 files changed

Lines changed: 9241 additions & 9 deletions

File tree

‎apps/dokploy/components/dashboard/settings/users/add-invitation.tsx‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ export const AddInvitation = () => {
106106
const { mutateAsync: createUserWithCredentials, isPending: isCreating } =
107107
api.user.createUserWithCredentials.useMutation();
108108
const { data: customRoles } = api.customRole.all.useQuery();
109+
const { data: activeOrganization } = api.organization.active.useQuery();
109110
const [error, setError] = useState<string | null>(null);
110111

111112
const form = useForm<AddInvitation>({
@@ -132,6 +133,16 @@ export const AddInvitation = () => {
132133
}
133134
}, [form, isCloud]);
134135

136+
useEffect(() => {
137+
if (
138+
activeOrganization?.defaultRole &&
139+
activeOrganization.defaultRole !== "owner" &&
140+
!form.formState.dirtyFields.role
141+
) {
142+
form.setValue("role", activeOrganization.defaultRole);
143+
}
144+
}, [form, activeOrganization?.defaultRole]);
145+
135146
const onSubmit = async (data: AddInvitation) => {
136147
setError(null);
137148

@@ -267,10 +278,7 @@ export const AddInvitation = () => {
267278
return (
268279
<FormItem>
269280
<FormLabel>Role</FormLabel>
270-
<Select
271-
onValueChange={field.onChange}
272-
defaultValue={field.value}
273-
>
281+
<Select onValueChange={field.onChange} value={field.value}>
274282
<FormControl>
275283
<SelectTrigger>
276284
<SelectValue placeholder="Select a role" />

‎apps/dokploy/components/proprietary/roles/manage-custom-roles.tsx‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ import {
4545
PopoverContent,
4646
PopoverTrigger,
4747
} from "@/components/ui/popover";
48+
import {
49+
Select,
50+
SelectContent,
51+
SelectItem,
52+
SelectTrigger,
53+
SelectValue,
54+
} from "@/components/ui/select";
4855
import { Switch } from "@/components/ui/switch";
4956
import { api } from "@/utils/api";
5057

@@ -780,6 +787,78 @@ function HandleCustomRole({
780787
);
781788
}
782789

790+
const DefaultRoleSection = ({ customRoles }: { customRoles: string[] }) => {
791+
const utils = api.useUtils();
792+
const { data: auth } = api.user.get.useQuery();
793+
const { data: activeOrganization } = api.organization.active.useQuery();
794+
const { mutateAsync: updateOrganization, isPending: isUpdating } =
795+
api.organization.update.useMutation();
796+
const [selectedRole, setSelectedRole] = useState<string>();
797+
798+
if (auth?.role !== "owner" || !activeOrganization) {
799+
return null;
800+
}
801+
802+
const currentRole = activeOrganization.defaultRole ?? "member";
803+
const value = selectedRole ?? currentRole;
804+
805+
const onSave = async () => {
806+
await updateOrganization({
807+
organizationId: activeOrganization.id,
808+
name: activeOrganization.name,
809+
logo: activeOrganization.logo ?? undefined,
810+
defaultRole: value,
811+
})
812+
.then(() => {
813+
toast.success("Default role updated");
814+
utils.organization.active.invalidate();
815+
})
816+
.catch((error) => {
817+
toast.error(
818+
error instanceof Error
819+
? error.message
820+
: "Error updating default role",
821+
);
822+
});
823+
};
824+
825+
return (
826+
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-3 rounded-lg border bg-muted/20 p-4">
827+
<div className="space-y-0.5">
828+
<p className="text-sm font-medium">Default role for new members</p>
829+
<p className="text-xs text-muted-foreground">
830+
Assigned automatically to users joining through SSO and preselected
831+
when creating invitations.
832+
</p>
833+
</div>
834+
<div className="flex items-center gap-2">
835+
<Select value={value} onValueChange={setSelectedRole}>
836+
<SelectTrigger className="w-40">
837+
<SelectValue placeholder="Select a role" />
838+
</SelectTrigger>
839+
<SelectContent>
840+
<SelectItem value="member">Member</SelectItem>
841+
<SelectItem value="admin">Admin</SelectItem>
842+
{customRoles.map((role) => (
843+
<SelectItem key={role} value={role}>
844+
{role}
845+
</SelectItem>
846+
))}
847+
</SelectContent>
848+
</Select>
849+
<Button
850+
size="sm"
851+
isLoading={isUpdating}
852+
disabled={value === currentRole}
853+
onClick={onSave}
854+
>
855+
Save
856+
</Button>
857+
</div>
858+
</div>
859+
);
860+
};
861+
783862
const CustomRolesContent = () => {
784863
const {
785864
data: customRoles,
@@ -821,6 +900,9 @@ const CustomRolesContent = () => {
821900

822901
return (
823902
<div className="space-y-4">
903+
<DefaultRoleSection
904+
customRoles={customRoles?.map((role) => role.role) ?? []}
905+
/>
824906
<div className="flex justify-end">
825907
<HandleCustomRole onSuccess={refetch} />
826908
</div>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE "organization" ADD COLUMN "default_role" text;

0 commit comments

Comments
 (0)