コード例 #1
0
def register():
    """
    用户注册
    :return: json
    """

    reg_info = g.json_data
    username = reg_info['account']
    password = reg_info['password']

    password = generate_password_hash(password)

    # 获取用户level
    level = reg_info.get('level')
    level = UserLevelEnum.check_value_or_default(level)

    # 获取用户类型
    client_type = reg_info.get('client_type')
    client_type = ClientTypeEnum.check_value_or_default(client_type)

    u1 = User.get_by_account(username)

    if u1:
        usr = {'id': u1.id, 'name': u1.account}
        return HttpHelper.error_handler(ResultEnum.USER_ALREADY_EXIST_ERROR,
                                        data=usr)

    # 已经算出了加密密码,用户属性字典中去掉password属性,否则保存将覆盖
    usr = User(account=username,
               password=password,
               level=level,
               client_type=client_type)
    user_dict = del_keys(reg_info, ['password', 'client_type'])
    result = usr.save(**user_dict)

    if not usr.id:
        return HttpHelper.error_handler(ResultEnum.USER_REGISTER_ERROR,
                                        data=result)

    # 添加默认的角色
    default_role = AuthRole.get_default().first()
    auth_user_role = AuthUserRoles(role_id=default_role.id,
                                   user_id=usr.id,
                                   status=0)
    auth_user_role.save()

    ret_user = {'id': usr.id, 'name': usr.account, 'role': default_role}
    return HttpHelper.json_response(ResultHelper.success(ret_user))
コード例 #2
0
ファイル: role.py プロジェクト: coder-yuan/vue-template-api
def get_role(role_id):
    """
    获取Role详情信息
    :param role_id:
    :return:
    """
    role = AuthRole.get_by_status(role_id)
    if not role:
        return HttpHelper.error_handler(ResultEnum.NOT_FOUND_ERROR)

    return HttpHelper.normal_handler(
        model_to_dict(role, handle_relationship_flag=True))
コード例 #3
0
def change_password(user_id):
    usr = User.get_by_status_or_404(user_id)

    password = g.json_data.get('password', None)
    if not password:
        raise APIException(ResultEnum.USER_OR_PASS_EMPTY_ERROR)

    usr.password = generate_password_hash(password)

    try:
        usr.update()
        return HttpHelper.normal_handler(usr)
    except Exception as e:
        logger.error(e)
        return HttpHelper.error_handler(ResultEnum.UNKNOWN_ERROR)
コード例 #4
0
def bind_user_roles(user_id):
    json_data = g.json_data
    if user_id != json_data.get('user_id'):
        raise APIException(ResultEnum.USER_INVALID_ID)

    role_ids = json_data.get('roles')

    if not role_ids:
        raise APIException(ResultEnum.INVALID_PARAMETER)

    try:
        user = User.get_by_status_or_404(user_id)
        user.refresh_roles(role_ids)
        return HttpHelper.normal_handler()
    except Exception as e:
        logger.error(e)
        return HttpHelper.error_handler(ResultEnum.UNKNOWN_ERROR)
コード例 #5
0
ファイル: role.py プロジェクト: coder-yuan/vue-template-api
def bind_role_permissions(role_id):
    json_data = g.json_data
    if role_id != json_data.get('role_id'):
        raise APIException(ResultEnum.ROLE_INVALID_ID)

    permission_ids = json_data.get('permissions')

    if not permission_ids:
        raise APIException(ResultEnum.INVALID_PARAMETER)

    try:
        role = AuthRole.get_by_status_or_404(role_id)
        role.refresh_permissions(permission_ids)
        return HttpHelper.normal_handler()
    except Exception as e:
        logger.error(e)
        return HttpHelper.error_handler(ResultEnum.UNKNOWN_ERROR, e.args)
コード例 #6
0
def file_upload():
    """
    文件上传,支持单文件和多文件
    1、 单文件上传,body中必须以file指定文件
    2、 多文件上传,body中必须以files指定文件列表
    :return:  成功、错误 json文件
    """

    # 如果是通过file关键字进行的文件上传,即使多个也只处理最后一个
    if 'file' in request.files:

        file = request.files.get('file')
        if file:
            succ, data = process_single_file(file)
            if succ:
                return HttpHelper.normal_handler(data)
            else:
                return HttpHelper.error_handler(data)

    else:
        errors = {}
        success = {}
        files = request.files.getlist('files')

        # 不是file,也不是files参数,抛异常退出
        if not files:
            raise APIException(ResultEnum.FILE_UPLOAD_METHOD_ERROR)

        for file in files:
            succ, data = process_single_file(file)
            if succ:
                success[file.filename] = data
            else:
                errors[file.filename] = data.msg

        data = {'success': success, 'errors': errors}
        return HttpHelper.normal_handler(data)
コード例 #7
0
def need_refresh_token(callback):
    return HttpHelper.error_handler(ResultEnum.TOKEN_NEED_REFRESH)
コード例 #8
0
def expired_token(callback):
    return HttpHelper.error_handler(ResultEnum.TOKEN_EXPIRED_ERROR)
コード例 #9
0
def unauthorized_response(callback):
    return HttpHelper.error_handler(ResultEnum.TOKEN_VALIDATE_ERROR)