|
| 1 | +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
| 2 | +// vim: ts=8 sw=2 smarttab ft=cpp |
| 3 | + |
| 4 | +#include "rgw_process_env.h" |
| 5 | +#include "rgw_rest_iam_account.h" |
| 6 | + |
| 7 | +int RGWGetAccountSummary::verify_permission(optional_yield y) |
| 8 | +{ |
| 9 | + std::string account_id; |
| 10 | + if (const auto& account = s->auth.identity->get_account(); account) { |
| 11 | + account_id = account->id; |
| 12 | + } else { |
| 13 | + return -ERR_METHOD_NOT_ALLOWED; |
| 14 | + } |
| 15 | + const rgw::ARN arn{"", "root", account_id, true}; |
| 16 | + if (verify_user_permission(this, s, arn, rgw::IAM::iamGetAccountSummary)) { |
| 17 | + return 0; |
| 18 | + } |
| 19 | + return -EACCES; |
| 20 | +} |
| 21 | + |
| 22 | +void RGWGetAccountSummary::add_entry(const std::string& type, int64_t value) |
| 23 | +{ |
| 24 | + s->formatter->open_object_section("entry"); |
| 25 | + s->formatter->dump_string("key", type); |
| 26 | + s->formatter->dump_int("value", value); |
| 27 | + s->formatter->close_section(); |
| 28 | +} |
| 29 | + |
| 30 | +void RGWGetAccountSummary::execute(optional_yield y) |
| 31 | +{ |
| 32 | + const auto& info = s->user->get_info(); |
| 33 | + const auto& account = s->auth.identity->get_account(); |
| 34 | + uint32_t users_count = 0; |
| 35 | + uint32_t groups_count = 0; |
| 36 | + |
| 37 | + if (account->max_users >= 0) { |
| 38 | + op_ret = driver->count_account_users(this, y, info.account_id, users_count); |
| 39 | + if (op_ret < 0) { |
| 40 | + ldpp_dout(this, 4) << "failed to count users for iam account " |
| 41 | + << info.account_id << ": " << op_ret << dendl; |
| 42 | + return; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + if (account->max_groups >= 0) { |
| 47 | + op_ret = driver->count_account_groups(this, y, info.account_id, groups_count); |
| 48 | + if (op_ret < 0) { |
| 49 | + ldpp_dout(this, 4) << "failed to count groups for iam account " |
| 50 | + << info.account_id << ": " << op_ret << dendl; |
| 51 | + return; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + s->formatter->open_object_section("GetAccountSummaryResponse"); |
| 56 | + s->formatter->open_object_section("ResponseMetadata"); |
| 57 | + s->formatter->dump_string("RequestId", s->trans_id); |
| 58 | + s->formatter->close_section(); |
| 59 | + s->formatter->open_object_section("GetAccountSummaryResult"); |
| 60 | + s->formatter->open_object_section("SummaryMap"); |
| 61 | + add_entry("Users", users_count); |
| 62 | + add_entry("Groups", groups_count); |
| 63 | + add_entry("UsersQuota", account->max_users); |
| 64 | + add_entry("GroupsQuota", account->max_groups); |
| 65 | + add_entry("AccessKeysPerUserQuota", account->max_access_keys); |
| 66 | + s->formatter->close_section(); |
| 67 | + s->formatter->close_section(); |
| 68 | + s->formatter->close_section(); |
| 69 | +} |
| 70 | + |
0 commit comments