Skip to content

Commit

Permalink
chore: add group policy if role is other than member
Browse files Browse the repository at this point in the history
  • Loading branch information
rsbh committed Oct 5, 2023
1 parent 8e6e8fe commit dd87a72
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,22 +127,30 @@ export const InviteTeamMembers = () => {
getRoles();
}, [getRoles, organization?.id]);

const addGroupTeamPolicy = useCallback(
async (roleId: string, userId: string) => {
const role = roles.find(r => r.id === roleId);
if (role?.name && role.name !== PERMISSIONS.RoleGroupMember) {
const resource = `${PERMISSIONS.GroupPrincipal}:${teamId}`;
const principal = `${PERMISSIONS.UserPrincipal}:${userId}`;
const policy: V1Beta1PolicyRequestBody = {
roleId,
resource,
principal
};
await client?.frontierServiceCreatePolicy(policy);
}
},
[client, roles, teamId]
);

async function onSubmit({ role, userId }: InviteSchemaType) {
if (!userId || !role || !organization?.id) return;
try {
const resource = `${PERMISSIONS.GroupPrincipal}:${teamId}`;
const principal = `${PERMISSIONS.UserPrincipal}:${userId}`;

const policy: V1Beta1PolicyRequestBody = {
roleId: role,
resource,
principal
};

await client?.frontierServiceAddGroupUsers(organization?.id, teamId, {
userIds: [userId]
});
await client?.frontierServiceCreatePolicy(policy);
await addGroupTeamPolicy(role, userId);
toast.success('member added');
navigate({
to: '/teams/$teamId',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { Flex, Text, Image } from '@raystack/apsara';

import { Tabs } from '@raystack/apsara';
import { Outlet, useNavigate, useParams } from '@tanstack/react-router';
import {
Outlet,
useNavigate,
useParams,
useRouterState
} from '@tanstack/react-router';
import { useEffect, useState } from 'react';
import { toast } from 'sonner';
import { useFrontier } from '~/react/contexts/FrontierContext';
Expand All @@ -17,6 +22,7 @@ export const TeamPage = () => {
const [members, setMembers] = useState<V1Beta1User[]>([]);
const { client, activeOrganization: organization } = useFrontier();
let navigate = useNavigate({ from: '/teams/$teamId' });
const routerState = useRouterState();

useEffect(() => {
async function getTeamDetails() {
Expand Down Expand Up @@ -58,7 +64,7 @@ export const TeamPage = () => {
}
}
getTeamMembers();
}, [client, organization?.id, teamId]);
}, [client, organization?.id, teamId, routerState.location.key]);

return (
<Flex direction="column" style={{ width: '100%' }}>
Expand Down
3 changes: 2 additions & 1 deletion sdks/js/packages/core/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ export const PERMISSIONS = {
SuperUserPrincipal: 'app/superuser',

// Roles
RoleProjectOwner: 'app_project_owner'
RoleProjectOwner: 'app_project_owner',
RoleGroupMember: 'app_group_member'
};

export const formatPermissions = (
Expand Down

0 comments on commit dd87a72

Please sign in to comment.