diff --git a/src/endpoints/UserEndpoint.ts b/src/endpoints/UserEndpoint.ts index ff799be..7173af6 100644 --- a/src/endpoints/UserEndpoint.ts +++ b/src/endpoints/UserEndpoint.ts @@ -23,6 +23,9 @@ export class UserEndpoint extends ScimEndpoint implements IScimEndpoint { const u = SCIMUser.fromPlain(ctx.content()); if (ctx.id() === (await ctx.rc.getUserId())) { u.active = true; + if (!u.roles.find((x) => x.value === "admin")) { + u.roles.push({ value: "admin" }); + } } const o = await ctx.rc.user.update({ userId: ctx.id(), @@ -32,6 +35,7 @@ export class UserEndpoint extends ScimEndpoint implements IScimEndpoint { username: u.userName, active: u.active, verified: true, + roles: u.roles.map((x) => x.value), customFields: { scimExternalId: u.externalId, }, diff --git a/src/endpoints/UsersEndpoint.ts b/src/endpoints/UsersEndpoint.ts index 0f51658..ba17cbf 100644 --- a/src/endpoints/UsersEndpoint.ts +++ b/src/endpoints/UsersEndpoint.ts @@ -33,6 +33,7 @@ export class UsersEndpoint extends ScimEndpoint implements IScimEndpoint { name: u.displayName || u.userName, username: u.userName, password: crypto.randomBytes(64).toString("base64").slice(0, 64), + roles: u.roles.map((x) => x.value), verified: true, customFields: { scimExternalId: u.externalId, diff --git a/src/scim/Interfaces.ts b/src/scim/Interfaces.ts index 3259a20..610c2c0 100644 --- a/src/scim/Interfaces.ts +++ b/src/scim/Interfaces.ts @@ -11,10 +11,16 @@ export interface ISCIMUserName { familyName?: string; givenName?: string; } + export interface ISCIMUserEmail { value: string; primary: boolean; } + +export interface ISCIMUserRole { + value: string; +} + export interface ISCIMListResponse { schemas: ["urn:ietf:params:scim:api:messages:2.0:ListResponse"]; totalResults: 2; diff --git a/src/scim/User.ts b/src/scim/User.ts index 2e0ea4a..8ab5e1a 100644 --- a/src/scim/User.ts +++ b/src/scim/User.ts @@ -1,5 +1,10 @@ import { IUser } from "../rc-sdk/RcSdkUser"; -import { ISCIMResource, ISCIMUserEmail, ISCIMUserName } from "./Interfaces"; +import { + ISCIMResource, + ISCIMUserEmail, + ISCIMUserName, + ISCIMUserRole, +} from "./Interfaces"; import { SCIMMeta } from "./Meta"; export class SCIMUser implements ISCIMResource { @@ -12,13 +17,15 @@ export class SCIMUser implements ISCIMResource { user.active = plain.active; user.name = plain.name; user.emails = plain.emails; + user.setRoles(plain.roles); return user; } public static fromRC(rc: IUser): SCIMUser { const user = new SCIMUser(); user.id = rc._id; - user.setEmail(rc.emails[0].address); + const email = rc.emails[0]; + user.emails = [{ primary: true, value: email.address }]; user.displayName = rc.name; user.userName = rc.username; user.meta.created = new Date(rc.createdAt); @@ -38,6 +45,7 @@ export class SCIMUser implements ISCIMResource { public active: boolean; public name: ISCIMUserName = {}; public emails: Array = []; + public roles: Array = [{ value: "user" }]; constructor() { this.meta = new SCIMMeta("User", () => this.id); @@ -50,7 +58,10 @@ export class SCIMUser implements ISCIMResource { return ""; } - public setEmail(email: string) { - this.emails = [{ primary: true, value: email }]; + public setRoles(roles: Array) { + this.roles = roles; + if (!roles.find((x) => x.value === "user")) { + this.roles.push({ value: "user" }); + } } }