Change request execution path
[platform/core/security/cynara.git] / src / service / logic / Logic.cpp
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        Logic.cpp
18  * @author      Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
19  * @version     1.0
20  * @brief       This file implements main class of logic layer in cynara service
21  */
22
23 #include <log/log.h>
24 #include <common.h>
25 #include <exceptions/PluginNotFoundException.h>
26
27 #include <main/Cynara.h>
28 #include <request/CheckRequest.h>
29 #include <response/CheckResponse.h>
30 #include <storage/Storage.h>
31
32 #include "Logic.h"
33
34 namespace Cynara {
35 Logic::Logic() {
36 }
37
38 Logic::~Logic() {
39 }
40
41 void Logic::execute(const RequestContext &context, CheckRequestPtr request) {
42     PolicyResult result(PredefinedPolicyType::DENY);
43     if (check(context, request->key(), result)) {
44         context.returnResponse(CheckResponse(result));
45     }
46 }
47
48 bool Logic::check(const RequestContext &context UNUSED, const PolicyKey &key,
49                   PolicyResult& result) {
50     result = Cynara::getStorage()->checkPolicy(key);
51
52     switch (result.policyType()) {
53         case PredefinedPolicyType::ALLOW :
54             LOGD("check of policy key <%s> returned ALLOW", key.toString().c_str());
55             return true;
56         case PredefinedPolicyType::DENY :
57             LOGD("check of policy key <%s> returned DENY", key.toString().c_str());
58             return true;
59     }
60     //todo pass question to proper plugin that:
61     //  1) returns false when answer has to be waited for (UI)
62     //  2) returns true if Response is to be generated
63     // In case 1) context should be saved in plugin in order to return answer when ready.
64
65     //in case no proper plugin is found
66     throw PluginNotFoundException(result);
67     return false;
68 }
69
70 } // namespace Cynara