-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(workspaces): assign project roles for workspace projects (#2499)
* feat(workspaces): drop createdByUserId from the dataschema * feat(workspaces): repositories WIP * merge * protect against removing last admin in workspace * quick impl and stub tests * add tests * services * unit tests for role services * feat(workspaces): authorize project creation if workspace specified * feat(workspaces): emit project created event * feat(workspaces): assign roles on project create in workspace * feat(workspaces): update project roles when user added to workspace * fix(workspaces): perform automatic project role update in service function * fix(workspaces): also delete roles * fix(workspaces): broke tests again oops * fix(workspaces): update `onProjectCreated` listener to use new repo method * fix(workspaces): use service function in event listener * fix(workspaces): get workspace projects via existing stream repo functions * fix(workspaces): roles mapping in domain, use enum * fix(workspaces): repair type reference in tests * fix(workspaces): consolidate files, use different existing stream-getter * fix(workspaces): more specific error * fix(workspaces): yield per page * fix(workspaces): some test dry * fix(workspaces): superdry * fix(workspaces): classic --------- Co-authored-by: Gergő Jedlicska <[email protected]>
- Loading branch information
1 parent
27179ad
commit 66eb539
Showing
21 changed files
with
866 additions
and
422 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
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,16 @@ | ||
import { Roles, StreamRoles, WorkspaceRoles } from '@speckle/shared' | ||
|
||
/** | ||
* Given a user's workspace role, return the role they should have for workspace projects. | ||
*/ | ||
export const mapWorkspaceRoleToProjectRole = ( | ||
workspaceRole: WorkspaceRoles | ||
): StreamRoles => { | ||
switch (workspaceRole) { | ||
case Roles.Workspace.Guest: | ||
case Roles.Workspace.Member: | ||
return Roles.Stream.Reviewer | ||
case Roles.Workspace.Admin: | ||
return Roles.Stream.Owner | ||
} | ||
} |
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
52 changes: 52 additions & 0 deletions
52
packages/server/modules/workspaces/events/eventListener.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,52 @@ | ||
import { | ||
ProjectsEmitter, | ||
ProjectEvents, | ||
ProjectEventsPayloads | ||
} from '@/modules/core/events/projectsEmitter' | ||
import { getWorkspaceRolesFactory } from '@/modules/workspaces/repositories/workspaces' | ||
import { grantStreamPermissions as repoGrantStreamPermissions } from '@/modules/core/repositories/streams' | ||
import { Knex } from 'knex' | ||
import { GetWorkspaceRoles } from '@/modules/workspaces/domain/operations' | ||
import { mapWorkspaceRoleToProjectRole } from '@/modules/workspaces/domain/roles' | ||
|
||
export const onProjectCreatedFactory = | ||
({ | ||
getWorkspaceRoles, | ||
grantStreamPermissions | ||
}: { | ||
getWorkspaceRoles: GetWorkspaceRoles | ||
grantStreamPermissions: typeof repoGrantStreamPermissions | ||
}) => | ||
async (payload: ProjectEventsPayloads[typeof ProjectEvents.Created]) => { | ||
const { id: projectId, workspaceId } = payload.project | ||
|
||
if (!workspaceId) { | ||
return | ||
} | ||
|
||
const workspaceMembers = await getWorkspaceRoles({ workspaceId }) | ||
|
||
await Promise.all( | ||
workspaceMembers.map(({ userId, role: workspaceRole }) => | ||
grantStreamPermissions({ | ||
streamId: projectId, | ||
userId, | ||
role: mapWorkspaceRoleToProjectRole(workspaceRole) | ||
}) | ||
) | ||
) | ||
} | ||
|
||
export const initializeEventListenersFactory = | ||
({ db }: { db: Knex }) => | ||
() => { | ||
const onProjectCreated = onProjectCreatedFactory({ | ||
getWorkspaceRoles: getWorkspaceRolesFactory({ db }), | ||
// TODO: Instantiate via factory function | ||
grantStreamPermissions: repoGrantStreamPermissions | ||
}) | ||
|
||
const quitCbs = [ProjectsEmitter.listen(ProjectEvents.Created, onProjectCreated)] | ||
|
||
return () => quitCbs.forEach((quit) => quit()) | ||
} |
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
Oops, something went wrong.