summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rgw-user.service.ts
blob: 66167bcabbd0777ff89778b0b04d778f99ed89ae (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import { HttpClient, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';

import _ from 'lodash';
import { forkJoin as observableForkJoin, Observable, of as observableOf } from 'rxjs';
import { catchError, mapTo, mergeMap } from 'rxjs/operators';

import { RgwDaemonService } from '~/app/shared/api/rgw-daemon.service';
import { cdEncode } from '~/app/shared/decorators/cd-encode';

@cdEncode
@Injectable({
  providedIn: 'root'
})
export class RgwUserService {
  private url = 'api/rgw/user';

  constructor(private http: HttpClient, private rgwDaemonService: RgwDaemonService) {}

  /**
   * Get the list of users.
   * @return {Observable<Object[]>}
   */
  list() {
    return this.enumerate().pipe(
      mergeMap((uids: string[]) => {
        if (uids.length > 0) {
          return observableForkJoin(
            uids.map((uid: string) => {
              return this.get(uid);
            })
          );
        }
        return observableOf([]);
      })
    );
  }

  /**
   * Get the list of usernames.
   * @return {Observable<string[]>}
   */
  enumerate() {
    return this.rgwDaemonService.request((params: HttpParams) => {
      return this.http.get(this.url, { params: params });
    });
  }

  enumerateEmail() {
    return this.rgwDaemonService.request((params: HttpParams) => {
      return this.http.get(`${this.url}/get_emails`, { params: params });
    });
  }

  get(uid: string) {
    return this.rgwDaemonService.request((params: HttpParams) => {
      return this.http.get(`${this.url}/${uid}`, { params: params });
    });
  }

  getQuota(uid: string) {
    return this.rgwDaemonService.request((params: HttpParams) => {
      return this.http.get(`${this.url}/${uid}/quota`, { params: params });
    });
  }

  create(args: Record<string, any>) {
    return this.rgwDaemonService.request((params: HttpParams) => {
      _.keys(args).forEach((key) => {
        params = params.append(key, args[key]);
      });
      return this.http.post(this.url, null, { params: params });
    });
  }

  update(uid: string, args: Record<string, any>) {
    return this.rgwDaemonService.request((params: HttpParams) => {
      _.keys(args).forEach((key) => {
        params = params.append(key, args[key]);
      });
      return this.http.put(`${this.url}/${uid}`, null, { params: params });
    });
  }

  updateQuota(uid: string, args: Record<string, string>) {
    return this.rgwDaemonService.request((params: HttpParams) => {
      _.keys(args).forEach((key) => {
        params = params.append(key, args[key]);
      });
      return this.http.put(`${this.url}/${uid}/quota`, null, { params: params });
    });
  }

  delete(uid: string) {
    return this.rgwDaemonService.request((params: HttpParams) => {
      return this.http.delete(`${this.url}/${uid}`, { params: params });
    });
  }

  createSubuser(uid: string, args: Record<string, string>) {
    return this.rgwDaemonService.request((params: HttpParams) => {
      _.keys(args).forEach((key) => {
        params = params.append(key, args[key]);
      });
      return this.http.post(`${this.url}/${uid}/subuser`, null, { params: params });
    });
  }

  deleteSubuser(uid: string, subuser: string) {
    return this.rgwDaemonService.request((params: HttpParams) => {
      return this.http.delete(`${this.url}/${uid}/subuser/${subuser}`, { params: params });
    });
  }

  addCapability(uid: string, type: string, perm: string) {
    return this.rgwDaemonService.request((params: HttpParams) => {
      params = params.append('type', type);
      params = params.append('perm', perm);
      return this.http.post(`${this.url}/${uid}/capability`, null, { params: params });
    });
  }

  deleteCapability(uid: string, type: string, perm: string) {
    return this.rgwDaemonService.request((params: HttpParams) => {
      params = params.append('type', type);
      params = params.append('perm', perm);
      return this.http.delete(`${this.url}/${uid}/capability`, { params: params });
    });
  }

  addS3Key(uid: string, args: Record<string, string>) {
    return this.rgwDaemonService.request((params: HttpParams) => {
      params = params.append('key_type', 's3');
      _.keys(args).forEach((key) => {
        params = params.append(key, args[key]);
      });
      return this.http.post(`${this.url}/${uid}/key`, null, { params: params });
    });
  }

  deleteS3Key(uid: string, accessKey: string) {
    return this.rgwDaemonService.request((params: HttpParams) => {
      params = params.append('key_type', 's3');
      params = params.append('access_key', accessKey);
      return this.http.delete(`${this.url}/${uid}/key`, { params: params });
    });
  }

  /**
   * Check if the specified user ID exists.
   * @param {string} uid The user ID to check.
   * @return {Observable<boolean>}
   */
  exists(uid: string): Observable<boolean> {
    return this.get(uid).pipe(
      mapTo(true),
      catchError((error: Event) => {
        if (_.isFunction(error.preventDefault)) {
          error.preventDefault();
        }
        return observableOf(false);
      })
    );
  }

  // Using @cdEncodeNot would be the preferred way here, but this
  // causes an error: https://tracker.ceph.com/issues/37505
  // Use decodeURIComponent as workaround.
  // emailExists(@cdEncodeNot email: string): Observable<boolean> {
  emailExists(email: string): Observable<boolean> {
    email = decodeURIComponent(email);
    return this.enumerateEmail().pipe(
      mergeMap((resp: any[]) => {
        const index = _.indexOf(resp, email);
        return observableOf(-1 !== index);
      })
    );
  }
}