forked from Kreis-Unna/PostNAS_Search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PostNAS_AccessControl.py
295 lines (251 loc) · 10.8 KB
/
PostNAS_AccessControl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# -*- coding: utf-8 -*-
"""
/***************************************************************************
PostNAS_Search
-------------------
Date : June 2016
copyright : (C) 2016 by Kreis-Unna
email : marvin.kinberger@kreis-unna.de
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
import os
import getpass
from qgis.PyQt.QtCore import QSettings
from qgis.PyQt.QtSql import QSqlDatabase, QSqlQuery
from qgis.PyQt.QtWidgets import QMessageBox
from qgis.core import *
import qgis.core
class PostNAS_AccessControl:
def __init__(self, username = None):
if(username == None):
self.username = getpass.getuser().lower()
else:
self.username = username.lower()
self.access = None
self.name = None
self.db = self.__loadDB()
def setUsername(self,username):
self.username = username
if(self.checkUserExists()):
sql = "SELECT name,access FROM postnas_search_access_control WHERE lower(username) = :username"
self.__openDB()
queryLoadUserData = QSqlQuery(self.db)
queryLoadUserData.prepare(sql)
queryLoadUserData.bindValue(":username",self.getUsername())
queryLoadUserData.exec_()
if(queryLoadUserData.size() == 1):
while(queryLoadUserData.next()):
self.setName(queryLoadUserData.value(queryLoadUserData.record().indexOf("name")))
self.setAccess(queryLoadUserData.value(queryLoadUserData.record().indexOf("access")))
def setAccess(self,access):
self.access = access
def setName(self,name):
self.name = name
def getUsername(self):
return self.username.lower()
def getAccess(self):
return self.access
def getName(self):
return self.name
def __checkUsername(self):
pass
def checkAccessControlIsActive(self):
settings = QSettings("PostNAS", "PostNAS-Suche")
if(settings.contains("accessControl") == True):
if(settings.value("accessControl",0) == 1):
if(self.checkAccessTable() == False):
settings.setValue("accessControl",0)
return False
else:
return True
else:
return False
else:
if(self.checkAccessTable() == True):
settings.setValue("accessControl",1)
return True
else:
settings.setValue("accessControl",0)
return False
def checkAccessTable(self):
sql = "SELECT table_name FROM information_schema.tables WHERE table_name = 'postnas_search_access_control'";
self.__openDB()
query = QSqlQuery(self.db)
query.exec_(sql)
if(query.size() > 0):
return True
else:
return False
def createAccessTable(self):
file_path = os.path.dirname(os.path.realpath(__file__)) + "/create_accesstable/create_table.sql"
sql = open(file_path).read()
self.__openDB()
query = QSqlQuery(self.db)
query.exec_(sql)
if(query.lastError().number() == -1):
return True
else:
return False
def checkAccessTableHasAdmin(self):
sql = "SELECT lower(username) FROM postnas_search_access_control WHERE access = 0";
self.__openDB()
query = QSqlQuery(self.db)
query.exec_(sql)
if(query.size() > 0):
return True
else:
return False
def insertUser(self):
if(self.getUsername() != None):
self.__openDB()
sql = "INSERT INTO postnas_search_access_control (username,name,access) VALUES (:username,:name,:access)"
query = QSqlQuery(self.db)
query.prepare(sql)
query.bindValue(":username",self.getUsername().lower())
query.bindValue(":name",self.name)
query.bindValue(":access",self.access)
query.exec_()
if(query.lastError().number() == -1):
return True
else:
return False
else:
return False
def insertAdminUser(self):
self.access = 0
return self.insertUser()
def updateUser(self,username_old):
if(self.getUsername() != None):
self.__openDB()
sql = "UPDATE postnas_search_access_control SET username = :username, name = :name, access = :access WHERE username = :username_old"
query = QSqlQuery(self.db)
query.prepare(sql)
query.bindValue(":username",self.getUsername().lower())
query.bindValue(":username_old",username_old)
query.bindValue(":name",self.name)
query.bindValue(":access",self.access)
query.exec_()
if(query.lastError().number() == -1):
return True
else:
QgsMessageLog.logMessage("Datenbankfehler beim Update: " + query.lastError().text(),'PostNAS-Suche', Qgis.Critical)
return False
else:
return False
def checkUserIsAdmin(self):
if(self.getUsername() != None):
self.__openDB()
sql = "SELECT lower(username) as username FROM postnas_search_access_control WHERE access = 0 AND lower(username) = :username"
query = QSqlQuery(self.db)
query.prepare(sql)
query.bindValue(":username",self.getUsername())
query.exec_()
if(query.lastError().number() == -1):
if(query.size() > 0):
return True
else:
return False
else:
return False
else:
return False
def checkUserHasEigentuemerAccess(self):
if(self.getUsername() != None):
self.__openDB()
sql = "SELECT lower(username) as username FROM postnas_search_access_control WHERE access IN (0,1) AND lower(username) = :username"
queryEigentuemerAccess = QSqlQuery(self.db)
queryEigentuemerAccess.prepare(sql)
queryEigentuemerAccess.bindValue(":username",self.getUsername())
queryEigentuemerAccess.exec_()
if(queryEigentuemerAccess.lastError().number() == -1):
if(queryEigentuemerAccess.size() > 0):
return True
else:
return False
else:
return False
else:
return False
def loadUserAccessTable(self):
sql = "SELECT lower(username) as username,name,bezeichnung FROM postnas_search_access_control LEFT JOIN postnas_search_accessmode ON postnas_search_access_control.access = postnas_search_accessmode.id";
self.__openDB()
queryLoadAccessTable = QSqlQuery(self.db)
queryLoadAccessTable.prepare(sql)
queryLoadAccessTable.exec_()
results = []
if(queryLoadAccessTable.size() > 0):
while(queryLoadAccessTable.next()):
list = {'username': queryLoadAccessTable.value(queryLoadAccessTable.record().indexOf("username")),
'name': queryLoadAccessTable.value(queryLoadAccessTable.record().indexOf("name")),
'access': queryLoadAccessTable.value(queryLoadAccessTable.record().indexOf("bezeichnung"))}
results.append(list)
return results
def deleteUser(self):
sql = "DELETE FROM postnas_search_access_control WHERE lower(username) = :username"
self.__openDB()
queryDeleteUser = QSqlQuery(self.db)
queryDeleteUser.prepare(sql)
queryDeleteUser.bindValue(":username",self.getUsername())
queryDeleteUser.exec_()
if(queryDeleteUser.lastError().number() == -1):
return True
else:
QgsMessageLog.logMessage("Datenbankfehler beim Löschen: " + queryDeleteUser.lastError().text(), 'PostNAS-Suche',Qgis.Critical)
return False
def getAccessModes(self):
sql = "SELECT id,bezeichnung FROM postnas_search_accessmode"
self.__openDB()
queryLoadAccessModes = QSqlQuery(self.db)
queryLoadAccessModes.prepare(sql)
queryLoadAccessModes.exec_()
results = []
if(queryLoadAccessModes.size() > 0):
while(queryLoadAccessModes.next()):
list = {'id': queryLoadAccessModes.value(queryLoadAccessModes.record().indexOf("id")),
'bezeichnung': queryLoadAccessModes.value(queryLoadAccessModes.record().indexOf("bezeichnung"))}
results.append(list)
return results
def checkUserExists(self):
sql = "SELECT lower(username) as username FROM postnas_search_access_control WHERE lower(username) = :username"
self.__openDB()
queryCheckUserExists = QSqlQuery(self.db)
queryCheckUserExists.prepare(sql)
queryCheckUserExists.bindValue(":username",self.getUsername())
queryCheckUserExists.exec_()
if(queryCheckUserExists.lastError().number() == -1):
if(queryCheckUserExists.size() > 0):
return True
else:
return False
else:
return False
def __loadDB(self):
settings = QSettings("PostNAS", "PostNAS-Suche")
dbHost = settings.value("host", "")
dbDatabasename = settings.value("dbname", "")
dbPort = settings.value("port", "5432")
dbUsername = settings.value("user", "")
dbPassword = settings.value("password", "")
authcfg = settings.value( "authcfg", "" )
if authcfg != "" and hasattr(qgis.core,'QgsAuthManager'):
amc = qgis.core.QgsAuthMethodConfig()
qgis.core.QgsAuthManager.instance().loadAuthenticationConfig( authcfg, amc, True)
dbUsername = amc.config( "username", dbUsername )
dbPassword = amc.config( "password", dbPassword )
db = QSqlDatabase.addDatabase("QPSQL")
db.setHostName(dbHost)
db.setPort(int(dbPort))
db.setDatabaseName(dbDatabasename)
db.setUserName(dbUsername)
db.setPassword(dbPassword)
return db
def __openDB(self):
if(self.db.isOpen() == False):
self.db.open()