Add admin api to check for activation
authorSangwan Kwon <sangwan.kwon@samsung.com>
Thu, 9 Jan 2020 06:19:46 +0000 (15:19 +0900)
committer권상완/Security 2Lab(SR)/Engineer/삼성전자 <sangwan.kwon@samsung.com>
Tue, 14 Jan 2020 01:57:30 +0000 (10:57 +0900)
API::Admin::IsActivated() in vist-policy

Signed-off-by: Sangwan Kwon <sangwan.kwon@samsung.com>
src/vist/policy/api.cpp
src/vist/policy/api.hpp
src/vist/policy/policy-manager.cpp
src/vist/policy/policy-manager.hpp
src/vist/policy/policy-storage.cpp
src/vist/policy/policy-storage.hpp
src/vist/policy/tests/core.cpp

index 6c0386f..947aafb 100644 (file)
@@ -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<std::string, int> API::Admin::GetAll()
 {
        return PolicyManager::Instance().getAdmins();
index dabbcd7..2998c1f 100644 (file)
@@ -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<std::string, int> GetAll();
        };
index f03b827..2ab3ca3 100644 (file)
@@ -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)
index 67d4994..3eda4d7 100644 (file)
@@ -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,
index d570f51..ba1ba20 100644 (file)
@@ -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)
index e86ecae..8cff289 100644 (file)
@@ -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,
index e8a2c37..57e9fee 100644 (file)
@@ -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