forked from Wolfenswan/FAMDB
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutils.py
More file actions
executable file
·114 lines (89 loc) · 3.11 KB
/
utils.py
File metadata and controls
executable file
·114 lines (89 loc) · 3.11 KB
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
import base64
import os
import sqlite3
from http.cookies import SimpleCookie
import psutil
currentPath = os.path.dirname(os.path.realpath(__file__))
__props = dict(line.strip().split('=') for line in open(currentPath + '/config.config'))
missionMainDir = __props['missionMainDir']
missionMakerDir = __props['missionMakerDir']
serverAddress = __props['serverAddress']
emailAddress = __props['emailAddress']
emailPassword = __props['emailPassword']
emailServer = __props['emailServer']
emailPort = __props['emailPort']
discordHookUrl = __props['discordHookUrl']
discordAdminRoleId = __props['discordAdminRoleId']
port = int(__props['port'])
def getCursor():
conn = sqlite3.connect(currentPath + '/famdb.db', detect_types=sqlite3.PARSE_DECLTYPES)
conn.row_factory = sqlite3.Row
c = conn.cursor()
return c
# 0 = new user
# 1 = trusted MM
# 2 = low admin (full mission rights)
# 3 = high admin (full rights)
class User:
def __init__(self, email, permissionLevel, login, id):
self.email = email
self.permissionLevel = permissionLevel
self.login = login
self.id = id
def userRowToSessionId(user):
return base64.b64encode(user['sessionKey']).decode()
def getCurrentUser(cookie):
if 'famdbSessionId' not in cookie:
return None
sessionId = cookie['famdbSessionId'].value
try:
decode = base64.b64decode(sessionId)
c = getCursor()
c.execute('''select * from users where sessionKey = ?''', [decode])
user = c.fetchone()
if user is None:
return None
return User(user['email'], user['permissionLevel'], user['login'], user['id'])
except Exception as exc:
print("error getting current User: {0}".format(exc))
return None
def environToContents(environ):
try:
length = int(environ.get('CONTENT_LENGTH', '0'))
except ValueError:
length = 0
if length != 0:
return environ['wsgi.input'].read(length).decode()
return ""
def AND(one: bool, two: bool):
return one and two
def OR(one: bool, two: bool):
return one or two
def checkUserPermissions(user: User, requiredPermissionLevel=-1, missionId=None, collector=OR):
authorMatch = False
if user is None:
return False
if missionId is not None:
c = getCursor()
c.execute("select * from missions where id = ?", [missionId])
mission = c.fetchone()
if mission is None or user.permissionLevel < 0:
authorMatch = False
else:
authorMatch = user.login in mission['missionAuthor'].split(",")
return collector(user.permissionLevel >= requiredPermissionLevel, authorMatch)
def isPidRunning(pid):
try:
p = psutil.Process(pid)
except psutil.NoSuchProcess:
return False
else:
return True
def handleBadSessionIds(environ):
if environ['user'] is None and "HTTP_COOKIE" in environ:
cookie = SimpleCookie(environ['HTTP_COOKIE'])
if 'famdbSessionId' in cookie:
return [('Set-Cookie', 'famdbSessionId=;expires=Thu, 01 Jan 1970 00:00:00 GMT;MaxAge=-1')]
return []
else:
return []