This repository has been archived on 2024-09-23. You can view files and clone it, but cannot push or open issues or pull requests.
rocketchat-scim/UserEndpoint.ts
2022-02-14 14:03:16 +01:00

94 lines
2.6 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 { RcHttp } from "./RcHttp";
import { SCIMUser } from "./scim/User";
import { IScimEndpoint, ScimEndpoint } from "./ScimEndpoint";
export class UserEndpoint extends ScimEndpoint implements IScimEndpoint {
public path = "Users/:id";
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.info?userId=${request.params.id}`,
);
const o = this.parseResponse(response);
this.handleError(o);
const user = SCIMUser.fromRC(o.user);
return this.success(user);
}
public async _put(
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.update",
this.scimToUserUpdate(
request.params.id,
SCIMUser.fromPlain(request.content),
),
);
const o = this.parseResponse(response);
this.handleError(o);
const user = SCIMUser.fromRC(o.user);
return this.success(user);
}
public async _delete(
request: IApiRequest,
endpoint: IApiEndpointInfo,
read: IRead,
modify: IModify,
http: IHttp,
persis: IPersistence,
): Promise<IApiResponse> {
const d: IUserDelete = {
userId: request.params.id,
confirmRelinquish: true,
};
const response = await new RcHttp(http, read).post("users.delete", d);
const o = this.parseResponse(response);
this.handleError(o);
return this.response({
status: HttpStatusCode.NO_CONTENT,
});
}
private scimToUserUpdate(userId: string, user: SCIMUser): IUserUpdate {
return {
userId,
data: {
email: user.getEmail(),
name: user.displayName,
username: user.userName,
active: user.active,
verified: true,
customFields: {
scimExternalId: user.externalId,
},
},
};
}
}