1 import { HttpClient } from '@angular/common/http';
2 import { Injectable } from '@angular/core';
4 import { Observable, of as observableOf } from 'rxjs';
5 import { catchError, mapTo } from 'rxjs/operators';
7 import { UserFormModel } from '~/app/core/auth/user-form/user-form.model';
12 export class UserService {
13 constructor(private http: HttpClient) {}
16 return this.http.get('api/user');
19 delete(username: string) {
20 return this.http.delete(`api/user/${username}`);
23 get(username: string) {
24 return this.http.get(`api/user/${username}`);
27 create(user: UserFormModel) {
28 return this.http.post(`api/user`, user);
31 update(user: UserFormModel) {
32 return this.http.put(`api/user/${user.username}`, user);
35 changePassword(username: string, oldPassword: string, newPassword: string) {
36 // Note, the specified user MUST be logged in to be able to change
37 // the password. The backend ensures that the password of another
38 // user can not be changed, otherwise an error will be thrown.
39 return this.http.post(`api/user/${username}/change_password`, {
40 old_password: oldPassword,
41 new_password: newPassword
45 validateUserName(user_name: string): Observable<boolean> {
46 return this.get(user_name).pipe(
48 catchError((error) => {
49 error.preventDefault();
50 return observableOf(false);
55 validatePassword(password: string, username: string = null, oldPassword: string = null) {
56 return this.http.post('api/user/validate_password', {
59 old_password: oldPassword