Beispiel #1
0
def login():
    request_data = request.get_json()
    email = request_data.get('email') if request_data else None
    pass_word = request_data.get('password') if request_data else None
    user = user_data_store.find_user(email=email)
    if not user:
        return jsonify({'status': 'failed', 'data': '当前用户不存在!'})
    if not user.is_active:
        return jsonify({'status': 'failed', 'data': '当前用户被禁用,请联系管理员!'})
    if user is not None and check_password_hash(user.password, pass_word):
        login_user(user, remember=True)
        roles_name = []
        for role in user.roles:
            if role and role.name not in roles_name:
                roles_name.append(role.name)
        token = generate_auth_token(email, roles_name)
        current_app.logger.info("login successfully. email: %s" % str(email))
        return jsonify({
            'status': 'ok',
            'data': {
                'email': email,
                'token': token.decode("ascii")
            }
        })
    else:
        return jsonify({'status': 'failed', 'data': '用户名 / 密码错误!'})
Beispiel #2
0
def get_user_roles(email):
    user = user_data_store.find_user(email=email)
    if user is not None:
        roles_name = []
        for role in user.roles:
            if role and role.name not in roles_name:
                roles_name.append(role.name)
        return jsonify({'status': 'ok', 'data': {'roles': roles_name}})
    else:
        return jsonify({'status': 'failed', 'data': '用户名 / 密码错误!'})
Beispiel #3
0
def update_user_status():
    try:
        data = request.get_json()
        user = user_data_store.find_user(email=data['email'])
        if data['active']:
            user_data_store.activate_user(user)
        else:
            user_data_store.deactivate_user(user)
        filtered_data = LeoUser.filter_field(data)
        update_response = LeoUser.update({'email': data['email']},
                                         {'$set': filtered_data})
        if update_response['n'] == 0:
            return jsonify({'status': 'failed', 'data': '未找到相应的更新数据!'})
        return jsonify({'status': 'ok', 'data': '变更用户状态成功'})
    except BaseException as e:
        current_app.logger.error("update_user_status failed. - %s" % str(e))
        return jsonify({'status': 'failed', 'data': '变更用户状态失败! %s' % e})
Beispiel #4
0
def get_user_list():
    total_num, users = common.get_total_num_and_arranged_data(
        LeoUser, request.args, fuzzy_fields=['email'])
    for user in users:
        user_data = user_data_store.find_user(email=user['email'])
        roles_name = []
        for role in user_data.roles:
            if role and role.name not in roles_name:
                roles_name.append(role.name)
        user['roleNames'] = roles_name
    return jsonify({
        'status': 'ok',
        'data': {
            'totalNum': total_num,
            'rows': users
        }
    })
Beispiel #5
0
def register():
    try:
        request_data = request.get_json()
        if query_user(request_data["email"]):
            return jsonify({'status': 'failed', 'data': '该邮箱已存在'})
        password_hash = generate_password_hash(request_data["password"])
        user_data_store.create_user(email=request_data['email'],
                                    password=password_hash,
                                    createAt=datetime.utcnow())
        user = user_data_store.find_user(email=request_data['email'])
        for role in request_data['roles']:
            user_data_store.add_role_to_user(user, role)
        current_app.logger.info("register user successfully. email: %s" %
                                str(request_data['email']))
        return jsonify({'status': 'ok', 'data': '注册成功'})
    except BaseException as e:
        current_app.logger.error("register user failed. - %s" % str(e))
        return jsonify({'status': 'failed', 'data': 'register failed %s' % e})
Beispiel #6
0
def change_projects(email):
    try:
        data = request.get_json()
        if "userProjects" not in data:
            return jsonify({'status': 'failed', 'data': '请输入用户Projects!'})
        email = data['email'] if data['email'] else email
        user = user_data_store.find_user(email=email)
        if user is not None:
            filtered_data = LeoUser.filter_field(data)
            update_response = LeoUser.update({'email': email},
                                             {'$set': filtered_data})
            if update_response['n'] == 0:
                return jsonify({'status': 'failed', 'data': '未找到要修改的用户!'})
            return jsonify({'status': 'ok', 'data': '变更用户项目成功: %s' % email})
        else:
            return jsonify({'status': 'failed', 'data': '未找到要修改的用户!'})
    except BaseException as e:
        current_app.logger.error("change_roles failed. - %s" % str(e))
        return jsonify({'status': 'failed', 'data': '变更用户项目失败! %s' % e})
Beispiel #7
0
def add_admin_user():
    try:
        (existed, data) = admin_user_existed()
        if existed:
            return jsonify({'status': 'ok', data: '"管理员用户已经存在, 邮箱为 %s' % data})
        create_roles()
        request_data = request.get_json()
        email = request_data['email']
        password = request_data['password']
        roles = ['admin', 'user', 'project']
        password_hash = generate_password_hash(password)
        user_data_store.create_user(email=email,
                                    password=password_hash,
                                    createAt=datetime.utcnow())
        user = user_data_store.find_user(email=email)
        for role in roles:
            user_data_store.add_role_to_user(user, role)
        return jsonify({'status': 'ok', 'data': '添加管理员用户成功'})
    except BaseException as e:
        current_app.logger.error("add_admin_user failed. - %s" % str(e))
        return jsonify({'status': 'failed', 'data': "出错了, Error: %s" % e})
Beispiel #8
0
def change_roles(email):
    try:
        data = request.get_json()
        email = data['email'] if data['email'] else email
        user = user_data_store.find_user(email=email)
        if user is not None:
            # 先remove所有权限
            current_roles_name = []
            for role in user.roles:
                if role and role.name not in current_roles_name:
                    current_roles_name.append(role.name)
            for role in current_roles_name:
                user_data_store.remove_role_from_user(user, role)
            for role_name in data['roleNames']:
                user_data_store.add_role_to_user(user, role_name)
            return jsonify({'status': 'ok', 'data': '变更权限成功'})
        else:
            return jsonify({'status': 'failed', 'data': '未找到要修改的用户!'})
    except BaseException as e:
        current_app.logger.error("change_roles failed. - %s" % str(e))
        return jsonify({'status': 'failed', 'data': '变更权限失败! %s' % e})