]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | import { HttpClient, HttpParams } from '@angular/common/http'; |
2 | import { Injectable } from '@angular/core'; | |
3 | ||
f67539c2 | 4 | import _ from 'lodash'; |
11fdf7f2 | 5 | import { forkJoin as observableForkJoin, Observable, of as observableOf } from 'rxjs'; |
cd265ab1 | 6 | import { catchError, mapTo, mergeMap } from 'rxjs/operators'; |
11fdf7f2 | 7 | |
f67539c2 TL |
8 | import { RgwDaemonService } from '~/app/shared/api/rgw-daemon.service'; |
9 | import { cdEncode } from '~/app/shared/decorators/cd-encode'; | |
11fdf7f2 TL |
10 | |
11 | @cdEncode | |
12 | @Injectable({ | |
f67539c2 | 13 | providedIn: 'root' |
11fdf7f2 TL |
14 | }) |
15 | export class RgwUserService { | |
16 | private url = 'api/rgw/user'; | |
17 | ||
f67539c2 | 18 | constructor(private http: HttpClient, private rgwDaemonService: RgwDaemonService) {} |
11fdf7f2 TL |
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() { | |
f67539c2 TL |
44 | return this.rgwDaemonService.request((params: HttpParams) => { |
45 | return this.http.get(this.url, { params: params }); | |
46 | }); | |
11fdf7f2 TL |
47 | } |
48 | ||
49 | enumerateEmail() { | |
f67539c2 TL |
50 | return this.rgwDaemonService.request((params: HttpParams) => { |
51 | return this.http.get(`${this.url}/get_emails`, { params: params }); | |
52 | }); | |
11fdf7f2 TL |
53 | } |
54 | ||
55 | get(uid: string) { | |
f67539c2 TL |
56 | return this.rgwDaemonService.request((params: HttpParams) => { |
57 | return this.http.get(`${this.url}/${uid}`, { params: params }); | |
58 | }); | |
11fdf7f2 TL |
59 | } |
60 | ||
61 | getQuota(uid: string) { | |
f67539c2 TL |
62 | return this.rgwDaemonService.request((params: HttpParams) => { |
63 | return this.http.get(`${this.url}/${uid}/quota`, { params: params }); | |
64 | }); | |
11fdf7f2 TL |
65 | } |
66 | ||
9f95a23c | 67 | create(args: Record<string, any>) { |
f67539c2 TL |
68 | return this.rgwDaemonService.request((params: HttpParams) => { |
69 | _.keys(args).forEach((key) => { | |
70 | params = params.append(key, args[key]); | |
71 | }); | |
72 | return this.http.post(this.url, null, { params: params }); | |
11fdf7f2 | 73 | }); |
11fdf7f2 TL |
74 | } |
75 | ||
9f95a23c | 76 | update(uid: string, args: Record<string, any>) { |
f67539c2 TL |
77 | return this.rgwDaemonService.request((params: HttpParams) => { |
78 | _.keys(args).forEach((key) => { | |
79 | params = params.append(key, args[key]); | |
80 | }); | |
81 | return this.http.put(`${this.url}/${uid}`, null, { params: params }); | |
11fdf7f2 | 82 | }); |
11fdf7f2 TL |
83 | } |
84 | ||
9f95a23c | 85 | updateQuota(uid: string, args: Record<string, string>) { |
f67539c2 TL |
86 | return this.rgwDaemonService.request((params: HttpParams) => { |
87 | _.keys(args).forEach((key) => { | |
88 | params = params.append(key, args[key]); | |
89 | }); | |
90 | return this.http.put(`${this.url}/${uid}/quota`, null, { params: params }); | |
11fdf7f2 | 91 | }); |
11fdf7f2 TL |
92 | } |
93 | ||
94 | delete(uid: string) { | |
f67539c2 TL |
95 | return this.rgwDaemonService.request((params: HttpParams) => { |
96 | return this.http.delete(`${this.url}/${uid}`, { params: params }); | |
97 | }); | |
11fdf7f2 TL |
98 | } |
99 | ||
9f95a23c | 100 | createSubuser(uid: string, args: Record<string, string>) { |
f67539c2 TL |
101 | return this.rgwDaemonService.request((params: HttpParams) => { |
102 | _.keys(args).forEach((key) => { | |
103 | params = params.append(key, args[key]); | |
104 | }); | |
105 | return this.http.post(`${this.url}/${uid}/subuser`, null, { params: params }); | |
11fdf7f2 | 106 | }); |
11fdf7f2 TL |
107 | } |
108 | ||
109 | deleteSubuser(uid: string, subuser: string) { | |
f67539c2 TL |
110 | return this.rgwDaemonService.request((params: HttpParams) => { |
111 | return this.http.delete(`${this.url}/${uid}/subuser/${subuser}`, { params: params }); | |
112 | }); | |
11fdf7f2 TL |
113 | } |
114 | ||
115 | addCapability(uid: string, type: string, perm: string) { | |
f67539c2 TL |
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 }); | |
120 | }); | |
11fdf7f2 TL |
121 | } |
122 | ||
123 | deleteCapability(uid: string, type: string, perm: string) { | |
f67539c2 TL |
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 }); | |
128 | }); | |
11fdf7f2 TL |
129 | } |
130 | ||
9f95a23c | 131 | addS3Key(uid: string, args: Record<string, string>) { |
f67539c2 TL |
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]); | |
136 | }); | |
137 | return this.http.post(`${this.url}/${uid}/key`, null, { params: params }); | |
11fdf7f2 | 138 | }); |
11fdf7f2 TL |
139 | } |
140 | ||
141 | deleteS3Key(uid: string, accessKey: string) { | |
f67539c2 TL |
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 }); | |
146 | }); | |
11fdf7f2 TL |
147 | } |
148 | ||
149 | /** | |
150 | * Check if the specified user ID exists. | |
151 | * @param {string} uid The user ID to check. | |
152 | * @return {Observable<boolean>} | |
153 | */ | |
154 | exists(uid: string): Observable<boolean> { | |
cd265ab1 TL |
155 | return this.get(uid).pipe( |
156 | mapTo(true), | |
157 | catchError((error: Event) => { | |
158 | if (_.isFunction(error.preventDefault)) { | |
159 | error.preventDefault(); | |
160 | } | |
161 | return observableOf(false); | |
11fdf7f2 TL |
162 | }) |
163 | ); | |
164 | } | |
165 | ||
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); | |
176 | }) | |
177 | ); | |
178 | } | |
179 | } |