2019-05-02 17:31:14 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
|
2019-05-03 16:52:05 +02:00
|
|
|
import os
|
2019-05-02 17:31:14 +02:00
|
|
|
import redis
|
2019-05-03 16:52:05 +02:00
|
|
|
import bcrypt
|
|
|
|
import configparser
|
2019-05-02 17:31:14 +02:00
|
|
|
|
|
|
|
from flask_login import UserMixin
|
|
|
|
|
|
|
|
class User(UserMixin):
|
|
|
|
|
|
|
|
def __init__(self, id):
|
|
|
|
|
2019-05-03 16:52:05 +02:00
|
|
|
configfile = os.path.join(os.environ['AIL_BIN'], 'packages/config.cfg')
|
|
|
|
if not os.path.exists(configfile):
|
|
|
|
raise Exception('Unable to find the configuration file. \
|
|
|
|
Did you set environment variables? \
|
|
|
|
Or activate the virtualenv.')
|
|
|
|
|
|
|
|
cfg = configparser.ConfigParser()
|
|
|
|
cfg.read(configfile)
|
|
|
|
|
|
|
|
self.r_serv_db = redis.StrictRedis(
|
|
|
|
host=cfg.get("ARDB_DB", "host"),
|
|
|
|
port=cfg.getint("ARDB_DB", "port"),
|
|
|
|
db=cfg.getint("ARDB_DB", "db"),
|
|
|
|
decode_responses=True)
|
|
|
|
|
|
|
|
if self.r_serv_db.hexists('user:all', id):
|
|
|
|
self.id = id
|
|
|
|
else:
|
|
|
|
self.id = "__anonymous__"
|
2019-05-02 17:31:14 +02:00
|
|
|
|
|
|
|
# return True or False
|
2019-05-03 16:52:05 +02:00
|
|
|
#def is_authenticated():
|
2019-05-02 17:31:14 +02:00
|
|
|
|
|
|
|
# return True or False
|
|
|
|
#def is_anonymous():
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get(self_class, id):
|
|
|
|
return self_class(id)
|
|
|
|
|
2019-06-20 15:49:40 +02:00
|
|
|
def user_is_anonymous(self):
|
|
|
|
if self.id == "__anonymous__":
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2019-05-02 17:31:14 +02:00
|
|
|
def check_password(self, password):
|
2019-06-20 15:49:40 +02:00
|
|
|
if self.user_is_anonymous():
|
|
|
|
return False
|
|
|
|
|
2019-05-03 16:52:05 +02:00
|
|
|
password = password.encode()
|
|
|
|
hashed_password = self.r_serv_db.hget('user:all', self.id).encode()
|
|
|
|
if bcrypt.checkpw(password, hashed_password):
|
2019-05-02 17:31:14 +02:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2019-05-03 16:52:05 +02:00
|
|
|
def request_password_change(self):
|
2019-06-06 21:27:13 +02:00
|
|
|
if self.r_serv_db.hget('user_metadata:{}'.format(self.id), 'change_passwd') == 'True':
|
2019-05-03 16:52:05 +02:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def is_in_role(self, role):
|
|
|
|
if self.r_serv_db.sismember('user_role:{}'.format(role), self.id):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|