Changeset 3964 for branches/HeuristicLab.Services.Authentication Prototype
- Timestamp:
- 06/26/10 13:11:44 (15 years ago)
- Location:
- branches/HeuristicLab.Services.Authentication Prototype
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Services.Authentication Prototype/Service/Provider/HeuristicLabRoleProvider.cs
r3963 r3964 24 24 } 25 25 db.SubmitChanges(); 26 db.Dispose(); 26 27 } 27 28 } … … 49 50 db.HeuristicLabRole.InsertOnSubmit(role); 50 51 db.SubmitChanges(); 52 db.Dispose(); 51 53 } 52 54 } … … 74 76 List<string> roleList = new List<string>(); 75 77 76 List<HeuristicLabRole> roles = new List<HeuristicLabRole>(DatabaseUtil.createDataClassesDataContext().HeuristicLabRole.Select(r => r)); 78 Persistence.DataClassesDataContext db = DatabaseUtil.createDataClassesDataContext(); 79 List<HeuristicLabRole> roles = new List<HeuristicLabRole>(db.HeuristicLabRole.Select(r => r)); 77 80 foreach (HeuristicLabRole r in roles) { 78 81 roleList.Add(r.RoleName); 79 82 } 83 db.Dispose(); 80 84 81 85 return roleList.ToArray(); … … 100 104 } 101 105 102 103 /// <summary>104 /// true if given username has exists in given Role, else false105 /// </summary>106 /// <param name="username"></param>107 /// <param name="roleName"></param>108 /// <returns>bool</returns>109 106 public override bool IsUserInRole(string username, string roleName) { 110 111 112 if (username != null && roleName != null)113 {114 returnValue = (context.HeuristicLabUserRole.Count(ur => ur.HeuristicLabRole.RoleName == roleName && ur.HeuristicLabUser.UserName == username) > 0);115 }116 107 bool returnValue = false; 108 DataClassesDataContext context = DatabaseUtil.createDataClassesDataContext(); 109 if (username != null && roleName != null) { 110 returnValue = (context.HeuristicLabUserRole.Count(ur => ur.HeuristicLabRole.RoleName == roleName && ur.HeuristicLabUser.UserName == username) > 0); 111 } 112 context.Dispose(); 113 return returnValue; 117 114 } 118 115 -
branches/HeuristicLab.Services.Authentication Prototype/UnitTests/HeuristicLabRoleProviderTest.cs
r3963 r3964 101 101 [TestMethod()] 102 102 public void IsUserInRoleTest() { 103 HeuristicLabRoleProvider target = new HeuristicLabRoleProvider(); // TODO: Initialize to an appropriate value 104 Dictionary<string, Persistence.HeuristicLabUser> users = new Dictionary<string, Persistence.HeuristicLabUser>(); 105 List<string> roles = new List<string>(); 106 users.Add("mholper", new Persistence.HeuristicLabUser("mholper", "foo", "password", "comment")); 107 108 roles.Add("admin"); 109 roles.Add("users"); 110 foreach (string role in roles) 111 { 112 target.CreateRole(role); 113 } 114 foreach (Persistence.HeuristicLabUser user in users.Values) 115 { 116 db.HeuristicLabUsers.InsertOnSubmit(user); 117 } 118 db.SubmitChanges(); 119 string[] rolesToTest = new string[1]; 120 rolesToTest[0] = "admin"; 121 target.AddUsersToRoles(users.Keys.ToArray(), rolesToTest); // roles.ToArray()); 122 Assert.IsTrue(target.IsUserInRole("mholper", "admin")); 123 Assert.IsFalse(target.IsUserInRole("mholper", "user")); 103 HeuristicLabRoleProvider target = new HeuristicLabRoleProvider(); // TODO: Initialize to an appropriate value 104 Dictionary<string, Persistence.HeuristicLabUser> users = new Dictionary<string, Persistence.HeuristicLabUser>(); 105 List<string> roles = new List<string>(); 106 users.Add("mholper", new Persistence.HeuristicLabUser("mholper", "foo", "password", "comment")); 107 108 roles.Add("admin"); 109 roles.Add("users"); 110 foreach (string role in roles) { 111 target.CreateRole(role); 112 } 113 foreach (Persistence.HeuristicLabUser user in users.Values) { 114 db.HeuristicLabUsers.InsertOnSubmit(user); 115 } 116 db.SubmitChanges(); 117 string[] rolesToTest = new string[1]; 118 rolesToTest[0] = "admin"; 119 target.AddUsersToRoles(users.Keys.ToArray(), rolesToTest); // roles.ToArray()); 120 Assert.IsTrue(target.IsUserInRole("mholper", "admin")); 121 Assert.IsFalse(target.IsUserInRole("mholper", "user")); 124 122 } 125 123 … … 173 171 Assert.IsTrue(rolesForUser.Contains("testRole1")); 174 172 175 176 173 174 177 175 } 178 176 … … 182 180 [TestMethod()] 183 181 public void GetAllRolesTest() { 184 HeuristicLabRoleProvider target = new HeuristicLabRoleProvider(); 182 HeuristicLabRoleProvider target = new HeuristicLabRoleProvider(); 185 183 List<string> roleNames = new List<string>(); 186 184 roleNames.Add("Pascal"); … … 193 191 target.CreateRole(null); 194 192 string[] roles = target.GetAllRoles(); 195 foreach(string role in roles) 196 { 193 foreach (string role in roles) { 197 194 Assert.IsTrue(roleNames.Remove(role)); 198 195 } … … 219 216 Assert.IsTrue(target.FindUsersInRole("testRole", "dummyUser3").Length == 0); 220 217 221 target.AddUsersToRoles(new string[] { "dummyUser", "dummyUser2" }, new string[] { "testRole" });218 target.AddUsersToRoles(new string[] { "dummyUser", "dummyUser2" }, new string[] { "testRole" }); 222 219 Assert.IsTrue(target.FindUsersInRole("testRole", "dummyUser").Length == 2); 223 220 Assert.IsTrue(target.FindUsersInRole("testRole", "dummyUser2").Length == 1); … … 247 244 [TestMethod()] 248 245 public void CreateRoleTest() { 249 HeuristicLabRoleProvider target = new HeuristicLabRoleProvider(); 246 HeuristicLabRoleProvider target = new HeuristicLabRoleProvider(); 250 247 target.CreateRole("role1"); 251 248 target.CreateRole("role2"); … … 257 254 258 255 protected int getUserRolesCount() { 259 return db.HeuristicLabUserRole.Count(); 256 return db.HeuristicLabUserRole.Count(); 260 257 } 261 258 … … 279 276 Assert.IsTrue(getUserRolesCount() == 0); 280 277 281 foreach (string role in roles) 282 { 278 foreach (string role in roles) { 283 279 target.CreateRole(role); 284 280 }
Note: See TracChangeset
for help on using the changeset viewer.