Beispiel #1
0
 def make_private(self, trans, history_id=None, all_histories=False, **kwd):
     """
     Sets the datasets within a history to private.  Also sets the default
     permissions for the history to private, for future datasets.
     """
     histories = []
     all_histories = string_as_bool(all_histories)
     if all_histories:
         histories = trans.user.histories
     elif history_id:
         history = self.history_manager.get_owned(self.decode_id(history_id), trans.user, current_history=trans.history)
         if history:
             histories.append(history)
     if not histories:
         return self.message_exception(trans, 'Invalid history or histories specified.')
     private_role = trans.app.security_agent.get_private_user_role(trans.user)
     user_roles = trans.user.all_roles()
     private_permissions = {
         trans.app.security_agent.permitted_actions.DATASET_MANAGE_PERMISSIONS: [private_role],
         trans.app.security_agent.permitted_actions.DATASET_ACCESS: [private_role],
     }
     for history in histories:
         # Set default role for history to private
         trans.app.security_agent.history_set_default_permissions(history, private_permissions)
         # Set private role for all datasets
         for hda in history.datasets:
             if (not hda.dataset.library_associations
                     and not trans.app.security_agent.dataset_is_private_to_user(trans, hda.dataset)
                     and trans.app.security_agent.can_manage_dataset(user_roles, hda.dataset)):
                 # If it's not private to me, and I can manage it, set fixed private permissions.
                 trans.app.security_agent.set_all_dataset_permissions(hda.dataset, private_permissions)
                 if not trans.app.security_agent.dataset_is_private_to_user(trans, hda.dataset):
                     raise exceptions.InternalServerError('An error occurred and the dataset is NOT private.')
     return {'message': f"Success, requested permissions have been changed in {'all histories' if all_histories else history.name}."}
Beispiel #2
0
    def list(self, trans, **kwargs):
        """List all available histories"""
        current_history = trans.get_history()
        message = kwargs.get('message')
        status = kwargs.get('status')
        if 'operation' in kwargs:
            operation = kwargs['operation'].lower()
            history_ids = listify(kwargs.get('id', []))
            # Display no message by default
            status, message = None, None
            # Load the histories and ensure they all belong to the current user
            histories = []
            for history_id in history_ids:
                history = self.history_manager.get_owned(self.decode_id(history_id), trans.user, current_history=trans.history)
                if history:
                    # Ensure history is owned by current user
                    if history.user_id is not None and trans.user:
                        assert trans.user.id == history.user_id, "History does not belong to current user"
                    histories.append(history)
                else:
                    log.warning("Invalid history id '%r' passed to list", history_id)
            if histories:
                if operation == "switch":
                    status, message = self._list_switch(trans, histories)
                    # Take action to update UI to reflect history switch. If
                    # grid is using panels, it is standalone and hence a redirect
                    # to root is needed; if grid is not using panels, it is nested
                    # in the main Galaxy UI and refreshing the history frame
                    # is sufficient.
                    use_panels = kwargs.get('use_panels', False) == 'True'
                    if use_panels:
                        return trans.response.send_redirect(url_for("/"))
                    else:
                        kwargs['refresh_frames'] = ['history']
                elif operation in ("delete", "delete permanently"):
                    status, message = self._list_delete(trans, histories, purge=(operation == "delete permanently"))
                    if current_history in histories:
                        # Deleted the current history, so a new, empty history was
                        # created automatically, and we need to refresh the history frame
                        kwargs['refresh_frames'] = ['history']
                elif operation == "undelete":
                    status, message = self._list_undelete(trans, histories)

                trans.sa_session.flush()
        # Render the list view
        if message and status:
            kwargs['message'] = sanitize_text(message)
            kwargs['status'] = status
        return self.stored_list_grid(trans, **kwargs)
Beispiel #3
0
 def rename(self, trans, payload=None, **kwd):
     id = kwd.get('id')
     if not id:
         return self.message_exception(trans, 'No history id received for renaming.')
     user = trans.get_user()
     id = listify(id)
     histories = []
     for history_id in id:
         history = self.history_manager.get_owned(self.decode_id(history_id), trans.user, current_history=trans.history)
         if history and history.user_id == user.id:
             histories.append(history)
     if trans.request.method == 'GET':
         return {
             'title': 'Change history name(s)',
             'inputs': [{
                 'name': 'name_%i' % i,
                 'label': f'Current: {h.name}',
                 'value': h.name
             } for i, h in enumerate(histories)]
         }
     else:
         messages = []
         for i, h in enumerate(histories):
             cur_name = h.get_display_name()
             new_name = payload.get('name_%i' % i)
             # validate name is empty
             if not isinstance(new_name, str) or not new_name.strip():
                 messages.append('You must specify a valid name for History \'%s\'.' % cur_name)
             # skip if not the owner
             elif h.user_id != user.id:
                 messages.append('History \'%s\' does not appear to belong to you.' % cur_name)
             # skip if it wouldn't be a change
             elif new_name != cur_name:
                 h.name = new_name
                 trans.sa_session.add(h)
                 trans.sa_session.flush()
                 trans.log_event(f'History renamed: id: {str(h.id)}, renamed to: {new_name}')
                 messages.append(f"History '{cur_name}' renamed to '{new_name}'.")
         message = sanitize_text(' '.join(messages)) if messages else 'History names remain unchanged.'
         return {'message': message, 'status': 'success'}