forked from laobubu/ssland
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.py
executable file
·96 lines (86 loc) · 2.42 KB
/
user.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# User Module.
# Including User class and reading/writing implementations
#
import re, os, json, time
import config
from hashlib import md5
class User:
def __init__(self, src, password=None):
'''
Create one User instance from a Dict or String object.
'''
self.username = ''
self.salted_password = ''
self.sskey = ''
self.id = 0
self.join_since = 0
self.name = ''
self.comment = ''
self.suspended = False
if not type(src) in [str, unicode]:
for k in self.__dict__.keys():
if k in src:
setattr(self, k, src[k])
else:
self.username = src
self.salted_password = salt_password(password)
self.join_since = time.time()
self.id = next_id()
def set_password(self, password):
self.salted_password = salt_password(password)
def write(self):
_USER_CACHE[self.username] = self
update_lookup_table()
f = file(user_filename(self.username), 'w')
json.dump(self.__dict__, f)
f.close()
salt_password = lambda password: md5(password + config.USER_SALT).hexdigest()
user_filename = lambda username: config.USER_ROOT + '/' + username + '.json'
# Every user info will be cached here to reduce harddisk IO
_USER_CACHE = {}
_USER_CACHE_ID = {}
def open(username):
'''
Get an existing User() instance.
'''
if not re.match(r'^[\w\-\.]+$', username): return None
try:
if not _USER_CACHE.has_key(username):
f = file(user_filename(username), 'r')
j = json.load(f)
u = User(j)
_USER_CACHE[username] = u
f.close()
return _USER_CACHE[username]
except:
return None
def cache_all():
'''
Load and cache all existing user files.
'''
for fn in os.listdir(config.USER_ROOT):
if fn[-5:] == '.json':
open(fn[:-5])
update_lookup_table()
def update_lookup_table():
'''
Update ID -> User lookup table.
'''
global _USER_CACHE_ID
_USER_CACHE_ID = {}
for i in _USER_CACHE:
u = _USER_CACHE[i]
_USER_CACHE_ID[u.id] = u
def next_id():
'''
Get next avaliable ID.
'''
ids = _USER_CACHE_ID.keys()
ids.sort()
l = len(ids)
for i in range(l):
if ids[i] > i: return i
return l