]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rgw-user.service.ts
aff3d803ce8775875913f4d70a8c34bf04a8d214
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / api / rgw-user.service.ts
1 import { HttpClient, HttpParams } from '@angular/common/http';
2 import { Injectable } from '@angular/core';
3
4 import * as _ from 'lodash';
5 import { forkJoin as observableForkJoin, Observable, of as observableOf } from 'rxjs';
6 import { mergeMap } from 'rxjs/operators';
7
8 import { cdEncode } from '../decorators/cd-encode';
9 import { ApiModule } from './api.module';
10
11 @cdEncode
12 @Injectable({
13 providedIn: ApiModule
14 })
15 export class RgwUserService {
16 private url = 'api/rgw/user';
17
18 constructor(private http: HttpClient) {}
19
20 /**
21 * Get the list of users.
22 * @return {Observable<Object[]>}
23 */
24 list() {
25 return this.enumerate().pipe(
26 mergeMap((uids: string[]) => {
27 if (uids.length > 0) {
28 return observableForkJoin(
29 uids.map((uid: string) => {
30 return this.get(uid);
31 })
32 );
33 }
34 return observableOf([]);
35 })
36 );
37 }
38
39 /**
40 * Get the list of usernames.
41 * @return {Observable<string[]>}
42 */
43 enumerate() {
44 return this.http.get(this.url);
45 }
46
47 enumerateEmail() {
48 return this.http.get(`${this.url}/get_emails`);
49 }
50
51 get(uid: string) {
52 return this.http.get(`${this.url}/${uid}`);
53 }
54
55 getQuota(uid: string) {
56 return this.http.get(`${this.url}/${uid}/quota`);
57 }
58
59 create(args: Record<string, any>) {
60 let params = new HttpParams();
61 _.keys(args).forEach((key) => {
62 params = params.append(key, args[key]);
63 });
64 return this.http.post(this.url, null, { params: params });
65 }
66
67 update(uid: string, args: Record<string, any>) {
68 let params = new HttpParams();
69 _.keys(args).forEach((key) => {
70 params = params.append(key, args[key]);
71 });
72 return this.http.put(`${this.url}/${uid}`, null, { params: params });
73 }
74
75 updateQuota(uid: string, args: Record<string, string>) {
76 let params = new HttpParams();
77 _.keys(args).forEach((key) => {
78 params = params.append(key, args[key]);
79 });
80 return this.http.put(`${this.url}/${uid}/quota`, null, { params: params });
81 }
82
83 delete(uid: string) {
84 return this.http.delete(`${this.url}/${uid}`);
85 }
86
87 createSubuser(uid: string, args: Record<string, string>) {
88 let params = new HttpParams();
89 _.keys(args).forEach((key) => {
90 params = params.append(key, args[key]);
91 });
92 return this.http.post(`${this.url}/${uid}/subuser`, null, { params: params });
93 }
94
95 deleteSubuser(uid: string, subuser: string) {
96 return this.http.delete(`${this.url}/${uid}/subuser/${subuser}`);
97 }
98
99 addCapability(uid: string, type: string, perm: string) {
100 let params = new HttpParams();
101 params = params.append('type', type);
102 params = params.append('perm', perm);
103 return this.http.post(`${this.url}/${uid}/capability`, null, { params: params });
104 }
105
106 deleteCapability(uid: string, type: string, perm: string) {
107 let params = new HttpParams();
108 params = params.append('type', type);
109 params = params.append('perm', perm);
110 return this.http.delete(`${this.url}/${uid}/capability`, { params: params });
111 }
112
113 addS3Key(uid: string, args: Record<string, string>) {
114 let params = new HttpParams();
115 params = params.append('key_type', 's3');
116 _.keys(args).forEach((key) => {
117 params = params.append(key, args[key]);
118 });
119 return this.http.post(`${this.url}/${uid}/key`, null, { params: params });
120 }
121
122 deleteS3Key(uid: string, accessKey: string) {
123 let params = new HttpParams();
124 params = params.append('key_type', 's3');
125 params = params.append('access_key', accessKey);
126 return this.http.delete(`${this.url}/${uid}/key`, { params: params });
127 }
128
129 /**
130 * Check if the specified user ID exists.
131 * @param {string} uid The user ID to check.
132 * @return {Observable<boolean>}
133 */
134 exists(uid: string): Observable<boolean> {
135 return this.enumerate().pipe(
136 mergeMap((resp: string[]) => {
137 const index = _.indexOf(resp, uid);
138 return observableOf(-1 !== index);
139 })
140 );
141 }
142
143 // Using @cdEncodeNot would be the preferred way here, but this
144 // causes an error: https://tracker.ceph.com/issues/37505
145 // Use decodeURIComponent as workaround.
146 // emailExists(@cdEncodeNot email: string): Observable<boolean> {
147 emailExists(email: string): Observable<boolean> {
148 email = decodeURIComponent(email);
149 return this.enumerateEmail().pipe(
150 mergeMap((resp: any[]) => {
151 const index = _.indexOf(resp, email);
152 return observableOf(-1 !== index);
153 })
154 );
155 }
156 }