]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/api/user.service.ts
import 15.2.4
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / api / user.service.ts
1 import { HttpClient, HttpParams } from '@angular/common/http';
2 import { Injectable } from '@angular/core';
3 import { Observable, of as observableOf } from 'rxjs';
4 import { catchError, mapTo } from 'rxjs/operators';
5
6 import { UserFormModel } from '../../core/auth/user-form/user-form.model';
7 import { ApiModule } from './api.module';
8
9 @Injectable({
10 providedIn: ApiModule
11 })
12 export class UserService {
13 constructor(private http: HttpClient) {}
14
15 list() {
16 return this.http.get('api/user');
17 }
18
19 delete(username: string) {
20 return this.http.delete(`api/user/${username}`);
21 }
22
23 get(username: string) {
24 return this.http.get(`api/user/${username}`);
25 }
26
27 create(user: UserFormModel) {
28 return this.http.post(`api/user`, user);
29 }
30
31 update(user: UserFormModel) {
32 return this.http.put(`api/user/${user.username}`, user);
33 }
34
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
42 });
43 }
44
45 validateUserName(user_name: string): Observable<boolean> {
46 return this.get(user_name).pipe(
47 mapTo(true),
48 catchError((error) => {
49 error.preventDefault();
50 return observableOf(false);
51 })
52 );
53 }
54
55 validatePassword(password: string, username: string = null, oldPassword: string = null) {
56 let params = new HttpParams();
57 params = params.append('password', password);
58 if (username) {
59 params = params.append('username', username);
60 }
61 if (oldPassword) {
62 params = params.append('old_password', oldPassword);
63 }
64 return this.http.post('api/user/validate_password', null, { params });
65 }
66 }