Changeset 6457 for branches/HeuristicLab.Hive-3.4/sources
- Timestamp:
- 06/20/11 17:04:35 (13 years ago)
- 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 35 35 [DataMember] 36 36 public DateTime? LastAccessed { get; set; } 37 [DataMember] 38 public Permission Permission { get; set; } // the permission for the currently logged in user 37 39 38 40 /* ==== some computed statistics ==== */ -
branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.Common/3.4/DataTransfer/Permission.cs
r6372 r6457 33 33 34 34 /// <summary> 35 /// User can also Pause, Stop, Restartexperiments35 /// User can also experiments 36 36 /// </summary> 37 37 Write, 38 38 39 39 /// <summary> 40 /// User can also delete experiment40 /// User can pause, stop, restart, delete experiment. can also grant other 41 41 /// </summary> 42 42 Full -
branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.Common/3.4/ServiceContracts/IHiveService.cs
r6452 r6457 110 110 #endregion 111 111 112 #region HiveExperimentPermission Methods 113 void GrantPermission(Guid hiveExperimentId, Guid grantedUserId, Permission permission); 114 void RevokePermission(Guid hiveExperimentId, Guid grantedUserId); 115 #endregion 116 112 117 #region Login Methods 113 118 [OperationContract] … … 199 204 IEnumerable<Downtime> GetDowntimesForResource(Guid resourceId); 200 205 #endregion 206 201 207 } 202 208 } -
branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.DataAccess/3.4/HiveDao.cs
r6452 r6457 31 31 public static HiveDataContext CreateContext(bool longRunning = false) { 32 32 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; 34 34 return context; 35 35 } … … 319 319 } 320 320 } 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 } 321 347 #endregion 322 348 … … 596 622 public Permission GetPermissionForExperiment(Guid experimentId, Guid userId) { 597 623 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; 598 627 HiveExperimentPermission permission = db.HiveExperimentPermissions.SingleOrDefault(p => p.HiveExperimentId == experimentId && p.GrantedUserId == userId); 599 628 return permission != null ? permission.Permission : Permission.NotAllowed; -
branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.DataAccess/3.4/Interfaces/IHiveDao.cs
r6452 r6457 69 69 void UpdateHiveExperimentPermission(DT.HiveExperimentPermission dto); 70 70 void DeleteHiveExperimentPermission(Guid hiveExperimentId, Guid grantedUserId); 71 void SetHiveExperimentPermission(Guid hiveExperimentId, Guid grantedByUserId, Guid grantedUserId, Permission permission); 71 72 #endregion 72 73 -
branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.Tests/Mocks/MockAuthorizationManager.cs
r6372 r6457 24 24 namespace HeuristicLab.Services.Hive.Tests.Mocks { 25 25 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; 27 31 28 32 public Guid UserId { … … 37 41 // o.k. 38 42 } 39 43 44 internal void SetUserId(Guid id) { 45 this.userId = id; 46 } 40 47 } 41 48 } -
branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.Tests/Mocks/MockServiceLocator.cs
r6452 r6457 22 22 using HeuristicLab.Services.Hive.DataAccess; 23 23 using HeuristicLab.Services.Hive.Tests.Mocks; 24 using System; 24 25 25 26 namespace HeuristicLab.Services.Hive.Tests { … … 35 36 } 36 37 38 private MockAuthorizationManager mockAuthorizationManager = new MockAuthorizationManager(); 37 39 public IAuthorizationManager AuthorizationManager { 38 get { return new MockAuthorizationManager(); }40 get { return mockAuthorizationManager; } 39 41 } 40 42 … … 53 55 get { return defaultServiceLocator.HeartbeatManager; } 54 56 } 57 58 #region Special mocking methods 59 public void SetCurrentUser(Guid id) { 60 mockAuthorizationManager.SetUserId(id); 61 } 62 63 #endregion 55 64 } 56 65 } -
branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.Tests/ServiceTests.cs
r6452 r6457 26 26 using HeuristicLab.Services.Hive.Common.DataTransfer; 27 27 using HeuristicLab.Services.Hive.Common.ServiceContracts; 28 using HeuristicLab.Services.Hive.Tests.Mocks; 28 29 using Microsoft.VisualStudio.TestTools.UnitTesting; 29 30 using DT = HeuristicLab.Services.Hive.Common.DataTransfer; … … 32 33 [TestClass] 33 34 public class ServiceTests { 35 // use the mock service locator to modify service properties (such as current user) 36 private static MockServiceLocator mockServiceLocator; 34 37 35 38 [ClassInitialize] 36 39 public static void MyClassInitialize(TestContext testContext) { 37 ServiceLocator.Instance = new MockServiceLocator(ServiceLocator.Instance); 40 mockServiceLocator = new MockServiceLocator(ServiceLocator.Instance); 41 ServiceLocator.Instance = mockServiceLocator; 38 42 } 39 43 … … 47 51 48 52 // 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 = "" }; 53 54 54 55 // 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 }; 60 57 job.State = JobState.Offline; 61 58 job.StateLog.Add(new StateLog { State = JobState.Offline, DateTime = DateTime.Now }); … … 78 75 plugin1.DateCreated = DateTime.Now; 79 76 plugin1.Hash = hash; 80 77 81 78 DT.PluginData pluginData1 = new DT.PluginData(); 82 79 pluginData1.FileName = "Tests.MyPlugin-1.0.dll"; … … 206 203 207 204 // 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 = "" }; 212 206 213 207 // create parent job … … 304 298 service.DeleteSlave(slave.Id); 305 299 } 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 } 306 365 } 307 366 } -
branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive/3.4/HiveService.cs
r6452 r6457 44 44 private IAuthorizationManager author { 45 45 get { return ServiceLocator.Instance.AuthorizationManager; } 46 } 46 } 47 47 private ITransactionManager trans { 48 48 get { return ServiceLocator.Instance.TransactionManager; } … … 212 212 public HiveExperiment GetHiveExperiment(Guid id) { 213 213 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; 218 220 } 219 221 220 222 public IEnumerable<HiveExperiment> GetHiveExperiments() { 221 223 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; 223 228 } 224 229 225 230 public IEnumerable<HiveExperiment> GetAllHiveExperiments() { 226 231 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; 228 236 } 229 237 … … 249 257 HiveExperiment he = dao.GetHiveExperiment(hiveExperimentId); 250 258 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); 251 283 }); 252 284 } … … 461 493 #endregion 462 494 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 463 521 #region Helper Methods 464 522 private IEnumerable<Job> GetChildJobs(Guid? parentJobId, bool recursive, bool includeParent) { … … 477 535 } 478 536 #endregion 479 480 #region Downtime Methods481 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 #endregion505 537 } 506 538 }
Note: See TracChangeset
for help on using the changeset viewer.