Пример #1
0
    def create_user(self, name: str, password: str) -> Users:
        logging.info(
            f'input information -> login name: {name}, login password: {password}'
        )
        # 会话工厂生产一个具体会话
        session = Session()
        query = session.query(Users).filter(Users.name == name)
        try:
            # 可能产生NoResultFound错误
            u = query.one()
            # # 提交事务, 事务提交后会丢失查询
            # session.commit()
            # 会话关闭之前初始化一下实例信息 print一下即可,他会调用__str__,__str__会调用__repr__,repr执行时用self.name执行了查询,并将信息保存到属性里
            logging.info(f'one user found -> {u}, authority {u.auth}')
            # # 把值都存到新对象里
            # auth = Auths(identity=u.auth.identity, authority=u.auth.authority)
            # user = Users(id=u.id, name=u.name, password=u.password, remote_identity=u.remote_identity, auth=auth)
        except NoResultFound:
            raise UserDoesNotExistError('用户不存在!')
        finally:
            # 关闭会话
            session.close()

        # 获得user则用户存在,进一步校验密码
        if password is None:
            raise PasswordError('密码不能为空!')
        if not self.password_verify(password, u.password):
            raise PasswordError('密码不正确!')
        # 密码校验通过,返回一个user对象
        logging.info(f'login user -> {u.name}: {u}')
        return u
Пример #2
0
    def query(self, *, identity: str = None, str_in_name: str = None) -> list:
        logging.info(
            f'query through identity={identity}, name field contains {str_in_name} string'
        )
        session = Session()
        q = session.query(Users.name, Users.password, Users.remote_identity, Auths.authority). \
            join(Auths)  # filter(Users.remote_identity == Auths.identity)

        if identity:
            q = q.filter(Users.remote_identity == identity)
        if str_in_name:
            q = q.filter(Users.name.like('%' + str_in_name + '%'))
        q.order_by(Users.remote_identity)
        try:
            users = q.all()
            users_list = [{
                'name': user[0],
                'password': user[1],
                'remote_identity': user[2],
                'authority': user[3],
            } for user in users]
        finally:
            session.close()
        logging.info(f'{len(users_list)} users were found')
        if not users_list:
            raise UserDoesNotExistError(f'查不到满足条件的用户!')
        return users_list
Пример #3
0
 def update(self,
            old_name: str,
            *,
            new_name: str = None,
            new_password: str = None,
            new_identity: str = None):
     logging.info(
         f'prepare to update user {old_name}, new information {new_name, new_password, new_identity}'
     )
     if all(map(lambda x: x is None,
                [new_name, new_password, new_identity])):
         return
     session = Session()
     query = session.query(Users).filter(Users.name == old_name)
     try:
         user = query.one()  # NoResultFound
         self.update_name(user, new_name)
         self.update_password(user, new_password)
         self.update_identity(user, new_identity)
         session.commit()
         logging.info(f'old user {old_name} has been updated')
     except NoResultFound:
         logging.info(f'user {old_name} does not exist')
         raise UserDoesNotExistError(f'用户 {old_name} 不存在')
     except IntegrityError as e:
         logging.info(
             f'database IntegrityError, error code {e.orig.args[0]}')
         session.rollback()
         if e.orig.args[0] == 1452:
             raise IdentityDoesNotExistError(f'新用户身份 {new_identity} 不存在')
         else:
             assert e.orig.args[0] == 1062
             raise UserExistError(f'新用户名 {new_name} 已存在,无法进行更新')
     finally:
         session.close()
Пример #4
0
 def delete_single(self, name: str):
     logging.info(f'user {name} prepare to be deleted')
     session = Session()
     query = session.query(Users).filter(Users.name == name)
     try:
         user = query.one()
         session.delete(user)
         session.commit()
         logging.info(f'user {name} has been deleted')
     except NoResultFound:
         session.rollback()
         logging.info(f'fail to delete user {name}')
         raise UserDoesNotExistError(f'用户 {name} 不存在')
     finally:
         session.close()
Пример #5
0
 def modify_password(self, new_password: str):
     logging.info(f'prepare update user password to {new_password}')
     session = Session()
     query = session.query(Users).filter(Users.id == self.user.id)
     try:
         u = query.one()
         hex_password = sha1_encrypt(new_password)
         u.password = hex_password
         session.commit()
         # 事务提交后要更新下当前的用户密码
         self.user.password = hex_password
     except NoResultFound:
         raise UserDoesNotExistError(f'用户不存在, 当前用户 {self.user} 已失效!')
     finally:
         session.close()
     logging.info(f'user password been changed to {new_password}')
Пример #6
0
 def delete_set(self, name_set: set):
     # 避免对容器类型数据直接操作
     internal_name_set = copy.copy(name_set)
     logging.info(f'these users {internal_name_set} prepare to be deleted')
     session = Session()
     query = session.query(Users).filter(Users.name.in_(internal_name_set))
     try:
         users = query.all()
         for user in users:
             internal_name_set.remove(user.name)
             session.delete(user)
         if len(internal_name_set) != 0:
             session.rollback()
             logging.info(f'these users {internal_name_set} do not exist, transaction has rolled back')
             raise UserDoesNotExistError(f'这些用户不存在 {internal_name_set}')
         session.commit()
         logging.info(f'all users have been deleted')
     finally:
         # 关闭会话
         session.close()
Пример #7
0
    def modify_name(self, new_name: str):
        logging.info(
            f'prepare update user name from {self.user.name} to {new_name}')
        session = Session()
        query_new_name = session.query(Users).filter(Users.name == new_name)

        # 如果用户名已经存在,引起错误
        if query_new_name.one_or_none():
            raise UserExistError(f'用户名 {new_name} 已存在')

        # 用户名不存在,则更新用户名
        query = session.query(Users).filter(Users.id == self.user.id)
        try:
            u = query.one()
            u.name = new_name
            session.commit()
            # 事务提交后要更新下当前的用户名
            self.user.name = new_name
        except NoResultFound:
            raise UserDoesNotExistError(f'用户不存在, 当前用户 {self.user} 已失效!')
        finally:
            session.close()
        logging.info(f'user name been changed to {new_name}')