Esempio n. 1
0
    def __init__(self, config=None, **kwargs):
        self.config = config or MockAppConfig(**kwargs)
        self.security = self.config.security
        self.name = kwargs.get('name', 'galaxy')
        self.object_store = objectstore.build_object_store_from_config(
            self.config)
        self.model = mapping.init("/tmp",
                                  self.config.database_connection,
                                  create_tables=True,
                                  object_store=self.object_store)
        self.security_agent = self.model.security_agent
        self.visualizations_registry = MockVisualizationsRegistry()
        self.tag_handler = tags.GalaxyTagHandler(self.model.context)
        self.quota_agent = quota.DatabaseQuotaAgent(self.model)
        self.init_datatypes()
        self.job_config = Bunch(dynamic_params=None, destinations={})
        self.tool_data_tables = {}
        self.dataset_collections_service = None
        self.container_finder = NullContainerFinder()
        self._toolbox_lock = MockLock()
        self.tool_shed_registry = Bunch(tool_sheds={})
        self.genome_builds = GenomeBuilds(self)
        self.job_manager = NoopManager()
        self.application_stack = ApplicationStack()
        self.auth_manager = AuthManager(self)
        self.user_manager = UserManager(self)
        self.execution_timer_factory = Bunch(
            get_timer=StructuredExecutionTimer)

        def url_for(*args, **kwds):
            return "/mock/url"

        self.url_for = url_for
Esempio n. 2
0
    def __init__(self, app):
        self.type_registry = DATASET_COLLECTION_TYPES_REGISTRY
        self.collection_type_descriptions = COLLECTION_TYPE_DESCRIPTION_FACTORY
        self.model = app.model
        self.security = app.security

        self.hda_manager = hdas.HDAManager(app)
        self.history_manager = histories.HistoryManager(app)
        self.tag_handler = tags.GalaxyTagHandler(app.model.context)
        self.ldda_manager = lddas.LDDAManager(app)
Esempio n. 3
0
    def show(self, trans, id, library_id, **kwd):
        """
        GET /api/libraries/{library_id}/contents/{id}

        Returns information about library file or folder.

        :param  id:         the encoded id of the library item to return
        :type   id:         str

        :param  library_id: the encoded id of the library that contains this item
        :type   library_id: str

        :returns:   detailed library item information
        :rtype:     dict

        .. seealso::
            :func:`galaxy.model.LibraryDataset.to_dict` and
            :attr:`galaxy.model.LibraryFolder.dict_element_visible_keys`
        """
        class_name, content_id = self._decode_library_content_id(id)
        if class_name == 'LibraryFolder':
            content = self.get_library_folder(trans,
                                              content_id,
                                              check_ownership=False,
                                              check_accessible=True)
            rval = content.to_dict(
                view='element', value_mapper={'id': trans.security.encode_id})
            rval['id'] = 'F' + str(rval['id'])
            if rval['parent_id'] is not None:  # This can happen for root folders.
                rval['parent_id'] = 'F' + str(
                    trans.security.encode_id(rval['parent_id']))
            rval['parent_library_id'] = trans.security.encode_id(
                rval['parent_library_id'])
        else:
            content = self.get_library_dataset(trans,
                                               content_id,
                                               check_ownership=False,
                                               check_accessible=True)
            rval = content.to_dict(view='element')
            rval['id'] = trans.security.encode_id(rval['id'])
            rval['ldda_id'] = trans.security.encode_id(rval['ldda_id'])
            rval['folder_id'] = 'F' + str(
                trans.security.encode_id(rval['folder_id']))
            rval['parent_library_id'] = trans.security.encode_id(
                rval['parent_library_id'])

            tag_manager = tags.GalaxyTagHandler(trans.sa_session)
            rval['tags'] = tag_manager.get_tags_str(
                content.library_dataset_dataset_association.tags)
        return rval
Esempio n. 4
0
    def __init__(self, config=None, **kwargs):
        super().__init__()
        self[BasicApp] = self
        self[MinimalManagerApp] = self
        self[StructuredApp] = self
        self.config = config or MockAppConfig(**kwargs)
        self.security = self.config.security
        self[idencoding.IdEncodingHelper] = self.security
        self.name = kwargs.get('name', 'galaxy')
        self.object_store = objectstore.build_object_store_from_config(
            self.config)
        self.model = mapping.init("/tmp",
                                  self.config.database_connection,
                                  create_tables=True,
                                  object_store=self.object_store)
        self[SharedModelMapping] = self.model
        self[GalaxyModelMapping] = self.model
        self[scoped_session] = self.model.context
        self.security_agent = self.model.security_agent
        self.visualizations_registry = MockVisualizationsRegistry()
        self.tag_handler = tags.GalaxyTagHandler(self.model.context)
        self[tags.GalaxyTagHandler] = self.tag_handler
        self.quota_agent = quota.DatabaseQuotaAgent(self.model)
        self.init_datatypes()
        self.job_config = Bunch(dynamic_params=None,
                                destinations={},
                                use_messaging=False,
                                assign_handler=lambda *args, **kwargs: None)
        self.tool_data_tables = {}
        self.dataset_collections_service = None
        self.container_finder = NullContainerFinder()
        self._toolbox_lock = MockLock()
        self.tool_shed_registry = Bunch(tool_sheds={})
        self.genome_builds = GenomeBuilds(self)
        self.job_manager = NoopManager()
        self.application_stack = ApplicationStack()
        self.auth_manager = AuthManager(self.config)
        self.user_manager = UserManager(self)
        self.execution_timer_factory = Bunch(
            get_timer=StructuredExecutionTimer)
        self.is_job_handler = False
        rebind_container_to_task(self)

        def url_for(*args, **kwds):
            return "/mock/url"

        self.url_for = url_for
Esempio n. 5
0
    def __init__(self, config=None, **kwargs):
        super().__init__()
        config = config or MockAppConfig(**kwargs)
        GalaxyDataTestApp.__init__(self, config=config, **kwargs)
        self[BasicSharedApp] = self
        self[MinimalManagerApp] = self
        self[StructuredApp] = self
        self[idencoding.IdEncodingHelper] = self.security
        self.name = kwargs.get('name', 'galaxy')
        self[SharedModelMapping] = self.model
        self[GalaxyModelMapping] = self.model
        self[galaxy_scoped_session] = self.model.context
        self.visualizations_registry = MockVisualizationsRegistry()
        self.tag_handler = tags.GalaxyTagHandler(self.model.context)
        self[tags.GalaxyTagHandler] = self.tag_handler
        self.quota_agent = quota.DatabaseQuotaAgent(self.model)
        self.job_config = Bunch(
            dynamic_params=None,
            destinations={},
            use_messaging=False,
            assign_handler=lambda *args, **kwargs: None
        )
        self.tool_data_tables = ToolDataTableManager(tool_data_path=self.config.tool_data_path)
        self.dataset_collections_service = None
        self.container_finder = NullContainerFinder()
        self._toolbox_lock = MockLock()
        self.tool_shed_registry = Bunch(tool_sheds={})
        self.genome_builds = GenomeBuilds(self)
        self.job_manager = NoopManager()
        self.application_stack = ApplicationStack()
        self.auth_manager = AuthManager(self.config)
        self.user_manager = UserManager(self)
        self.execution_timer_factory = Bunch(get_timer=StructuredExecutionTimer)
        self.file_sources = Bunch(to_dict=lambda *args, **kwargs: {})
        self.interactivetool_manager = Bunch(create_interactivetool=lambda *args, **kwargs: None)
        self.is_job_handler = False
        self.biotools_metadata_source = None
        rebind_container_to_task(self)

        def url_for(*args, **kwds):
            return "/mock/url"
        self.url_for = url_for
Esempio n. 6
0
    def index(self,
              trans,
              folder_id,
              limit=None,
              offset=None,
              search_text=None,
              include_deleted=False):
        """
        Displays a collection (list) of a folder's contents (files and folders). Encoded folder ID is prepended
        with 'F' if it is a folder as opposed to a data set which does not have it. Full path is provided in
        response as a separate object providing data for breadcrumb path building.

        ..example:
            limit and offset can be combined. Skip the first two and return five:
                '?offset=2&limit=5'

        :param  folder_id: encoded ID of the folder which contents should be library_dataset_dict
        :type   folder_id: encoded string

        :param  offset: number of folder contents to skip
        :type   offset: optional int

        :param  limit: maximum number of folder contents to return
        :type   limit: optional int

        :param  include_deleted: whether to include deleted items in the results
        :type   include_deleted: optional bool (default False)

        :returns: dictionary containing all items and metadata
        :type:    dict

        :raises: MalformedId, InconsistentDatabase, ObjectNotFound,
             InternalServerError
        """
        is_admin = trans.user_is_admin
        current_user_roles = trans.get_current_user_roles()

        decoded_folder_id = self.folder_manager.cut_and_decode(
            trans, folder_id)
        folder = self.folder_manager.get(trans, decoded_folder_id)

        # Special level of security on top of libraries.
        if trans.app.security_agent.can_access_library(
                current_user_roles, folder.parent_library) or is_admin:
            pass
        else:
            if trans.user:
                log.warning(
                    f"SECURITY: User (id: {trans.user.id}) without proper access rights is trying to load folder with ID of {decoded_folder_id}"
                )
            else:
                log.warning(
                    f"SECURITY: Anonymous user is trying to load restricted folder with ID of {decoded_folder_id}"
                )
            raise exceptions.ObjectNotFound(
                f'Folder with the id provided ( {folder_id} ) was not found')

        folder_contents = []
        update_time = ''
        create_time = ''

        folders, datasets = self._apply_preferences(folder, include_deleted,
                                                    search_text)

        #  Go through every accessible item (folders, datasets) in the folder and include its metadata.
        for content_item in self._load_folder_contents(trans, folders,
                                                       datasets, offset,
                                                       limit):
            return_item = {}
            encoded_id = trans.security.encode_id(content_item.id)
            create_time = content_item.create_time.strftime(TIME_FORMAT)

            if content_item.api_type == FOLDER_TYPE_NAME:
                encoded_id = f'F{encoded_id}'
                can_modify = is_admin or (
                    trans.user
                    and trans.app.security_agent.can_modify_library_item(
                        current_user_roles, folder))
                can_manage = is_admin or (
                    trans.user
                    and trans.app.security_agent.can_manage_library_item(
                        current_user_roles, folder))
                update_time = content_item.update_time.strftime(TIME_FORMAT)
                return_item.update(
                    dict(can_modify=can_modify, can_manage=can_manage))
                if content_item.description:
                    return_item.update(
                        dict(description=content_item.description))

            elif content_item.api_type == FILE_TYPE_NAME:
                #  Is the dataset public or private?
                #  When both are False the dataset is 'restricted'
                #  Access rights are checked on the dataset level, not on the ld or ldda level to maintain consistency
                dataset = content_item.library_dataset_dataset_association.dataset
                is_unrestricted = trans.app.security_agent.dataset_is_public(
                    dataset)
                is_private = not is_unrestricted and trans.user and trans.app.security_agent.dataset_is_private_to_user(
                    trans, dataset)

                # Can user manage the permissions on the dataset?
                can_manage = is_admin or (
                    trans.user and trans.app.security_agent.can_manage_dataset(
                        current_user_roles, content_item.
                        library_dataset_dataset_association.dataset))
                raw_size = int(content_item.
                               library_dataset_dataset_association.get_size())
                nice_size = util.nice_size(raw_size)
                update_time = content_item.library_dataset_dataset_association.update_time.strftime(
                    TIME_FORMAT)

                library_dataset_dict = content_item.to_dict()
                encoded_ldda_id = trans.security.encode_id(
                    content_item.library_dataset_dataset_association.id)

                tag_manager = tags.GalaxyTagHandler(trans.sa_session)
                ldda_tags = tag_manager.get_tags_str(
                    content_item.library_dataset_dataset_association.tags)

                return_item.update(
                    dict(file_ext=library_dataset_dict['file_ext'],
                         date_uploaded=library_dataset_dict['date_uploaded'],
                         update_time=update_time,
                         is_unrestricted=is_unrestricted,
                         is_private=is_private,
                         can_manage=can_manage,
                         state=library_dataset_dict['state'],
                         file_size=nice_size,
                         raw_size=raw_size,
                         ldda_id=encoded_ldda_id,
                         tags=ldda_tags))
                if content_item.library_dataset_dataset_association.message:
                    return_item.update(
                        dict(message=content_item.
                             library_dataset_dataset_association.message))
                elif content_item.library_dataset_dataset_association.info:
                    # There is no message but ldda info contains something so we display that instead.
                    return_item.update(
                        dict(message=content_item.
                             library_dataset_dataset_association.info))

            # For every item include the default metadata
            return_item.update(
                dict(id=encoded_id,
                     type=content_item.api_type,
                     name=content_item.name,
                     update_time=update_time,
                     create_time=create_time,
                     deleted=content_item.deleted))
            folder_contents.append(return_item)

        # Return the reversed path so it starts with the library node.
        full_path = self._build_path(trans, folder)[::-1]

        user_can_add_library_item = is_admin or trans.app.security_agent.can_add_library_item(
            current_user_roles, folder)

        user_can_modify_folder = is_admin or trans.app.security_agent.can_modify_library_item(
            current_user_roles, folder)

        parent_library_id = None
        if folder.parent_library is not None:
            parent_library_id = trans.security.encode_id(
                folder.parent_library.id)

        total_rows = len(folders) + len(datasets)

        metadata = dict(full_path=full_path,
                        total_rows=total_rows,
                        can_add_library_item=user_can_add_library_item,
                        can_modify_folder=user_can_modify_folder,
                        folder_name=folder.name,
                        folder_description=folder.description,
                        parent_library_id=parent_library_id)
        folder_container = dict(metadata=metadata,
                                folder_contents=folder_contents)
        return folder_container
Esempio n. 7
0
 def __init__(self, app):
     self.app = app
     self.tag_handler = tags.GalaxyTagHandler(app.model.context)
Esempio n. 8
0
def __new_library_upload(trans,
                         cntrller,
                         uploaded_dataset,
                         library_bunch,
                         state=None):
    current_user_roles = trans.get_current_user_roles()
    if not ((trans.user_is_admin and cntrller in ['library_admin', 'api'])
            or trans.app.security_agent.can_add_library_item(
                current_user_roles, library_bunch.folder)):
        # This doesn't have to be pretty - the only time this should happen is if someone's being malicious.
        raise Exception(
            "User is not authorized to add datasets to this library.")
    folder = library_bunch.folder
    if uploaded_dataset.get('in_folder', False):
        # Create subfolders if desired
        for name in uploaded_dataset.in_folder.split(os.path.sep):
            trans.sa_session.refresh(folder)
            matches = [
                x for x in active_folders(trans, folder) if x.name == name
            ]
            if matches:
                folder = matches[0]
            else:
                new_folder = trans.app.model.LibraryFolder(
                    name=name,
                    description='Automatically created by upload tool')
                new_folder.genome_build = trans.app.genome_builds.default_value
                folder.add_folder(new_folder)
                trans.sa_session.add(new_folder)
                trans.sa_session.flush()
                trans.app.security_agent.copy_library_permissions(
                    trans, folder, new_folder)
                folder = new_folder
    if library_bunch.replace_dataset:
        ld = library_bunch.replace_dataset
    else:
        ld = trans.app.model.LibraryDataset(folder=folder,
                                            name=uploaded_dataset.name)
        trans.sa_session.add(ld)
        trans.sa_session.flush()
        trans.app.security_agent.copy_library_permissions(trans, folder, ld)
    ldda = trans.app.model.LibraryDatasetDatasetAssociation(
        name=uploaded_dataset.name,
        extension=uploaded_dataset.file_type,
        dbkey=uploaded_dataset.dbkey,
        library_dataset=ld,
        user=trans.user,
        create_dataset=True,
        sa_session=trans.sa_session)
    if uploaded_dataset.get('tag_using_filenames', False):
        tag_from_filename = os.path.splitext(
            os.path.basename(uploaded_dataset.name))[0]
        tag_manager = tags.GalaxyTagHandler(trans.sa_session)
        tag_manager.apply_item_tag(item=ldda,
                                   user=trans.user,
                                   name='name',
                                   value=tag_from_filename)

    trans.sa_session.add(ldda)
    if state:
        ldda.state = state
    else:
        ldda.state = ldda.states.QUEUED
    ldda.message = library_bunch.message
    trans.sa_session.flush()
    # Permissions must be the same on the LibraryDatasetDatasetAssociation and the associated LibraryDataset
    trans.app.security_agent.copy_library_permissions(trans, ld, ldda)
    if library_bunch.replace_dataset:
        # Copy the Dataset level permissions from replace_dataset to the new LibraryDatasetDatasetAssociation.dataset
        trans.app.security_agent.copy_dataset_permissions(
            library_bunch.replace_dataset.library_dataset_dataset_association.
            dataset, ldda.dataset)
    else:
        # Copy the current user's DefaultUserPermissions to the new LibraryDatasetDatasetAssociation.dataset
        trans.app.security_agent.set_all_dataset_permissions(
            ldda.dataset,
            trans.app.security_agent.user_get_default_permissions(trans.user))
        folder.add_library_dataset(ld, genome_build=uploaded_dataset.dbkey)
        trans.sa_session.add(folder)
        trans.sa_session.flush()
    ld.library_dataset_dataset_association_id = ldda.id
    trans.sa_session.add(ld)
    trans.sa_session.flush()
    # Handle template included in the upload form, if any.  If the upload is not asynchronous ( e.g., URL paste ),
    # then the template and contents will be included in the library_bunch at this point.  If the upload is
    # asynchronous ( e.g., uploading a file ), then the template and contents will be included in the library_bunch
    # in the get_uploaded_datasets() method below.
    if library_bunch.template and library_bunch.template_field_contents:
        # Since information templates are inherited, the template fields can be displayed on the upload form.
        # If the user has added field contents, we'll need to create a new form_values and info_association
        # for the new library_dataset_dataset_association object.
        # Create a new FormValues object, using the template we previously retrieved
        form_values = trans.app.model.FormValues(
            library_bunch.template, library_bunch.template_field_contents)
        trans.sa_session.add(form_values)
        trans.sa_session.flush()
        # Create a new info_association between the current ldda and form_values
        # TODO: Currently info_associations at the ldda level are not inheritable to the associated LibraryDataset,
        # we need to figure out if this is optimal
        info_association = trans.app.model.LibraryDatasetDatasetInfoAssociation(
            ldda, library_bunch.template, form_values)
        trans.sa_session.add(info_association)
        trans.sa_session.flush()
    # If roles were selected upon upload, restrict access to the Dataset to those roles
    if library_bunch.roles:
        for role in library_bunch.roles:
            dp = trans.app.model.DatasetPermissions(
                trans.app.security_agent.permitted_actions.DATASET_ACCESS.
                action, ldda.dataset, role)
            trans.sa_session.add(dp)
            trans.sa_session.flush()
    return ldda
Esempio n. 9
0
    def index(self, trans, folder_id, **kwd):
        """
        GET /api/folders/{encoded_folder_id}/contents

        Displays a collection (list) of a folder's contents
        (files and folders). Encoded folder ID is prepended
        with 'F' if it is a folder as opposed to a data set
        which does not have it. Full path is provided in
        response as a separate object providing data for
        breadcrumb path building.

        :param  folder_id: encoded ID of the folder which
            contents should be library_dataset_dict
        :type   folder_id: encoded string

        :param kwd: keyword dictionary with other params
        :type  kwd: dict

        :returns: dictionary containing all items and metadata
        :type:    dict

        :raises: MalformedId, InconsistentDatabase, ObjectNotFound,
             InternalServerError
        """
        is_admin = trans.user_is_admin
        deleted = kwd.get('include_deleted', 'missing')
        current_user_roles = trans.get_current_user_roles()
        try:
            deleted = util.asbool(deleted)
        except ValueError:
            deleted = False

        decoded_folder_id = self.folder_manager.cut_and_decode(
            trans, folder_id)
        folder = self.folder_manager.get(trans, decoded_folder_id)

        # Special level of security on top of libraries.
        if trans.app.security_agent.can_access_library(
                current_user_roles, folder.parent_library) or is_admin:
            pass
        else:
            if trans.user:
                log.warning(
                    "SECURITY: User (id: %s) without proper access rights is trying to load folder with ID of %s"
                    % (trans.user.id, decoded_folder_id))
            else:
                log.warning(
                    "SECURITY: Anonymous user is trying to load restricted folder with ID of %s"
                    % (decoded_folder_id))
            raise exceptions.ObjectNotFound(
                'Folder with the id provided ( %s ) was not found' %
                str(folder_id))

        folder_contents = []
        update_time = ''
        create_time = ''
        #  Go through every accessible item (folders, datasets) in the folder and include its metadata.
        for content_item in self._load_folder_contents(trans, folder, deleted):
            return_item = {}
            encoded_id = trans.security.encode_id(content_item.id)
            create_time = content_item.create_time.strftime(
                "%Y-%m-%d %I:%M %p")

            if content_item.api_type == 'folder':
                encoded_id = 'F' + encoded_id
                can_modify = is_admin or (
                    trans.user
                    and trans.app.security_agent.can_modify_library_item(
                        current_user_roles, folder))
                can_manage = is_admin or (
                    trans.user
                    and trans.app.security_agent.can_manage_library_item(
                        current_user_roles, folder))
                update_time = content_item.update_time.strftime(
                    "%Y-%m-%d %I:%M %p")
                return_item.update(
                    dict(can_modify=can_modify, can_manage=can_manage))
                if content_item.description:
                    return_item.update(
                        dict(description=content_item.description))

            elif content_item.api_type == 'file':
                #  Is the dataset public or private?
                #  When both are False the dataset is 'restricted'
                #  Access rights are checked on the dataset level, not on the ld or ldda level to maintain consistency
                dataset = content_item.library_dataset_dataset_association.dataset
                is_unrestricted = trans.app.security_agent.dataset_is_public(
                    dataset)
                if not is_unrestricted and trans.user and trans.app.security_agent.dataset_is_private_to_user(
                        trans, dataset):
                    is_private = True
                else:
                    is_private = False

                # Can user manage the permissions on the dataset?
                can_manage = is_admin or (
                    trans.user and trans.app.security_agent.can_manage_dataset(
                        current_user_roles, content_item.
                        library_dataset_dataset_association.dataset))
                raw_size = int(content_item.
                               library_dataset_dataset_association.get_size())
                nice_size = util.nice_size(raw_size)
                update_time = content_item.library_dataset_dataset_association.update_time.strftime(
                    "%Y-%m-%d %I:%M %p")

                library_dataset_dict = content_item.to_dict()
                encoded_ldda_id = trans.security.encode_id(
                    content_item.library_dataset_dataset_association.id)

                tag_manager = tags.GalaxyTagHandler(trans.sa_session)
                ldda_tags = tag_manager.get_tags_str(
                    content_item.library_dataset_dataset_association.tags)

                return_item.update(
                    dict(file_ext=library_dataset_dict['file_ext'],
                         date_uploaded=library_dataset_dict['date_uploaded'],
                         update_time=update_time,
                         is_unrestricted=is_unrestricted,
                         is_private=is_private,
                         can_manage=can_manage,
                         state=library_dataset_dict['state'],
                         file_size=nice_size,
                         raw_size=raw_size,
                         ldda_id=encoded_ldda_id,
                         tags=ldda_tags))
                if content_item.library_dataset_dataset_association.message:
                    return_item.update(
                        dict(message=content_item.
                             library_dataset_dataset_association.message))
                elif content_item.library_dataset_dataset_association.info:
                    # There is no message but ldda info contains something so we display that instead.
                    return_item.update(
                        dict(message=content_item.
                             library_dataset_dataset_association.info))

            # For every item include the default metadata
            return_item.update(
                dict(id=encoded_id,
                     type=content_item.api_type,
                     name=content_item.name,
                     update_time=update_time,
                     create_time=create_time,
                     deleted=content_item.deleted))
            folder_contents.append(return_item)

        # Return the reversed path so it starts with the library node.
        full_path = self.build_path(trans, folder)[::-1]

        # Check whether user can add items to the current folder
        can_add_library_item = is_admin or trans.app.security_agent.can_add_library_item(
            current_user_roles, folder)

        # Check whether user can modify the current folder
        can_modify_folder = is_admin or trans.app.security_agent.can_modify_library_item(
            current_user_roles, folder)

        parent_library_id = None
        if folder.parent_library is not None:
            parent_library_id = trans.security.encode_id(
                folder.parent_library.id)

        metadata = dict(full_path=full_path,
                        can_add_library_item=can_add_library_item,
                        can_modify_folder=can_modify_folder,
                        folder_name=folder.name,
                        folder_description=folder.description,
                        parent_library_id=parent_library_id)
        folder_container = dict(metadata=metadata,
                                folder_contents=folder_contents)
        return folder_container