1 import { HttpClient, HttpParams } from '@angular/common/http';
2 import { Injectable } from '@angular/core';
4 import _ from 'lodash';
5 import { forkJoin as observableForkJoin, Observable, of as observableOf } from 'rxjs';
6 import { catchError, mapTo, mergeMap } from 'rxjs/operators';
8 import { RgwDaemonService } from '~/app/shared/api/rgw-daemon.service';
9 import { cdEncode } from '~/app/shared/decorators/cd-encode';
15 export class RgwUserService {
16 private url = 'api/rgw/user';
18 constructor(private http: HttpClient, private rgwDaemonService: RgwDaemonService) {}
21 * Get the list of users.
22 * @return {Observable<Object[]>}
25 return this.enumerate().pipe(
26 mergeMap((uids: string[]) => {
27 if (uids.length > 0) {
28 return observableForkJoin(
29 uids.map((uid: string) => {
34 return observableOf([]);
40 * Get the list of usernames.
41 * @return {Observable<string[]>}
44 return this.rgwDaemonService.request((params: HttpParams) => {
45 return this.http.get(this.url, { params: params });
50 return this.rgwDaemonService.request((params: HttpParams) => {
51 return this.http.get(`${this.url}/get_emails`, { params: params });
56 return this.rgwDaemonService.request((params: HttpParams) => {
57 return this.http.get(`${this.url}/${uid}`, { params: params });
61 getQuota(uid: string) {
62 return this.rgwDaemonService.request((params: HttpParams) => {
63 return this.http.get(`${this.url}/${uid}/quota`, { params: params });
67 create(args: Record<string, any>) {
68 return this.rgwDaemonService.request((params: HttpParams) => {
69 _.keys(args).forEach((key) => {
70 params = params.append(key, args[key]);
72 return this.http.post(this.url, null, { params: params });
76 update(uid: string, args: Record<string, any>) {
77 return this.rgwDaemonService.request((params: HttpParams) => {
78 _.keys(args).forEach((key) => {
79 params = params.append(key, args[key]);
81 return this.http.put(`${this.url}/${uid}`, null, { params: params });
85 updateQuota(uid: string, args: Record<string, string>) {
86 return this.rgwDaemonService.request((params: HttpParams) => {
87 _.keys(args).forEach((key) => {
88 params = params.append(key, args[key]);
90 return this.http.put(`${this.url}/${uid}/quota`, null, { params: params });
95 return this.rgwDaemonService.request((params: HttpParams) => {
96 return this.http.delete(`${this.url}/${uid}`, { params: params });
100 createSubuser(uid: string, args: Record<string, string>) {
101 return this.rgwDaemonService.request((params: HttpParams) => {
102 _.keys(args).forEach((key) => {
103 params = params.append(key, args[key]);
105 return this.http.post(`${this.url}/${uid}/subuser`, null, { params: params });
109 deleteSubuser(uid: string, subuser: string) {
110 return this.rgwDaemonService.request((params: HttpParams) => {
111 return this.http.delete(`${this.url}/${uid}/subuser/${subuser}`, { params: params });
115 addCapability(uid: string, type: string, perm: string) {
116 return this.rgwDaemonService.request((params: HttpParams) => {
117 params = params.append('type', type);
118 params = params.append('perm', perm);
119 return this.http.post(`${this.url}/${uid}/capability`, null, { params: params });
123 deleteCapability(uid: string, type: string, perm: string) {
124 return this.rgwDaemonService.request((params: HttpParams) => {
125 params = params.append('type', type);
126 params = params.append('perm', perm);
127 return this.http.delete(`${this.url}/${uid}/capability`, { params: params });
131 addS3Key(uid: string, args: Record<string, string>) {
132 return this.rgwDaemonService.request((params: HttpParams) => {
133 params = params.append('key_type', 's3');
134 _.keys(args).forEach((key) => {
135 params = params.append(key, args[key]);
137 return this.http.post(`${this.url}/${uid}/key`, null, { params: params });
141 deleteS3Key(uid: string, accessKey: string) {
142 return this.rgwDaemonService.request((params: HttpParams) => {
143 params = params.append('key_type', 's3');
144 params = params.append('access_key', accessKey);
145 return this.http.delete(`${this.url}/${uid}/key`, { params: params });
150 * Check if the specified user ID exists.
151 * @param {string} uid The user ID to check.
152 * @return {Observable<boolean>}
154 exists(uid: string): Observable<boolean> {
155 return this.get(uid).pipe(
157 catchError((error: Event) => {
158 if (_.isFunction(error.preventDefault)) {
159 error.preventDefault();
161 return observableOf(false);
166 // Using @cdEncodeNot would be the preferred way here, but this
167 // causes an error: https://tracker.ceph.com/issues/37505
168 // Use decodeURIComponent as workaround.
169 // emailExists(@cdEncodeNot email: string): Observable<boolean> {
170 emailExists(email: string): Observable<boolean> {
171 email = decodeURIComponent(email);
172 return this.enumerateEmail().pipe(
173 mergeMap((resp: any[]) => {
174 const index = _.indexOf(resp, email);
175 return observableOf(-1 !== index);