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

53 lines
1.7 KiB
TypeScript
Raw Normal View History

2022-02-15 12:38:33 +00:00
import { HttpStatusCode } from "@rocket.chat/apps-engine/definition/accessors";
import { IApiResponse } from "@rocket.chat/apps-engine/definition/api";
import { Context } from "./Context";
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-15 12:38:33 +00:00
public async _get(ctx: Context): Promise<IApiResponse> {
const o = await ctx.rc.user.info(ctx.id());
2022-02-14 13:03:16 +00:00
this.handleError(o);
const user = SCIMUser.fromRC(o.user);
return this.success(user);
2022-02-09 15:35:37 +00:00
}
2022-02-15 12:38:33 +00:00
public async _put(ctx: Context): Promise<IApiResponse> {
const o = await ctx.rc.user.update(
this.scimToUserUpdate(ctx.id(), SCIMUser.fromPlain(ctx.content())),
2022-02-14 13:03:16 +00:00
);
this.handleError(o);
const user = SCIMUser.fromRC(o.user);
return this.success(user);
2022-02-09 15:35:37 +00:00
}
2022-02-15 12:38:33 +00:00
public async _delete(ctx: Context): Promise<IApiResponse> {
const o = await ctx.rc.user.delete({
userId: ctx.id(),
2022-02-09 15:35:37 +00:00
confirmRelinquish: true,
2022-02-15 12:38:33 +00:00
});
2022-02-14 13:03:16 +00:00
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
}