Changeset 16209


Ignore:
Timestamp:
10/03/18 15:06:21 (14 months ago)
Author:
jzenisek
Message:

#2839:

  • adapted job execution implementation at ProjectJobsView
  • prohibited resource checking for non-admins
Location:
trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/HeuristicLab.Clients.Hive.Administrator/3.3/Views/ProjectJobsView.cs

    r16208 r16209  
    4444    private const string JOB_STATE = "State";
    4545    private const string JOB_EXECUTIONSTATE = "Execution State";
     46    private const string JOB_EXECUTIONTIME = "Execution Time";
    4647    private const string JOB_DESCRIPTION = "Description";
    4748    private const string JOB_TASKCOUNT = "Tasks";
     49    private const string JOB_WAITINGTASKCOUNT = "Waiting";
    4850    private const string JOB_CALCULATINGTASKCOUNT = "Calculating";
    4951    private const string JOB_FINISHEDTASKCOUNT = "Finished";
     
    112114        var jobs = GetSelectedJobs().ToList();
    113115        if (jobs.Any()) {
    114 
    115           // TODO: include isControllable?!
     116                   
    116117          startButton.Enabled = jobs.All(x =>
    117             !x.IsProgressing && x.HiveTasks != null && x.HiveTasks.Count > 0
    118             && x.Job.ProjectId != null && x.Job.ProjectId != Guid.Empty && x.Job.ResourceIds != null && x.Job.ResourceIds.Any()
     118            !x.IsProgressing && HiveAdminClient.Instance.Tasks.ContainsKey(x.Id) && HiveAdminClient.Instance.Tasks[x.Id].Count > 0
     119            && x.Job.ProjectId != Guid.Empty //&& x.Job.ResourceIds != null && x.Job.ResourceIds.Any()
    119120            && (x.ExecutionState == ExecutionState.Prepared || x.ExecutionState == ExecutionState.Paused));
    120121          pauseButton.Enabled = jobs.All(x => !x.IsProgressing && x.ExecutionState == ExecutionState.Started);
     
    138139
    139140    private void refreshButton_Click(object sender, EventArgs e) {
    140       var task = System.Threading.Tasks.Task.Factory.StartNew(RefreshJobsAsync);
    141141      progress.Start("Refreshing jobs...");
    142142      SetEnabledStateOfControls();
     143      var task = System.Threading.Tasks.Task.Factory.StartNew(RefreshJobsAsync);
    143144
    144145      task.ContinueWith((t) => {
    145         matrixView.DataGridView.ClearSelection();
     146        progress.Finish();       
     147        SetEnabledStateOfControls();
    146148      });
    147149    }
     
    157159          MessageBoxIcon.Question);
    158160
    159         if (result == DialogResult.Yes) {         
     161        if (result == DialogResult.Yes) {
     162          progress.Start("Removing job(s)...");
     163          SetEnabledStateOfControls();
    160164          var task = System.Threading.Tasks.Task.Factory.StartNew(RemoveJobsAsync, jobs);
    161           progress.Start();
    162           SetEnabledStateOfControls();
    163 
    164           task.ContinueWith((t) => {
    165             matrixView.DataGridView.ClearSelection();
    166             RefreshJobs();
    167             progress.Finish();
     165
     166          task.ContinueWith((t) => {
     167            RefreshJobs();
     168            progress.Finish();
     169            SetEnabledStateOfControls();
    168170          }, TaskContinuationOptions.NotOnFaulted);
    169171
    170172          task.ContinueWith((t) => {
    171             matrixView.DataGridView.ClearSelection();
    172             RefreshJobs();
    173             progress.Finish();
    174             MessageBox.Show("An error occured removing the job(s). " + t.Exception.Message, "HeuristicLab Hive Administrator", MessageBoxButtons.OK, MessageBoxIcon.Error);
     173            RefreshJobs();
     174            progress.Finish();
     175            SetEnabledStateOfControls();
     176            MessageBox.Show("An error occured removing the job(s).", "HeuristicLab Hive Administrator", MessageBoxButtons.OK, MessageBoxIcon.Error);
    175177          }, TaskContinuationOptions.OnlyOnFaulted);
    176178        }
     
    189191
    190192        if (result == DialogResult.Yes) {
     193          progress.Start("Resuming job(s)...");
     194          SetEnabledStateOfControls();
    191195          var task = System.Threading.Tasks.Task.Factory.StartNew(ResumeJobsAsync, jobs);
    192           progress.Start();
    193           SetEnabledStateOfControls();
    194 
    195           task.ContinueWith((t) => {
    196             matrixView.DataGridView.ClearSelection();
    197             RefreshJobs();
    198             progress.Finish();
    199           });
    200 
    201           task.ContinueWith((t) => {
    202             matrixView.DataGridView.ClearSelection();
    203             RefreshJobs();
    204             progress.Finish();
     196
     197          task.ContinueWith((t) => {
     198            RefreshJobs();           
     199            progress.Finish();
     200            SetEnabledStateOfControls();
     201          }, TaskContinuationOptions.NotOnFaulted);
     202
     203          task.ContinueWith((t) => {
     204            RefreshJobs();
     205            progress.Finish();
     206            SetEnabledStateOfControls();
    205207            MessageBox.Show("An error occured resuming the job(s).", "HeuristicLab Hive Administrator", MessageBoxButtons.OK, MessageBoxIcon.Error);
    206208          }, TaskContinuationOptions.OnlyOnFaulted);
     
    220222
    221223        if (result == DialogResult.Yes) {
     224          progress.Start("Pausing job(s)...");
     225          SetEnabledStateOfControls();
    222226          var task = System.Threading.Tasks.Task.Factory.StartNew(PauseJobsAsync, jobs);
    223           progress.Start();
    224           SetEnabledStateOfControls();
    225 
    226           task.ContinueWith((t) => {
    227             matrixView.DataGridView.ClearSelection();
    228             RefreshJobs();
    229             progress.Finish();
    230           });
    231 
    232           task.ContinueWith((t) => {
    233             matrixView.DataGridView.ClearSelection();
    234             RefreshJobs();
    235             progress.Finish();
     227
     228          task.ContinueWith((t) => {
     229            RefreshJobs();           
     230            progress.Finish();
     231            SetEnabledStateOfControls();
     232          }, TaskContinuationOptions.NotOnFaulted);
     233
     234          task.ContinueWith((t) => {
     235            RefreshJobs();
     236            progress.Finish();
     237            SetEnabledStateOfControls();
    236238            MessageBox.Show("An error occured pausing the job(s).", "HeuristicLab Hive Administrator", MessageBoxButtons.OK, MessageBoxIcon.Error);
    237239          }, TaskContinuationOptions.OnlyOnFaulted);
     
    251253
    252254        if (result == DialogResult.Yes) {
     255          progress.Start("Stopping job(s)...");
     256          SetEnabledStateOfControls();
    253257          var task = System.Threading.Tasks.Task.Factory.StartNew(StopJobsAsync, jobs);
    254           progress.Start();
    255           SetEnabledStateOfControls();
    256 
    257           task.ContinueWith((t) => {
    258             matrixView.DataGridView.ClearSelection();
    259             RefreshJobs();
    260             progress.Finish();
    261           });
    262 
    263           task.ContinueWith((t) => {
    264             matrixView.DataGridView.ClearSelection();
    265             RefreshJobs();
    266             progress.Finish();
     258
     259          task.ContinueWith((t) => {
     260            RefreshJobs();
     261            progress.Finish();
     262            SetEnabledStateOfControls();
     263          }, TaskContinuationOptions.NotOnFaulted);
     264
     265          task.ContinueWith((t) => {
     266            RefreshJobs();
     267            progress.Finish();
     268            SetEnabledStateOfControls();
    267269            MessageBox.Show("An error occured stopping the job(s).", "HeuristicLab Hive Administrator", MessageBoxButtons.OK, MessageBoxIcon.Error);           
    268270          }, TaskContinuationOptions.OnlyOnFaulted);
     
    287289      foreach (DataGridViewRow r in matrixView.DataGridView.SelectedRows) {
    288290        if (((string)r.Cells[0].Value) == JobState.Online.ToString()) {
    289           jobs.Add(HiveAdminClient.Instance.Jobs[Content.Id].FirstOrDefault(x => x.Id == Guid.Parse((string) r.Cells[9].Value)));
     291          jobs.Add(HiveAdminClient.Instance.Jobs[Content.Id].FirstOrDefault(x => x.Id == Guid.Parse((string) r.Cells[11].Value)));
    290292        }
    291293      }
     
    306308      var jobs = HiveAdminClient.Instance.Jobs[Content.Id];
    307309      var resources = HiveAdminClient.Instance.Resources;
    308       string[,] values = new string[jobs.Count, 11];
     310      string[,] values = new string[jobs.Count, 13];
    309311
    310312      for(int i = 0; i < jobs.Count; i++) {
     
    312314        values[i, 0] = job.Job.State.ToString();
    313315        values[i, 1] = job.ExecutionState.ToString();
    314         values[i, 2] = job.Job.DateCreated.ToString();
    315         values[i, 3] = job.Job.OwnerUsername;
    316         values[i, 4] = job.Job.Name;
    317         values[i, 5] = job.Job.JobCount.ToString();
    318         values[i, 6] = job.Job.CalculatingCount.ToString();
    319         values[i, 7] = job.Job.FinishedCount.ToString();
    320         values[i, 8] = job.Job.Description;       
    321         values[i, 9] = job.Job.Id.ToString();
    322         values[i, 10] = job.Job.OwnerUserId.ToString();
     316        values[i, 2] = job.ExecutionTime.ToString();
     317        values[i, 3] = job.Job.DateCreated.ToString();
     318        values[i, 4] = job.Job.OwnerUsername;
     319        values[i, 5] = job.Job.Name;
     320        values[i, 6] = job.Job.JobCount.ToString();
     321        values[i, 7] = (job.Job.JobCount - job.Job.CalculatingCount - job.Job.FinishedCount).ToString();
     322        values[i, 8] = job.Job.CalculatingCount.ToString();
     323        values[i, 9] = job.Job.FinishedCount.ToString();
     324        values[i, 10] = job.Job.Description;       
     325        values[i, 11] = job.Job.Id.ToString();
     326        values[i, 12] = job.Job.OwnerUserId.ToString();
    323327      }
    324328     
    325329      var matrix = new StringMatrix(values);
    326       matrix.ColumnNames = new string[] { JOB_STATE, JOB_EXECUTIONSTATE, JOB_DATECREATED, JOB_OWNER, JOB_NAME, JOB_TASKCOUNT, JOB_CALCULATINGTASKCOUNT, JOB_FINISHEDTASKCOUNT, JOB_DESCRIPTION, JOB_ID, JOB_OWNERID };
     330      matrix.ColumnNames = new string[] { JOB_STATE, JOB_EXECUTIONSTATE, JOB_EXECUTIONTIME, JOB_DATECREATED, JOB_OWNER, JOB_NAME, JOB_TASKCOUNT, JOB_WAITINGTASKCOUNT, JOB_CALCULATINGTASKCOUNT, JOB_FINISHEDTASKCOUNT, JOB_DESCRIPTION, JOB_ID, JOB_OWNERID };
    327331      matrix.SortableView = true;
    328332      return matrix;
     
    356360
    357361    private void RefreshJobsAsync() {
    358       RefreshJobs();
     362      HiveAdminClient.Instance.RefreshJobs();
     363      UpdateJobs();
    359364    }
    360365
     
    387392      progress.Start();
    388393      foreach (var job in jobList) {
    389         progress.Status = "Deleting job \"" + job.Job.Name + "\"...";
     394        progress.Status = "Removing job \"" + job.Job.Name + "\"...";
    390395        HiveAdminClient.RemoveJob(job);
    391396      }
  • trunk/HeuristicLab.Clients.Hive.Administrator/3.3/Views/ResourcesView.cs

    r16185 r16209  
    318318
    319319    private void treeSlaveGroup_BeforeCheck(object sender, TreeViewCancelEventArgs e) {
    320       if (e.Node == ungroupedGroupNode) {
     320      if (!IsAdmin() || e.Node == ungroupedGroupNode) {
    321321        e.Cancel = true;
    322322      } else {
  • trunk/HeuristicLab.Clients.Hive.JobManager/3.3/Views/RefreshableHiveJobView.cs

    r16117 r16209  
    139139          }
    140140
     141          // project look up
    141142          if (Content.Job != null && Content.Job.ProjectId == Guid.Empty) {
    142143            if (HiveClient.Instance != null && HiveClient.Instance.Projects != null && HiveClient.Instance.Projects.Count == 1) {
     
    150151              }               
    151152            }
    152           }
    153 
    154 
    155           nameTextBox.Text = Content.Job.Name;
    156           descriptionTextBox.Text = Content.Job.Description;
    157           executionTimeTextBox.Text = Content.ExecutionTime.ToString();
    158           refreshAutomaticallyCheckBox.Checked = Content.RefreshAutomatically;
    159 
    160           // project look up
    161           if(Content.Job.ProjectId != null && Content.Job.ProjectId != Guid.Empty) {
    162             if(selectedProject == null || selectedProject.Id != Content.Job.ProjectId) {
     153          } else if (Content.Job != null && Content.Job.ProjectId != Guid.Empty) {
     154            if (selectedProject == null || selectedProject.Id != Content.Job.ProjectId) {
    163155              selectedProject = GetProject(Content.Job.ProjectId);
     156              hiveResourceSelectorDialog = null;
    164157              if (selectedProject != null) {
    165158                projectNameTextBox.Text = selectedProject.Name;
     
    168161              }
    169162            }
     163
     164            if(hiveResourceSelectorDialog == null)
     165              hiveResourceSelectorDialog = new HiveResourceSelectorDialog(Content.Job.Id, Content.Job.ProjectId);
     166
     167            if (Content.Job.ResourceIds == null) {
     168              Content.Job.ResourceIds = HiveClient.Instance.GetAssignedResourcesForJob(Content.Job.Id)
     169                .Select(x => x.Id)
     170                .ToList();
     171              hiveResourceSelectorDialog.SelectedResourceIds = Content.Job.ResourceIds;
     172            }
     173
    170174          } else {
    171175            selectedProject = null;
     
    173177            Content.Job.ResourceIds = null;
    174178          }
     179
     180
     181          nameTextBox.Text = Content.Job.Name;
     182          descriptionTextBox.Text = Content.Job.Description;
     183          executionTimeTextBox.Text = Content.ExecutionTime.ToString();
     184          refreshAutomaticallyCheckBox.Checked = Content.RefreshAutomatically;
    175185         
    176186          logView.Content = Content.Log;
     
    501511    }
    502512
    503     private void updateButton_Click2(object sender, EventArgs e) {
    504       if (Content.ExecutionState == ExecutionState.Stopped) {
    505         MessageBox.Show("Job cannot be updated once it stopped.", "HeuristicLab Hive Job Manager", MessageBoxButtons.OK, MessageBoxIcon.Information);
    506         return;
    507       }
    508 
    509       HiveClient.UpdateJob(
    510         (Exception ex) => ErrorHandling.ShowErrorDialog(this, "Update failed.", ex),
    511         Content,
    512         new CancellationToken());
    513       UpdateSelectorDialog();
    514     }
    515 
    516513    private void updateButton_Click(object sender, EventArgs e) {
    517514      if (Content.ExecutionState == ExecutionState.Stopped) {
     
    556553      } else {
    557554        startButton.Enabled = Content.IsControllable && Content.HiveTasks != null && Content.HiveTasks.Count > 0
    558           && Content.Job.ProjectId != null && Content.Job.ProjectId != Guid.Empty && Content.Job.ResourceIds != null  && Content.Job.ResourceIds.Any()
     555          && Content.Job.ProjectId != Guid.Empty && Content.Job.ResourceIds != null  && Content.Job.ResourceIds.Any()
    559556          && (Content.ExecutionState == ExecutionState.Prepared || Content.ExecutionState == ExecutionState.Paused) && !Content.IsProgressing;
    560557        pauseButton.Enabled = Content.IsControllable && Content.ExecutionState == ExecutionState.Started && !Content.IsProgressing;
  • trunk/HeuristicLab.Clients.Hive/3.3/HiveAdminClient.cs

    r16208 r16209  
    8080    }
    8181
     82    private Dictionary<Guid, List<LightweightTask>> tasks;
     83    public Dictionary<Guid, List<LightweightTask>> Tasks {
     84      get { return tasks; }
     85    }
     86
    8287    private Dictionary<Guid, HashSet<Guid>> projectAncestors;
    8388    public Dictionary<Guid, HashSet<Guid>> ProjectAncestors {
     
    145150        projectResourceAssignments = new ItemList<AssignedProjectResource>();
    146151        jobs = new Dictionary<Guid, HiveItemCollection<RefreshableJob>>();
     152        tasks = new Dictionary<Guid, List<LightweightTask>>();
    147153        projectNames = new Dictionary<Guid, string>();
    148154        resourceNames = new Dictionary<Guid, string>();
     
    311317      var projectIds = new List<Guid>();
    312318      jobs = new Dictionary<Guid, HiveItemCollection<RefreshableJob>>();
     319      tasks = new Dictionary<Guid, List<LightweightTask>>();
    313320
    314321      HiveServiceLocator.Instance.CallHiveService(service => {
     
    329336    }
    330337
    331     public static void LoadLightweightJob(RefreshableJob refreshableJob) {
     338    public void LoadLightweightJob(RefreshableJob refreshableJob) {
    332339      var job = refreshableJob.Job;
    333       var tasks = HiveServiceLocator.Instance.CallHiveService(s => s.GetLightweightJobTasksWithoutStateLog(job.Id));
    334       if (tasks != null && tasks.Count > 0 && tasks.All(x => x.Id != Guid.Empty)) {
    335         if (tasks.All(x =>
     340      var lightweightTasks = HiveServiceLocator.Instance.CallHiveService(s => s.GetLightweightJobTasksWithoutStateLog(job.Id));
     341
     342      if (tasks.ContainsKey(job.Id)) {
     343        tasks[job.Id].Clear();
     344        tasks[job.Id].AddRange(lightweightTasks);
     345      } else {
     346        tasks.Add(job.Id, new List<LightweightTask>(lightweightTasks));       
     347      }
     348
     349      if (lightweightTasks != null && lightweightTasks.Count > 0 && lightweightTasks.All(x => x.Id != Guid.Empty)) {
     350        if (lightweightTasks.All(x =>
    336351          x.State == TaskState.Finished
    337352          || x.State == TaskState.Aborted
     
    340355          refreshableJob.RefreshAutomatically = false;
    341356        } else if (
    342           tasks
     357          lightweightTasks
    343358            .Where(x => x.ParentTaskId != null)
    344359            .All(x =>
     
    346361              || x.State != TaskState.Transferring
    347362              || x.State != TaskState.Calculating)
    348           && tasks
     363          && lightweightTasks
    349364             .Where(x => x.ParentTaskId != null)
    350365             .Any(x => x.State == TaskState.Paused)) {
    351366          refreshableJob.ExecutionState = ExecutionState.Paused;
    352367          refreshableJob.RefreshAutomatically = false;
    353         } else if (tasks.Any(x => x.State == TaskState.Calculating
     368        } else if (lightweightTasks.Any(x => x.State == TaskState.Calculating
    354369                                  || x.State == TaskState.Transferring
    355370                                  || x.State == TaskState.Waiting)) {
    356371          refreshableJob.ExecutionState = ExecutionState.Started;
    357372        }
     373
     374        refreshableJob.ExecutionTime = TimeSpan.FromMilliseconds(lightweightTasks.Sum(x => x.ExecutionTime.TotalMilliseconds));
    358375      }
    359376    }
     
    451468    public static void ResumeJob(RefreshableJob refreshableJob) {
    452469      HiveServiceLocator.Instance.CallHiveService(service => {
    453         foreach (HiveTask task in refreshableJob.GetAllHiveTasks()) {
    454           if (task.Task.State == TaskState.Paused) {
    455             service.RestartTask(task.Task.Id);
     470        var tasks = service.GetLightweightJobTasksWithoutStateLog(refreshableJob.Id);
     471        foreach (var task in tasks) {
     472          if (task.State == TaskState.Paused) {
     473            service.RestartTask(task.Id);
    456474          }
    457475        }
     
    462480    public static void PauseJob(RefreshableJob refreshableJob) {
    463481      HiveServiceLocator.Instance.CallHiveService(service => {
    464         foreach (HiveTask task in refreshableJob.GetAllHiveTasks()) {
    465           if (task.Task.State != TaskState.Finished && task.Task.State != TaskState.Aborted && task.Task.State != TaskState.Failed)
    466             service.PauseTask(task.Task.Id);
     482        var tasks = service.GetLightweightJobTasksWithoutStateLog(refreshableJob.Id);
     483        foreach (var task in tasks) {
     484          if (task.State != TaskState.Finished && task.State != TaskState.Aborted && task.State != TaskState.Failed)
     485            service.PauseTask(task.Id);
    467486        }
    468487      });
     
    472491    public static void StopJob(RefreshableJob refreshableJob) {
    473492      HiveServiceLocator.Instance.CallHiveService(service => {
    474         foreach (HiveTask task in refreshableJob.GetAllHiveTasks()) {
    475           if (task.Task.State != TaskState.Finished && task.Task.State != TaskState.Aborted && task.Task.State != TaskState.Failed)
    476             service.StopTask(task.Task.Id);
     493        var tasks = service.GetLightweightJobTasksWithoutStateLog(refreshableJob.Id);
     494        foreach (var task in tasks) {
     495          if (task.State != TaskState.Finished && task.State != TaskState.Aborted && task.State != TaskState.Failed)
     496            service.StopTask(task.Id);
    477497        }
    478498      });
     
    481501
    482502    public static void RemoveJob(RefreshableJob refreshableJob) {
    483       HiveServiceLocator.Instance.CallHiveService((s) => {
    484         s.UpdateJobState(refreshableJob.Id, JobState.StatisticsPending);
     503      HiveServiceLocator.Instance.CallHiveService((service) => {
     504        service.UpdateJobState(refreshableJob.Id, JobState.StatisticsPending);
    485505      });
    486506    }
     
    624644    }
    625645
     646    public IEnumerable<Resource> GetAssignedResourcesForJob(Guid jobId) {
     647      var assignedJobResource =  HiveServiceLocator.Instance.CallHiveService(service => service.GetAssignedResourcesForJob(jobId));
     648      return Resources.Where(x => assignedJobResource.Select(y => y.ResourceId).Contains(x.Id));
     649    }
     650
    626651    private bool IsAdmin() {
    627652      return HiveRoles.CheckAdminUserPermissions();
  • trunk/HeuristicLab.Clients.Hive/3.3/HiveClient.cs

    r16202 r16209  
    756756      return string.Join(" » ", projects.Select(x => ProjectNames[x]).ToArray());
    757757    }
     758
     759    public IEnumerable<Resource> GetAssignedResourcesForJob(Guid jobId) {
     760      var assignedJobResource = HiveServiceLocator.Instance.CallHiveService(service => service.GetAssignedResourcesForJob(jobId));
     761      return Resources.Where(x => assignedJobResource.Select(y => y.ResourceId).Contains(x.Id));
     762    }
    758763  }
    759764}
  • trunk/HeuristicLab.Services.Hive/3.3/HiveService.cs

    r16208 r16209  
    483483          }
    484484
    485 
    486485          jobDto.CopyToEntity(job);
    487486
     
    523522
    524523      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
    525       bool isAdministrator = RoleVerifier.IsInRole(HiveRoles.Administrator);
    526       var currentUserId = UserManager.CurrentUserId;
     524      // check if user is an admin, or granted to administer a job-parenting project, or job owner
     525      AuthorizationManager.AuthorizeForJob(jobId, DT.Permission.Full);
    527526
    528527      var pm = PersistenceManager;
    529528      using (new PerformanceLogger("UpdateJobState")) {
    530         var jobDao = pm.JobDao;
    531         var projectDao = pm.ProjectDao;
     529        var jobDao = pm.JobDao;       
    532530        pm.UseTransaction(() => {
    533531          var job = jobDao.GetById(jobId);
    534           if (job != null) {
    535 
    536             var administrationGrantedProjects = projectDao
    537               .GetAdministrationGrantedProjectsForUser(currentUserId)
    538               .ToList();
    539 
    540             // check if user is an admin, or granted to administer a job-parenting project,...
    541             if (!isAdministrator && !administrationGrantedProjects.Contains(job.Project))
    542               AuthorizationManager.AuthorizeForJob(jobId, DT.Permission.Full); // ... or job owner
     532          if (job != null) {           
    543533
    544534            // note: allow solely state changes from "Online" to "StatisticsPending" = deletion request by user for HiveStatisticGenerator           
     
    563553
    564554      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
    565       bool isAdministrator = RoleVerifier.IsInRole(HiveRoles.Administrator);
    566       var currentUserId = UserManager.CurrentUserId;
     555      // check if user is an admin, or granted to administer a job-parenting project, or job owner
     556      foreach (var jobId in jobIds)
     557          AuthorizationManager.AuthorizeForJob(jobId, DT.Permission.Full);
    567558
    568559      var pm = PersistenceManager;
     
    571562        var projectDao = pm.ProjectDao;
    572563        pm.UseTransaction(() => {
    573           var administrationGrantedProjects = projectDao
    574             .GetAdministrationGrantedProjectsForUser(currentUserId)
    575             .ToList();
    576 
    577564          foreach (var jobId in jobIds) {
    578565            var job = jobDao.GetById(jobId);
    579566            if (job != null) {
    580 
    581               // check if user is an admin, or granted to administer a job-parenting project,...
    582               if (!isAdministrator && !administrationGrantedProjects.Contains(job.Project))
    583                 AuthorizationManager.AuthorizeForJob(jobId, DT.Permission.Full); // ... or job owner
    584567
    585568              // note: allow solely state changes from "Online" to "StatisticsPending" = deletion request by user for HiveStatisticGenerator
  • trunk/HeuristicLab.Services.Hive/3.3/Manager/AuthorizationManager.cs

    r16117 r16209  
    3838    private const string NOT_AUTHORIZED_PROJECTRESOURCE = "Selected project is not authorized to access the requested resource";
    3939    private const string USER_NOT_IDENTIFIED = "User could not be identified";
     40    private const string JOB_NOT_EXISTENT = "Queried job could not be found";
    4041    private const string TASK_NOT_EXISTENT = "Queried task could not be found";
    4142    private const string PROJECT_NOT_EXISTENT = "Queried project could not be found";
     
    6061    public void AuthorizeForTask(Guid taskId, DT.Permission requiredPermission) {
    6162      if (ServiceLocator.Instance.RoleVerifier.IsInRole(HiveRoles.Slave)) return; // slave-users can access all tasks
     63      if (ServiceLocator.Instance.RoleVerifier.IsInRole(HiveRoles.Administrator)) return; // administrator can access all tasks
     64      var currentUserId = UserManager.CurrentUserId;
    6265      var pm = PersistenceManager;
    6366      var taskDao = pm.TaskDao;
     67      var projectDao = pm.ProjectDao;
    6468      pm.UseTransaction(() => {
    6569        var task = taskDao.GetById(taskId);
    6670        if (task == null) throw new SecurityException(TASK_NOT_EXISTENT);
     71
     72        // check if user is granted to administer a job-parenting project
     73        var administrationGrantedProjects = projectDao
     74          .GetAdministrationGrantedProjectsForUser(currentUserId)
     75          .ToList();
     76        if (administrationGrantedProjects.Contains(task.Job.Project)) return;
     77
    6778        AuthorizeJob(pm, task.JobId, requiredPermission);
    6879      });
     
    7081
    7182    public void AuthorizeForJob(Guid jobId, DT.Permission requiredPermission) {
    72       var pm = PersistenceManager;
    73       pm.UseTransaction(() => {
     83      if (ServiceLocator.Instance.RoleVerifier.IsInRole(HiveRoles.Administrator)) return; // administrator can access all jobs
     84      var currentUserId = UserManager.CurrentUserId;
     85      var pm = PersistenceManager;
     86      var jobDao = pm.JobDao;
     87      var projectDao = pm.ProjectDao;
     88      pm.UseTransaction(() => {
     89        var job = jobDao.GetById(jobId);
     90        if(job == null) throw new SecurityException(JOB_NOT_EXISTENT);
     91
     92        // check if user is granted to administer a job-parenting project
     93        var administrationGrantedProjects = projectDao
     94          .GetAdministrationGrantedProjectsForUser(currentUserId)
     95          .ToList();
     96        if (administrationGrantedProjects.Contains(job.Project)) return;
     97
    7498        AuthorizeJob(pm, jobId, requiredPermission);
    7599      });
Note: See TracChangeset for help on using the changeset viewer.