Tizen 2.1 base
[framework/web/wrt-commons.git] / modules / ace / dao / AceDAOReadOnly.cpp
1 /*
2  * Copyright (c) 2011 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  *
18  *
19  * @file       AceDAOReadOnlyReadOnly.cpp
20  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
21  * @author     Grzegorz Krawczyk (g.krawczyk@samsung.com)
22  * @version    0.1
23  * @brief
24  */
25
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>
30
31 #include <dpl/foreach.h>
32
33 using namespace DPL::DB::ORM;
34 using namespace DPL::DB::ORM::ace;
35 using namespace AceDB::AceDaoUtilities;
36 using namespace AceDB::AceDaoConversions;
37
38 namespace AceDB {
39
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;
46
47 int AceDAOReadOnly::promptDecisionToInt(PromptDecision decision)
48 {
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;
59     }
60     // DENY_FOR_SESSION
61     return DB_DENY_FOR_SESSION;
62 }
63
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;
75     }
76     // DB_DENY_FOR_SESSION
77     return PromptDecision::DENY_FOR_SESSION;
78 }
79
80 void AceDAOReadOnly::attachToThread()
81 {
82     AceDaoUtilities::m_databaseInterface.AttachToThread();
83 }
84
85 void AceDAOReadOnly::detachFromThread()
86 {
87     AceDaoUtilities::m_databaseInterface.DetachFromThread();
88 }
89
90 OptionalCachedPromptDecision AceDAOReadOnly::getPromptDecision(
91     const DPL::String &hash,
92     const std::string &userParam)
93 {
94     Try {
95         ScopedTransaction transaction(&AceDaoUtilities::m_databaseInterface);
96
97         // get matching subject verdict
98         ACE_DB_SELECT(select, AcePromptDecision, &AceDaoUtilities::m_databaseInterface);
99         DPL::String DPLParam = DPL::FromUTF8String(userParam);
100
101         select->Where(
102             And(
103                 Equals<AcePromptDecision::hash>(hash),
104                 Equals<AcePromptDecision::user_param>(DPLParam)));
105
106         std::list<AcePromptDecision::Row> rows = select->GetRowList();
107         if (rows.empty()) {
108             transaction.Commit();
109             return OptionalCachedPromptDecision();
110         }
111
112         AcePromptDecision::Row row = rows.front();
113         CachedPromptDecision decision;
114         decision.decision = intToPromptDecision(row.Get_decision());
115         decision.session = row.Get_session();
116
117         return OptionalCachedPromptDecision(decision);
118     }
119     Catch(DPL::DB::SqlConnection::Exception::Base) {
120         ReThrowMsg(Exception::DatabaseError, "Failed to getPromptDecision");
121     }
122 }
123
124 OptionalCachedPromptDecision AceDAOReadOnly::getPromptDecision(
125     const BaseAttributeSet &attributes,
126     const std::string &userParam)
127 {
128     return getPromptDecision(convertToHash(attributes), userParam);
129 }
130
131 void AceDAOReadOnly::getAttributes(BaseAttributeSet *attributes)
132 {
133     if (NULL == attributes) {
134         LogError("NULL pointer");
135         return;
136     }
137     attributes->clear();
138     std::string aname;
139     int type;
140     Try {
141         ScopedTransaction transaction(&AceDaoUtilities::m_databaseInterface);
142
143         ACE_DB_SELECT(select, AceAttribute, &AceDaoUtilities::m_databaseInterface);
144         typedef std::list<AceAttribute::Row> RowList;
145         RowList list = select->GetRowList();
146
147         FOREACH(i, list) {
148             BaseAttributePtr attribute(new BaseAttribute());
149             DPL::String name = i->Get_name();
150             aname = DPL::ToUTF8String(name);
151             type = i->Get_type();
152
153             attribute->setName(&aname);
154             attribute->setType(intToAttributeType(type));
155             attributes->insert(attribute);
156         }
157     }
158     Catch(DPL::DB::SqlConnection::Exception::Base) {
159         ReThrowMsg(Exception::DatabaseError, "Failed to getAttributes");
160     }
161 }
162
163 OptionalPolicyResult AceDAOReadOnly::getPolicyResult(
164         const BaseAttributeSet &attributes)
165 {
166
167     auto attrHash = convertToHash(attributes);
168     return getPolicyResult(attrHash);
169 }
170
171 OptionalPolicyResult AceDAOReadOnly::getPolicyResult(
172     const DPL::String &attrHash)
173 {
174     Try {
175         ScopedTransaction transaction(&AceDaoUtilities::m_databaseInterface);
176
177         // get matching subject verdict
178         ACE_DB_SELECT(select, AcePolicyResult, &AceDaoUtilities::m_databaseInterface);
179         Equals<AcePolicyResult::hash> e1(attrHash);
180         select->Where(e1);
181
182         std::list<AcePolicyResult::Row> rows = select->GetRowList();
183         if (rows.empty()) {
184             transaction.Commit();
185             return OptionalPolicyResult();
186         }
187
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);
193     }
194     Catch(DPL::DB::SqlConnection::Exception::Base) {
195         ReThrowMsg(Exception::DatabaseError, "Failed to getVerdict");
196     }
197 }
198
199 PreferenceTypes AceDAOReadOnly::getDevCapSetting(const std::string &resource)
200 {
201     Try {
202         AceDevCap::Row row;
203         if (!getResourceByUri(resource, row)) {
204             return PreferenceTypes::PREFERENCE_DEFAULT;
205         }
206         return intToPreference(row.Get_general_setting());
207     }
208     Catch(DPL::DB::SqlConnection::Exception::Base) {
209         ReThrowMsg(Exception::DatabaseError, "Failed to getResourceSetting");
210     }
211 }
212
213 void AceDAOReadOnly::getDevCapSettings(PreferenceTypesMap *globalSettingsMap)
214 {
215     if (NULL == globalSettingsMap) {
216         LogError("Null pointer");
217         return;
218     }
219     globalSettingsMap->clear();
220     Try {
221         ACE_DB_SELECT(select, AceDevCap, &AceDaoUtilities::m_databaseInterface);
222         typedef std::list<AceDevCap::Row> RowList;
223         RowList list = select->GetRowList();
224
225         FOREACH(i, list) {
226             PreferenceTypes p = intToPreference(i->Get_general_setting());
227             globalSettingsMap->insert(make_pair(DPL::ToUTF8String(
228                 i->Get_id_uri()), p));
229         }
230     }
231     Catch(DPL::DB::SqlConnection::Exception::Base) {
232         ReThrowMsg(Exception::DatabaseError, "Failed to getResourceSettings");
233     }
234 }
235
236
237
238 void AceDAOReadOnly::getWidgetDevCapSettings(BasePermissionList *outputList)
239 {
240     if (NULL == outputList) {
241         LogError("NULL pointer");
242         return;
243     }
244     outputList->clear();
245     Try {
246         ScopedTransaction transaction(&AceDaoUtilities::m_databaseInterface);
247
248         std::string resourceName;
249         PreferenceTypes allowAccess;
250
251         ACE_DB_SELECT(select,
252                       AceWidgetDevCapSetting,
253                       &AceDaoUtilities::m_databaseInterface);
254
255         typedef std::list<AceWidgetDevCapSetting::Row> RowList;
256         RowList list = select->GetRowList();
257
258         // TODO JOIN
259         FOREACH(i, list) {
260             int app_id = i->Get_app_id();
261             int res_id = i->Get_resource_id();
262
263             ACE_DB_SELECT(resourceSelect, AceDevCap, &AceDaoUtilities::m_databaseInterface);
264             resourceSelect->Where(Equals<AceDevCap::resource_id>(res_id));
265             AceDevCap::Row rrow = resourceSelect->GetSingleRow();
266
267             resourceName = DPL::ToUTF8String(rrow.Get_id_uri());
268
269             if (!resourceName.empty()) {
270                 allowAccess = intToPreference(i->Get_access_value());
271                 outputList->push_back(
272                     BasePermission(app_id,
273                     resourceName,
274                     allowAccess));
275             }
276         }
277     }
278     Catch(DPL::DB::SqlConnection::Exception::Base) {
279         ReThrowMsg(Exception::DatabaseError, "Failed to findUserSettings");
280     }
281 }
282
283 PreferenceTypes AceDAOReadOnly::getWidgetDevCapSetting(
284         const std::string &resource,
285         WidgetHandle handler)
286 {
287     Try {
288         ScopedTransaction transaction(&AceDaoUtilities::m_databaseInterface);
289
290         AceDevCap::Row rrow;
291         if (!getResourceByUri(resource, rrow)) {
292             return PreferenceTypes::PREFERENCE_DEFAULT;
293         }
294         int resourceId = rrow.Get_resource_id();
295
296         // get matching user setting
297         ACE_DB_SELECT(select, AceWidgetDevCapSetting, &AceDaoUtilities::m_databaseInterface);
298
299         select->Where(And(Equals<AceWidgetDevCapSetting::resource_id>(resourceId),
300                 Equals<AceWidgetDevCapSetting::app_id>(handler)));
301
302         std::list<int> values =
303             select->GetValueList<AceWidgetDevCapSetting::access_value>();
304         if (values.empty()) {
305             return PreferenceTypes::PREFERENCE_DEFAULT;
306         }
307         return intToPreference(values.front());
308     }
309     Catch(DPL::DB::SqlConnection::Exception::Base) {
310         ReThrowMsg(Exception::DatabaseError, "Failed in getUserSetting");
311     }
312 }
313
314 void AceDAOReadOnly::getStaticDevCapPermissions(
315     int widgetHandle,
316     std::set<DPL::String> *permissions)
317 {
318     if (NULL == permissions) {
319         LogError("NULL pointer");
320         return;
321     }
322     permissions->clear();
323     Try {
324         ScopedTransaction transaction(&AceDaoUtilities::m_databaseInterface);
325
326         ACE_DB_SELECT(select, AceStaticDevCapPermission,
327                       &AceDaoUtilities::m_databaseInterface);
328         select->Where(
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");
336     }
337 }
338 }