Exemple #1
0
    def add_current_user_as_member(self, workspace_id: int) -> Workspace:
        """
        Adds the current user as a member of the given workspace id.
        Only works for OPEN workspaces.

        Role is set to the workspace default role.
        """
        query = self._base_query_without_roles().filter(
            Workspace.workspace_id == workspace_id)
        query = query.filter(Workspace.access_type == WorkspaceAccessType.OPEN)
        try:
            workspace = query.one()
        except NoResultFound as exc:
            raise WorkspaceNotFound(
                "workspace {} does not exist or not visible for user".format(
                    workspace_id)) from exc
        rapi = RoleApi(
            current_user=self._user,
            session=self._session,
            config=self._config,
        )
        rapi.create_one(self._user,
                        workspace,
                        workspace.default_user_role.level,
                        with_notif=True)
        return workspace
Exemple #2
0
 def get_one_by_label(self, label: str) -> Workspace:
     try:
         return self._base_query().filter(Workspace.label == label).one()
     except NoResultFound as exc:
         raise WorkspaceNotFound(
             'workspace {} does not exist or not visible for user'.format(
                 id)) from exc  # nopep8
Exemple #3
0
 def _get_current_workspace(
         self,
         user: User,
         request: 'TracimRequest'
 ) -> Workspace:
     """
     Get current workspace from request
     :param user: User who want to check the workspace
     :param request: pyramid request
     :return: current workspace
     """
     workspace_id = ''
     try:
         if 'workspace_id' in request.matchdict:
             workspace_id_str = request.matchdict['workspace_id']
             if not isinstance(workspace_id_str, str) or not workspace_id_str.isdecimal():  # nopep8
                 raise InvalidWorkspaceId('workspace_id is not a correct integer')  # nopep8
             workspace_id = int(request.matchdict['workspace_id'])
         if not workspace_id:
             raise WorkspaceNotFoundInTracimRequest('No workspace_id property found in request')  # nopep8
         wapi = WorkspaceApi(
             current_user=user,
             session=request.dbsession,
             config=request.registry.settings['CFG'],
             show_deleted=True,
         )
         workspace = wapi.get_one(workspace_id)
     except NoResultFound as exc:
         raise WorkspaceNotFound(
             'Workspace {} does not exist '
             'or is not visible for this user'.format(workspace_id)
         ) from exc
     return workspace
Exemple #4
0
 def get_one(self, id):
     try:
         return self._base_query().filter(
             Workspace.workspace_id == id).one()
     except NoResultFound as exc:
         raise WorkspaceNotFound(
             'workspace {} does not exist or not visible for user'.format(
                 id)) from exc  # nopep8
Exemple #5
0
 def get_one(self, workspace_id: int) -> Workspace:
     query = self._base_query().filter(
         Workspace.workspace_id == workspace_id)
     try:
         return query.one()
     except NoResultFound as exc:
         raise WorkspaceNotFound(
             "workspace {} does not exist or not visible for user".format(
                 workspace_id)) from exc
Exemple #6
0
    def get_one_by_filemanager_filename(
            self,
            filemanager_filename: str,
            parent: typing.Optional[Workspace] = None) -> Workspace:
        """
        get workspace according to filemanager_filename given and parent, if multiple workspace have
        same filemanager_filename, return first one found.

        filemanager_filename is filename like version of workspace with specific extension '.space'
        """
        if not filemanager_filename.endswith(Workspace.FILEMANAGER_EXTENSION):
            raise WorkspaceNotFound(
                'Invalid Workspace name. Filemanager_filename should end with "{}"'
                .format(Workspace.FILEMANAGER_EXTENSION))
        label = filemanager_filename[:-len(Workspace.FILEMANAGER_EXTENSION)]
        query = self._base_query().filter(Workspace.label == label)
        if parent:
            query = query.filter(Workspace.parent_id == parent.workspace_id)
        else:
            rapi = RoleApi(session=self._session,
                           current_user=self._user,
                           config=self._config)
            workspace_ids = rapi.get_user_workspaces_ids(
                user_id=self._user.user_id,
                min_role=UserRoleInWorkspace.READER)
            query = query.filter(
                or_(
                    Workspace.parent_id == None,  # noqa: E711
                    Workspace.parent_id.notin_(workspace_ids),
                ))
        result = self.default_order_workspace(query).all()
        if len(result) == 0:
            raise WorkspaceNotFound(
                "workspace {} does not exist or not visible for user".format(
                    id))

        return result[0]
Exemple #7
0
 def _get_candidate_workspace(
         self,
         user: User,
         request: 'TracimRequest'
 ) -> Workspace:
     """
     Get current workspace from request
     :param user: User who want to check the workspace
     :param request: pyramid request
     :return: current workspace
     """
     workspace_id = ''
     try:
         if 'new_workspace_id' in request.json_body:
             workspace_id = request.json_body['new_workspace_id']
             if not isinstance(workspace_id, int):
                 if workspace_id.isdecimal():
                     workspace_id = int(workspace_id)
                 else:
                     raise InvalidWorkspaceId('workspace_id is not a correct integer')  # nopep8
         if not workspace_id:
             raise WorkspaceNotFoundInTracimRequest('No new_workspace_id property found in body')  # nopep8
         wapi = WorkspaceApi(
             current_user=user,
             session=request.dbsession,
             config=request.registry.settings['CFG'],
             show_deleted=True,
         )
         workspace = wapi.get_one(workspace_id)
     except JSONDecodeError as exc:
         raise WorkspaceNotFound('Invalid JSON content') from exc
     except NoResultFound as exc:
         raise WorkspaceNotFound(
             'Workspace {} does not exist '
             'or is not visible for this user'.format(workspace_id)
         ) from exc
     return workspace