Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive/3.4/HiveService.cs @ 6463

Last change on this file since 6463 was 6463, checked in by cneumuel, 13 years ago

#1233

  • created user interface for experiment sharing
  • created UserManager which provides access to the users
  • inserted a lot of security and authorization checks serverside
  • minor fixes in experiment manager
File size: 24.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.ServiceModel;
26using HeuristicLab.Services.Hive.Common;
27using HeuristicLab.Services.Hive.Common.DataTransfer;
28using HeuristicLab.Services.Hive.Common.ServiceContracts;
29
30namespace HeuristicLab.Services.Hive {
31
32  /// <summary>
33  /// Implementation of the Hive service (interface <see cref="IHiveService"/>).
34  /// We need 'IgnoreExtensionDataObject' Attribute for the slave to work.
35  /// </summary>
36  [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, IgnoreExtensionDataObject = true)]
37  public class HiveService : IHiveService {
38    private DataAccess.IHiveDao dao {
39      get { return ServiceLocator.Instance.HiveDao; }
40    }
41    private IAuthenticationManager authen {
42      get { return ServiceLocator.Instance.AuthenticationManager; }
43    }
44    private IAuthorizationManager author {
45      get { return ServiceLocator.Instance.AuthorizationManager; }
46    }
47    private ITransactionManager trans {
48      get { return ServiceLocator.Instance.TransactionManager; }
49    }
50    private ILifecycleManager lifecycleManager {
51      get { return ServiceLocator.Instance.LifecycleManager; }
52    }
53    private IUserManager userManager {
54      get { return ServiceLocator.Instance.UserManager; }
55    }
56    private HeartbeatManager heartbeatManager {
57      get { return ServiceLocator.Instance.HeartbeatManager; }
58    }
59
60    #region Job Methods
61    public Guid AddJob(Job job, JobData jobData, IEnumerable<Guid> resourceIds) {
62      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
63      return trans.UseTransaction(() => {
64        job.Id = dao.AddJob(job);
65        jobData.JobId = job.Id;
66        jobData.LastUpdate = DateTime.Now;
67        foreach (Guid slaveGroupId in resourceIds) {
68          dao.AssignJobToResource(job.Id, slaveGroupId);
69        }
70        dao.AddJobData(jobData);
71        dao.UpdateJobState(job.Id, JobState.Waiting, null, userManager.CurrentUserId, null);
72        return jobData.JobId;
73      }, false, true);
74    }
75
76    public Guid AddChildJob(Guid parentJobId, Job job, JobData jobData) {
77      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
78      return trans.UseTransaction(() => {
79        job.ParentJobId = parentJobId;
80        return AddJob(job, jobData, dao.GetAssignedResources(parentJobId).Select(x => x.Id));
81      }, false, true);
82    }
83
84    public Job GetJob(Guid jobId) {
85      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
86      author.AuthorizeForJob(jobId, Permission.Read);
87      return dao.GetJob(jobId);
88    }
89
90    public IEnumerable<Job> GetJobs() {
91      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
92      var jobs = dao.GetJobs(x => true);
93      foreach (var job in jobs)
94        author.AuthorizeForJob(job.Id, Permission.Read);
95      return jobs;
96    }
97
98    public IEnumerable<LightweightJob> GetLightweightJobs(IEnumerable<Guid> jobIds) {
99      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
100      var jobs = dao.GetJobs(x => jobIds.Contains(x.JobId)).Select(x => new LightweightJob(x)).ToArray();
101      foreach (var job in jobs)
102        author.AuthorizeForJob(job.Id, Permission.Read);
103      return jobs;
104    }
105
106    public IEnumerable<LightweightJob> GetLightweightChildJobs(Guid? parentJobId, bool recursive, bool includeParent) {
107      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
108      var jobs = GetChildJobs(parentJobId, recursive, includeParent).Select(x => new LightweightJob(x)).ToArray();
109      foreach (var job in jobs)
110        author.AuthorizeForJob(job.Id, Permission.Read);
111      return jobs;
112    }
113
114    public IEnumerable<LightweightJob> GetLightweightExperimentJobs(Guid experimentId) {
115      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
116      author.AuthorizeForExperiment(experimentId, Permission.Read);
117      return dao.GetJobs(x => x.HiveExperimentId == experimentId).Select(x => new LightweightJob(x)).ToArray();
118    }
119
120    public JobData GetJobData(Guid jobId) {
121      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
122      author.AuthorizeForJob(jobId, Permission.Read);
123      return dao.GetJobData(jobId);
124    }
125
126    public void UpdateJob(Job job) {
127      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
128      author.AuthorizeForJob(job.Id, Permission.Full);
129      trans.UseTransaction(() => {
130        dao.UpdateJob(job);
131      });
132    }
133
134    public void UpdateJobData(Job job, JobData jobData) {
135      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
136      author.AuthorizeForJob(job.Id, Permission.Full);
137      author.AuthorizeForJob(jobData.JobId, Permission.Full);
138      //trans.UseTransaction(() => { // cneumuel: try without transaction
139      jobData.LastUpdate = DateTime.Now;
140      dao.UpdateJob(job);
141      dao.UpdateJobData(jobData);
142      //}, false, true);
143    }
144
145    public void DeleteJob(Guid jobId) {
146      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
147      author.AuthorizeForJob(jobId, Permission.Full);
148      trans.UseTransaction(() => {
149        dao.DeleteJob(jobId);
150      });
151    }
152
153    public void DeleteChildJobs(Guid parentJobId) {
154      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
155      author.AuthorizeForJob(parentJobId, Permission.Full);
156      trans.UseTransaction(() => {
157        var jobs = GetChildJobs(parentJobId, true, false);
158        foreach (var job in jobs) {
159          dao.DeleteJob(job.Id);
160          dao.DeleteJobData(job.Id);
161        };
162      });
163    }
164
165    public Job UpdateJobState(Guid jobId, JobState jobState, Guid? slaveId, Guid? userId, string exception) {
166      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
167      author.AuthorizeForJob(jobId, Permission.Full);
168      return trans.UseTransaction(() => {
169        Job job = dao.UpdateJobState(jobId, jobState, slaveId, userId, exception);
170
171        if (job.Command.HasValue && job.Command.Value == Command.Pause && job.State == JobState.Paused) {
172          job.Command = null;
173        } else if (job.Command.HasValue && job.Command.Value == Command.Abort && job.State == JobState.Aborted) {
174          job.Command = null;
175        } else if (job.Command.HasValue && job.Command.Value == Command.Stop && job.State == JobState.Aborted) {
176          job.Command = null;
177        } else if (jobState == JobState.Paused && !job.Command.HasValue) {
178          // slave paused and uploaded the job (no user-command) -> set waiting.
179          job = dao.UpdateJobState(jobId, JobState.Waiting, slaveId, userId, exception);
180        }
181
182        dao.UpdateJob(job);
183        return job;
184      });
185    }
186
187    public IEnumerable<Job> GetJobsByResourceId(Guid resourceId) {
188      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
189      var jobs = trans.UseTransaction(() => dao.GetJobsByResourceId(resourceId));
190      foreach(var job in jobs)
191        author.AuthorizeForJob(job.Id, Permission.Read);
192      return jobs;
193    }
194    #endregion
195
196    #region Job Control Methods
197    public void StopJob(Guid jobId) {
198      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
199      author.AuthorizeForJob(jobId, Permission.Full);
200      trans.UseTransaction(() => {
201        var job = dao.GetJob(jobId);
202        if (job.State == JobState.Calculating || job.State == JobState.Transferring) {
203          job.Command = Command.Stop;
204          dao.UpdateJob(job);
205        } else {
206          if (job.State != JobState.Aborted && job.State != JobState.Finished && job.State != JobState.Failed) {
207            job = UpdateJobState(jobId, JobState.Aborted, null, null, string.Empty);
208          }
209        }
210      });
211    }
212
213    public void PauseJob(Guid jobId) {
214      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
215      author.AuthorizeForJob(jobId, Permission.Full);
216      trans.UseTransaction(() => {
217        var job = dao.GetJob(jobId);
218        if (job.State == JobState.Calculating || job.State == JobState.Transferring) {
219          job.Command = Command.Pause;
220          dao.UpdateJob(job);
221        } else {
222          job = UpdateJobState(jobId, JobState.Paused, null, null, string.Empty);
223        }
224      });
225    }
226
227    public void RestartJob(Guid jobId) {
228      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
229      author.AuthorizeForJob(jobId, Permission.Full);
230      trans.UseTransaction(() => {
231        Job job = dao.UpdateJobState(jobId, JobState.Waiting, null, userManager.CurrentUserId, string.Empty);
232        job.Command = null;
233        dao.UpdateJob(job);
234      });
235    }
236    #endregion
237
238    #region HiveExperiment Methods
239    public HiveExperiment GetHiveExperiment(Guid id) {
240      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
241      author.AuthorizeForExperiment(id, Permission.Read);
242      var hiveExperiment = dao.GetHiveExperiments(x =>
243            x.HiveExperimentId == id
244            && (x.OwnerUserId == userManager.CurrentUserId || x.HiveExperimentPermissions.Count(hep => hep.Permission != Permission.NotAllowed && hep.GrantedUserId == userManager.CurrentUserId) > 0)
245          ).FirstOrDefault();
246      if (hiveExperiment != null) hiveExperiment.Permission = dao.GetPermissionForExperiment(hiveExperiment.Id, userManager.CurrentUserId);
247      return hiveExperiment;
248    }
249
250    public IEnumerable<HiveExperiment> GetHiveExperiments() {
251      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
252      var hiveExperiments = dao.GetHiveExperiments(x => x.OwnerUserId == userManager.CurrentUserId || x.HiveExperimentPermissions.Count(hep => hep.Permission != Permission.NotAllowed && hep.GrantedUserId == userManager.CurrentUserId) > 0);
253      foreach (var he in hiveExperiments) {
254        author.AuthorizeForExperiment(he.Id, Permission.Read);
255        he.Permission = dao.GetPermissionForExperiment(he.Id, userManager.CurrentUserId);
256      }
257      return hiveExperiments;
258    }
259
260    public IEnumerable<HiveExperiment> GetAllHiveExperiments() {
261      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
262      var hiveExperiments = dao.GetHiveExperiments(x => true);
263      foreach (var he in hiveExperiments) // no authorization here, since this method is admin-only! (admin is allowed to read all jobs)
264        he.Permission = dao.GetPermissionForExperiment(he.Id, userManager.CurrentUserId);
265      return hiveExperiments;
266    }
267
268    public Guid AddHiveExperiment(HiveExperiment hiveExperimentDto) {
269      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
270      return trans.UseTransaction(() => {
271        hiveExperimentDto.OwnerUserId = userManager.CurrentUserId;
272        hiveExperimentDto.DateCreated = DateTime.Now;
273        return dao.AddHiveExperiment(hiveExperimentDto);
274      });
275    }
276
277    public void UpdateHiveExperiment(HiveExperiment hiveExperimentDto) {
278      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
279      author.AuthorizeForExperiment(hiveExperimentDto.Id, Permission.Full);
280      trans.UseTransaction(() => {
281        dao.UpdateHiveExperiment(hiveExperimentDto);
282      });
283    }
284
285    public void DeleteHiveExperiment(Guid hiveExperimentId) {
286      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
287      author.AuthorizeForExperiment(hiveExperimentId, Permission.Full);
288      trans.UseTransaction(() => {
289        HiveExperiment he = dao.GetHiveExperiment(hiveExperimentId);
290        dao.DeleteHiveExperiment(hiveExperimentId); // child jobs will be deleted by db-trigger
291      });
292    }
293    #endregion
294
295    #region HiveExperimentPermission Methods
296    public void GrantPermission(Guid hiveExperimentId, Guid grantedUserId, Permission permission) {
297      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
298      trans.UseTransaction(() => {
299        HiveExperiment he = dao.GetHiveExperiment(hiveExperimentId);
300        if (he == null) throw new FaultException<FaultReason>(new FaultReason("Could not find hiveExperiment with id " + hiveExperimentId));
301        Permission perm = dao.GetPermissionForExperiment(he.Id, userManager.CurrentUserId);
302        if (perm != Permission.Full) throw new FaultException<FaultReason>(new FaultReason("Not allowed to grant permissions for this experiment"));
303        dao.SetHiveExperimentPermission(hiveExperimentId, userManager.CurrentUserId, grantedUserId, permission);
304      });
305    }
306
307    public void RevokePermission(Guid hiveExperimentId, Guid grantedUserId) {
308      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
309      trans.UseTransaction(() => {
310        HiveExperiment he = dao.GetHiveExperiment(hiveExperimentId);
311        if (he == null) throw new FaultException<FaultReason>(new FaultReason("Could not find hiveExperiment with id " + hiveExperimentId));
312        Permission perm = dao.GetPermissionForExperiment(he.Id, userManager.CurrentUserId);
313        if (perm != Permission.Full) throw new FaultException<FaultReason>(new FaultReason("Not allowed to grant permissions for this experiment"));
314        dao.SetHiveExperimentPermission(hiveExperimentId, userManager.CurrentUserId, grantedUserId, Permission.NotAllowed);
315      });
316    }
317    public IEnumerable<HiveExperimentPermission> GetHiveExperimentPermissions(Guid hiveExperimentId) {
318      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
319      return trans.UseTransaction(() => {
320        Permission currentUserPermission = dao.GetPermissionForExperiment(hiveExperimentId, userManager.CurrentUserId);
321        if (currentUserPermission != Permission.Full) throw new FaultException<FaultReason>(new FaultReason("Not allowed to list permissions for this experiment"));
322        return dao.GetHiveExperimentPermissions(x => x.HiveExperimentId == hiveExperimentId);
323      });
324    }
325    #endregion
326
327    #region Login Methods
328    public void Hello(Slave slaveInfo) {
329      authen.AuthenticateForAnyRole(HiveRoles.Slave);
330      trans.UseTransaction(() => {
331        var slave = dao.GetSlave(slaveInfo.Id);
332
333        if (slave == null) {
334          dao.AddSlave(slaveInfo);
335        } else {
336          var dbSlave = dao.GetSlave(slaveInfo.Id);
337
338          dbSlave.Name = slaveInfo.Name;
339          dbSlave.Description = slaveInfo.Description;
340
341          dbSlave.Cores = slaveInfo.Cores;
342          dbSlave.CpuArchitecture = slaveInfo.CpuArchitecture;
343          dbSlave.CpuSpeed = slaveInfo.CpuSpeed;
344          dbSlave.FreeCores = slaveInfo.FreeCores;
345          dbSlave.FreeMemory = slaveInfo.FreeMemory;
346          dbSlave.Memory = slaveInfo.Memory;
347          dbSlave.OperatingSystem = slaveInfo.OperatingSystem;
348
349          dbSlave.LastHeartbeat = DateTime.Now;
350          dbSlave.SlaveState = SlaveState.Idle;
351
352          // don't update those properties: dbSlave.IsAllowedToCalculate, dbSlave.ParentResourceId
353
354          dao.UpdateSlave(dbSlave);
355        }
356      });
357    }
358
359    public void GoodBye(Guid slaveId) {
360      authen.AuthenticateForAnyRole(HiveRoles.Slave);
361      trans.UseTransaction(() => {
362        var slave = dao.GetSlave(slaveId);
363        if (slave != null) {
364          slave.SlaveState = SlaveState.Offline;
365          dao.UpdateSlave(slave);
366        }
367      });
368    }
369    #endregion
370
371    #region Heartbeat Methods
372    public List<MessageContainer> Heartbeat(Heartbeat heartbeat) {
373      authen.AuthenticateForAnyRole(HiveRoles.Slave);
374      TriggerLifecycle(false);
375      return trans.UseTransaction(() => heartbeatManager.ProcessHeartbeat(heartbeat));
376    }
377    #endregion
378
379    #region Plugin Methods
380    public Guid AddPlugin(Plugin plugin, List<PluginData> pluginDatas) {
381      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
382      return trans.UseTransaction(() => {
383        plugin.UserId = userManager.CurrentUserId;
384        plugin.DateCreated = DateTime.Now;
385
386        var existing = dao.GetPlugins(x => x.Hash != null).Where(x => x.Hash.SequenceEqual(plugin.Hash));
387        if (existing.Count() > 0) {
388          // a plugin already exists.
389          throw new FaultException<PluginAlreadyExistsFault>(new PluginAlreadyExistsFault(existing.Single().Id));
390        }
391
392        Guid pluginId = dao.AddPlugin(plugin);
393        foreach (PluginData pluginData in pluginDatas) {
394          pluginData.PluginId = pluginId;
395          dao.AddPluginData(pluginData);
396        }
397        return pluginId;
398      });
399    }
400
401    public Plugin GetPlugin(Guid pluginId) {
402      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
403      return dao.GetPlugin(pluginId);
404    }
405
406    public Plugin GetPluginByHash(byte[] hash) {
407      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
408      return dao.GetPlugins(x => x.Hash == hash).FirstOrDefault();
409    }
410
411    // note: this is a possible security problem, since a client is able to download all plugins, which may contain proprietary code (which can be disassembled)
412    //       change so that only with GetPluginByHash it is possible to download plugins
413    public IEnumerable<Plugin> GetPlugins() {
414      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
415      return dao.GetPlugins(x => x.Hash != null);
416    }
417
418    public IEnumerable<PluginData> GetPluginDatas(List<Guid> pluginIds) {
419      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
420      var pluginDatas = new List<PluginData>();
421      return trans.UseTransaction(() => {
422        foreach (Guid guid in pluginIds) {
423          pluginDatas.AddRange(dao.GetPluginDatas(x => x.PluginId == guid).ToList());
424        }
425        return pluginDatas;
426      });
427    }
428
429    public void DeletePlugin(Guid pluginId) {
430      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
431      dao.DeletePlugin(pluginId);
432    }
433    #endregion
434
435    #region Slave Methods
436    public Guid AddSlave(Slave slave) {
437      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
438      return trans.UseTransaction(() => dao.AddSlave(slave));
439    }
440
441    public Guid AddSlaveGroup(SlaveGroup slaveGroup) {
442      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
443      return trans.UseTransaction(() => dao.AddSlaveGroup(slaveGroup));
444    }
445
446    public Slave GetSlave(Guid slaveId) {
447      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
448      return dao.GetSlave(slaveId);
449    }
450
451    public SlaveGroup GetSlaveGroup(Guid slaveGroupId) {
452      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
453      return dao.GetSlaveGroup(slaveGroupId);
454    }
455
456    public IEnumerable<Slave> GetSlaves() {
457      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
458      return dao.GetSlaves(x => true);
459    }
460
461    public IEnumerable<SlaveGroup> GetSlaveGroups() {
462      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
463      return dao.GetSlaveGroups(x => true);
464    }
465
466    public void UpdateSlave(Slave slave) {
467      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
468      trans.UseTransaction(() => {
469        dao.UpdateSlave(slave);
470      });
471    }
472
473    public void UpdateSlaveGroup(SlaveGroup slaveGroup) {
474      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
475      trans.UseTransaction(() => {
476        dao.UpdateSlaveGroup(slaveGroup);
477      });
478    }
479
480    public void DeleteSlave(Guid slaveId) {
481      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
482      trans.UseTransaction(() => {
483        dao.DeleteSlave(slaveId);
484      });
485    }
486
487    public void DeleteSlaveGroup(Guid slaveGroupId) {
488      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
489      trans.UseTransaction(() => {
490        dao.DeleteSlaveGroup(slaveGroupId);
491      });
492    }
493
494    public void AddResourceToGroup(Guid slaveGroupId, Guid resourceId) {
495      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
496      trans.UseTransaction(() => {
497        var resource = dao.GetResource(resourceId);
498        resource.ParentResourceId = slaveGroupId;
499        dao.UpdateResource(resource);
500      });
501    }
502
503    public void RemoveResourceFromGroup(Guid slaveGroupId, Guid resourceId) {
504      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
505      trans.UseTransaction(() => {
506        var resource = dao.GetResource(resourceId);
507        resource.ParentResourceId = null;
508        dao.UpdateResource(resource);
509      });
510    }
511
512    public Guid GetResourceId(string resourceName) {
513      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
514      return trans.UseTransaction(() => {
515        var resource = dao.GetResources(x => x.Name == resourceName).FirstOrDefault();
516        if (resource != null) {
517          return resource.Id;
518        } else {
519          return Guid.Empty;
520        }
521      });
522    }
523
524    public void TriggerLifecycle(bool force) {
525      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Slave);
526      // use a serializable transaction here to ensure not two threads execute this simultaniously (mutex-lock would not work since IIS may use multiple AppDomains)
527      trans.UseTransaction(() => {
528        DateTime lastCleanup = dao.GetLastCleanup();
529        if (force || DateTime.Now - lastCleanup > TimeSpan.FromSeconds(59)) {
530          dao.SetLastCleanup(DateTime.Now);
531          lifecycleManager.Cleanup();
532        }
533      }, true);
534    }
535    #endregion
536
537    #region Downtime Methods
538    public Guid AddDowntime(Downtime downtime) {
539      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
540      return trans.UseTransaction(() => dao.AddDowntime(downtime));
541    }
542
543    public void DeleteDowntime(Guid downtimeId) {
544      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
545      trans.UseTransaction(() => {
546        dao.DeleteDowntime(downtimeId);
547      });
548    }
549
550    public void UpdateDowntime(Downtime downtime) {
551      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
552      trans.UseTransaction(() => {
553        dao.UpdateDowntime(downtime);
554      });
555    }
556
557    public IEnumerable<Downtime> GetDowntimesForResource(Guid resourceId) {
558      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
559      return trans.UseTransaction(() => dao.GetDowntimes(x => x.ResourceId == resourceId));
560    }
561    #endregion
562
563    #region User Methods
564    public string GetUsernameByUserId(Guid userId) {
565      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
566      var user = ServiceLocator.Instance.UserManager.GetUserById(userId);
567      if (user != null)
568        return user.UserName;
569      else
570        return null;
571    }
572
573    public Guid GetUserIdByUsername(string username) {
574      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
575      var user = ServiceLocator.Instance.UserManager.GetUserByName(username);
576      return user != null ? (Guid)user.ProviderUserKey : Guid.Empty;
577    }
578    #endregion
579
580    #region Helper Methods
581    private IEnumerable<Job> GetChildJobs(Guid? parentJobId, bool recursive, bool includeParent) {
582      var jobs = new List<Job>(dao.GetJobs(x => parentJobId == null ? !x.ParentJobId.HasValue : x.ParentJobId.Value == parentJobId));
583
584      if (recursive) {
585        var childs = new List<Job>();
586        foreach (var job in jobs) {
587          childs.AddRange(GetChildJobs(job.Id, recursive, false));
588        }
589        jobs.AddRange(childs);
590      }
591
592      if (includeParent) jobs.Add(GetJob(parentJobId.Value));
593      return jobs;
594    }
595    #endregion
596  }
597}
Note: See TracBrowser for help on using the repository browser.