02e3d0bfafb55638abce72b740fc59ffb9300bee
[platform/core/security/security-manager.git] / src / server / db / include / privilege_db.h
1 /*
2  * security-manager, database access
3  *
4  * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
5  *
6  * Contact: Rafal Krypa <r.krypa@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 /*
23  * @file        privilege_db.h
24  * @author      Krzysztof Sasiak <k.sasiak@samsung.com>
25  * @author      Rafal Krypa <r.krypa@samsung.com>
26  * @version     1.0
27  * @brief       This file contains declaration of the API to privilges database.
28  */
29
30 #include <cstdio>
31 #include <list>
32 #include <map>
33 #include <stdbool.h>
34 #include <string>
35
36 #include <dpl/db/sql_connection.h>
37
38 #ifndef PRIVILEGE_DB_H_
39 #define PRIVILEGE_DB_H_
40
41 namespace SecurityManager {
42
43 typedef std::vector<std::string> TPermissionsList;
44
45 enum class QueryType {
46     EGetPkgPermissions,
47     EAddApplication,
48     ERemoveApplication,
49     EAddAppPermissions,
50     ERemoveAppPermissions,
51     EPkgIdExists,
52 };
53
54 class PrivilegeDb {
55     /**
56      * PrivilegeDb database class
57      */
58
59 private:
60     SecurityManager::DB::SqlConnection *mSqlConnection;
61     const std::map<QueryType, const char * const > Queries = {
62         { QueryType::EGetPkgPermissions, "SELECT permission_name FROM app_permission_view WHERE pkg_name=?"},
63         { QueryType::EAddApplication, "INSERT INTO app_pkg_view (app_name, pkg_name) VALUES (?, ?)" },
64         { QueryType::ERemoveApplication, "DELETE FROM app_pkg_view WHERE app_name=? AND pkg_name=?" },
65         { QueryType::EAddAppPermissions, "INSERT INTO app_permission_view (app_name, pkg_name, permission_name) VALUES (?, ?, ?)" },
66         { QueryType::ERemoveAppPermissions, "DELETE FROM app_permission_view WHERE app_name=? AND pkg_name=? AND permission_name=?" },
67         { QueryType::EPkgIdExists, "SELECT * FROM pkg WHERE name=?" }
68     };
69
70     /**
71      * Check if pkgId is already registered in database
72      *
73      * @param pkgId - package identifier
74      * @exception DB::SqlConnection::Exception::InternalError on internal error
75      * @return true if pkgId exists in the database
76      *
77      */
78     bool PkgIdExists(const std::string &pkgId);
79
80     /**
81      * Check if there's a tuple of (appId, packageId) inside the database
82      *
83      * @param appId - application identifier
84      * @param pkgId - package identifier
85      * @param[out] currentPermissions - list of current permissions assigned to tuple (appId, pkgId)
86      * @exception DB::SqlConnection::Exception::InternalError on internal error
87      */
88     void GetPkgPermissions(const std::string &pkgId,
89             TPermissionsList &currentPermission);
90
91 public:
92     class Exception
93     {
94       public:
95         DECLARE_EXCEPTION_TYPE(SecurityManager::Exception, Base)
96         DECLARE_EXCEPTION_TYPE(Base, IOError)
97         DECLARE_EXCEPTION_TYPE(Base, InternalError)
98     };
99
100     /**
101      * Constructor
102      * @exception DB::SqlConnection::Exception::IOError on problems with database access
103      *
104      */
105     PrivilegeDb(const std::string &path);
106     ~PrivilegeDb(void);
107
108     /**
109      * Begin transaction
110      * @exception DB::SqlConnection::Exception::InternalError on internal error
111      *
112      */
113     void BeginTransaction(void);
114
115     /**
116      * Commit transaction
117      * @exception DB::SqlConnection::Exception::InternalError on internal error
118      *
119      */
120     void CommitTransaction(void);
121
122     /**
123      * Rollback transaction
124      * @exception DB::SqlConnection::Exception::InternalError on internal error
125      *
126      */
127     void RollbackTransaction(void);
128
129     /**
130      * Add an application into the database
131      *
132      * @param appId - application identifier
133      * @param pkgId - package identifier
134      * @param[out] pkgIdIsNew - return info if pkgId is new to the database
135      * @exception DB::SqlConnection::Exception::InternalError on internal error
136      */
137     void AddApplication(const std::string &appId, const std::string &pkgId,
138             bool &pkgIdIsNew);
139
140     /**
141      * Remove an application from the database
142      *
143      * @param appId - application identifier
144      * @param pkgId - package identifier
145      * @param[out] pkgIdIsNoMore - return info if pkgId is in the database
146      * @exception DB::SqlConnection::Exception::InternalError on internal error
147      */
148     void RemoveApplication(const std::string &appId, const std::string &pkgId,
149             bool &pkgIdIsNoMore);
150
151     /**
152      * Update permissions belonging to tuple (appId, pkgId)
153      *
154      * @param appId - application identifier
155      * @param pkgId - package identifier
156      * @param permissions - list of permissions to assign
157      * @param[out] addedPermissions - return list of added permissions
158      * @param[out] removedPermissions - return list of removed permissions
159      * @exception DB::SqlConnection::Exception::InternalError on internal error
160      */
161     void UpdatePermissions(const std::string &appId,
162             const std::string &pkgId, const TPermissionsList &permissions,
163             TPermissionsList &addedPermissions,
164             TPermissionsList &removedPermissions);
165
166 };
167
168 } //namespace SecurityManager
169
170 #endif // PRIVILEGE_DB_H_