2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file AceDAOReadOnlyReadOnly.cpp
20 * @author Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
21 * @author Grzegorz Krawczyk (g.krawczyk@samsung.com)
26 #include <dpl/ace-dao-ro/AceDAOReadOnly.h>
27 #include <dpl/ace-dao-ro/AceDAOUtilities.h>
28 #include <dpl/ace-dao-ro/AceDAOConversions.h>
29 #include <dpl/ace-dao-ro/AceDatabase.h>
31 #include <dpl/foreach.h>
33 using namespace DPL::DB::ORM;
34 using namespace DPL::DB::ORM::ace;
35 using namespace AceDB::AceDaoUtilities;
36 using namespace AceDB::AceDaoConversions;
40 static const int DB_ALLOW_ALWAYS = 0;
41 static const int DB_ALLOW_FOR_SESSION = 1;
42 static const int DB_ALLOW_THIS_TIME = 2;
43 static const int DB_DENY_ALWAYS = 3;
44 static const int DB_DENY_FOR_SESSION = 4;
45 static const int DB_DENY_THIS_TIME = 5;
47 int AceDAOReadOnly::promptDecisionToInt(PromptDecision decision)
49 if (PromptDecision::ALLOW_ALWAYS == decision) {
50 return DB_ALLOW_ALWAYS;
51 } else if (PromptDecision::DENY_ALWAYS == decision) {
52 return DB_DENY_ALWAYS;
53 } else if (PromptDecision::ALLOW_THIS_TIME == decision) {
54 return DB_ALLOW_THIS_TIME;
55 } else if (PromptDecision::DENY_THIS_TIME == decision) {
56 return DB_DENY_THIS_TIME;
57 } else if (PromptDecision::ALLOW_FOR_SESSION == decision) {
58 return DB_ALLOW_FOR_SESSION;
61 return DB_DENY_FOR_SESSION;
64 PromptDecision AceDAOReadOnly::intToPromptDecision(int dec) {
65 if (dec == DB_ALLOW_ALWAYS) {
66 return PromptDecision::ALLOW_ALWAYS;
67 } else if (dec == DB_DENY_ALWAYS) {
68 return PromptDecision::DENY_ALWAYS;
69 } else if (dec == DB_ALLOW_THIS_TIME) {
70 return PromptDecision::ALLOW_THIS_TIME;
71 } else if (dec == DB_DENY_THIS_TIME) {
72 return PromptDecision::DENY_THIS_TIME;
73 } else if (dec == DB_ALLOW_FOR_SESSION) {
74 return PromptDecision::ALLOW_FOR_SESSION;
76 // DB_DENY_FOR_SESSION
77 return PromptDecision::DENY_FOR_SESSION;
80 void AceDAOReadOnly::attachToThread()
82 AceDaoUtilities::m_databaseInterface.AttachToThread();
85 void AceDAOReadOnly::detachFromThread()
87 AceDaoUtilities::m_databaseInterface.DetachFromThread();
90 OptionalCachedPromptDecision AceDAOReadOnly::getPromptDecision(
91 const DPL::String &hash,
92 const std::string &userParam)
95 ScopedTransaction transaction(&AceDaoUtilities::m_databaseInterface);
97 // get matching subject verdict
98 ACE_DB_SELECT(select, AcePromptDecision, &AceDaoUtilities::m_databaseInterface);
99 DPL::String DPLParam = DPL::FromUTF8String(userParam);
103 Equals<AcePromptDecision::hash>(hash),
104 Equals<AcePromptDecision::user_param>(DPLParam)));
106 std::list<AcePromptDecision::Row> rows = select->GetRowList();
108 transaction.Commit();
109 return OptionalCachedPromptDecision();
112 AcePromptDecision::Row row = rows.front();
113 CachedPromptDecision decision;
114 decision.decision = intToPromptDecision(row.Get_decision());
115 decision.session = row.Get_session();
117 return OptionalCachedPromptDecision(decision);
119 Catch(DPL::DB::SqlConnection::Exception::Base) {
120 ReThrowMsg(Exception::DatabaseError, "Failed to getPromptDecision");
124 OptionalCachedPromptDecision AceDAOReadOnly::getPromptDecision(
125 const BaseAttributeSet &attributes,
126 const std::string &userParam)
128 return getPromptDecision(convertToHash(attributes), userParam);
131 void AceDAOReadOnly::getAttributes(BaseAttributeSet *attributes)
133 if (NULL == attributes) {
134 LogError("NULL pointer");
141 ScopedTransaction transaction(&AceDaoUtilities::m_databaseInterface);
143 ACE_DB_SELECT(select, AceAttribute, &AceDaoUtilities::m_databaseInterface);
144 typedef std::list<AceAttribute::Row> RowList;
145 RowList list = select->GetRowList();
148 BaseAttributePtr attribute(new BaseAttribute());
149 DPL::String name = i->Get_name();
150 aname = DPL::ToUTF8String(name);
151 type = i->Get_type();
153 attribute->setName(&aname);
154 attribute->setType(intToAttributeType(type));
155 attributes->insert(attribute);
158 Catch(DPL::DB::SqlConnection::Exception::Base) {
159 ReThrowMsg(Exception::DatabaseError, "Failed to getAttributes");
163 OptionalPolicyResult AceDAOReadOnly::getPolicyResult(
164 const BaseAttributeSet &attributes)
167 auto attrHash = convertToHash(attributes);
168 return getPolicyResult(attrHash);
171 OptionalPolicyResult AceDAOReadOnly::getPolicyResult(
172 const DPL::String &attrHash)
175 ScopedTransaction transaction(&AceDaoUtilities::m_databaseInterface);
177 // get matching subject verdict
178 ACE_DB_SELECT(select, AcePolicyResult, &AceDaoUtilities::m_databaseInterface);
179 Equals<AcePolicyResult::hash> e1(attrHash);
182 std::list<AcePolicyResult::Row> rows = select->GetRowList();
184 transaction.Commit();
185 return OptionalPolicyResult();
188 AcePolicyResult::Row row = rows.front();
189 int decision = row.Get_decision();
190 PolicyResult res = PolicyResult::deserialize(decision);
191 transaction.Commit();
192 return OptionalPolicyResult(res);
194 Catch(DPL::DB::SqlConnection::Exception::Base) {
195 ReThrowMsg(Exception::DatabaseError, "Failed to getVerdict");
199 PreferenceTypes AceDAOReadOnly::getDevCapSetting(const std::string &resource)
203 if (!getResourceByUri(resource, row)) {
204 return PreferenceTypes::PREFERENCE_DEFAULT;
206 return intToPreference(row.Get_general_setting());
208 Catch(DPL::DB::SqlConnection::Exception::Base) {
209 ReThrowMsg(Exception::DatabaseError, "Failed to getResourceSetting");
213 void AceDAOReadOnly::getDevCapSettings(PreferenceTypesMap *globalSettingsMap)
215 if (NULL == globalSettingsMap) {
216 LogError("Null pointer");
219 globalSettingsMap->clear();
221 ACE_DB_SELECT(select, AceDevCap, &AceDaoUtilities::m_databaseInterface);
222 typedef std::list<AceDevCap::Row> RowList;
223 RowList list = select->GetRowList();
226 PreferenceTypes p = intToPreference(i->Get_general_setting());
227 globalSettingsMap->insert(make_pair(DPL::ToUTF8String(
228 i->Get_id_uri()), p));
231 Catch(DPL::DB::SqlConnection::Exception::Base) {
232 ReThrowMsg(Exception::DatabaseError, "Failed to getResourceSettings");
238 void AceDAOReadOnly::getWidgetDevCapSettings(BasePermissionList *outputList)
240 if (NULL == outputList) {
241 LogError("NULL pointer");
246 ScopedTransaction transaction(&AceDaoUtilities::m_databaseInterface);
248 std::string resourceName;
249 PreferenceTypes allowAccess;
251 ACE_DB_SELECT(select,
252 AceWidgetDevCapSetting,
253 &AceDaoUtilities::m_databaseInterface);
255 typedef std::list<AceWidgetDevCapSetting::Row> RowList;
256 RowList list = select->GetRowList();
260 int app_id = i->Get_app_id();
261 int res_id = i->Get_resource_id();
263 ACE_DB_SELECT(resourceSelect, AceDevCap, &AceDaoUtilities::m_databaseInterface);
264 resourceSelect->Where(Equals<AceDevCap::resource_id>(res_id));
265 AceDevCap::Row rrow = resourceSelect->GetSingleRow();
267 resourceName = DPL::ToUTF8String(rrow.Get_id_uri());
269 if (!resourceName.empty()) {
270 allowAccess = intToPreference(i->Get_access_value());
271 outputList->push_back(
272 BasePermission(app_id,
278 Catch(DPL::DB::SqlConnection::Exception::Base) {
279 ReThrowMsg(Exception::DatabaseError, "Failed to findUserSettings");
283 PreferenceTypes AceDAOReadOnly::getWidgetDevCapSetting(
284 const std::string &resource,
285 WidgetHandle handler)
288 ScopedTransaction transaction(&AceDaoUtilities::m_databaseInterface);
291 if (!getResourceByUri(resource, rrow)) {
292 return PreferenceTypes::PREFERENCE_DEFAULT;
294 int resourceId = rrow.Get_resource_id();
296 // get matching user setting
297 ACE_DB_SELECT(select, AceWidgetDevCapSetting, &AceDaoUtilities::m_databaseInterface);
299 select->Where(And(Equals<AceWidgetDevCapSetting::resource_id>(resourceId),
300 Equals<AceWidgetDevCapSetting::app_id>(handler)));
302 std::list<int> values =
303 select->GetValueList<AceWidgetDevCapSetting::access_value>();
304 if (values.empty()) {
305 return PreferenceTypes::PREFERENCE_DEFAULT;
307 return intToPreference(values.front());
309 Catch(DPL::DB::SqlConnection::Exception::Base) {
310 ReThrowMsg(Exception::DatabaseError, "Failed in getUserSetting");
314 void AceDAOReadOnly::getStaticDevCapPermissions(
316 std::set<DPL::String> *permissions)
318 if (NULL == permissions) {
319 LogError("NULL pointer");
322 permissions->clear();
324 ScopedTransaction transaction(&AceDaoUtilities::m_databaseInterface);
326 ACE_DB_SELECT(select, AceStaticDevCapPermission,
327 &AceDaoUtilities::m_databaseInterface);
329 Equals<AceStaticDevCapPermission::app_id>(widgetHandle));
330 std::list<DPL::String> devCapNames =
331 select->GetValueList<AceStaticDevCapPermission::dev_cap>();
332 permissions->insert(devCapNames.begin(), devCapNames.end());
333 transaction.Commit();
334 } Catch(DPL::DB::SqlConnection::Exception::Base) {
335 ReThrowMsg(Exception::DatabaseError, "Failed to getStaticDevCapPermissions");