Beispiel #1
0
    def user_menu(self):
        """用户菜单"""

        cur = {
            'current_identity': get_jwt_identity(),
            'current_type': get_jwt_claims()
        }
        if not cur['current_identity']:
            return ResponseHelper.return_false_data(msg='请登录', status=200)
        user_type = cur.get('current_type')
        if user_type:
            try:
                menus = Menuconfig.query.filter_by(
                    UserType=EnumUserType(user_type)).all()
            except Exception as e:
                self.logger.error('服务器错误:', str(e))
                return ResponseHelper.return_false_data(msg='Server Error',
                                                        status=500)
            if menus:
                menu_list = [
                    dict(menuname=menu.Menu.Name,
                         menuurl=menu.Menu.Url,
                         menupid=menu.Menu.Pid) for menu in menus
                ]
                return ResponseHelper.return_true_data(menu_list)
            else:
                return ResponseHelper.return_false_data(msg='请添加菜单',
                                                        status=200)
        else:
            return ResponseHelper.return_false_data(msg='请添加角色', status=200)
Beispiel #2
0
 def modify_user(self, user_id):
     """用户信息修改"""
     try:
         identity = self.get_user()
         if not identity.get('userID'):
             return self.Response.return_false_data(msg='请登录!')
         user_type = identity.get('usertype')
         if user_type != EnumUserType.Admin.name:
             return self.Response.return_false_data(msg='权限不足!', status=403)
         user_info = self.parser_json()
         user_type_modify = user_info.get('usertype')
         if not all([user_id, user_type_modify]):
             return self.Response.return_false_data(msg='参数错误!')
         try:
             User.query.filter_by(ID=user_id).update(
                 {'UserType': EnumUserType(user_type_modify)})
             self.db.session.commit()
         except Exception as e:
             self.db.session.rollback()
             self.logger.error('Database Error: %s', str(e))
             return self.Response.return_server_error()
         return self.Response.return_true_data('用户类型修改成功!')
     except Exception as e:
         self.logger.error('Server Error: %s', str(e))
         return self.Response.return_server_error()
Beispiel #3
0
    def user_type_upd(self):
        """用户类型修改"""

        cur = {
            'current_identity': get_jwt_identity(),
            'current_type': get_jwt_claims()
        }
        if not cur['current_identity']:
            return ResponseHelper.return_false_data(msg='请登录', status=200)
        user_type = cur.get('current_type')
        if 1 == user_type:
            user_info = request.get_json()
            if not user_info:
                return ResponseHelper.return_false_data(msg='参数错误', status=200)
            user_id = user_info.get('user_id')
            user_type = user_info.get('user_type')
            if not all([user_id, user_type]):
                return ResponseHelper.return_false_data(msg='参数不完整',
                                                        status=200)
            try:
                User.query.filter_by(ID=user_id).update(
                    {'UserType': EnumUserType(user_type)})
                db.session.commit()
            except Exception as e:
                self.logger.error('服务器错误:', str(e))
                db.session.rollback()
                return ResponseHelper.return_false_data(msg='Server Error',
                                                        status=500)
            return ResponseHelper.return_true_data('用户类型修改成功')
        else:
            return ResponseHelper.return_false_data(msg='权限不足', status=200)
Beispiel #4
0
    def user_register(self):
        """用户添加【被动】"""

        ret = {
            'current_identity': get_jwt_identity(),
            'current_type': get_jwt_claims()
        }
        if not ret['current_identity']:
            return ResponseHelper.return_false_data(msg='请登陆', status=200)
        user_id = ret.get('current_identity')[0]
        user_type = ret.get('current_type')
        if 1 == user_type:
            user_info = request.get_json()
            if not user_info:
                return ResponseHelper.return_false_data(msg='参数错误', status=200)
            user_name = user_info.get('user_name')
            password = user_info.get('password')
            name = user_info.get('name')
            # 验证完整性
            if not all([user_name, password, user_type]):
                return ResponseHelper.return_false_data(msg='账号密码不完整',
                                                        status=200)
            if not re.match(r'[a-z, A-Z]*\d*$', user_name):
                return ResponseHelper.return_false_data(msg='账号格式不正确',
                                                        status=200)
            try:
                user = User.query.filter_by(UserName=user_name).first()
            except Exception as e:
                self.logger.error('服务器错误:', str(e))
                return ResponseHelper.return_false_data(msg='Server Error',
                                                        status=500)
            if user:
                return ResponseHelper.return_false_data(msg='用户名已注册',
                                                        status=200)
            # 添加用户
            user = User(UserName=user_name,
                        Name=name,
                        CreatorId=user_id,
                        UserType=EnumUserType(int(user_type)))
            user.password = password
            try:
                db.session.add(user)
                db.session.commit()
            except Exception as e:
                self.logger.error('服务器错误:', str(e))
                db.session.rollback()
                return ResponseHelper.return_false_data(msg='Server Error',
                                                        status=500)
            user_data = {'username': user.UserName, 'name': user.Name}
            return ResponseHelper.return_true_data(data=user_data)
        else:
            return ResponseHelper.return_false_data(msg='权限不足', status=200)
Beispiel #5
0
    def add_user_by_admin(self):
        """管理员添加用户

            :doc 手动添加系统用户
        """
        try:
            identity = self.get_user()
            if not identity.get('userID'):
                return self.Response.return_false_data(msg='请登陆'), 401
            userID = identity.get('userID')
            identity_type = identity.get('usertype')
            if identity_type == EnumUserType.Admin.name:
                user_info = request.get_json()
                if not user_info:
                    return self.Response.return_false_data(msg='参数错误')
                username = user_info.get('userName')
                password = user_info.get('password', '123456')
                nickname = user_info.get('nickName', username)
                user_type = user_info.get('userType', 9)
                remark = user_info.get('remark')
                if not all([username, password, user_type]):
                    return self.Response.return_false_data(msg='新建账号信息不完整')
                if not re.match(r'[a-z, A-Z]*\d*$', username):
                    return self.Response.return_false_data(msg='账号格式不正确')
                user = User.query.filter_by(UserName=username).first()
                if user:
                    return self.Response.return_false_data(msg='用户名已注册')
                # ^添加用户
                user = User(UserName=username,
                            Name=nickname,
                            Remark=remark,
                            CreatorId=userID,
                            UserType=EnumUserType(int(user_type)).name)
                user.password = password
                # 自动添加头像
                user.AvatarUrl = user.generateAvatar()
                try:
                    self.db.session.add(user)
                    self.db.session.commit()
                except Exception as e:
                    self.logger.error('数据库错误:%s', str(e))
                    self.db.session.rollback()
                    return self.Response.return_server_error()
                data = {'username': username, 'nickname': nickname}
                return self.Response.return_true_data(data=data)
            else:
                return self.Response.return_false_data(msg='权限不足')
        except Exception as e:
            self.logger.error('服务器错误:%s', str(e))
            return self.Response.return_server_error()
Beispiel #6
0
 def register(self):
     """用户注册"""
     try:
         user_info = request.get_json()
         if not user_info:
             return self.Response.return_false_data(msg='参数错误')
         username = user_info.get('userName')
         password = user_info.get('password')
         user_type = user_info.get('userType', 9)
         if not all([username, password]):
             return self.Response.return_false_data(msg='账号密码不完整!')
         if not re.match(r'[a-z, A-Z]*\d*$', password):
             return self.Response.return_false_data(msg='密码格式不正确!')
         try:
             user = User.query.filter_by(UserName=username).first()
         except Exception as e:
             self.logger.error('Database Error:%s', str(e))
             return self.Response.return_server_error()
         if user:
             return self.Response.return_false_data(msg='用户名已注册!')
         # ^添加用户
         user = User(UserName=username,
                     Name=None,
                     UserType=EnumUserType(int(user_type)).name)
         user.password = password
         # 自动添加头像
         user.AvatarUrl = user.generateAvatar()
         try:
             self.db.session.add(user)
             self.db.session.commit()
         except Exception as e:
             self.logger.error('Database Error:%s', str(e))
             self.db.session.rollback()
             return self.Response.return_server_error()
         data = {
             'username': user.UserName,
             'nickName': user.Name,
             'userType': user.UserType.name
         }
         return self.Response.return_true_data(data=data)
     except Exception as e:
         self.logger.error('Server Error: %s', str(e))
         return self.Response.return_server_error()
Beispiel #7
0
    def register(self):
        """用户注册【主动】"""

        user_info = request.get_json()
        if not user_info:
            return ResponseHelper.return_false_data(msg='参数错误', status=200)
        user_name = user_info.get('user_name')
        password = user_info.get('password')
        name = user_info.get("name")
        user_type = user_info.get("user_type")
        if not all([user_name, password]):
            return ResponseHelper.return_false_data(msg='账号密码不完整', status=200)
        if not re.match(r'[a-z, A-Z]*\d*$', user_name):
            return ResponseHelper.return_false_data(msg='账号格式不正确', status=200)
        try:
            user = User.query.filter_by(UserName=user_name).first()
        except Exception as e:
            self.logger.error('服务器错误:', str(e))
            return ResponseHelper.return_false_data(msg='Server Error',
                                                    status=500)
        if user:
            return ResponseHelper.return_false_data(msg='用户名已注册', status=200)
        # 添加用户
        user = User(UserName=user_name,
                    Name=name,
                    UserType=EnumUserType(int(user_type)))
        user.password = password
        try:
            db.session.add(user)
            db.session.commit()
        except Exception as e:
            self.logger.error('服务器错误:', str(e))
            db.session.rollback()
            return ResponseHelper.return_false_data(msg='Server Error',
                                                    status=500)
        user_data = {'username': user.UserName, 'name': user.Name}
        return ResponseHelper.return_true_data(data=user_data)