PrivilegeDb: drop TPrivilegesList typedef
[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 enum class QueryType {
44     EGetPkgPrivileges,
45     EAddApplication,
46     ERemoveApplication,
47     EAddAppPrivileges,
48     ERemoveAppPrivileges,
49     EPkgIdExists,
50 };
51
52 class PrivilegeDb {
53     /**
54      * PrivilegeDb database class
55      */
56
57 private:
58     SecurityManager::DB::SqlConnection *mSqlConnection;
59     const std::map<QueryType, const char * const > Queries = {
60         { QueryType::EGetPkgPrivileges, "SELECT privilege_name FROM app_privilege_view WHERE pkg_name=?"},
61         { QueryType::EAddApplication, "INSERT INTO app_pkg_view (app_name, pkg_name) VALUES (?, ?)" },
62         { QueryType::ERemoveApplication, "DELETE FROM app_pkg_view WHERE app_name=? AND pkg_name=?" },
63         { QueryType::EAddAppPrivileges, "INSERT INTO app_privilege_view (app_name, privilege_name) VALUES (?, ?)" },
64         { QueryType::ERemoveAppPrivileges, "DELETE FROM app_privilege_view WHERE app_name=?" },
65         { QueryType::EPkgIdExists, "SELECT * FROM pkg WHERE name=?" }
66     };
67
68     /**
69      * Check if pkgId is already registered in database
70      *
71      * @param pkgId - package identifier
72      * @exception DB::SqlConnection::Exception::InternalError on internal error
73      * @return true if pkgId exists in the database
74      *
75      */
76     bool PkgIdExists(const std::string &pkgId);
77
78 public:
79     class Exception
80     {
81       public:
82         DECLARE_EXCEPTION_TYPE(SecurityManager::Exception, Base)
83         DECLARE_EXCEPTION_TYPE(Base, IOError)
84         DECLARE_EXCEPTION_TYPE(Base, InternalError)
85     };
86
87     /**
88      * Constructor
89      * @exception DB::SqlConnection::Exception::IOError on problems with database access
90      *
91      */
92     PrivilegeDb(const std::string &path);
93     ~PrivilegeDb(void);
94
95     /**
96      * Begin transaction
97      * @exception DB::SqlConnection::Exception::InternalError on internal error
98      *
99      */
100     void BeginTransaction(void);
101
102     /**
103      * Commit transaction
104      * @exception DB::SqlConnection::Exception::InternalError on internal error
105      *
106      */
107     void CommitTransaction(void);
108
109     /**
110      * Rollback transaction
111      * @exception DB::SqlConnection::Exception::InternalError on internal error
112      *
113      */
114     void RollbackTransaction(void);
115
116     /**
117      * Retrieve list of privileges assigned to a pkgId
118      *
119      * @param pkgId - package identifier
120      * @param[out] currentPrivileges - list of current privileges assigned to pkgId
121      * @exception DB::SqlConnection::Exception::InternalError on internal error
122      */
123     void GetPkgPrivileges(const std::string &pkgId,
124             std::vector<std::string> &currentPrivilege);
125
126     /**
127      * Add an application into the database
128      *
129      * @param appId - application identifier
130      * @param pkgId - package identifier
131      * @param[out] pkgIdIsNew - return info if pkgId is new to the database
132      * @exception DB::SqlConnection::Exception::InternalError on internal error
133      */
134     void AddApplication(const std::string &appId, const std::string &pkgId,
135             bool &pkgIdIsNew);
136
137     /**
138      * Remove an application from the database
139      *
140      * @param appId - application identifier
141      * @param pkgId - package identifier
142      * @param[out] pkgIdIsNoMore - return info if pkgId is in the database
143      * @exception DB::SqlConnection::Exception::InternalError on internal error
144      */
145     void RemoveApplication(const std::string &appId, const std::string &pkgId,
146             bool &pkgIdIsNoMore);
147
148     /**
149      * Remove privileges assigned to application
150      *
151      * @param appId - application identifier
152      * @exception DB::SqlConnection::Exception::InternalError on internal error
153      */
154     void RemoveAppPrivileges(const std::string &appId);
155
156     /**
157      * Update privileges assigned to application
158      * To assure data integrity this method must be called inside db transaction.
159      *
160      * @param appId - application identifier
161      * @param privileges - list of privileges to assign
162      * @exception DB::SqlConnection::Exception::InternalError on internal error
163      */
164     void UpdateAppPrivileges(const std::string &appId,
165             const std::vector<std::string> &privileges);
166
167 };
168
169 } //namespace SecurityManager
170
171 #endif // PRIVILEGE_DB_H_