48 lines
1.7 KiB
TypeScript
48 lines
1.7 KiB
TypeScript
import { HttpStatusCode } from "@rocket.chat/apps-engine/definition/accessors";
|
|
import { IApiResponse } from "@rocket.chat/apps-engine/definition/api";
|
|
import crypto = require("crypto");
|
|
import { Context } from "./Context";
|
|
import { SCIMListResponse } from "./scim/ListResponse";
|
|
import { SCIMUser } from "./scim/User";
|
|
import { IScimEndpoint, ScimEndpoint } from "./ScimEndpoint";
|
|
|
|
export class UsersEndpoint extends ScimEndpoint implements IScimEndpoint {
|
|
public path = "Users";
|
|
|
|
public async _get(ctx: Context): Promise<IApiResponse> {
|
|
const o = await ctx.rc.user.list();
|
|
this.handleError(o);
|
|
const list = new SCIMListResponse();
|
|
list.Resources = o.users.map(SCIMUser.fromRC);
|
|
list.totalResults = o.total;
|
|
return this.success(list);
|
|
}
|
|
|
|
public async _post(ctx: Context): Promise<IApiResponse> {
|
|
const o = await ctx.rc.user.create(
|
|
this.scimToUserCreate(SCIMUser.fromPlain(ctx.content())),
|
|
);
|
|
this.handleError(o);
|
|
const user = SCIMUser.fromRC(o.user);
|
|
return this.response({
|
|
status: HttpStatusCode.CREATED,
|
|
content: user,
|
|
});
|
|
}
|
|
|
|
private scimToUserCreate(user: SCIMUser): IUserCreate {
|
|
return {
|
|
email: user.getEmail(),
|
|
name:
|
|
user.displayName ||
|
|
`${user.name.givenName} ${user.name.familyName}` ||
|
|
user.userName,
|
|
username: user.userName,
|
|
password: crypto.randomBytes(64).toString("base64").slice(0, 64),
|
|
verified: true,
|
|
customFields: {
|
|
scimExternalId: user.externalId,
|
|
},
|
|
};
|
|
}
|
|
}
|