(feat) role support
This commit is contained in:
parent
59917cc25d
commit
55c7753c86
4 changed files with 26 additions and 4 deletions
|
@ -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,
|
||||
},
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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<ISCIMUserEmail> = [];
|
||||
public roles: Array<ISCIMUserRole> = [{ 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<ISCIMUserRole>) {
|
||||
this.roles = roles;
|
||||
if (!roles.find((x) => x.value === "user")) {
|
||||
this.roles.push({ value: "user" });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue