Line | Branch | Exec | Source |
---|---|---|---|
1 | /* GATE PROJECT LICENSE: | ||
2 | +----------------------------------------------------------------------------+ | ||
3 | | Copyright(c) 2018-2025, Stefan Meislinger | | ||
4 | | All rights reserved. | | ||
5 | | | | ||
6 | | Redistribution and use in source and binary forms, with or without | | ||
7 | | modification, are permitted provided that the following conditions are met:| | ||
8 | | | | ||
9 | | 1. Redistributions of source code must retain the above copyright notice, | | ||
10 | | this list of conditions and the following disclaimer. | | ||
11 | | 2. Redistributions in binary form must reproduce the above copyright | | ||
12 | | notice, this list of conditions and the following disclaimer in the | | ||
13 | | documentation and/or other materials provided with the distribution. | | ||
14 | | | | ||
15 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"| | ||
16 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | | ||
17 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | ||
18 | | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | | ||
19 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | ||
20 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | ||
21 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | ||
22 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | ||
23 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | ||
24 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | | ||
25 | | THE POSSIBILITY OF SUCH DAMAGE. | | ||
26 | +----------------------------------------------------------------------------+ | ||
27 | */ | ||
28 | |||
29 | #include "gate/system/accounts.hpp" | ||
30 | #include "gate/memalloc.hpp" | ||
31 | #include "gate/exceptions.hpp" | ||
32 | #include "gate/wrappers.hpp" | ||
33 | |||
34 | namespace gate | ||
35 | { | ||
36 | namespace sys | ||
37 | { | ||
38 | |||
39 | ✗ | Accounts::User::User() noexcept | |
40 | { | ||
41 | ✗ | Mem::clear(this->impl); | |
42 | ✗ | this->impl.short_id = GATE_ACCOUNT_INVALID_SHORT_ID; | |
43 | ✗ | } | |
44 | |||
45 | 21 | Accounts::User::User(gate_account_user_t const& user) | |
46 | { | ||
47 | 21 | Mem::copy(this->impl, user); | |
48 | 21 | } | |
49 | 38 | Accounts::User::User(User const& src) | |
50 | { | ||
51 | 38 | Mem::copy(this->impl, src.impl); | |
52 | 38 | } | |
53 | ✗ | Accounts::User& Accounts::User::operator=(User const& src) | |
54 | { | ||
55 | ✗ | if (this != &src) | |
56 | { | ||
57 | ✗ | this->id_shadow = String(); | |
58 | ✗ | this->name_shadow = String(); | |
59 | ✗ | this->info_shadow = String(); | |
60 | ✗ | this->descr_shadow = String(); | |
61 | ✗ | Mem::copy(this->impl, src.impl); | |
62 | } | ||
63 | ✗ | return *this; | |
64 | } | ||
65 | |||
66 | 59 | Accounts::User::~User() noexcept | |
67 | { | ||
68 | 59 | Mem::clear(this->impl); | |
69 | 59 | } | |
70 | |||
71 | 7 | static String& shadow_update(char const* src, String& dst) | |
72 | { | ||
73 |
5/6✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 5 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 5 times.
✓ Branch 7 taken 2 times.
|
7 | if (dst.empty() && !gate_str_is_empty(src)) |
74 | { | ||
75 | 5 | dst = String(src); | |
76 | } | ||
77 | 7 | return dst; | |
78 | } | ||
79 | |||
80 | 1 | String Accounts::User::Id() | |
81 | { | ||
82 | 1 | return shadow_update(this->impl.id, this->id_shadow); | |
83 | } | ||
84 | 1 | uint64_t Accounts::User::ShortId() const | |
85 | { | ||
86 | 1 | return this->impl.short_id; | |
87 | } | ||
88 | 1 | String Accounts::User::Name() | |
89 | { | ||
90 | 1 | return shadow_update(this->impl.name, this->name_shadow); | |
91 | } | ||
92 | 1 | String Accounts::User::Info() | |
93 | { | ||
94 | 1 | return shadow_update(this->impl.info, this->info_shadow); | |
95 | } | ||
96 | 1 | String Accounts::User::Description() | |
97 | { | ||
98 | 1 | return shadow_update(this->impl.description, this->descr_shadow); | |
99 | } | ||
100 | 1 | uint32_t Accounts::User::Flags() const | |
101 | { | ||
102 | 1 | return this->impl.flags; | |
103 | } | ||
104 | 1 | gate_account_user_t const* Accounts::User::c_impl() const | |
105 | { | ||
106 | 1 | return &this->impl; | |
107 | } | ||
108 | |||
109 | ✗ | Accounts::Group::Group() noexcept | |
110 | { | ||
111 | ✗ | Mem::clear(this->impl); | |
112 | ✗ | } | |
113 | 41 | Accounts::Group::Group(gate_account_group_t const& group) | |
114 | { | ||
115 | 41 | Mem::copy(this->impl, group); | |
116 | 41 | } | |
117 | 84 | Accounts::Group::Group(Group const& src) | |
118 | { | ||
119 | 84 | Mem::copy(this->impl, src.impl); | |
120 | 84 | } | |
121 | ✗ | Accounts::Group& Accounts::Group::operator=(Group const& src) | |
122 | { | ||
123 | ✗ | if (this != &src) | |
124 | { | ||
125 | ✗ | this->id_shadow = String(); | |
126 | ✗ | this->name_shadow = String(); | |
127 | ✗ | this->descr_shadow = String(); | |
128 | ✗ | Mem::copy(this->impl, src.impl); | |
129 | } | ||
130 | ✗ | return *this; | |
131 | } | ||
132 | 125 | Accounts::Group::~Group() noexcept | |
133 | { | ||
134 | 125 | Mem::clear(this->impl); | |
135 | 125 | } | |
136 | |||
137 | 1 | String Accounts::Group::Id() | |
138 | { | ||
139 | 1 | return shadow_update(this->impl.id, this->id_shadow); | |
140 | } | ||
141 | 1 | uint64_t Accounts::Group::ShortId() const | |
142 | { | ||
143 | 1 | return this->impl.short_id; | |
144 | } | ||
145 | 1 | String Accounts::Group::Name() | |
146 | { | ||
147 | 1 | return shadow_update(this->impl.name, this->name_shadow); | |
148 | } | ||
149 | 1 | String Accounts::Group::Description() | |
150 | { | ||
151 | 1 | return shadow_update(this->impl.description, this->descr_shadow); | |
152 | } | ||
153 | 1 | uint32_t Accounts::Group::Flags() const | |
154 | { | ||
155 | 1 | return this->impl.flags; | |
156 | } | ||
157 | ✗ | gate_account_group_t const* Accounts::Group::c_impl() const | |
158 | { | ||
159 | ✗ | return &this->impl; | |
160 | } | ||
161 | |||
162 | |||
163 | |||
164 | |||
165 | struct GATE_API_LOCAL enumUsers_callback_dispatcher_param | ||
166 | { | ||
167 | Accounts::EnumUserCallback const* callback; | ||
168 | void* original_param; | ||
169 | Optional<Exception> error; | ||
170 | |||
171 | ✗ | enumUsers_callback_dispatcher_param() | |
172 | ✗ | : callback(NULL), original_param(NULL) | |
173 | { | ||
174 | ✗ | } | |
175 | }; | ||
176 | |||
177 | ✗ | static gate_bool_t Accounts_enumUsers_callback_dispatcher(gate_account_user_t const* account_user, void* param) | |
178 | { | ||
179 | ✗ | if (account_user && param) | |
180 | { | ||
181 | ✗ | enumUsers_callback_dispatcher_param* disp_param = static_cast<enumUsers_callback_dispatcher_param*>(param); | |
182 | try | ||
183 | { | ||
184 | ✗ | Accounts::User user(*account_user); | |
185 | ✗ | disp_param->callback->invoke(user, disp_param->original_param); | |
186 | } | ||
187 | ✗ | catch (Exception const& xcpt) | |
188 | { | ||
189 | ✗ | disp_param->error.create(xcpt); | |
190 | ✗ | return false; | |
191 | } | ||
192 | ✗ | catch (...) | |
193 | { | ||
194 | ✗ | disp_param->error.create(Exception(results::UnknownException, "Unsupported exception type", "Accounts_enumUsers_callback_dispatcher")); | |
195 | ✗ | return false; | |
196 | } | ||
197 | } | ||
198 | ✗ | return true; | |
199 | } | ||
200 | |||
201 | struct GATE_API_LOCAL enumGroups_callback_dispatcher_param | ||
202 | { | ||
203 | Accounts::EnumGroupCallback const* callback; | ||
204 | void* original_param; | ||
205 | Optional<Exception> error; | ||
206 | |||
207 | ✗ | enumGroups_callback_dispatcher_param() | |
208 | ✗ | : callback(NULL), original_param(NULL) | |
209 | { | ||
210 | ✗ | } | |
211 | }; | ||
212 | |||
213 | ✗ | static gate_bool_t Accounts_enumGroups_callback_dispatcher(gate_account_group_t const* account_group, void* param) | |
214 | { | ||
215 | ✗ | if (account_group && param) | |
216 | { | ||
217 | ✗ | enumGroups_callback_dispatcher_param* disp_param = static_cast<enumGroups_callback_dispatcher_param*>(param); | |
218 | try | ||
219 | { | ||
220 | ✗ | Accounts::Group group(*account_group); | |
221 | ✗ | disp_param->callback->invoke(group, disp_param->original_param); | |
222 | } | ||
223 | ✗ | catch (Exception const& xcpt) | |
224 | { | ||
225 | ✗ | disp_param->error.create(xcpt); | |
226 | ✗ | return false; | |
227 | } | ||
228 | ✗ | catch (...) | |
229 | { | ||
230 | ✗ | disp_param->error.create(Exception(results::UnknownException, "Unsupported exception type", "Accounts_enumGroups_callback_dispatcher")); | |
231 | ✗ | return false; | |
232 | } | ||
233 | } | ||
234 | ✗ | return true; | |
235 | } | ||
236 | |||
237 | 21 | static gate_bool_t Accounts_listUsers_callback(gate_account_user_t const* account_user, void* param) | |
238 | { | ||
239 |
2/4✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
|
21 | if (account_user && param) |
240 | { | ||
241 | 21 | ArrayList<Accounts::User>* list = static_cast<ArrayList<Accounts::User>*>(param); | |
242 | try | ||
243 | { | ||
244 | 42 | Accounts::User user(*account_user); | |
245 |
1/2✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
|
21 | list->add(user); |
246 | } | ||
247 | ✗ | catch (...) | |
248 | { | ||
249 | } | ||
250 | } | ||
251 | 21 | return true; | |
252 | } | ||
253 | |||
254 | 41 | static gate_bool_t Accounts_listGroups_callback(gate_account_group_t const* account_group, void* param) | |
255 | { | ||
256 |
2/4✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 41 times.
✗ Branch 3 not taken.
|
41 | if (account_group && param) |
257 | { | ||
258 | 41 | ArrayList<Accounts::Group>* list = static_cast<ArrayList<Accounts::Group>*>(param); | |
259 | try | ||
260 | { | ||
261 | 82 | Accounts::Group user(*account_group); | |
262 |
1/2✓ Branch 1 taken 41 times.
✗ Branch 2 not taken.
|
41 | list->add(user); |
263 | } | ||
264 | ✗ | catch (...) | |
265 | { | ||
266 | } | ||
267 | } | ||
268 | 41 | return true; | |
269 | } | ||
270 | ✗ | void Accounts::enumUsers(EnumUserCallback const& callback, void* param) | |
271 | { | ||
272 | ✗ | enumUsers_callback_dispatcher_param disp_param; | |
273 | ✗ | disp_param.callback = &callback; | |
274 | ✗ | disp_param.original_param = param; | |
275 | ✗ | result_t result = gate_account_enum_users(&Accounts_enumUsers_callback_dispatcher, &disp_param); | |
276 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
277 | |||
278 | ✗ | if (!disp_param.error.empty()) | |
279 | { | ||
280 | ✗ | if (disp_param.error->getResult() != results::Canceled) | |
281 | { | ||
282 | ✗ | Exception xcpt(*disp_param.error); | |
283 | ✗ | throw xcpt; | |
284 | } | ||
285 | } | ||
286 | ✗ | } | |
287 | |||
288 | 1 | Array<Accounts::User> Accounts::listUsers() | |
289 | { | ||
290 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
2 | ArrayList<User> list(16); |
291 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | result_t result = gate_account_enum_users(&Accounts_listUsers_callback, &list); |
292 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1 | GATEXX_CHECK_EXCEPTION(result); |
293 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
2 | return list.toArray(); |
294 | } | ||
295 | ✗ | Accounts::User Accounts::getUser(String const& id) | |
296 | { | ||
297 | gate_account_user_t user; | ||
298 | ✗ | result_t result = gate_account_get_user(id.c_impl(), &user); | |
299 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
300 | ✗ | return User(user); | |
301 | } | ||
302 | ✗ | Accounts::User Accounts::resolveUser(String const& name) | |
303 | { | ||
304 | gate_account_user_t user; | ||
305 | ✗ | result_t result = gate_account_resolve_user(name.c_impl(), &user); | |
306 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
307 | ✗ | return User(user); | |
308 | } | ||
309 | ✗ | void Accounts::createUser(String const& name, String const& password) | |
310 | { | ||
311 | ✗ | result_t result = gate_account_create_user(name.c_impl(), password.empty() ? NULL : password.c_impl()); | |
312 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
313 | ✗ | } | |
314 | ✗ | void Accounts::deleteUser(User const& user) | |
315 | { | ||
316 | ✗ | result_t result = gate_account_delete_user(user.c_impl()); | |
317 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
318 | ✗ | } | |
319 | ✗ | void Accounts::enumUserMemberships(User const& user, EnumGroupCallback const& callback, void* param) | |
320 | { | ||
321 | ✗ | enumGroups_callback_dispatcher_param disp_param; | |
322 | ✗ | disp_param.callback = &callback; | |
323 | ✗ | disp_param.original_param = param; | |
324 | ✗ | result_t result = gate_account_enum_user_memberships(user.c_impl(), &Accounts_enumGroups_callback_dispatcher, &disp_param); | |
325 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
326 | |||
327 | ✗ | if (!disp_param.error.empty()) | |
328 | { | ||
329 | ✗ | if (disp_param.error->getResult() != results::Canceled) | |
330 | { | ||
331 | ✗ | Exception xcpt(*disp_param.error); | |
332 | ✗ | throw xcpt; | |
333 | } | ||
334 | } | ||
335 | ✗ | } | |
336 | ✗ | Array<Accounts::Group> Accounts::listUserMemberships(User const& user) | |
337 | { | ||
338 | ✗ | ArrayList<Group> list(16); | |
339 | ✗ | result_t result = gate_account_enum_user_memberships(user.c_impl(), &Accounts_listGroups_callback, &list); | |
340 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
341 | ✗ | return list.toArray(); | |
342 | } | ||
343 | ✗ | Property Accounts::getUserProperties(User const& user) | |
344 | { | ||
345 | ✗ | gate_property_t prop = GATE_INIT_EMPTY; | |
346 | ✗ | result_t result = gate_account_get_user_properties(user.c_impl(), &prop); | |
347 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
348 | try | ||
349 | { | ||
350 | ✗ | Property propObj(prop); | |
351 | ✗ | gate_property_destroy(&prop); | |
352 | ✗ | return propObj; | |
353 | } | ||
354 | ✗ | catch (...) | |
355 | { | ||
356 | ✗ | gate_property_destroy(&prop); | |
357 | ✗ | throw; | |
358 | } | ||
359 | } | ||
360 | ✗ | void Accounts::setUserProperty(User const& user, String const& propName, Property const& propValue) | |
361 | { | ||
362 | ✗ | result_t result = gate_account_set_user_property(user.c_impl(), propName.c_impl(), propValue.c_impl()); | |
363 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
364 | ✗ | } | |
365 | |||
366 | ✗ | void Accounts::enumGroups(EnumGroupCallback const& callback, void* param) | |
367 | { | ||
368 | ✗ | enumGroups_callback_dispatcher_param disp_param; | |
369 | ✗ | disp_param.callback = &callback; | |
370 | ✗ | disp_param.original_param = param; | |
371 | ✗ | result_t result = gate_account_enum_groups(&Accounts_enumGroups_callback_dispatcher, &disp_param); | |
372 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
373 | |||
374 | ✗ | if (!disp_param.error.empty()) | |
375 | { | ||
376 | ✗ | if (disp_param.error->getResult() != results::Canceled) | |
377 | { | ||
378 | ✗ | Exception xcpt(*disp_param.error); | |
379 | ✗ | throw xcpt; | |
380 | } | ||
381 | } | ||
382 | |||
383 | ✗ | } | |
384 | 1 | Array<Accounts::Group> Accounts::listGroups() | |
385 | { | ||
386 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
2 | ArrayList<Group> list(16); |
387 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | result_t result = gate_account_enum_groups(&Accounts_listGroups_callback, &list); |
388 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1 | GATEXX_CHECK_EXCEPTION(result); |
389 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
2 | return list.toArray(); |
390 | } | ||
391 | ✗ | Accounts::Group Accounts::getGroup(String const& id) | |
392 | { | ||
393 | gate_account_group_t group; | ||
394 | ✗ | result_t result = gate_account_get_group(id.c_impl(), &group); | |
395 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
396 | ✗ | return Group(group); | |
397 | } | ||
398 | ✗ | Accounts::Group Accounts::resolveGroup(String const& name) | |
399 | { | ||
400 | gate_account_group_t group; | ||
401 | ✗ | result_t result = gate_account_resolve_group(name.c_impl(), &group); | |
402 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
403 | ✗ | return Group(group); | |
404 | } | ||
405 | ✗ | void Accounts::createGroup(String const& name) | |
406 | { | ||
407 | ✗ | result_t result = gate_account_create_group(name.c_impl()); | |
408 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
409 | ✗ | } | |
410 | ✗ | void Accounts::deleteGroup(Group const& group) | |
411 | { | ||
412 | ✗ | result_t result = gate_account_delete_group(group.c_impl()); | |
413 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
414 | ✗ | } | |
415 | ✗ | void Accounts::enumGroupMembers(Group const& group, EnumUserCallback const& callback, void* param) | |
416 | { | ||
417 | ✗ | enumUsers_callback_dispatcher_param disp_param; | |
418 | ✗ | disp_param.callback = &callback; | |
419 | ✗ | disp_param.original_param = param; | |
420 | ✗ | result_t result = gate_account_enum_group_members(group.c_impl(), &Accounts_enumUsers_callback_dispatcher, param); | |
421 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
422 | |||
423 | ✗ | if (!disp_param.error.empty()) | |
424 | { | ||
425 | ✗ | if (disp_param.error->getResult() != results::Canceled) | |
426 | { | ||
427 | ✗ | Exception xcpt(*disp_param.error); | |
428 | ✗ | throw xcpt; | |
429 | } | ||
430 | } | ||
431 | ✗ | } | |
432 | ✗ | Array<Accounts::User> Accounts::listGroupMembers(Group const& group) | |
433 | { | ||
434 | ✗ | ArrayList<User> list(16); | |
435 | ✗ | result_t result = gate_account_enum_group_members(group.c_impl(), &Accounts_listUsers_callback, &list); | |
436 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
437 | ✗ | return list.toArray(); | |
438 | } | ||
439 | |||
440 | |||
441 | ✗ | void Accounts::addUserToGroup(User const& user, Group const& group) | |
442 | { | ||
443 | ✗ | result_t result = gate_account_add_user_to_group(user.c_impl(), group.c_impl()); | |
444 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
445 | ✗ | } | |
446 | ✗ | void Accounts::removeUserFromGroup(User const& user, Group const& group) | |
447 | { | ||
448 | ✗ | result_t result = gate_account_remove_user_from_group(user.c_impl(), group.c_impl()); | |
449 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
450 | ✗ | } | |
451 | |||
452 | } // end of namespace sys | ||
453 | } // end of namespace gate | ||
454 |