import {Injectable} from '@angular/core'; import {BackendService, RequestTypes} from "./backend.service"; import {User, UserEditor, UserLogin} from "../entitys/user"; import {AccessToken} from "../entitys/accessToken"; @Injectable({ providedIn: 'root' }) export class UserApi { private user: User; constructor(private backend: BackendService) { } public async getUsers(): Promise { const response = await this.backend.sendRequest(RequestTypes.GET, "users", undefined, {authorized: true}); if (!response.success) return []; return response.content; } public async getUser(id: string): Promise { const response = await this.backend.sendRequest(RequestTypes.GET, "users/" + id, undefined, {authorized: true}); return response.content; } public async editUser(id: string, editor: UserEditor): Promise { const response = await this.backend.sendRequest(RequestTypes.PUT, "users/" + id, editor, {authorized: true}); return response.success; } public async deleteUser(id: string): Promise { const response = await this.backend.sendRequest(RequestTypes.DELETE, "users/" + id, undefined, {authorized: true}); return response.success; } public async getUserPermissions(id: string, includeGroupPermissions: boolean = true): Promise { const response = await this.backend.sendRequest(RequestTypes.GET, "users/" + id + "/permissions" + (includeGroupPermissions ? "/raw" : ""), undefined, {authorized: true}); if (!response.success) return []; return response.content; } public async addUserPermissions(id: string, permissions: string[]): Promise { const response = await this.backend.sendRequest(RequestTypes.POST, "users/" + id + "/permissions", permissions, {authorized: true}); return response.success; } public async removeUserPermissions(id: string, permissions: string[]): Promise { const response = await this.backend.sendRequest(RequestTypes.PUT, "users/" + id + "/permissions", permissions, {authorized: true}); return response.success; } public async login(login: UserLogin): Promise<{success: boolean, errorMessage: string}> { const response = await this.backend.sendRequest(RequestTypes.PUT, "users/login", login, {withCredentials: true}); if (response.success) { this.backend.setToken(response.content.id); await this.getAuthorizedUser(); } return {success: response.success, errorMessage: response.message}; } public async register(register: UserEditor): Promise<{success: boolean, errorMessage: string}> { const response = await this.backend.sendRequest(RequestTypes.POST, "users/register", register, {withCredentials: true}); if (response.success) { this.backend.setToken(response.content.id); await this.getAuthorizedUser(); } return {success: response.success, errorMessage: response.message}; } public async logout(id: string): Promise { const response = await this.backend.sendRequest(RequestTypes.DELETE, "users/" + id + "/logout", undefined, {authorized: true, withCredentials: true}); return response.success; } public async getAuthorizedUser(): Promise { if (this.user != undefined) return this.user; const response = await this.backend.sendRequest(RequestTypes.GET, "users/self", undefined, {authorized: true}); if (response.success) this.user = response.content; return response.content; } }