'Set new full permissions on a dataset, eliminating all current permissions.
Permission looks like: { Action : [ Role, Role ] }'

def set_all_dataset_permissions(self, dataset, permissions={}, new=False, flush=True):

    has_dataset_manage_permissions = False
    for (action, roles) in permissions.items():
        if isinstance(action, Action):
            if ((action == self.permitted_actions.DATASET_MANAGE_PERMISSIONS) and roles):
                has_dataset_manage_permissions = True
                break
        elif ((action == self.permitted_actions.DATASET_MANAGE_PERMISSIONS.action) and roles):
            has_dataset_manage_permissions = True
            break
    if (not has_dataset_manage_permissions):
        return 'At    least    1    role    must    be    associated    with    the    <b>manage    permissions</b>    permission    on    this    dataset.'
    flush_needed = False
    if (not new):
        for dp in dataset.actions:
            self.sa_session.delete(dp)
            flush_needed = True
    for (action, roles) in permissions.items():
        if isinstance(action, Action):
            action = action.action
        for role in roles:
            dp = self.model.DatasetPermissions(action, dataset, role_id=role.id)
            self.sa_session.add(dp)
            flush_needed = True
    if (flush_needed and flush):
        self.sa_session.flush()
    return ''
