generated from graasp/graasp-repo
-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: create permission table #604
Draft
pyphilia
wants to merge
1
commit into
main
Choose a base branch
from
permission-table
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
import { Operation, can } from './permissions.js'; | ||
import { PermissionLevel } from '@/enums/permissionLevel/permissionLevel.js'; | ||
import { PackedFolderItemFactory } from '@/item/folderItem/folderItem.factory.js'; | ||
import { MemberFactory } from '@/member/factory.js'; | ||
import { MemberType } from '@/member/member.js'; | ||
|
||
const privateItem = PackedFolderItemFactory({}, { permission: null }); | ||
const privateItemWithAdmin = PackedFolderItemFactory( | ||
{}, | ||
{ permission: PermissionLevel.Admin }, | ||
); | ||
const privateItemWithWrite = PackedFolderItemFactory( | ||
{}, | ||
{ permission: PermissionLevel.Write }, | ||
); | ||
const privateItemWithRead = PackedFolderItemFactory( | ||
{}, | ||
{ permission: PermissionLevel.Read }, | ||
); | ||
const publicItem = PackedFolderItemFactory( | ||
{}, | ||
{ permission: null, publicTag: {} }, | ||
); | ||
const publicItemWithAdmin = PackedFolderItemFactory( | ||
{}, | ||
{ permission: PermissionLevel.Admin, publicTag: {} }, | ||
); | ||
const publicItemWithWrite = PackedFolderItemFactory( | ||
{}, | ||
{ permission: PermissionLevel.Admin, publicTag: {} }, | ||
); | ||
const publicItemWithRead = PackedFolderItemFactory( | ||
{}, | ||
{ permission: PermissionLevel.Read, publicTag: {} }, | ||
); | ||
|
||
const signedInMember = MemberFactory(); | ||
// TODO: to change | ||
const pseudonymizedMember = MemberFactory({ type: MemberType.Group }); | ||
|
||
describe('can', () => { | ||
it(Operation.CopyItem + ' and signed in', () => { | ||
expect(can(Operation.CopyItem, privateItem, signedInMember)).toBeFalsy(); | ||
expect( | ||
can(Operation.CopyItem, privateItemWithAdmin, signedInMember), | ||
).toBeTruthy(); | ||
expect( | ||
can(Operation.CopyItem, privateItemWithWrite, signedInMember), | ||
).toBeTruthy(); | ||
expect( | ||
can(Operation.CopyItem, privateItemWithRead, signedInMember), | ||
).toBeTruthy(); | ||
expect(can(Operation.CopyItem, publicItem, signedInMember)).toBeTruthy(); | ||
expect( | ||
can(Operation.CopyItem, publicItemWithAdmin, signedInMember), | ||
).toBeTruthy(); | ||
expect( | ||
can(Operation.CopyItem, publicItemWithWrite, signedInMember), | ||
).toBeTruthy(); | ||
expect( | ||
can(Operation.CopyItem, publicItemWithRead, signedInMember), | ||
).toBeTruthy(); | ||
}); | ||
it(Operation.CopyItem + ' and signed out', () => { | ||
expect(can(Operation.CopyItem, privateItem, undefined)).toBeFalsy(); | ||
expect( | ||
can(Operation.CopyItem, privateItemWithAdmin, undefined), | ||
).toBeFalsy(); | ||
expect( | ||
can(Operation.CopyItem, privateItemWithWrite, undefined), | ||
).toBeFalsy(); | ||
expect(can(Operation.CopyItem, privateItemWithRead, undefined)).toBeFalsy(); | ||
expect(can(Operation.CopyItem, publicItem, undefined)).toBeFalsy(); | ||
expect(can(Operation.CopyItem, publicItemWithAdmin, undefined)).toBeFalsy(); | ||
expect(can(Operation.CopyItem, publicItemWithWrite, undefined)).toBeFalsy(); | ||
expect(can(Operation.CopyItem, publicItemWithRead, undefined)).toBeFalsy(); | ||
}); | ||
it(Operation.CopyItem + ' and pseudonimized', () => { | ||
expect( | ||
can(Operation.CopyItem, privateItem, pseudonymizedMember), | ||
).toBeFalsy(); | ||
expect( | ||
can(Operation.CopyItem, privateItemWithAdmin, pseudonymizedMember), | ||
).toBeFalsy(); | ||
expect( | ||
can(Operation.CopyItem, privateItemWithWrite, pseudonymizedMember), | ||
).toBeFalsy(); | ||
expect( | ||
can(Operation.CopyItem, privateItemWithRead, pseudonymizedMember), | ||
).toBeFalsy(); | ||
expect( | ||
can(Operation.CopyItem, publicItem, pseudonymizedMember), | ||
).toBeFalsy(); | ||
expect( | ||
can(Operation.CopyItem, publicItemWithAdmin, pseudonymizedMember), | ||
).toBeFalsy(); | ||
expect( | ||
can(Operation.CopyItem, publicItemWithWrite, pseudonymizedMember), | ||
).toBeFalsy(); | ||
expect( | ||
can(Operation.CopyItem, publicItemWithRead, pseudonymizedMember), | ||
).toBeFalsy(); | ||
}); | ||
|
||
it(Operation.ReadItem + ' and pseudonimzed', () => { | ||
expect( | ||
can(Operation.ReadItem, privateItem, pseudonymizedMember), | ||
).toBeTruthy(); | ||
expect( | ||
can(Operation.ReadItem, publicItem, pseudonymizedMember), | ||
).toBeTruthy(); | ||
}); | ||
}); |
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,92 @@ | ||
import { PackedItem } from '@/item/packedItem.js'; | ||
import { CompleteMember, MemberType } from '@/member/member.js'; | ||
|
||
// TODO: reuse action op? | ||
export enum Operation { | ||
ReadItem = 'read-item', | ||
MoveItem = 'move-item', | ||
CopyItem = 'copy-item', | ||
HideItem = 'hide-item', | ||
DownloadItem = 'download-item', | ||
} | ||
|
||
// don't take into account hidden: suppose backend will prevent read from beginning | ||
// pseudonymized users cannot create items and can only view a specific item | ||
const SIGNED_IN_OPERATIONS_PERMISSIONS = { | ||
[Operation.ReadItem]: { | ||
admin: true, | ||
write: true, | ||
read: true, | ||
pseudonymized: true, | ||
public: true, | ||
}, | ||
[Operation.CopyItem]: { | ||
admin: true, | ||
write: true, | ||
read: true, | ||
pseudonymized: false, | ||
public: true, | ||
}, | ||
[Operation.MoveItem]: { | ||
admin: true, | ||
write: true, | ||
read: false, | ||
pseudonymized: false, | ||
public: false, | ||
}, | ||
[Operation.DownloadItem]: { | ||
admin: true, | ||
write: true, | ||
read: true, | ||
pseudonymized: true, | ||
public: true, | ||
}, | ||
[Operation.HideItem]: { | ||
admin: true, | ||
write: true, | ||
read: false, | ||
pseudonymized: false, | ||
public: false, | ||
}, | ||
}; | ||
|
||
const SIGNED_OUT_OPERATIONS_PERMISSIONS = { | ||
[Operation.ReadItem]: { | ||
public: true, | ||
}, | ||
[Operation.CopyItem]: { | ||
public: false, | ||
}, | ||
[Operation.MoveItem]: { | ||
public: false, | ||
}, | ||
[Operation.DownloadItem]: { | ||
public: true, | ||
}, | ||
[Operation.HideItem]: { | ||
public: false, | ||
}, | ||
}; | ||
|
||
export const can = ( | ||
operation: Operation, | ||
item: PackedItem, | ||
member?: CompleteMember, | ||
) => { | ||
// TODO: correct | ||
const isPseudoMember = member?.type !== MemberType.Individual; | ||
|
||
if (!member) { | ||
const p = SIGNED_OUT_OPERATIONS_PERMISSIONS[operation]; | ||
return item.public ? p.public : false; | ||
} | ||
|
||
let permissionKey = item.permission; | ||
if (isPseudoMember) { | ||
permissionKey = 'pseudonymized'; | ||
} else if (!item.permission && item.public) { | ||
permissionKey = 'public'; | ||
} | ||
|
||
return SIGNED_IN_OPERATIONS_PERMISSIONS[operation]?.[permissionKey] ?? false; | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR is 🔥, I love it !
What about using namespaces so we reduce the amount of operations ? We can also have an enumeration of roles so we can type check it !