78 lines
2.3 KiB
TypeScript
78 lines
2.3 KiB
TypeScript
import {
|
|
HttpStatusCode,
|
|
IHttp,
|
|
IModify,
|
|
IPersistence,
|
|
IRead,
|
|
} from "@rocket.chat/apps-engine/definition/accessors";
|
|
import {
|
|
IApiEndpointInfo,
|
|
IApiRequest,
|
|
IApiResponse,
|
|
} from "@rocket.chat/apps-engine/definition/api";
|
|
import crypto = require("crypto");
|
|
import { RcHttp } from "./RcHttp";
|
|
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(
|
|
request: IApiRequest,
|
|
endpoint: IApiEndpointInfo,
|
|
read: IRead,
|
|
modify: IModify,
|
|
http: IHttp,
|
|
persis: IPersistence,
|
|
): Promise<IApiResponse> {
|
|
const response = await new RcHttp(http, read).get(
|
|
`users.list?query={"type":{"$eq":"user"}}&fields={"createdAt":1}`,
|
|
);
|
|
const o = this.parseResponse(response);
|
|
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(
|
|
request: IApiRequest,
|
|
endpoint: IApiEndpointInfo,
|
|
read: IRead,
|
|
modify: IModify,
|
|
http: IHttp,
|
|
persis: IPersistence,
|
|
): Promise<IApiResponse> {
|
|
this.hasContent(request);
|
|
const response = await new RcHttp(http, read).post(
|
|
`users.create`,
|
|
this.scimToUserCreate(SCIMUser.fromPlain(request.content)),
|
|
);
|
|
const o = this.parseResponse(response);
|
|
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,
|
|
},
|
|
};
|
|
}
|
|
}
|