summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/shared/api/user.service.ts
blob: 95c80dd4665a039cc41e67e9c162b30d43a9650a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';

import { Observable, of as observableOf } from 'rxjs';
import { catchError, mapTo } from 'rxjs/operators';

import { UserFormModel } from '~/app/core/auth/user-form/user-form.model';

@Injectable({
  providedIn: 'root'
})
export class UserService {
  constructor(private http: HttpClient) {}

  list() {
    return this.http.get('api/user');
  }

  delete(username: string) {
    return this.http.delete(`api/user/${username}`);
  }

  get(username: string) {
    return this.http.get(`api/user/${username}`);
  }

  create(user: UserFormModel) {
    return this.http.post(`api/user`, user);
  }

  update(user: UserFormModel) {
    return this.http.put(`api/user/${user.username}`, user);
  }

  changePassword(username: string, oldPassword: string, newPassword: string) {
    // Note, the specified user MUST be logged in to be able to change
    // the password. The backend ensures that the password of another
    // user can not be changed, otherwise an error will be thrown.
    return this.http.post(`api/user/${username}/change_password`, {
      old_password: oldPassword,
      new_password: newPassword
    });
  }

  validateUserName(user_name: string): Observable<boolean> {
    return this.get(user_name).pipe(
      mapTo(true),
      catchError((error) => {
        error.preventDefault();
        return observableOf(false);
      })
    );
  }

  validatePassword(password: string, username: string = null, oldPassword: string = null) {
    return this.http.post('api/user/validate_password', {
      password: password,
      username: username,
      old_password: oldPassword
    });
  }
}