PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/src/cyad/CynaraAdminPolicies.cpp

https://gitlab.com/github-cloud-corporation/cynara
C++ | 161 lines | 106 code | 33 blank | 22 comment | 27 complexity | 5637b766d7ed23587935cde9f0ce5d6d MD5 | raw file
  1. /*
  2. * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /**
  17. * @file src/cyad/CynaraAdminPolicies.cpp
  18. * @author Aleksander Zdyb <a.zdyb@samsung.com>
  19. * @version 1.0
  20. * @brief Collection of cynara_admin_policy structs
  21. */
  22. #include <algorithm>
  23. #include <cstring>
  24. #include <functional>
  25. #include <memory>
  26. #include <new>
  27. #include <stdexcept>
  28. #include <string>
  29. #include <cynara-admin-types.h>
  30. #include "CynaraAdminPolicies.h"
  31. namespace Cynara {
  32. CynaraAdminPolicies::CynaraAdminPolicies() : m_sealed(false) {}
  33. CynaraAdminPolicies::~CynaraAdminPolicies() {
  34. auto freePolicy = [] (cynara_admin_policy *admin_policy) {
  35. if (admin_policy == nullptr)
  36. return;
  37. free(admin_policy->bucket);
  38. free(admin_policy->client);
  39. free(admin_policy->user);
  40. free(admin_policy->privilege);
  41. free(admin_policy->result_extra);
  42. delete admin_policy;
  43. };
  44. std::for_each(m_policies.begin(), m_policies.end(), freePolicy);
  45. }
  46. void CynaraAdminPolicies::add(const PolicyBucketId &bucketId, const PolicyResult &policyResult,
  47. const PolicyKey &policyKey) {
  48. if (sealed()) {
  49. throw std::logic_error("Collection is sealed");
  50. }
  51. // TODO: Optimize -- try not to malloc every item
  52. cynara_admin_policy *policy = new cynara_admin_policy();
  53. auto duplicateString = [] (const std::string &str) -> char * {
  54. auto ret = strdup(str.c_str());
  55. if (ret == nullptr)
  56. throw std::bad_alloc();
  57. return ret;
  58. };
  59. policy->bucket = duplicateString(bucketId);
  60. policy->client = duplicateString(policyKey.client().toString());
  61. policy->user = duplicateString(policyKey.user().toString());
  62. policy->privilege = duplicateString(policyKey.privilege().toString());
  63. policy->result = policyResult.policyType();
  64. if (policyResult.metadata().empty())
  65. policy->result_extra = nullptr;
  66. else
  67. policy->result_extra = duplicateString(policyResult.metadata());
  68. m_policies.push_back(policy);
  69. }
  70. void CynaraAdminPolicies::seal(void) {
  71. m_policies.push_back(nullptr);
  72. m_sealed = true;
  73. }
  74. cynara_admin_policy* const *CynaraAdminPolicies::data(void) const {
  75. if (sealed() == false) {
  76. throw std::logic_error("Collection is not sealed");
  77. }
  78. return m_policies.data();
  79. }
  80. cynara_admin_policy **CynaraAdminPolicies::duplicate(void) const {
  81. if (sealed() == false) {
  82. throw std::logic_error("Collection is not sealed");
  83. }
  84. auto freePolicies = [this] (cynara_admin_policy **policies) {
  85. for (auto i = 0u; i < m_policies.size() - 1; ++i) {
  86. auto p = policies[i];
  87. ::free(p->bucket);
  88. ::free(p->client);
  89. ::free(p->user);
  90. ::free(p->privilege);
  91. ::free(p->result_extra);
  92. ::free(p);
  93. }
  94. free(policies);
  95. };
  96. auto dup = static_cast<cynara_admin_policy **>(calloc(m_policies.size(),
  97. sizeof(cynara_admin_policy *)));
  98. if (dup == nullptr)
  99. throw std::bad_alloc();
  100. std::unique_ptr<cynara_admin_policy *, decltype(freePolicies)> dupPtr(dup, freePolicies);
  101. for (auto i = 0u; i < m_policies.size() - 1; ++i) {
  102. dup[i] = static_cast<cynara_admin_policy *>(calloc(1, sizeof(cynara_admin_policy)));
  103. if (dup[i] == nullptr)
  104. throw std::bad_alloc();
  105. dup[i]->bucket = strdup(m_policies.at(i)->bucket);
  106. if (dup[i]->bucket == nullptr)
  107. throw std::bad_alloc();
  108. dup[i]->client = strdup(m_policies.at(i)->client);
  109. if (dup[i]->client == nullptr)
  110. throw std::bad_alloc();
  111. dup[i]->user = strdup(m_policies.at(i)->user);
  112. if (dup[i]->user == nullptr)
  113. throw std::bad_alloc();
  114. dup[i]->privilege = strdup(m_policies.at(i)->privilege);
  115. if (dup[i]->privilege == nullptr)
  116. throw std::bad_alloc();
  117. dup[i]->result = m_policies.at(i)->result;
  118. if (m_policies.at(i)->result_extra) {
  119. dup[i]->result_extra = strdup(m_policies.at(i)->result_extra);
  120. if (dup[i]->result_extra == nullptr)
  121. throw std::bad_alloc();
  122. } else
  123. dup[i]->result_extra = nullptr;
  124. }
  125. dup[m_policies.size() - 1] = nullptr;
  126. return dupPtr.release();
  127. }
  128. } /* namespace Cynara */