Exemple #1
0
    def patch(self, role_id):
        """
        """

        # To do check if user is admin

        role_schema = RoleSchema(partial=True)

        update_data = request.get_json()

        validated_update_data, errors = role_schema.load(update_data)

        if errors:
            return dict(status="fail", message=errors), 400

        role = Role.get_by_id(role_id)

        if not role:
            return dict(
                status="fail",
                message=f"Role with id {role_id} not found"
                ), 404

        if 'name' in validated_update_data:
            role.name = validated_update_data['name']

        updated_role = role.save()

        if not updated_role:
            return dict(status='fail', message='Internal Server Error'), 500

        return dict(
            status="success",
            message=f"Role {role.name} updated successfully"
            ), 200
Exemple #2
0
def role_update(id):
    edit_one_field = request.form.get('edit_one_field', None)
    if not edit_one_field:
        form = RoleEditForm(formdata=request.form, id=id)
    else:
        form = RoleEditOneKeyForm(formdata=request.form, id=id)
    if not form.validate():
        return Fail(message=form.first_error)
    role = Role.get_by_id(id=id)
    role.update(form.data, edit_one_field)

    return Success(message="操作成功!")
Exemple #3
0
    def post(self, user_id):
        """
        """

        user_role_schema = UserRoleSchema()

        user_role_data = request.get_json()

        validated_user_role_data, errors = user_role_schema.load(
            user_role_data)

        if errors:
            return dict(status='fail', message=errors), 400

        # Get User
        user = User.get_by_id(user_id)

        if not user:
            return dict(status='fail', message='User not found'), 404

        # Get role
        role = Role.get_by_id(validated_user_role_data.get('role_id', None))

        if not role:
            return dict(status='fail', message='Role not found'), 404

        if role in user.roles:
            return dict(status='fail', message='User already has role'), 409

        # adding role to user roles
        if role in user.roles:
            return dict(status='fail', message='Role already Exists'), 404

        user.roles.append(role)

        saved_user_role = user.save()

        user_schema = UserSchema()

        if not saved_user_role:
            return dict(status='fail', message='Internal Server Error'), 500

        new_user_role_data, errors = user_schema.dumps(user)

        return dict(status='success',
                    data=dict(user_role=json.loads(new_user_role_data))), 201
Exemple #4
0
    def delete(self, role_id):
        """
        """
        # To do get current user and check if the user is admin

        role = Role.get_by_id(role_id)

        if not role:
            return dict(
                status="fail",
                message=f"Role with id {role_id} not found"
                ), 404

        deleted_role = role.delete()

        if not deleted_role:
            return dict(status='fail', message='Internal Server Error'), 500

        return dict(status='success', message="Successfully deleted"), 200
Exemple #5
0
    def delete(self, user_id):
        """
        """
        user_role_schema = UserRoleSchema()

        user_role_data = request.get_json()

        validated_user_role_data, errors = user_role_schema.load(
            user_role_data)

        if errors:
            return dict(status='fail', message=errors), 400

        # Get User
        user = User.get_by_id(user_id)

        if not user:
            return dict(status='fail', message='User not found'), 404

        # Get role
        role = Role.get_by_id(validated_user_role_data.get('role_id', None))

        if not role:
            return dict(status='fail', message='Role not found'), 404

        # removing user from role
        try:
            user.roles.remove(role)
        except Exception:
            return dict(status='fail', message='User role not found'), 404

        saved_user_role = user.save()

        user_schema = UserSchema()

        if not saved_user_role:
            return dict(status='fail', message='Internal Server Error'), 500

        new_user_role_data, errors = user_schema.dumps(user)

        return dict(status='success',
                    data=dict(user_role=json.loads(new_user_role_data))), 201
Exemple #6
0
    def get(self, role_id):
        """
        """
        role_schema = RoleSchema()

        role = Role.get_by_id(role_id)

        if not role:
            return dict(
                status="fail",
                message=f"Role with id {role_id} not found"
                ), 404

        role_data, errors = role_schema.dumps(role)

        if errors:
            return dict(status="fail", message=errors), 500

        return dict(
            status='success',
            data=dict(role=json.loads(role_data))
            ), 200
Exemple #7
0
def role_edit(id):
    role = Role.get_by_id(id)
    menus = Role.get_menu_for_role()
    temp = []
    for menu in menus:
        menu.checkbox = False
        permissions = menu.permissions
        if permissions:
            for permission in permissions:
                if role in permission.roles:
                    menu.checkbox = True
                    break
        temp.append(menu)

    permission_ids = []
    if role.permissions:
        for permission in role.permissions:
            permission_ids.append(permission.id)

    return render_template('admin/role/edit.html',
                           role=role,
                           menus=temp,
                           permission_ids=permission_ids)