-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
92 changed files
with
5,818 additions
and
3,015 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
NEXTAUTH_URL= | ||
NEXTAUTH_SECRET= | ||
|
||
AUTH0_CLIENT_ID= | ||
AUTH0_CLIENT_SECRET= | ||
AUTH0_ISSUER= | ||
AUTH0_SCOPE= |
67 changes: 2 additions & 65 deletions
67
src/management-system-v2/app/(dashboard)/iam/roles/[roleId]/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/management-system-v2/app/(dashboard)/iam/roles/[roleId]/role-page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
'use client'; | ||
|
||
import Content from '@/components/content'; | ||
import { useGetAsset } from '@/lib/fetch-data'; | ||
import { Button, Result, Skeleton, Space, Tabs } from 'antd'; | ||
import { LeftOutlined } from '@ant-design/icons'; | ||
import { ComponentProps } from 'react'; | ||
import RoleGeneralData from './roleGeneralData'; | ||
import { useRouter } from 'next/navigation'; | ||
import RolePermissions from './rolePermissions'; | ||
import RoleMembers from './role-members'; | ||
|
||
type Items = ComponentProps<typeof Tabs>['items']; | ||
|
||
function RolePage({ params: { roleId } }: { params: { roleId: string } }) { | ||
const router = useRouter(); | ||
const { | ||
data: role, | ||
isLoading, | ||
error, | ||
} = useGetAsset('/roles/{id}', { | ||
params: { path: { id: roleId } }, | ||
}); | ||
|
||
const items: Items = role | ||
? [ | ||
{ | ||
key: 'generalData', | ||
label: 'General Data', | ||
children: <RoleGeneralData roleId={roleId} />, | ||
}, | ||
{ key: 'permissions', label: 'Permissions', children: <RolePermissions role={role} /> }, | ||
{ | ||
key: 'members', | ||
label: 'Manage Members', | ||
children: <RoleMembers role={role} isLoadingRole={isLoading} />, | ||
}, | ||
] | ||
: []; | ||
|
||
if (error) | ||
return ( | ||
<Result | ||
status="error" | ||
title="Failed to fetch role" | ||
subTitle="An error ocurred while fetching role, please try again." | ||
/> | ||
); | ||
|
||
return ( | ||
<Content | ||
title={ | ||
<Space> | ||
<Button icon={<LeftOutlined />} onClick={() => router.push('/iam/roles')} type="text"> | ||
Back | ||
</Button> | ||
{role?.name} | ||
</Space> | ||
} | ||
> | ||
<div style={{ maxWidth: '800px', margin: 'auto' }}> | ||
<Skeleton loading={isLoading}> | ||
<Tabs items={items} /> | ||
</Skeleton> | ||
</div> | ||
</Content> | ||
); | ||
} | ||
|
||
export default RolePage; |
72 changes: 72 additions & 0 deletions
72
src/management-system-v2/app/(dashboard)/iam/roles/[roleId]/role-permissions-helper.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import Ability from '@/lib/ability/abilityHelper'; | ||
import { ResourceActionType } from '@/lib/ability/caslAbility'; | ||
import { ApiData } from '@/lib/fetch-data'; | ||
|
||
type Role = ApiData<'/roles', 'get'>[number]; | ||
|
||
// permission mapping to verbs | ||
const PERMISSION_MAPPING = { | ||
none: 0, | ||
view: 1, | ||
update: 2, | ||
create: 4, | ||
delete: 8, | ||
manage: 16, | ||
share: 32, | ||
'manage-roles': 64, | ||
'manage-groups': 128, | ||
'manage-password': 256, | ||
admin: 9007199254740991, | ||
} as const; | ||
|
||
export function togglePermission( | ||
permissions: Role['permissions'], | ||
resource: keyof Role['permissions'], | ||
permission: keyof typeof PERMISSION_MAPPING, | ||
) { | ||
const currentValue = permissions[resource] ?? 0; | ||
|
||
if (permission !== 'admin') { | ||
const permissionBit = PERMISSION_MAPPING[permission]; | ||
permissions[resource] = currentValue ^ permissionBit; | ||
} else { | ||
permissions[resource] = | ||
currentValue === PERMISSION_MAPPING.admin ? 0 : PERMISSION_MAPPING.admin; | ||
} | ||
|
||
// New pointer for role object to trigger a rerender | ||
return { ...permissions }; | ||
} | ||
|
||
export function switchChecked( | ||
permissions: Role['permissions'] | undefined, | ||
resource: keyof Role['permissions'], | ||
action: ResourceActionType, | ||
) { | ||
if (!(permissions !== undefined && typeof permissions === 'object' && resource in permissions)) | ||
return false; | ||
|
||
const permissionNumber = permissions[resource]!; | ||
|
||
if (action === 'admin') return permissionNumber === PERMISSION_MAPPING.admin; | ||
else if (permissionNumber === PERMISSION_MAPPING.admin) return false; | ||
|
||
return !!(PERMISSION_MAPPING[action] & permissionNumber); | ||
} | ||
|
||
export function switchDisabled( | ||
permissions: Role['permissions'] | undefined, | ||
resource: keyof Role['permissions'], | ||
action: ResourceActionType, | ||
ability: Ability, | ||
) { | ||
if (action === 'admin' && !ability.can('admin', resource)) return true; | ||
|
||
if (!(permissions !== undefined && typeof permissions === 'object' && resource in permissions)) | ||
return false; | ||
|
||
const permissionNumber = permissions[resource]!; | ||
if (permissionNumber === PERMISSION_MAPPING.admin && action !== 'admin') return true; | ||
|
||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.