Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/20/11 17:04:35 (13 years ago)
Author:
cneumuel
Message:

#1233

  • added methods for granting and revoking hive experiment permissions
  • added unit tests for hive experiment permissions
  • added a status webpage to see how some statistics and current status
Location:
branches/HeuristicLab.Hive-3.4/sources
Files:
2 added
9 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.Common/3.4/DataTransfer/HiveExperiment.cs

    r6372 r6457  
    3535    [DataMember]
    3636    public DateTime? LastAccessed { get; set; }
     37    [DataMember]
     38    public Permission Permission { get; set; } // the permission for the currently logged in user
    3739
    3840    /* ==== some computed statistics ==== */
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.Common/3.4/DataTransfer/Permission.cs

    r6372 r6457  
    3333
    3434    /// <summary>
    35     /// User can also Pause, Stop, Restart experiments
     35    /// User can also experiments
    3636    /// </summary>
    3737    Write,
    3838
    3939    /// <summary>
    40     /// User can also delete experiment
     40    /// User can pause, stop, restart, delete experiment. can also grant other
    4141    /// </summary>
    4242    Full
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.Common/3.4/ServiceContracts/IHiveService.cs

    r6452 r6457  
    110110    #endregion
    111111
     112    #region HiveExperimentPermission Methods
     113    void GrantPermission(Guid hiveExperimentId, Guid grantedUserId, Permission permission);
     114    void RevokePermission(Guid hiveExperimentId, Guid grantedUserId);
     115    #endregion
     116
    112117    #region Login Methods
    113118    [OperationContract]
     
    199204    IEnumerable<Downtime> GetDowntimesForResource(Guid resourceId);
    200205    #endregion
     206
    201207  }
    202208}
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.DataAccess/3.4/HiveDao.cs

    r6452 r6457  
    3131    public static HiveDataContext CreateContext(bool longRunning = false) {
    3232      var context = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString);
    33       if (longRunning) context.CommandTimeout = (int)Settings.Default.LongRunningDatabaseCommandTimeout.TotalSeconds;     
     33      if (longRunning) context.CommandTimeout = (int)Settings.Default.LongRunningDatabaseCommandTimeout.TotalSeconds;
    3434      return context;
    3535    }
     
    319319      }
    320320    }
     321
     322    /// <summary>
     323    /// Sets the permissions for a experiment. makes sure that only one permission per user exists.
     324    /// </summary>
     325    public void SetHiveExperimentPermission(Guid hiveExperimentId, Guid grantedByUserId, Guid grantedUserId, Permission permission) {
     326      using (var db = CreateContext()) {
     327        HiveExperimentPermission hiveExperimentPermission = db.HiveExperimentPermissions.SingleOrDefault(x => x.HiveExperimentId == hiveExperimentId && x.GrantedUserId == grantedUserId);
     328        if (hiveExperimentPermission != null) {
     329          if (permission == Permission.NotAllowed) {
     330            // not allowed, delete
     331            db.HiveExperimentPermissions.DeleteOnSubmit(hiveExperimentPermission);
     332          } else {
     333            // update
     334            hiveExperimentPermission.Permission = permission;
     335            hiveExperimentPermission.GrantedByUserId = grantedByUserId; // update grantedByUserId, always the last "granter" is stored
     336          }
     337        } else {
     338          // insert
     339          if (permission != Permission.NotAllowed) {
     340            hiveExperimentPermission = new HiveExperimentPermission() { HiveExperimentId = hiveExperimentId, GrantedByUserId = grantedByUserId, GrantedUserId = grantedUserId, Permission = permission };
     341            db.HiveExperimentPermissions.InsertOnSubmit(hiveExperimentPermission);
     342          }
     343        }
     344        db.SubmitChanges();
     345      }
     346    }
    321347    #endregion
    322348
     
    596622    public Permission GetPermissionForExperiment(Guid experimentId, Guid userId) {
    597623      using (var db = CreateContext()) {
     624        HiveExperiment hiveExperiment = db.HiveExperiments.SingleOrDefault(x => x.HiveExperimentId == experimentId);
     625        if (hiveExperiment == null) return Permission.NotAllowed;
     626        if (hiveExperiment.OwnerUserId == userId) return Permission.Full;
    598627        HiveExperimentPermission permission = db.HiveExperimentPermissions.SingleOrDefault(p => p.HiveExperimentId == experimentId && p.GrantedUserId == userId);
    599628        return permission != null ? permission.Permission : Permission.NotAllowed;
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.DataAccess/3.4/Interfaces/IHiveDao.cs

    r6452 r6457  
    6969    void UpdateHiveExperimentPermission(DT.HiveExperimentPermission dto);
    7070    void DeleteHiveExperimentPermission(Guid hiveExperimentId, Guid grantedUserId);
     71    void SetHiveExperimentPermission(Guid hiveExperimentId, Guid grantedByUserId, Guid grantedUserId, Permission permission);
    7172    #endregion
    7273
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.Tests/Mocks/MockAuthorizationManager.cs

    r6372 r6457  
    2424namespace HeuristicLab.Services.Hive.Tests.Mocks {
    2525  public class MockAuthorizationManager : IAuthorizationManager {
    26     private Guid userId = new Guid("6C7911A1-46EA-4E51-97DF-5582653AAFCE");
     26    public static Guid MockUserId1 = new Guid("6C7911A1-46EA-4E51-97DF-5582653AAFCE");
     27    public static Guid MockUserId2 = new Guid("897660EB-C90F-4054-988C-D39D530A0A02");
     28    public static Guid MockUserId3 = new Guid("9F7B4EA8-A38D-4BC2-802F-E148AC7A6A87");
     29
     30    private Guid userId = MockUserId1;
    2731
    2832    public Guid UserId {
     
    3741      // o.k.
    3842    }
    39 
     43   
     44    internal void SetUserId(Guid id) {
     45      this.userId = id;
     46    }
    4047  }
    4148}
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.Tests/Mocks/MockServiceLocator.cs

    r6452 r6457  
    2222using HeuristicLab.Services.Hive.DataAccess;
    2323using HeuristicLab.Services.Hive.Tests.Mocks;
     24using System;
    2425
    2526namespace HeuristicLab.Services.Hive.Tests {
     
    3536    }
    3637
     38    private MockAuthorizationManager mockAuthorizationManager = new MockAuthorizationManager();
    3739    public IAuthorizationManager AuthorizationManager {
    38       get { return new MockAuthorizationManager(); }
     40      get { return mockAuthorizationManager; }
    3941    }
    4042
     
    5355      get { return defaultServiceLocator.HeartbeatManager; }
    5456    }
     57
     58    #region Special mocking methods
     59    public void SetCurrentUser(Guid id) {
     60      mockAuthorizationManager.SetUserId(id);
     61    }
     62
     63    #endregion
    5564  }
    5665}
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.Tests/ServiceTests.cs

    r6452 r6457  
    2626using HeuristicLab.Services.Hive.Common.DataTransfer;
    2727using HeuristicLab.Services.Hive.Common.ServiceContracts;
     28using HeuristicLab.Services.Hive.Tests.Mocks;
    2829using Microsoft.VisualStudio.TestTools.UnitTesting;
    2930using DT = HeuristicLab.Services.Hive.Common.DataTransfer;
     
    3233  [TestClass]
    3334  public class ServiceTests {
     35    // use the mock service locator to modify service properties (such as current user)
     36    private static MockServiceLocator mockServiceLocator;
    3437
    3538    [ClassInitialize]
    3639    public static void MyClassInitialize(TestContext testContext) {
    37       ServiceLocator.Instance = new MockServiceLocator(ServiceLocator.Instance);
     40      mockServiceLocator = new MockServiceLocator(ServiceLocator.Instance);
     41      ServiceLocator.Instance = mockServiceLocator;
    3842    }
    3943
     
    4751
    4852      // create hive experiment
    49       DT.HiveExperiment experiment = new DT.HiveExperiment() {
    50         Name = "TestExperiment",
    51         Description = ""
    52       };
     53      DT.HiveExperiment experiment = new DT.HiveExperiment() { Name = "TestExperiment", Description = "" };
    5354
    5455      // create job
    55       DT.Job job = new DT.Job() {
    56         CoresNeeded = 1,
    57         MemoryNeeded = 0,
    58         Priority = 0
    59       };
     56      DT.Job job = new DT.Job() { CoresNeeded = 1, MemoryNeeded = 0, Priority = 0 };
    6057      job.State = JobState.Offline;
    6158      job.StateLog.Add(new StateLog { State = JobState.Offline, DateTime = DateTime.Now });
     
    7875      plugin1.DateCreated = DateTime.Now;
    7976      plugin1.Hash = hash;
    80      
     77
    8178      DT.PluginData pluginData1 = new DT.PluginData();
    8279      pluginData1.FileName = "Tests.MyPlugin-1.0.dll";
     
    206203
    207204      // create hive experiment
    208       DT.HiveExperiment experiment = new DT.HiveExperiment() {
    209         Name = "TestExperiment",
    210         Description = ""
    211       };
     205      DT.HiveExperiment experiment = new DT.HiveExperiment() { Name = "TestExperiment", Description = "" };
    212206
    213207      // create parent job
     
    304298      service.DeleteSlave(slave.Id);
    305299    }
     300
     301
     302    [TestMethod]
     303    public void TestHiveExperimentPermissions() {
     304      var service = GetLocalService();
     305      mockServiceLocator.SetCurrentUser(MockAuthorizationManager.MockUserId1);
     306
     307      // create hive experiment
     308      DT.HiveExperiment e1 = new DT.HiveExperiment() { Name = "TestExperiment", Description = "" };
     309      e1.Id = service.AddHiveExperiment(e1);
     310     
     311      var e1loaded = service.GetHiveExperiment(e1.Id);
     312      Assert.AreEqual(Permission.Full, e1loaded.Permission);
     313
     314      // change to user2
     315      mockServiceLocator.SetCurrentUser(MockAuthorizationManager.MockUserId2);
     316      e1loaded = service.GetHiveExperiment(e1.Id);
     317      Assert.AreEqual(null, e1loaded); // no access
     318
     319      // user2 should not be able to grant permissions
     320      try {
     321        service.GrantPermission(e1.Id, MockAuthorizationManager.MockUserId2, Permission.ReadOnly);
     322        Assert.Fail("Should not be possible to grant permission due to missing permission for User2");
     323      }
     324      catch { /* ok, cool */ }
     325
     326      // switch back to user1 (owner) and grant user2 permissions
     327      mockServiceLocator.SetCurrentUser(MockAuthorizationManager.MockUserId1);
     328      service.GrantPermission(e1.Id, MockAuthorizationManager.MockUserId2, Permission.ReadOnly);
     329
     330      // back to user2
     331      mockServiceLocator.SetCurrentUser(MockAuthorizationManager.MockUserId2);
     332      e1loaded = service.GetHiveExperiment(e1.Id);
     333      Assert.AreEqual(Permission.ReadOnly, e1loaded.Permission);
     334
     335      // user2 should still not be able to grant permissions
     336      try {
     337        service.GrantPermission(e1.Id, MockAuthorizationManager.MockUserId2, Permission.ReadOnly);
     338        Assert.Fail("Should not be possible to grant permission due to missing permission for User2");
     339      }
     340      catch { /* ok, cool */ }
     341
     342      // back to user1
     343      mockServiceLocator.SetCurrentUser(MockAuthorizationManager.MockUserId1);
     344      service.GrantPermission(e1.Id, MockAuthorizationManager.MockUserId2, Permission.Full);
     345
     346      // back to user2
     347      mockServiceLocator.SetCurrentUser(MockAuthorizationManager.MockUserId2);
     348      e1loaded = service.GetHiveExperiment(e1.Id);
     349      Assert.AreEqual(Permission.Full, e1loaded.Permission);
     350
     351      // grant rights to user3, now this should be possible due to full permissions
     352      service.GrantPermission(e1.Id, MockAuthorizationManager.MockUserId3, Permission.ReadOnly);
     353
     354      // back to user1 and revoke rights for user2
     355      mockServiceLocator.SetCurrentUser(MockAuthorizationManager.MockUserId1);
     356      service.RevokePermission(e1.Id, MockAuthorizationManager.MockUserId2);
     357
     358      // back to user2
     359      mockServiceLocator.SetCurrentUser(MockAuthorizationManager.MockUserId2);
     360      e1loaded = service.GetHiveExperiment(e1.Id);
     361      Assert.AreEqual(null, e1loaded); // no access
     362
     363      service.DeleteHiveExperiment(e1.Id);
     364    }
    306365  }
    307366}
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive/3.4/HiveService.cs

    r6452 r6457  
    4444    private IAuthorizationManager author {
    4545      get { return ServiceLocator.Instance.AuthorizationManager; }
    46     } 
     46    }
    4747    private ITransactionManager trans {
    4848      get { return ServiceLocator.Instance.TransactionManager; }
     
    212212    public HiveExperiment GetHiveExperiment(Guid id) {
    213213      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
    214       return dao.GetHiveExperiments(x =>
    215         x.HiveExperimentId == id
    216         && (x.OwnerUserId == author.UserId || x.HiveExperimentPermissions.Count(hep => hep.Permission != Permission.NotAllowed && hep.GrantedUserId == author.UserId) > 0)
    217       ).FirstOrDefault();
     214      var hiveExperiment = dao.GetHiveExperiments(x =>
     215            x.HiveExperimentId == id
     216            && (x.OwnerUserId == author.UserId || x.HiveExperimentPermissions.Count(hep => hep.Permission != Permission.NotAllowed && hep.GrantedUserId == author.UserId) > 0)
     217          ).FirstOrDefault();
     218      if (hiveExperiment != null) hiveExperiment.Permission = dao.GetPermissionForExperiment(hiveExperiment.Id, author.UserId);
     219      return hiveExperiment;
    218220    }
    219221
    220222    public IEnumerable<HiveExperiment> GetHiveExperiments() {
    221223      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
    222       return dao.GetHiveExperiments(x => x.OwnerUserId == author.UserId || x.HiveExperimentPermissions.Count(hep => hep.Permission != Permission.NotAllowed && hep.GrantedUserId == author.UserId) > 0);
     224      var hiveExperiments = dao.GetHiveExperiments(x => x.OwnerUserId == author.UserId || x.HiveExperimentPermissions.Count(hep => hep.Permission != Permission.NotAllowed && hep.GrantedUserId == author.UserId) > 0);
     225      foreach (var he in hiveExperiments)
     226        he.Permission = dao.GetPermissionForExperiment(he.Id, author.UserId);
     227      return hiveExperiments;
    223228    }
    224229
    225230    public IEnumerable<HiveExperiment> GetAllHiveExperiments() {
    226231      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
    227       return dao.GetHiveExperiments(x => true);
     232      var hiveExperiments = dao.GetHiveExperiments(x => true);
     233      foreach (var he in hiveExperiments)
     234        he.Permission = dao.GetPermissionForExperiment(he.Id, author.UserId);
     235      return hiveExperiments;
    228236    }
    229237
     
    249257        HiveExperiment he = dao.GetHiveExperiment(hiveExperimentId);
    250258        dao.DeleteHiveExperiment(hiveExperimentId); // child jobs will be deleted by db-trigger
     259      });
     260    }
     261    #endregion
     262
     263    #region HiveExperimentPermission Methods
     264    public void GrantPermission(Guid hiveExperimentId, Guid grantedUserId, Permission permission) {
     265      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
     266      trans.UseTransaction(() => {
     267        HiveExperiment he = dao.GetHiveExperiment(hiveExperimentId);
     268        if (he == null) throw new FaultException<FaultReason>(new FaultReason("Could not find hiveExperiment with id " + hiveExperimentId));
     269        Permission perm = dao.GetPermissionForExperiment(he.Id, author.UserId);
     270        if (perm != Permission.Full) throw new FaultException<FaultReason>(new FaultReason("Not allowed to grant permissions for this experiment"));
     271        dao.SetHiveExperimentPermission(hiveExperimentId, author.UserId, grantedUserId, permission);
     272      });
     273    }
     274
     275    public void RevokePermission(Guid hiveExperimentId, Guid grantedUserId) {
     276      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
     277      trans.UseTransaction(() => {
     278        HiveExperiment he = dao.GetHiveExperiment(hiveExperimentId);
     279        if (he == null) throw new FaultException<FaultReason>(new FaultReason("Could not find hiveExperiment with id " + hiveExperimentId));
     280        Permission perm = dao.GetPermissionForExperiment(he.Id, author.UserId);
     281        if (perm != Permission.Full) throw new FaultException<FaultReason>(new FaultReason("Not allowed to grant permissions for this experiment"));
     282        dao.SetHiveExperimentPermission(hiveExperimentId, author.UserId, grantedUserId, Permission.NotAllowed);
    251283      });
    252284    }
     
    461493    #endregion
    462494
     495    #region Downtime Methods
     496    public Guid AddDowntime(Downtime downtime) {
     497      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
     498      return trans.UseTransaction(() => dao.AddDowntime(downtime));
     499    }
     500
     501    public void DeleteDowntime(Guid downtimeId) {
     502      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
     503      trans.UseTransaction(() => {
     504        dao.DeleteDowntime(downtimeId);
     505      });
     506    }
     507
     508    public void UpdateDowntime(Downtime downtime) {
     509      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
     510      trans.UseTransaction(() => {
     511        dao.UpdateDowntime(downtime);
     512      });
     513    }
     514
     515    public IEnumerable<Downtime> GetDowntimesForResource(Guid resourceId) {
     516      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
     517      return trans.UseTransaction(() => dao.GetDowntimes(x => x.ResourceId == resourceId));
     518    }
     519    #endregion
     520
    463521    #region Helper Methods
    464522    private IEnumerable<Job> GetChildJobs(Guid? parentJobId, bool recursive, bool includeParent) {
     
    477535    }
    478536    #endregion
    479 
    480     #region Downtime Methods
    481     public Guid AddDowntime(Downtime downtime) {
    482       authen.AuthenticateForAnyRole(HiveRoles.Administrator);
    483       return trans.UseTransaction(() => dao.AddDowntime(downtime));
    484     }
    485 
    486     public void DeleteDowntime(Guid downtimeId) {
    487       authen.AuthenticateForAnyRole(HiveRoles.Administrator);
    488       trans.UseTransaction(() => {
    489         dao.DeleteDowntime(downtimeId);
    490       });
    491     }
    492 
    493     public void UpdateDowntime(Downtime downtime) {
    494       authen.AuthenticateForAnyRole(HiveRoles.Administrator);
    495       trans.UseTransaction(() => {
    496         dao.UpdateDowntime(downtime);
    497       });
    498     }
    499 
    500     public IEnumerable<Downtime> GetDowntimesForResource(Guid resourceId) {
    501       authen.AuthenticateForAnyRole(HiveRoles.Administrator);
    502       return trans.UseTransaction(() => dao.GetDowntimes(x => x.ResourceId == resourceId));
    503     }
    504     #endregion
    505537  }
    506538}
Note: See TracChangeset for help on using the changeset viewer.