Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cherrypy
- import hashlib
- import json
- import os
- import codecs
- USER_FILE = 'data/users.json'
- AUTOINC_FILE = 'data/auto.json'
- ROLE_ANONYM = 0
- ROLE_EDITOR = 1
- ROLE_ADMIN = 2
- #--------------------------------------
- class User_cl(object):
- #--------------------------------------
- #--------------------------------------
- def __init__(self):
- #--------------------------------------
- self.data = None
- self.load()
- #--------------------------------------
- def load(self):
- #--------------------------------------
- self.data = json.load(codecs.open(os.getcwd() + "/" + USER_FILE, "r", "utf-8"))
- #--------------------------------------
- def saveUsers(self):
- #--------------------------------------
- import json
- with open(USER_FILE, 'w+') as fp:
- json.dump(self.data, fp)
- #--------------------------------------
- def getByName(self, name):
- #--------------------------------------
- try:
- for user in self.data:
- if self.data[user]["name"].lower() == name.lower():
- return self.data[user]
- except Exception:
- return "{}"
- #--------------------------------------
- def getById(self, id):
- #--------------------------------------
- try:
- return self.data[id]
- except Exception:
- return "{}"
- #--------------------------------------
- def getUserId(self, name):
- #--------------------------------------
- try:
- for user in self.data:
- if 'name' in self.data[user]:
- if self.data[user]["name"].lower() == name.lower():
- return user
- except Exception:
- return "{}"
- #--------------------------------------
- def getAll(self):
- #--------------------------------------
- return self.data
- #--------------------------------------
- def getNewUserId(self):
- #--------------------------------------
- data = json.load(codecs.open(os.getcwd() + "/" + AUTOINC_FILE, "r", "utf-8"))
- return data
- #--------------------------------------
- def create(self, username, password, role = ROLE_ANONYM):
- #--------------------------------------
- username = username.strip()
- if(self.exist(username)):
- return False
- newUserId = self.getNewUserId()
- self.saveAutoInc(int(newUserId)+1)
- import datetime
- self.data.update({ str(newUserId) : {
- "name": username,
- "password": self.hashPassword(password),
- "role": role,
- "registerDate": str(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
- }})
- self.saveUsers()
- return True
- #--------------------------------------
- def update(self, id, username, password, role):
- #--------------------------------------
- self.data[id]["name"] = username
- if password:
- self.data[id]["password"] = self.hashPassword(password)
- self.data[id]["role"] = role
- self.saveUsers()
- #--------------------------------------
- def delete(self, id):
- #--------------------------------------
- if self.data[id]["role"] == "2":
- return False
- del self.data[id]
- self.saveUsers()
- return True
- #--------------------------------------
- def exist(self, username):
- #--------------------------------------
- for user in self.data:
- if self.data[user]["name"].title() == username.title():
- return True
- return False
- #--------------------------------------
- def saveAutoInc(self, id):
- #--------------------------------------
- with open(AUTOINC_FILE, 'w+') as f:
- json.dump(id, f)
- #--------------------------------------
- def hashPassword(self, password):
- #--------------------------------------
- return hashlib.sha256(password.encode('utf-8')).hexdigest()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement