From 7a7c376f6fea69ea58329e181abdc7ebf09f8402 Mon Sep 17 00:00:00 2001 From: Sangwan Kwon Date: Thu, 9 Jan 2020 15:19:46 +0900 Subject: [PATCH] Add admin api to check for activation API::Admin::IsActivated() in vist-policy Signed-off-by: Sangwan Kwon --- src/vist/policy/api.cpp | 7 ++++++- src/vist/policy/api.hpp | 3 ++- src/vist/policy/policy-manager.cpp | 7 ++++++- src/vist/policy/policy-manager.hpp | 3 ++- src/vist/policy/policy-storage.cpp | 11 ++++++++++- src/vist/policy/policy-storage.hpp | 4 +++- src/vist/policy/tests/core.cpp | 23 +++++++++++++++++++++++ 7 files changed, 52 insertions(+), 6 deletions(-) diff --git a/src/vist/policy/api.cpp b/src/vist/policy/api.cpp index 6c0386f..947aafb 100644 --- a/src/vist/policy/api.cpp +++ b/src/vist/policy/api.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2019-present Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -52,6 +52,11 @@ void API::Admin::Activate(const std::string& admin, bool state) PolicyManager::Instance().activate(admin, state); } +bool API::Admin::IsActivated() +{ + return PolicyManager::Instance().isActivated(); +} + std::unordered_map API::Admin::GetAll() { return PolicyManager::Instance().getAdmins(); diff --git a/src/vist/policy/api.hpp b/src/vist/policy/api.hpp index dabbcd7..2998c1f 100644 --- a/src/vist/policy/api.hpp +++ b/src/vist/policy/api.hpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2019-present Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -35,6 +35,7 @@ struct API { static void Disenroll(const std::string& admin); static void Activate(const std::string& admin, bool state = true); + static bool IsActivated(); static std::unordered_map GetAll(); }; diff --git a/src/vist/policy/policy-manager.cpp b/src/vist/policy/policy-manager.cpp index f03b827..2ab3ca3 100644 --- a/src/vist/policy/policy-manager.cpp +++ b/src/vist/policy/policy-manager.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2019-present Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -108,6 +108,11 @@ void PolicyManager::activate(const std::string& admin, bool state) this->storage.activate(admin, state); } +bool PolicyManager::isActivated() +{ + return this->storage.isActivated(); +} + void PolicyManager::set(const std::string& policy, const PolicyValue& value, const std::string& admin) diff --git a/src/vist/policy/policy-manager.hpp b/src/vist/policy/policy-manager.hpp index 67d4994..3eda4d7 100644 --- a/src/vist/policy/policy-manager.hpp +++ b/src/vist/policy/policy-manager.hpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2019-present Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -49,6 +49,7 @@ public: void disenroll(const std::string& admin); void activate(const std::string& admin, bool state); + bool isActivated(); void set(const std::string& policy, const PolicyValue& value, diff --git a/src/vist/policy/policy-storage.cpp b/src/vist/policy/policy-storage.cpp index d570f51..ba1ba20 100644 --- a/src/vist/policy/policy-storage.cpp +++ b/src/vist/policy/policy-storage.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2019-present Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -228,6 +228,15 @@ bool PolicyStorage::isActivated(const std::string& admin) return this->admins[admin].activated; } +bool PolicyStorage::isActivated() +{ + for (const auto& admin : this->admins) + if (admin.second.activated) + return true; + + return false; +} + void PolicyStorage::update(const std::string& admin, const std::string& policy, const PolicyValue& value) diff --git a/src/vist/policy/policy-storage.hpp b/src/vist/policy/policy-storage.hpp index e86ecae..8cff289 100644 --- a/src/vist/policy/policy-storage.hpp +++ b/src/vist/policy/policy-storage.hpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2019-present Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -50,6 +50,8 @@ public: void disenroll(const std::string& admin); void activate(const std::string& admin, bool state = true); bool isActivated(const std::string& admin); + /// Check that none of admins are activated. + bool isActivated(); void define(const std::string& policy, const PolicyValue& ivalue); void update(const std::string& admin, diff --git a/src/vist/policy/tests/core.cpp b/src/vist/policy/tests/core.cpp index e8a2c37..57e9fee 100644 --- a/src/vist/policy/tests/core.cpp +++ b/src/vist/policy/tests/core.cpp @@ -109,5 +109,28 @@ TEST(PolicyCoreTests, admin) { manager.disenroll("testAdmin"); } +TEST(PolicyCoreTests, is_activated) { + auto& manager = PolicyManager::Instance(); + manager.enroll("testAdmin1"); + manager.enroll("testAdmin2"); + + EXPECT_FALSE(manager.isActivated()); + + manager.activate("testAdmin1", true); + EXPECT_TRUE(manager.isActivated()); + + manager.activate("testAdmin2", true); + EXPECT_TRUE(manager.isActivated()); + + manager.activate("testAdmin1", false); + EXPECT_TRUE(manager.isActivated()); + + manager.activate("testAdmin2", false); + EXPECT_FALSE(manager.isActivated()); + + manager.disenroll("testAdmin1"); + manager.disenroll("testAdmin2"); +} + } // namespace policy } // namespace vist -- 2.7.4