def is_self(account_id, token): token = Token.objects(token=token).first() if token is None: return False if account_id != token.user_id: return False return True
def is_stuff(token): token = Token.objects(token=token).first() if token is None: return False account = Account.objects(id=token.user_id).first() if account is None: return False return True
def is_admin(token): token = Token.objects(token=token).first() if token is None: return False account = Account.objects(id=token.user_id).first() if account is None: return False if account.role == 'stuff': return False return True
def is_root(token): token = Token.objects(token=token).first() if token is None: return False account = Account.objects(id=token.user_id).first() if account is None: return False if account.username == 'root': return True return False
def login(username, password): account = Account.objects(username=username).first() if account is None: return {'message': 'this account does not exist'} if Account.check_password(account, password): new_token = create_token() token = Token.objects(user_id=str(account.id)).first() if token is None: Token(user_id=str(account.id), token=new_token).save() else: token.update(token=new_token) return { 'id': account.id, 'success': 1, 'token': new_token } else: return { 'message': 'password is wrong.' }
def logout(token): token = Token.objects(token=token).first() token.delete() return {'success': 1}