-
Notifications
You must be signed in to change notification settings - Fork 617
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow assignment of multiple teams (#4198)
- Loading branch information
Showing
40 changed files
with
767 additions
and
373 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
60 changes: 60 additions & 0 deletions
60
packages/api-admin-users/src/createExternalIdpAdminUserHooks.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,60 @@ | ||
import { ContextPlugin } from "@webiny/api"; | ||
import { AdminUsersContext } from "~/types"; | ||
|
||
export const createExternalIdpAdminUserHooks = () => { | ||
return new ContextPlugin<AdminUsersContext>(async context => { | ||
const { security, adminUsers } = context; | ||
|
||
security.onLogin.subscribe(async ({ identity }) => { | ||
await security.withoutAuthorization(async () => { | ||
const user = await adminUsers.getUser({ where: { id: identity.id } }); | ||
|
||
const id = identity.id; | ||
const email = identity.email || `id:${id}`; | ||
const displayName = identity.displayName || "Missing display name"; | ||
|
||
const data = { | ||
displayName, | ||
email, | ||
groups: [] as string[], | ||
teams: [] as string[] | ||
}; | ||
|
||
let groupSlugs: string[] = []; | ||
if (identity.group) { | ||
groupSlugs = [identity.group]; | ||
} | ||
|
||
if (Array.isArray(identity.groups)) { | ||
groupSlugs = groupSlugs.concat(identity.groups); | ||
} | ||
|
||
let teamSlugs: string[] = []; | ||
if (identity.team) { | ||
teamSlugs = [identity.team]; | ||
} | ||
|
||
if (Array.isArray(identity.teams)) { | ||
teamSlugs = teamSlugs.concat(identity.teams); | ||
} | ||
|
||
if (groupSlugs.length > 0) { | ||
const groups = await security.listGroups({ where: { slug_in: groupSlugs } }); | ||
data.groups = groups.map(group => group.id); | ||
} | ||
|
||
if (teamSlugs.length > 0) { | ||
const teams = await security.listTeams({ where: { slug_in: teamSlugs } }); | ||
data.teams = teams.map(team => team.id); | ||
} | ||
|
||
if (user) { | ||
await adminUsers.updateUser(identity.id, data); | ||
return; | ||
} | ||
|
||
await adminUsers.createUser({ id, ...data }); | ||
}); | ||
}); | ||
}); | ||
}; |
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 |
---|---|---|
@@ -1,49 +1,3 @@ | ||
import { ContextPlugin } from "@webiny/api"; | ||
import { AdminUsersContext } from "@webiny/api-admin-users/types"; | ||
import { createExternalIdpAdminUserHooks } from "@webiny/api-admin-users/createExternalIdpAdminUserHooks"; | ||
|
||
export const createAdminUsersHooks = () => { | ||
return new ContextPlugin<AdminUsersContext>(async context => { | ||
const { security, adminUsers } = context; | ||
|
||
security.onLogin.subscribe(async ({ identity }) => { | ||
await security.withoutAuthorization(async () => { | ||
const user = await adminUsers.getUser({ where: { id: identity.id } }); | ||
|
||
const id = identity.id; | ||
const email = identity.email || `id:${id}`; | ||
const displayName = identity.displayName || "Missing display name"; | ||
|
||
let groupId: string | null = null; | ||
let teamId: string | null = null; | ||
|
||
if (identity.group) { | ||
const group = await security.getGroup({ where: { slug: identity.group } }); | ||
if (group) { | ||
groupId = group.id; | ||
} | ||
} | ||
|
||
if (identity.team) { | ||
const team = await security.getTeam({ where: { slug: identity.team } }); | ||
if (team) { | ||
teamId = team.id; | ||
} | ||
} | ||
|
||
const data = { | ||
displayName, | ||
email, | ||
group: groupId, | ||
team: teamId | ||
}; | ||
|
||
if (user) { | ||
await adminUsers.updateUser(identity.id, data); | ||
return; | ||
} | ||
|
||
await adminUsers.createUser({ id, ...data }); | ||
}); | ||
}); | ||
}); | ||
}; | ||
export const createAdminUsersHooks = createExternalIdpAdminUserHooks; |
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 |
---|---|---|
|
@@ -6,7 +6,7 @@ const DATA_FIELD = /* GraphQL */ ` | |
lastName | ||
avatar | ||
gravatar | ||
group { | ||
groups { | ||
id | ||
slug | ||
name | ||
|
Oops, something went wrong.