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

95 lines
2.6 KiB
TypeScript
Raw Normal View History

2022-02-09 15:35:37 +00:00
import {
HttpStatusCode,
IHttp,
IModify,
IPersistence,
IRead,
} from "@rocket.chat/apps-engine/definition/accessors";
import {
IApiEndpointInfo,
IApiRequest,
IApiResponse,
} from "@rocket.chat/apps-engine/definition/api";
2022-02-14 13:03:16 +00:00
import { RcHttp } from "./RcHttp";
2022-02-11 13:13:06 +00:00
import { SCIMUser } from "./scim/User";
2022-02-14 13:03:16 +00:00
import { IScimEndpoint, ScimEndpoint } from "./ScimEndpoint";
2022-02-09 15:35:37 +00:00
2022-02-14 13:03:16 +00:00
export class UserEndpoint extends ScimEndpoint implements IScimEndpoint {
2022-02-09 15:35:37 +00:00
public path = "Users/:id";
2022-02-14 13:03:16 +00:00
public async _get(
2022-02-09 15:35:37 +00:00
request: IApiRequest,
endpoint: IApiEndpointInfo,
read: IRead,
modify: IModify,
http: IHttp,
2022-02-14 13:03:16 +00:00
persis: IPersistence,
2022-02-09 15:35:37 +00:00
): Promise<IApiResponse> {
2022-02-14 13:03:16 +00:00
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);
2022-02-09 15:35:37 +00:00
}
2022-02-14 13:03:16 +00:00
public async _put(
2022-02-09 15:35:37 +00:00
request: IApiRequest,
endpoint: IApiEndpointInfo,
read: IRead,
modify: IModify,
http: IHttp,
2022-02-14 13:03:16 +00:00
persis: IPersistence,
2022-02-09 15:35:37 +00:00
): Promise<IApiResponse> {
2022-02-14 13:03:16 +00:00
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);
2022-02-09 15:35:37 +00:00
}
2022-02-14 13:03:16 +00:00
public async _delete(
2022-02-09 15:35:37 +00:00
request: IApiRequest,
endpoint: IApiEndpointInfo,
read: IRead,
modify: IModify,
http: IHttp,
2022-02-14 13:03:16 +00:00
persis: IPersistence,
2022-02-09 15:35:37 +00:00
): Promise<IApiResponse> {
2022-02-14 13:03:16 +00:00
const d: IUserDelete = {
2022-02-09 15:35:37 +00:00
userId: request.params.id,
confirmRelinquish: true,
};
2022-02-14 13:03:16 +00:00
const response = await new RcHttp(http, read).post("users.delete", d);
const o = this.parseResponse(response);
this.handleError(o);
return this.response({
2022-02-09 15:35:37 +00:00
status: HttpStatusCode.NO_CONTENT,
2022-02-14 13:03:16 +00:00
});
2022-02-11 13:13:06 +00:00
}
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,
},
},
};
}
2022-02-09 15:35:37 +00:00
}