-
Notifications
You must be signed in to change notification settings - Fork 0
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 disable duplicate user or group permission #242
Changes from 2 commits
d8e3cd7
ab9465e
339f3eb
aa0513b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,43 @@ import { | |
WORKSPACE_UPDATE_APP_ID, | ||
} from '../common/constants'; | ||
|
||
const validatePermissionModesCombinations = [ | ||
[WorkspacePermissionMode.LibraryRead, WorkspacePermissionMode.Read], // Read | ||
[WorkspacePermissionMode.LibraryWrite, WorkspacePermissionMode.Read], // Write | ||
[WorkspacePermissionMode.LibraryWrite, WorkspacePermissionMode.Write], // Admin | ||
]; | ||
|
||
const isValidatePermissionModesCombination = (permissionModes: string[]) => | ||
validatePermissionModesCombinations.some( | ||
(combination) => | ||
combination.length === permissionModes.length && | ||
combination.every((mode) => permissionModes.includes(mode)) | ||
); | ||
const isValidatePermissions = (permissions: Permissions) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we move Not only make it easier to validate the permission mode combinations, but also make it easier to validate the user/group duplications here as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good idea. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we also can rename |
||
const userOrGroupKey2PermissionModes = permissions | ||
? Object.keys(permissions).reduce<{ | ||
[key: string]: string[]; | ||
}>((previousValue, permissionMode) => { | ||
permissions[permissionMode].users?.forEach((user) => { | ||
const key = `user-${user}`; | ||
previousValue[key] = [...(previousValue[key] || []), permissionMode]; | ||
}); | ||
permissions[permissionMode].groups?.forEach((user) => { | ||
const key = `group-${user}`; | ||
previousValue[key] = [...(previousValue[key] || []), permissionMode]; | ||
}); | ||
return previousValue; | ||
}, {}) | ||
: {}; | ||
|
||
for (const key in userOrGroupKey2PermissionModes) { | ||
if (!isValidatePermissionModesCombination(userOrGroupKey2PermissionModes[key])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}; | ||
|
||
const WORKSPACE_ID_SIZE = 6; | ||
|
||
const DUPLICATE_WORKSPACE_NAME_ERROR = i18n.translate('workspace.duplicate.name.error', { | ||
|
@@ -49,6 +86,10 @@ const RESERVED_WORKSPACE_NAME_ERROR = i18n.translate('workspace.reserved.name.er | |
defaultMessage: 'reserved workspace name cannot be changed', | ||
}); | ||
|
||
const INVALID_PERMISSION_MODES_COMBINATION = i18n.translate('workspace.invalid.permission.error', { | ||
defaultMessage: 'Invalid workspace permission mode combination', | ||
}); | ||
|
||
export class WorkspaceClientWithSavedObject implements IWorkspaceDBImpl { | ||
private setupDep: CoreSetup; | ||
private logger: Logger; | ||
|
@@ -207,6 +248,11 @@ export class WorkspaceClientWithSavedObject implements IWorkspaceDBImpl { | |
if (existingWorkspaceRes && existingWorkspaceRes.total > 0) { | ||
throw new Error(DUPLICATE_WORKSPACE_NAME_ERROR); | ||
} | ||
|
||
if (permissions && !isValidatePermissions(permissions)) { | ||
throw new Error(INVALID_PERMISSION_MODES_COMBINATION); | ||
} | ||
|
||
const result = await client.create<Omit<WorkspaceAttribute, 'id'>>( | ||
WORKSPACE_TYPE, | ||
attributes, | ||
|
@@ -359,6 +405,10 @@ export class WorkspaceClientWithSavedObject implements IWorkspaceDBImpl { | |
} | ||
} | ||
|
||
if (permissions && !isValidatePermissions(permissions)) { | ||
throw new Error(INVALID_PERMISSION_MODES_COMBINATION); | ||
} | ||
|
||
await client.create<Omit<WorkspaceAttribute, 'id'>>(WORKSPACE_TYPE, attributes, { | ||
id, | ||
permissions, | ||
|
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.
Maybe not in the scope, but should
permissions
be optional? Because user may not have permission control turned on.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.
Sure, the permissions could be optional.