示例#1
0
def update_setting(user_id: int, resource: str, data: dict):
    """
    HTTP `PUT`/`PATCH` route for update a single user setting resource.

    Args:
        user_id (int): Public ID of the user.
        resource (str): Identifier/Name of the user setting.
        data (UserModel.SCHEMA): New setting data to update.

    Raises:
        ManagerGetError: When the setting with the `identifier` was not found.
        ManagerUpdateError: When something went wrong during the update.

    Returns:
        UpdateSingleResponse: With update result of the new updated user setting.
    """
    settings_manager: UserSettingsManager = UserSettingsManager(
        database_manager=current_app.database_manager)
    try:
        setting = UserSettingModel.from_data(data=data)
        settings_manager.update(user_id=user_id,
                                resource=resource,
                                setting=setting)
        api_response = UpdateSingleResponse(result=data,
                                            url=request.url,
                                            model=UserSettingModel.MODEL)
    except ManagerGetError as err:
        return abort(404, err.message)
    except ManagerUpdateError as err:
        return abort(400, err.message)

    return api_response.make_response()
def update_category(public_id: int, data: dict):
    """
    HTTP `PUT`/`PATCH` route for update a single category resource.

    Args:
        public_id (int): Public ID of the updatable category
        data (CategoryModel.SCHEMA): New category data to update

    Raises:
        ManagerGetError: When the category with the `public_id` was not found.
        ManagerUpdateError: When something went wrong during the update.

    Returns:
        UpdateSingleResponse: With update result of the new updated category.
    """
    category_manager: CategoryManager = CategoryManager(
        database_manager=current_app.database_manager)
    try:
        category = CategoryModel.from_data(data=data)
        category_manager.update(public_id=PublicID(public_id),
                                category=CategoryModel.to_json(category))
        api_response = UpdateSingleResponse(result=data,
                                            url=request.url,
                                            model=CategoryModel.MODEL)
    except ManagerGetError as err:
        return abort(404, err.message)
    except ManagerUpdateError as err:
        return abort(400, err.message)

    return api_response.make_response()
示例#3
0
def update_group(public_id: int, data: dict):
    """
    HTTP `PUT`/`PATCH` route for update a single group resource.

    Args:
        public_id (int): Public ID of the updatable group.
        data (UserGroupModel.SCHEMA): New group data to update.

    Raises:
        ManagerGetError: When the group with the `public_id` was not found.
        ManagerUpdateError: When something went wrong during the update.

    Returns:
        UpdateSingleResponse: With update result of the new updated group.
    """
    group_manager: GroupManager = GroupManager(database_manager=current_app.database_manager,
                                               right_manager=RightManager(rights))
    try:
        group = UserGroupModel.from_data(data=data, rights=RightManager(rights).rights)
        group_dict = UserGroupModel.to_dict(group)
        group_dict['rights'] = [right.get('name') for right in group_dict.get('rights', [])]
        group_manager.update(public_id=PublicID(public_id), group=group_dict)
        api_response = UpdateSingleResponse(result=group_dict, url=request.url,
                                            model=UserGroupModel.MODEL)
    except ManagerGetError as err:
        return abort(404, err.message)
    except ManagerUpdateError as err:
        return abort(400, err.message)
    return api_response.make_response()
示例#4
0
def change_user_password(public_id: int):
    """
    HTTP `PATCH` route for updating a single user password.

    Args:
        public_id (int): Public ID of the user.

    Raises:
        ManagerGetError: When the user with the `public_id` was not found.
        ManagerUpdateError: When something went wrong during the updated.

    Returns:
        UpdateSingleResponse: User with new password
    """
    user_manager: UserManager = UserManager(
        database_manager=current_app.database_manager)
    security_manager: SecurityManager = SecurityManager(
        database_manager=current_app.database_manager)
    try:
        user = user_manager.get(public_id=public_id)
        password = security_manager.generate_hmac(request.json.get('password'))
        user.password = password
        user_manager.update(public_id=PublicID(public_id), user=user)
        api_response = UpdateSingleResponse(result=UserModel.to_dict(user),
                                            url=request.url,
                                            model=UserModel.MODEL)
    except ManagerGetError as err:
        return abort(404, err.message)
    except ManagerUpdateError as err:
        return abort(400, err.message)

    return api_response.make_response()
示例#5
0
def update_user(public_id: int, data: dict):
    """
    HTTP `PUT`/`PATCH` route for update a single user resource.

    Args:
        public_id (int): Public ID of the updatable user.
        data (UserModel.SCHEMA): New user data to update.

    Raises:
        ManagerGetError: When the user with the `public_id` was not found.
        ManagerUpdateError: When something went wrong during the update.

    Returns:
        UpdateSingleResponse: With update result of the new updated user.
    """
    user_manager: UserManager = UserManager(
        database_manager=current_app.database_manager)
    try:
        user = UserModel.from_data(data=data)
        user_manager.update(public_id=PublicID(public_id), user=user)
        api_response = UpdateSingleResponse(result=UserModel.to_dict(user),
                                            url=request.url,
                                            model=UserModel.MODEL)
    except ManagerGetError as err:
        return abort(404, err.message)
    except ManagerUpdateError as err:
        return abort(400, err.message)
    return api_response.make_response()
示例#6
0
def update_object_state(public_id: int, request_user: UserModel):
    if isinstance(request.json, bool):
        state = request.json
    else:
        return abort(400)
    try:
        manager = ObjectManager(database_manager=current_app.database_manager,
                                event_queue=current_app.event_queue)
        founded_object = manager.get(public_id=public_id,
                                     user=request_user,
                                     permission=AccessControlPermission.READ)
    except ObjectManagerGetError as err:
        LOGGER.error(err)
        return abort(404, err.message)
    if founded_object.active == state:
        return make_response(False, 204)
    try:
        founded_object.active = state
        manager.update(public_id,
                       founded_object,
                       user=request_user,
                       permission=AccessControlPermission.UPDATE)
    except AccessDeniedError as err:
        return abort(403, err.message)
    except ObjectManagerUpdateError as err:
        LOGGER.error(err)
        return abort(500, err.message)

        # get current object state
    try:
        current_type_instance = type_manager.get(founded_object.get_type_id())
        current_object_render_result = CmdbRender(
            object_instance=founded_object,
            object_manager=object_manager,
            type_instance=current_type_instance,
            render_user=request_user).result()
    except ObjectManagerGetError as err:
        LOGGER.error(err)
        return abort(404)
    except RenderError as err:
        LOGGER.error(err)
        return abort(500)
    try:
        # generate log
        change = {'old': not state, 'new': state}
        log_data = {
            'object_id':
            public_id,
            'version':
            founded_object.version,
            'user_id':
            request_user.get_public_id(),
            'user_name':
            request_user.get_display_name(),
            'render_state':
            json.dumps(current_object_render_result,
                       default=default).encode('UTF-8'),
            'comment':
            'Active status has changed',
            'changes':
            change,
        }
        log_manager.insert(action=LogAction.ACTIVE_CHANGE,
                           log_type=CmdbObjectLog.__name__,
                           **log_data)
    except (CMDBError, LogManagerInsertError) as err:
        LOGGER.error(err)

    api_response = UpdateSingleResponse(result=founded_object.__dict__,
                                        url=request.url,
                                        model=CmdbObject.MODEL)
    return api_response.make_response()