Example #1
0
    def create_one(
        self,
        user: User,
        workspace: Workspace,
        role_level: int,
        with_notif: bool,
        flush: bool=True
    ) -> UserRoleInWorkspace:

        # INFO - G.M - 2018-10-29 - Check if role already exist
        query = self._get_one_rsc(user.user_id, workspace.workspace_id)
        if query.count() > 0:
            raise RoleAlreadyExistError(
                'Role already exist for user {} in workspace {}.'.format(
                    user.user_id,
                    workspace.workspace_id
                )
            )
        role = UserRoleInWorkspace()
        role.user_id = user.user_id
        role.workspace = workspace
        role.role = role_level
        role.do_notify = with_notif
        if flush:
            self._session.flush()
        return role
Example #2
0
    def test_unit__crud_caller__ok__user_role_in_workspace(self, session):

        hook = UserRoleInWorkspaceHookImpl()
        session.context.plugin_manager.register(hook)

        owner = User(email="john")
        workspace = Workspace(label="Hello", owner=owner)
        session.add(workspace)
        session.flush()

        role = UserRoleInWorkspace(role=UserRoleInWorkspace.READER,
                                   user=owner,
                                   workspace=workspace)
        session.add(role)
        session.flush()
        hook.mock_hooks.assert_called_with("created",
                                           role=role,
                                           context=session.context)

        role.role = UserRoleInWorkspace.WORKSPACE_MANAGER
        session.add(role)
        session.flush()
        hook.mock_hooks.assert_called_with("modified",
                                           role=role,
                                           context=session.context)

        session.delete(role)
        session.flush()
        hook.mock_hooks.assert_called_with("deleted",
                                           role=role,
                                           context=session.context)
Example #3
0
    def update_role(
        self,
        role: UserRoleInWorkspace,
        role_level: int,
        with_notif: typing.Optional[bool] = None,
        save_now: bool = False,
    ):
        """
        Update role of user in this workspace
        :param role: UserRoleInWorkspace object
        :param role_level: level of new role wanted
        :param with_notif: is user notification enabled in this workspace ?
        :param save_now: database flush
        :return: updated role
        """
        role.role = role_level
        if with_notif is not None:
            role.do_notify = with_notif
        if save_now:
            self.save(role)

        return role
Example #4
0
    def update_role(
        self,
        role: UserRoleInWorkspace,
        role_level: int,
        with_notif: typing.Optional[bool] = None,
        save_now: bool=False,
    ):
        """
        Update role of user in this workspace
        :param role: UserRoleInWorkspace object
        :param role_level: level of new role wanted
        :param with_notif: is user notification enabled in this workspace ?
        :param save_now: database flush
        :return: updated role
        """
        role.role = role_level
        if with_notif is not None:
            role.do_notify = with_notif
        if save_now:
            self.save(role)

        return role