Esempio n. 1
0
    def workspaces(self, context, request: TracimRequest, hapic_data=None):
        """
        Returns the list of all workspaces. This route is for admin only.
        Standard users must use their own route: /api/v2/users/me/workspaces
        """
        app_config = request.registry.settings['CFG']
        wapi = WorkspaceApi(
            current_user=request.current_user,  # User
            session=request.dbsession,
            config=app_config,
        )

        workspaces = wapi.get_all()
        return [
            wapi.get_workspace_with_context(workspace)
            for workspace in workspaces
        ]
Esempio n. 2
0
    def workspaces(self, context, request: TracimRequest, hapic_data=None):
        """
        Returns the list of all workspaces. This route is for admin only.
        Standard users must use their own route: /api/v2/users/me/workspaces
        """
        app_config = request.registry.settings['CFG']
        wapi = WorkspaceApi(
            current_user=request.current_user,  # User
            session=request.dbsession,
            config=app_config,
        )

        workspaces = wapi.get_all()
        return [
            wapi.get_workspace_with_context(workspace)
            for workspace in workspaces
        ]
Esempio n. 3
0
class CaldavSyncCommand(AppContextCommand):
    def get_description(self) -> str:
        return "synchronize tracim with radicale"

    def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
        parser = super().get_parser(prog_name)
        return parser

    def take_app_action(self, parsed_args: argparse.Namespace,
                        app_context: AppEnvironment) -> None:
        # TODO - G.M - 05-04-2018 -Refactor this in order
        # to not setup object var outside of __init__ .
        self._session = app_context["request"].dbsession
        self._app_config = app_context["registry"].settings["CFG"]
        self._user_api = UserApi(current_user=None,
                                 session=self._session,
                                 config=self._app_config)
        self._workspace_api = WorkspaceApi(current_user=None,
                                           force_role=True,
                                           session=self._session,
                                           config=self._app_config)
        self._agenda_api = AgendaApi(current_user=None,
                                     session=self._session,
                                     config=self._app_config)

        # INFO - G.M - 2019-03-13 - check users agendas
        users = self._user_api.get_all()
        nb_error_agenda_access = 0
        for user in users:
            try:
                already_exist = self._agenda_api.ensure_user_agenda_exists(
                    user)
                if not already_exist:
                    print("New created agenda for user {}".format(user))
            except CannotCreateAgenda as exc:
                nb_error_agenda_access += 1
                print("Cannot create agenda for user {}".format(user.user_id))
                logger.exception(self, exc)
            except AgendaServerConnectionError as exc:
                nb_error_agenda_access += 1
                print("Cannot access to agenda server: connection error.")
                logger.exception(self, exc)
            except Exception as exc:
                nb_error_agenda_access += 1
                print("Something goes wrong during agenda create/update")
                logger.exception(self, exc)
        nb_user_agendas = len(users)
        nb_verified_user_agenda = len(users) - nb_error_agenda_access
        print("{}/{} users agenda verified".format(nb_verified_user_agenda,
                                                   nb_user_agendas))

        # # INFO - G.M - 2019-03-13 - check workspaces agendas
        workspaces = self._workspace_api.get_all()
        nb_error_agenda_access = 0
        nb_workspaces = 0
        nb_agenda_enabled_workspace = 0
        for workspace in workspaces:
            nb_workspaces += 1
            if workspace.agenda_enabled:
                nb_agenda_enabled_workspace += 1
                try:
                    already_exist = self._agenda_api.ensure_workspace_agenda_exists(
                        workspace)
                    if not already_exist:
                        print("New created agenda for workspace {}".format(
                            workspace.workspace_id))
                except CannotCreateAgenda as exc:
                    print("Cannot create agenda for workspace {}".format(
                        workspace.workspace_id))
                    logger.exception(self, exc)
                except AgendaServerConnectionError as exc:
                    nb_error_agenda_access += 1
                    print("Cannot access to agenda server: connection error.")
                    logger.exception(self, exc)
        nb_verified_workspace_agenda = nb_agenda_enabled_workspace - nb_error_agenda_access
        nb_workspace_without_agenda_enabled = nb_workspaces - nb_agenda_enabled_workspace
        print(
            "{}/{} workspace agenda verified ({} workspace without agenda feature enabled)"
            .format(
                nb_verified_workspace_agenda,
                nb_agenda_enabled_workspace,
                nb_workspace_without_agenda_enabled,
            ))
Esempio n. 4
0
class RootResource(DAVCollection):
    """
    RootResource ressource that represents tracim's home, which contains all workspaces
    """
    def __init__(self, path: str, environ: dict,
                 tracim_context: "WebdavTracimContext"):
        super(RootResource, self).__init__(path, environ)
        self.tracim_context = tracim_context
        self.user = tracim_context.current_user
        self.session = tracim_context.dbsession
        # TODO BS 20170221: Web interface should list all workspace to. We
        # disable it here for moment. When web interface will be updated to
        # list all workspace, change this here to.
        self.workspace_api = WorkspaceApi(
            current_user=self.user,
            session=self.session,
            force_role=True,
            config=tracim_context.app_config,
        )

    def __repr__(self) -> str:
        return "<DAVCollection: RootResource>"

    @webdav_check_right(is_user)
    def getMemberNames(self) -> [str]:
        """
        This method returns the names (here workspace's labels) of all its children

        Though for perfomance issue, we're not using this function anymore
        """
        members_names = []
        for workspace in self.workspace_api.get_all():
            if webdav_convert_file_name_to_display(
                    workspace.label) in members_names:
                label = "{workspace_label}~~{workspace_id}".format(
                    workspace_label=workspace.label,
                    workspace_id=workspace.workspace_id)
            else:
                label = workspace.label
            members_names.append(webdav_convert_file_name_to_display(label))

    @webdav_check_right(is_user)
    def getMember(self, label: str) -> DAVCollection:
        """
        This method returns the child Workspace that corresponds to a given name

        Though for perfomance issue, we're not using this function anymore
        """
        try:
            workspace = self.workspace_api.get_one_by_label(label)
            # fix path
            workspace_path = "%s%s%s" % (
                self.path,
                "" if self.path == "/" else "/",
                webdav_convert_file_name_to_display(workspace.label),
            )
            # return item
            return WorkspaceResource(
                path=workspace_path,
                environ=self.environ,
                workspace=workspace,
                tracim_context=self.tracim_context,
                label=workspace.label,
            )
        except AttributeError:
            return None

    @webdav_check_right(is_trusted_user)
    def createEmptyResource(self, name: str):
        """
        This method is called whenever the user wants to create a DAVNonCollection resource (files in our case).

        There we don't allow to create files at the root;
        only workspaces (thus collection) can be created.
        """
        raise DAVError(HTTP_FORBIDDEN,
                       contextinfo="Not allowed to create new root")

    @webdav_check_right(is_trusted_user)
    def createCollection(self, name: str):
        """
        This method is called whenever the user wants to create a DAVCollection resource as a child (in our case,
        we create workspaces as this is the root).

        [For now] we don't allow to create new workspaces through
        webdav client. Though if we come to allow it, deleting the error's raise will
        make it possible.
        """
        # TODO : remove comment here
        # raise DAVError(HTTP_FORBIDDEN)
        workspace_name = webdav_convert_file_name_to_bdd(name)
        try:
            new_workspace = self.workspace_api.create_workspace(workspace_name)
        except (UserNotAllowedToCreateMoreWorkspace,
                EmptyLabelNotAllowed) as exc:
            raise DAVError(HTTP_FORBIDDEN, contextinfo=str(exc))
        self.workspace_api.save(new_workspace)
        self.workspace_api.execute_created_workspace_actions(new_workspace)
        transaction.commit()
        # fix path
        workspace_path = "%s%s%s" % (
            self.path,
            "" if self.path == "/" else "/",
            webdav_convert_file_name_to_display(new_workspace.label),
        )

        # create item
        return WorkspaceResource(
            path=workspace_path,
            environ=self.environ,
            workspace=new_workspace,
            tracim_context=self.tracim_context,
            label=new_workspace.label,
        )

    @webdav_check_right(is_user)
    def getMemberList(self):
        """
        This method is called by wsgidav when requesting with a depth > 0, it will return a list of _DAVResource
        of all its direct children
        """

        members = []
        members_names = []
        for workspace in self.workspace_api.get_all():
            if webdav_convert_file_name_to_display(
                    workspace.label) in members_names:
                label = "{workspace_label}~~{workspace_id}".format(
                    workspace_label=workspace.label,
                    workspace_id=workspace.workspace_id)
            else:
                label = workspace.label
            # fix path
            workspace_label = webdav_convert_file_name_to_display(label)
            path = add_trailing_slash(self.path)
            # return item
            workspace_path = "{}{}".format(path, workspace_label)
            members.append(
                WorkspaceResource(
                    path=workspace_path,
                    environ=self.environ,
                    workspace=workspace,
                    tracim_context=self.tracim_context,
                    label=label,
                ))
            members_names.append(workspace_label)

        return members
Esempio n. 5
0
class RootResource(DAVCollection):
    """
    RootResource ressource that represents tracim's home, which contains all workspaces
    """

    def __init__(self, path: str, environ: dict, tracim_context: 'WebdavTracimContext'):
        super(RootResource, self).__init__(path, environ)
        self.tracim_context = tracim_context
        self.user = tracim_context.current_user
        self.session = tracim_context.dbsession
        # TODO BS 20170221: Web interface should list all workspace to. We
        # disable it here for moment. When web interface will be updated to
        # list all workspace, change this here to.
        self.workspace_api = WorkspaceApi(
            current_user=self.user,
            session=self.session,
            force_role=True,
            config=tracim_context.app_config
        )

    def __repr__(self) -> str:
        return '<DAVCollection: RootResource>'

    @webdav_check_right(is_user)
    def getMemberNames(self) -> [str]:
        """
        This method returns the names (here workspace's labels) of all its children

        Though for perfomance issue, we're not using this function anymore
        """
        return [
            webdav_convert_file_name_to_display(workspace.label)
            for workspace in self.workspace_api.get_all()
        ]

    @webdav_check_right(is_user)
    def getMember(self, label: str) -> DAVCollection:
        """
        This method returns the child Workspace that corresponds to a given name

        Though for perfomance issue, we're not using this function anymore
        """
        try:
            workspace = self.workspace_api.get_one_by_label(label)
            # fix path
            workspace_path = '%s%s%s' % (self.path, '' if self.path == '/' else '/', webdav_convert_file_name_to_display(workspace.label))
            # return item
            return WorkspaceResource(
                workspace_path,
                self.environ,
                workspace,
                tracim_context=self.tracim_context
            )
        except AttributeError:
            return None

    @webdav_check_right(is_trusted_user)
    def createEmptyResource(self, name: str):
        """
        This method is called whenever the user wants to create a DAVNonCollection resource (files in our case).

        There we don't allow to create files at the root;
        only workspaces (thus collection) can be created.
        """
        raise DAVError(HTTP_FORBIDDEN)

    @webdav_check_right(is_trusted_user)
    def createCollection(self, name: str):
        """
        This method is called whenever the user wants to create a DAVCollection resource as a child (in our case,
        we create workspaces as this is the root).

        [For now] we don't allow to create new workspaces through
        webdav client. Though if we come to allow it, deleting the error's raise will
        make it possible.
        """
        # TODO : remove comment here
        # raise DAVError(HTTP_FORBIDDEN)
        workspace_name = webdav_convert_file_name_to_bdd(name)
        new_workspace = self.workspace_api.create_workspace(workspace_name)
        self.workspace_api.save(new_workspace)
        transaction.commit()
        # fix path
        workspace_path = '%s%s%s' % (
            self.path, '' if self.path == '/' else '/', webdav_convert_file_name_to_display(new_workspace.label)
        )

        # create item
        return WorkspaceResource(
            workspace_path,
            self.environ,
            new_workspace,
            tracim_context=self.tracim_context
        )

    @webdav_check_right(is_user)
    def getMemberList(self):
        """
        This method is called by wsgidav when requesting with a depth > 0, it will return a list of _DAVResource
        of all its direct children
        """

        members = []
        for workspace in self.workspace_api.get_all():
            # fix path
            workspace_label = webdav_convert_file_name_to_display(workspace.label)
            path = add_trailing_slash(self.path)
            # return item
            workspace_path = '{}{}'.format(path, workspace_label)
            members.append(
                WorkspaceResource(
                    path=workspace_path,
                    environ=self.environ,
                    workspace=workspace,
                    tracim_context=self.tracim_context
                )
            )

        return members
Esempio n. 6
0
class RootResource(DAVCollection):
    """
    RootResource ressource that represents tracim's home, which contains all workspaces
    """
    def __init__(self, path: str, environ: dict,
                 tracim_context: 'WebdavTracimContext'):
        super(RootResource, self).__init__(path, environ)
        self.tracim_context = tracim_context
        self.user = tracim_context.current_user
        self.session = tracim_context.dbsession
        # TODO BS 20170221: Web interface should list all workspace to. We
        # disable it here for moment. When web interface will be updated to
        # list all workspace, change this here to.
        self.workspace_api = WorkspaceApi(current_user=self.user,
                                          session=self.session,
                                          force_role=True,
                                          config=tracim_context.app_config)

    def __repr__(self) -> str:
        return '<DAVCollection: RootResource>'

    @webdav_check_right(is_user)
    def getMemberNames(self) -> [str]:
        """
        This method returns the names (here workspace's labels) of all its children

        Though for perfomance issue, we're not using this function anymore
        """
        return [workspace.label for workspace in self.workspace_api.get_all()]

    @webdav_check_right(is_user)
    def getMember(self, label: str) -> DAVCollection:
        """
        This method returns the child Workspace that corresponds to a given name

        Though for perfomance issue, we're not using this function anymore
        """
        try:
            workspace = self.workspace_api.get_one_by_label(label)
            workspace_path = '%s%s%s' % (
                self.path, '' if self.path == '/' else '/',
                webdav_convert_file_name_to_display(workspace.label))

            return WorkspaceResource(workspace_path,
                                     self.environ,
                                     workspace,
                                     tracim_context=self.tracim_context)
        except AttributeError:
            return None

    @webdav_check_right(is_trusted_user)
    def createEmptyResource(self, name: str):
        """
        This method is called whenever the user wants to create a DAVNonCollection resource (files in our case).

        There we don't allow to create files at the root;
        only workspaces (thus collection) can be created.
        """
        raise DAVError(HTTP_FORBIDDEN)

    @webdav_check_right(is_trusted_user)
    def createCollection(self, name: str):
        """
        This method is called whenever the user wants to create a DAVCollection resource as a child (in our case,
        we create workspaces as this is the root).

        [For now] we don't allow to create new workspaces through
        webdav client. Though if we come to allow it, deleting the error's raise will
        make it possible.
        """
        # TODO : remove comment here
        # raise DAVError(HTTP_FORBIDDEN)

        new_workspace = self.workspace_api.create_workspace(name)
        self.workspace_api.save(new_workspace)

        workspace_path = '%s%s%s' % (
            self.path, '' if self.path == '/' else '/',
            webdav_convert_file_name_to_display(new_workspace.label))

        transaction.commit()
        return WorkspaceResource(workspace_path,
                                 self.environ,
                                 new_workspace,
                                 tracim_context=self.tracim_context)

    @webdav_check_right(is_user)
    def getMemberList(self):
        """
        This method is called by wsgidav when requesting with a depth > 0, it will return a list of _DAVResource
        of all its direct children
        """

        members = []
        for workspace in self.workspace_api.get_all():
            workspace_path = '%s%s%s' % (self.path, '' if self.path == '/' else
                                         '/', workspace.label)
            members.append(
                WorkspaceResource(path=workspace_path,
                                  environ=self.environ,
                                  workspace=workspace,
                                  tracim_context=self.tracim_context))

        return members