Client for managing users with the RESTful API

Jens Pelzetter 2020-08-03 17:04:55 +02:00
parent 280af3f6e7
commit d091c31d55
1 changed files with 326 additions and 0 deletions

View File

@ -0,0 +1,326 @@
import {
ApiResponse,
LibreCcmApiClient,
ListView,
} from "@libreccm/ccm-apiclient-commons";
import { buildIdentifierParam } from "@libreccm/ccm-apiclient-commons";
import * as Constants from "../constants";
import {
User,
buildUserFromRecord,
buildUserGroupMembershipFromRecord,
UserGroupMembership,
} from "../entities/user";
import {
buildPartyRoleMembershipFromRecord,
PartyRoleMembership,
} from "../entities/party";
/**
* Client for managing roles using the RESTful API of LibreCCM
*/
export class UsersApiClient {
#apiClient: LibreCcmApiClient;
readonly #USERS_API_PREFIX = `${Constants.ADMIN_API_PREFIX}/users`;
constructor(apiClient: LibreCcmApiClient) {
this.#apiClient = apiClient;
}
async getUsers(limit: number, offset: number): Promise<ListView<User>> {
try {
const response: ApiResponse = await this.#apiClient.get(
`${this.#USERS_API_PREFIX}`,
{
limit: limit.toString(),
offset: offset.toString(),
}
);
if (response.ok) {
const result: Record<
string,
unknown
> = (await response.json()) as Record<string, unknown>;
const list: Record<string, unknown>[] = result.list as Record<
string,
unknown
>[];
const users: User[] = list.map((record) =>
buildUserFromRecord(record)
);
return {
list: users,
count: result.count as number,
limit: result.limit as number,
offset: result.offset as number,
};
} else {
throw `Failed to get users:
${response.status} ${response.statusText}`;
}
} catch (err) {
throw `Failed to get users: ${err}`;
}
}
async getUser(userIdentifier: string | number): Promise<User> {
try {
const response: ApiResponse = await this.#apiClient.get(
`${this.#USERS_API_PREFIX}/${buildIdentifierParam(
userIdentifier
)}`
);
if (response.ok) {
return buildUserFromRecord(
(await response.json()) as Record<string, unknown>
);
} else {
throw `Failed to get user ${userIdentifier}:
${response.status} ${response.statusText}`;
}
} catch (err) {
throw `Failed to get user ${userIdentifier}: ${err}`;
}
}
async addUser(user: User): Promise<string> {
try {
const response: ApiResponse = await this.#apiClient.post(
`${this.#USERS_API_PREFIX}`,
JSON.stringify(user)
);
if (response.ok) {
return user.name;
} else {
throw `Failed to add user ${user.name}:
${response.status} ${response.statusText}`;
}
} catch (err) {
throw `Failed to add user ${user.name}: ${err}`;
}
}
async updateUser(userIdentifier: string, user: User): Promise<void> {
try {
const response: ApiResponse = await this.#apiClient.put(
`${this.#USERS_API_PREFIX}/${buildIdentifierParam(
userIdentifier
)}`,
JSON.stringify(user)
);
if (response.ok) {
return;
} else {
throw `Failed to update user ${userIdentifier}:
${response.status} ${response.statusText}`;
}
} catch (err) {
throw `Failed to update user ${userIdentifier}: ${err}`;
}
}
async deleteUser(userIdentifier: string): Promise<void> {
try {
const response: ApiResponse = await this.#apiClient.delete(
`${this.#USERS_API_PREFIX}/${buildIdentifierParam(
userIdentifier
)}`
);
if (response.ok) {
return;
} else {
throw `Failed to delete user ${name}:
${response.status} ${response.statusText}`;
}
} catch (err) {
throw `Failed to delete user ${name}: ${err}`;
}
}
async getGroupMemberships(
userIdentifier: string | number,
limit: number,
offset: number
): Promise<ListView<UserGroupMembership>> {
try {
const userParam = buildIdentifierParam(userIdentifier);
const response: ApiResponse = await this.#apiClient.get(
`${this.#USERS_API_PREFIX}/${userParam}/groups`,
{
limit: limit.toString(),
offset: offset.toString(),
}
);
if (response.ok) {
const result: Record<
string,
unknown
> = (await response.json()) as Record<string, unknown>;
const list: Record<string, unknown>[] = result.list as Record<
string,
unknown
>[];
const groups: UserGroupMembership[] = list.map((record) =>
buildUserGroupMembershipFromRecord(record)
);
return {
list: groups,
count: result.count as number,
limit: result.limit as number,
offset: result.limit as number,
};
} else {
throw `Failed to get group memberships of
user ${userIdentifier}:
${response.status} ${response.statusText}`;
}
} catch (err) {
throw `Failed to get group memberships of user
${userIdentifier}: ${err}`;
}
}
async addGroupMembership(
userIdentifier: string | number,
groupIdentifier: string | number
): Promise<void> {
try {
const userParam = buildIdentifierParam(userIdentifier);
const groupParam = buildIdentifierParam(groupIdentifier);
const response: ApiResponse = await this.#apiClient.put(
`${this.#USERS_API_PREFIX}/${userParam}/groups/${groupParam}`
);
if (response.ok) {
return;
} else {
throw `Failed to add group membership for group ${groupIdentifier}
to user
${userIdentifier}:
${response.status} ${response.statusText}`;
}
} catch (err) {
throw `Failed to add group membership for group ${groupIdentifier}
to user
${userIdentifier}: ${err}`;
}
}
async removeGroupMembership(
userIdentifier: string | number,
groupIdentifier: string | number
): Promise<void> {
try {
const userParam = buildIdentifierParam(userIdentifier);
const groupParam = buildIdentifierParam(groupIdentifier);
const response: ApiResponse = await this.#apiClient.delete(
`${this.#USERS_API_PREFIX}/${userParam}/groups/${groupParam}`
);
if (response.ok) {
return;
} else {
throw `Failed to remote group membership for group ${groupIdentifier}
from user
${userIdentifier}:
${response.status} ${response.statusText}`;
}
} catch (err) {
throw `Failed to remove group membership for group ${groupIdentifier}
from user
${userIdentifier}: ${err}`;
}
}
async getRoles(
userIdentifier: string
): Promise<ListView<PartyRoleMembership>> {
const userParam: string = buildIdentifierParam(userIdentifier);
try {
const response: ApiResponse = await this.#apiClient.get(
`${this.#USERS_API_PREFIX}/${userParam}/roles`
);
if (response.ok) {
const result: Record<
string,
unknown
> = (await response.json()) as Record<string, unknown>;
const list: Record<string, unknown>[] = result.list as Record<
string,
unknown
>[];
const roles: PartyRoleMembership[] = list.map((record) =>
buildPartyRoleMembershipFromRecord(record)
);
return {
list: roles,
count: result.count as number,
limit: result.limit as number,
offset: result.offset as number,
};
} else {
throw `Failed to get roles of user ${userIdentifier}:
${response.status} ${response.statusText}`;
}
} catch (err) {
throw `Failed to get roles of user ${userIdentifier}: ${err}`;
}
}
async addRole(
userIdentifier: string | number,
roleIdentifier: string | number
): Promise<void> {
try {
const groupParam = buildIdentifierParam(userIdentifier);
const roleParam = buildIdentifierParam(roleIdentifier);
const response: ApiResponse = await this.#apiClient.put(
`${this.#USERS_API_PREFIX}/${groupParam}/roles/${roleParam}`
);
if (response.ok) {
return;
} else {
throw `Failed to add role ${roleIdentifier} to
user ${userIdentifier}:
${response.status} ${response.statusText}`;
}
} catch (err) {
throw `Failed to add role ${roleIdentifier} to
user ${userIdentifier}: ${err}`;
}
}
async removeRole(
userIdentifier: string | number,
roleIdentifier: string | number
): Promise<void> {
try {
const userParam = buildIdentifierParam(userIdentifier);
const roleParam = buildIdentifierParam(roleIdentifier);
const response: ApiResponse = await this.#apiClient.delete(
`${this.#USERS_API_PREFIX}/${userParam}/roles/${roleParam}`
);
if (response.ok) {
return;
} else {
throw `Failed to remove role ${roleIdentifier} from
user ${userIdentifier}:
${response.status} ${response.statusText}`;
}
} catch (err) {
throw `Failed to remote role ${roleIdentifier} from
user ${userIdentifier}: ${err}`;
}
}
}