- Timestamp:
- 01/08/11 14:39:29 (14 years ago)
- Location:
- branches/UserManagement/HeuristicLab.Services.Authentication
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/UserManagement/HeuristicLab.Services.Authentication/AuthenticationService.cs
r4979 r5257 1 using System; 1 #region License Information 2 /* HeuristicLab 3 * Copyright (C) 2002-2010 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 22 using System; 2 23 using System.Collections.Generic; 3 24 using System.Linq; … … 8 29 using System.Diagnostics; 9 30 10 namespace HeuristicLab.Services.Authentication 11 { 12 [ServiceBehavior(IncludeExceptionDetailInFaults = true)] 13 public class AuthenticationService : IAuthenticationService 14 { 15 16 #region User 17 18 19 public DataTransfer.User GetUser(Guid id) 20 { 21 using (UserManagementDataContext db = new UserManagementDataContext()) 22 { 23 var user = db.aspnet_Users.Where(x => x.UserId == id).FirstOrDefault(); 24 var membership = db.aspnet_Memberships.Where(x => x.UserId == id).FirstOrDefault(); 25 26 27 return Convert.ToDataTransfer(user, membership); 28 29 30 } 31 } 32 33 public IEnumerable<DataTransfer.User> GetAllUsers() { 34 List<DataTransfer.User> userList = new List<DataTransfer.User>(); 35 using (UserManagementDataContext db = new UserManagementDataContext()) { 36 var users = db.aspnet_Users.ToList<aspnet_User>(); 37 foreach (aspnet_User user in users) { 38 var membership = db.aspnet_Memberships.Where(x => x.UserId == user.UserId).FirstOrDefault(); 39 userList.Add(Convert.ToDataTransfer(user, membership)); 40 } 41 } 42 43 return userList; 44 } 45 46 public IEnumerable<DataTransfer.User> GetUsers(Guid applicationId) 47 { 48 List<DataTransfer.User> userList = new List<DataTransfer.User>(); 49 using (UserManagementDataContext db = new UserManagementDataContext()) 50 { 51 var users = db.aspnet_Users.Where(x => x.ApplicationId == applicationId).ToList<aspnet_User>(); 52 53 foreach (aspnet_User user in users) 54 { 55 var membership = db.aspnet_Memberships.Where(x => x.UserId == user.UserId).FirstOrDefault(); 56 57 userList.Add(Convert.ToDataTransfer(user, membership)); 58 59 } 60 61 62 63 } 64 return userList; 65 } 66 67 public Guid AddUser(User user) 68 { 69 if (user != null) 70 { 71 using (UserManagementDataContext db = new UserManagementDataContext()) 72 { 73 74 aspnet_User eUser = new aspnet_User(); 75 aspnet_Membership eMembership = new aspnet_Membership(); 76 77 Convert.ToEntity(user, eUser, eMembership); 78 try 79 { 80 db.aspnet_Users.InsertOnSubmit(eUser); 81 db.aspnet_Memberships.InsertOnSubmit(eMembership); 82 db.SubmitChanges(); 83 } 84 catch (Exception ex) 85 { 86 return Guid.Empty; 87 } 88 89 return user.Id; 90 91 92 } 93 94 } 95 return Guid.Empty; 96 } 97 98 public bool DeleteUser(Guid id) 99 { 100 101 using (UserManagementDataContext db = new UserManagementDataContext()) 102 { 103 var user = db.aspnet_Users.Where(x => x.UserId == id).FirstOrDefault(); 104 var membership = db.aspnet_Memberships.Where(x => x.UserId == id).FirstOrDefault(); 105 var userinroles = db.aspnet_UsersInRoles.Where(x => x.UserId == id).ToList<aspnet_UsersInRole>(); 106 try 107 { 108 db.aspnet_UsersInRoles.DeleteAllOnSubmit(userinroles); 109 db.aspnet_Memberships.DeleteOnSubmit(membership); 110 db.aspnet_Users.DeleteOnSubmit(user); 111 db.SubmitChanges(); 112 } 113 catch (Exception ex) 114 { 115 return false; 116 } 117 return true; 118 119 } 120 return false; 121 } 122 123 124 public bool UpdateUser(User user) 125 { 126 if (user != null) 127 { 128 using (UserManagementDataContext db = new UserManagementDataContext()) 129 { 130 131 var eUser = db.aspnet_Users.Where(x => x.UserId == user.Id).FirstOrDefault(); 132 var eMembership = db.aspnet_Memberships.Where(x => x.UserId == user.Id).FirstOrDefault(); 133 Convert.ToEntity(user, eUser, eMembership); 134 try 135 { 136 137 db.SubmitChanges(); 138 } 139 catch (Exception ex) 140 { 141 return false; 142 } 143 144 return true; 145 146 } 147 148 } 149 return false; 150 } 151 152 153 public bool AddUserToRole(Guid roleId, Guid userId) { 154 using (UserManagementDataContext db = new UserManagementDataContext()) { 155 try { 156 aspnet_UsersInRole r = new aspnet_UsersInRole(); 157 r.RoleId = roleId; 158 r.UserId = userId; 159 db.aspnet_UsersInRoles.InsertOnSubmit(r); 160 db.SubmitChanges(); 161 return true; 162 } 163 catch (Exception ex) { 164 Debug.WriteLine(ex.InnerException); 165 return false; 166 } 167 } 168 } 169 170 public IEnumerable<Guid> GetUsersInRole(Guid roleId) { 171 List<Guid> userList = new List<Guid>(); 172 using (UserManagementDataContext db = new UserManagementDataContext()) { 173 try { 174 var users = db.aspnet_UsersInRoles.Where(x => x.RoleId == roleId).ToList<aspnet_UsersInRole>(); 175 176 foreach (aspnet_UsersInRole u in users) { 177 userList.Add(GetUser(u.UserId).Id); 178 } 179 180 } 181 catch (Exception ex) { 182 Debug.WriteLine(ex.InnerException); 183 return new List<Guid>(); 184 185 } 186 187 } 188 189 return userList; 190 } 191 192 /* 193 public IEnumerable<User> GetUsersInRole(Guid roleId) { 194 List<User> userList = new List<User>(); 195 using (UserManagementDataContext db = new UserManagementDataContext()) { 196 try { 197 var users = db.aspnet_UsersInRoles.Where(x => x.RoleId == roleId).ToList<aspnet_UsersInRole>(); 198 199 foreach (aspnet_UsersInRole u in users) { 200 userList.Add(GetUser(u.UserId)); 201 202 } 203 204 } 205 catch (Exception ex) { 206 Debug.WriteLine(ex.InnerException); 207 return new List<User>(); 208 } 209 } 210 211 return userList; 212 } 213 */ 214 215 public bool IsUserInRole(Guid userId, Guid roleId) { 216 bool isInRole = false; 217 using (UserManagementDataContext db = new UserManagementDataContext()) { 218 var users = db.aspnet_UsersInRoles.Where(x => x.RoleId == roleId && x.UserId == userId).ToList<aspnet_UsersInRole>(); 219 foreach (aspnet_UsersInRole u in users) { 220 isInRole = true; 221 } 222 } 223 return isInRole; 224 225 } 226 227 228 public bool RemoveUserFromRole(Guid roleId, Guid userId) { 229 using (UserManagementDataContext db = new UserManagementDataContext()) { 230 try { 231 var role = db.aspnet_UsersInRoles.Where(x => x.RoleId == roleId && x.UserId == userId).FirstOrDefault(); 232 233 db.aspnet_UsersInRoles.DeleteOnSubmit(role); 234 db.SubmitChanges(); 235 return true; 236 237 } 238 catch (Exception ex) { 239 Debug.WriteLine(ex.InnerException); 240 return false; 241 } 242 } 243 244 } 245 246 247 248 249 #endregion 250 251 252 253 #region Role 254 255 public Role GetRole(Guid id) 256 { 257 using (UserManagementDataContext db = new UserManagementDataContext()) 258 { 259 var role = db.aspnet_Roles.Where(x => x.RoleId == id).FirstOrDefault(); 260 return Convert.ToDataTransfer(role); 261 } 262 } 263 264 public IEnumerable<Role> GetAllRoles() { 265 List<DataTransfer.Role> roleList = new List<DataTransfer.Role>(); 266 using (UserManagementDataContext db = new UserManagementDataContext()) { 267 var roles = db.aspnet_Roles.ToList<aspnet_Role>(); 268 foreach (aspnet_Role role in roles) { 269 roleList.Add(Convert.ToDataTransfer(role)); 270 } 271 } 272 return roleList; 273 } 274 275 public IEnumerable<Role> GetRoles(Guid applicationId) 276 { 277 List<DataTransfer.Role> roleList = new List<DataTransfer.Role>(); 278 using (UserManagementDataContext db = new UserManagementDataContext()) 279 { 280 var roles = db.aspnet_Roles.Where(x => x.ApplicationId == applicationId).ToList<aspnet_Role>(); 281 282 foreach (aspnet_Role role in roles) 283 { 284 roleList.Add(Convert.ToDataTransfer(role)); 285 } 286 287 } 288 return roleList; 289 } 290 291 public Guid AddRole(Role role) 292 { 293 294 if (role != null) 295 { 296 using (UserManagementDataContext db = new UserManagementDataContext()) 297 { 298 299 aspnet_Role eRole = new aspnet_Role(); 300 301 Convert.ToEntity(role, eRole); 302 try 303 { 304 db.aspnet_Roles.InsertOnSubmit(eRole); 305 db.SubmitChanges(); 306 } 307 catch (Exception ex) 308 { 309 return Guid.Empty; 310 } 311 312 return role.Id; 313 } 314 315 } 316 return Guid.Empty; 317 } 318 319 public bool DeleteRole(Guid id) 320 { 321 using (UserManagementDataContext db = new UserManagementDataContext()) 322 { 323 var role = db.aspnet_Roles.Where(x => x.RoleId == id).FirstOrDefault(); 324 325 326 try 327 { 328 db.aspnet_Roles.DeleteOnSubmit(role); 329 db.SubmitChanges(); 330 } 331 catch (Exception ex) 332 { 333 return false; 334 } 335 return true; 336 337 338 } 339 } 340 341 public IEnumerable<Guid> GetRolesForUser(Guid userId) { 342 List<Guid> roleList = new List<Guid>(); 343 using (UserManagementDataContext db = new UserManagementDataContext()) { 344 try { 345 var roles = db.aspnet_UsersInRoles.Where(x => x.UserId == userId).ToList<aspnet_UsersInRole>(); 346 foreach (aspnet_UsersInRole r in roles) { 347 roleList.Add(GetRole(r.RoleId).Id); 348 } 349 } 350 catch (Exception ex) { 351 Debug.WriteLine(ex.InnerException); 352 return new List<Guid>(); 353 } 354 } 355 356 return roleList; 357 358 } 359 360 /* 361 public IEnumerable<Role> GetRolesForUser(Guid userId) 362 { 363 List<Role> roleList = new List<Role>(); 364 using (UserManagementDataContext db = new UserManagementDataContext()) 365 { 366 try 367 { 368 var roles = db.aspnet_UsersInRoles.Where(x => x.UserId == userId).ToList<aspnet_UsersINRole>(); 369 370 foreach (aspnet_UsersInRole r in roles) 371 { 372 roleList.Add(GetRole(r.RoleId)); 373 374 } 375 376 } 377 catch (Exception ex) 378 { 379 Debug.WriteLine(ex.InnerException); 380 return new List<Role>(); 381 } 382 } 383 384 return roleList; 385 } 386 */ 387 388 389 390 public bool UpdateRole(Role role) 391 { 392 if (role != null) 393 { 394 using (UserManagementDataContext db = new UserManagementDataContext()) 395 { 396 397 var eRole = db.aspnet_Roles.Where(x => x.RoleId == role.Id).FirstOrDefault(); 398 399 Convert.ToEntity(role, eRole); 400 try 401 { 402 403 db.SubmitChanges(); 404 } 405 catch (Exception ex) 406 { 407 return false; 408 } 409 410 return true; 411 412 } 413 414 } 415 return false; 416 } 417 418 419 420 #endregion 421 422 423 424 #region Application 425 426 public Guid AddApplication(Application application) 427 { 428 if (application != null) 429 { 430 using (UserManagementDataContext db = new UserManagementDataContext()) 431 { 432 433 aspnet_Application eApplication = new aspnet_Application(); 434 435 Convert.ToEntity(application, eApplication); 436 try 437 { 438 db.aspnet_Applications.InsertOnSubmit(eApplication); 439 db.SubmitChanges(); 440 } 441 catch (Exception ex) 442 { 443 return Guid.Empty; 444 } 445 446 return application.Id; 447 448 } 449 450 } 451 return Guid.Empty; 452 } 453 454 public bool UpdateApplication(Application application) 455 { 456 if (application != null) 457 { 458 using (UserManagementDataContext db = new UserManagementDataContext()) 459 { 460 461 var eApplication = db.aspnet_Applications.Where(x => x.ApplicationId == application.Id).FirstOrDefault(); 462 463 Convert.ToEntity(application, eApplication); 464 try 465 { 466 467 db.SubmitChanges(); 468 } 469 catch (Exception ex) 470 { 471 return false; 472 } 473 474 return true; 475 476 } 477 478 } 479 return false; 480 } 481 482 483 public bool DeleteApplication(Guid applicationId) 484 { 485 using (UserManagementDataContext db = new UserManagementDataContext()) 486 { 487 var users = db.aspnet_Users.Where(x => x.ApplicationId == applicationId).ToList<aspnet_User>(); 488 var memberships = db.aspnet_Memberships.Where(x => x.ApplicationId == applicationId).ToList<aspnet_Membership>(); 489 490 var roles = db.aspnet_Roles.Where(x => x.ApplicationId == applicationId).ToList<aspnet_Role>(); 491 var application = db.aspnet_Applications.Where(x => x.ApplicationId == applicationId).FirstOrDefault(); 492 493 try 494 { 495 foreach (aspnet_User u in users) 496 { 497 var userinroles = db.aspnet_UsersInRoles.Where(x => x.UserId == u.UserId).ToList<aspnet_UsersInRole>(); 498 db.aspnet_UsersInRoles.DeleteAllOnSubmit(userinroles); 499 } 500 501 db.aspnet_Memberships.DeleteAllOnSubmit(memberships); 502 db.aspnet_Users.DeleteAllOnSubmit(users); 503 db.aspnet_Roles.DeleteAllOnSubmit(roles); 504 db.aspnet_Applications.DeleteOnSubmit(application); 505 db.SubmitChanges(); 506 } 507 catch (Exception ex) 508 { 509 return false; 510 } 511 return true; 512 513 514 } 515 return false; 516 } 517 518 public Application GetApplication(Guid id) 519 { 520 using (UserManagementDataContext db = new UserManagementDataContext()) 521 { 522 var application = db.aspnet_Applications.Where(x => x.ApplicationId == id).FirstOrDefault(); 523 return Convert.ToDataTransfer(application); 524 } 525 } 526 527 public IEnumerable<DataTransfer.Application> GetApplications() 528 { 529 List<DataTransfer.Application> applicationList = new List<DataTransfer.Application>(); 530 using (UserManagementDataContext db = new UserManagementDataContext()) 531 { 532 var apps = db.aspnet_Applications.ToList<aspnet_Application>(); 533 534 foreach (aspnet_Application app in apps) 535 { 536 applicationList.Add(Convert.ToDataTransfer(app)); 537 } 538 539 } 540 return applicationList; 541 } 542 543 #endregion 544 } 545 31 namespace HeuristicLab.Services.Authentication { 32 [ServiceBehavior(IncludeExceptionDetailInFaults = true)] 33 public class AuthenticationService : IAuthenticationService { 34 35 #region User 36 37 public DataTransfer.User GetUser(Guid id) { 38 using (UserManagementDataContext db = new UserManagementDataContext()) { 39 var user = db.aspnet_Users.FirstOrDefault(x => x.UserId == id); 40 var membership = db.aspnet_Memberships.Where(x => x.UserId == id).FirstOrDefault(); 41 return Convert.ToDto(user, membership); 42 } 43 } 44 45 public IEnumerable<User> GetUsers() { 46 using (UserManagementDataContext db = new UserManagementDataContext()) { 47 var users = db.aspnet_Users.OrderBy(x => x.UserId).ToList().Zip(db.aspnet_Memberships.OrderBy(x => x.UserId), (x, y) => Convert.ToDto(x, y)); 48 return users; 49 } 50 } 51 52 53 public IEnumerable<User> GetUsersForApplication(Guid applicationId) { 54 using (UserManagementDataContext db = new UserManagementDataContext()) { 55 var users = db.aspnet_Users.Where(x => x.ApplicationId == applicationId).OrderBy(x => x.UserId).ToList().Zip(db.aspnet_Memberships.Where(x => x.ApplicationId == applicationId).OrderBy(x => x.UserId), (x, y) => Convert.ToDto(x, y)).ToArray(); 56 return users; 57 } 58 } 59 60 61 public Guid AddUser(User user) { 62 if (user != null) { 63 using (UserManagementDataContext db = new UserManagementDataContext()) { 64 aspnet_User eUser; 65 aspnet_Membership eMembership; 66 user.Id = Guid.NewGuid(); 67 Convert.ToEntity(user, out eUser, out eMembership); 68 db.aspnet_Users.InsertOnSubmit(eUser); 69 db.aspnet_Memberships.InsertOnSubmit(eMembership); 70 db.SubmitChanges(); 71 return user.Id; 72 } 73 } 74 return Guid.Empty; 75 76 } 77 78 public void DeleteUser(Guid id) { 79 using (UserManagementDataContext db = new UserManagementDataContext()) { 80 var user = db.aspnet_Users.Where(x => x.UserId == id).FirstOrDefault(); 81 var membership = db.aspnet_Memberships.Where(x => x.UserId == id).FirstOrDefault(); 82 var userinroles = db.aspnet_UsersInRoles.Where(x => x.UserId == id).ToList<aspnet_UsersInRole>(); 83 db.aspnet_UsersInRoles.DeleteAllOnSubmit(userinroles); 84 db.aspnet_Memberships.DeleteOnSubmit(membership); 85 db.aspnet_Users.DeleteOnSubmit(user); 86 db.SubmitChanges(); 87 } 88 89 } 90 91 92 public void UpdateUser(User user) { 93 using (UserManagementDataContext db = new UserManagementDataContext()) { 94 var eUser = db.aspnet_Users.Where(x => x.UserId == user.Id).FirstOrDefault(); 95 var eMembership = db.aspnet_Memberships.Where(x => x.UserId == user.Id).FirstOrDefault(); 96 Convert.ToEntity(user, eUser, eMembership); 97 db.SubmitChanges(); 98 } 99 } 100 101 102 public void AddUserToRole(Guid roleId, Guid userId) { 103 using (UserManagementDataContext db = new UserManagementDataContext()) { 104 aspnet_UsersInRole r = new aspnet_UsersInRole(); 105 r.RoleId = roleId; 106 r.UserId = userId; 107 db.aspnet_UsersInRoles.InsertOnSubmit(r); 108 db.SubmitChanges(); 109 } 110 } 111 112 113 114 public IEnumerable<Guid> GetUsersInRole(Guid roleId) { 115 using (UserManagementDataContext db = new UserManagementDataContext()) { 116 List<Guid> userList = new List<Guid>(); 117 var users = db.aspnet_UsersInRoles.Where(x => x.RoleId == roleId).ToList<aspnet_UsersInRole>(); 118 foreach (aspnet_UsersInRole u in users) { 119 userList.Add(GetUser(u.UserId).Id); 120 } 121 return userList; 122 } 123 } 124 125 126 public bool IsUserInRole(Guid userId, Guid roleId) { 127 using (UserManagementDataContext db = new UserManagementDataContext()) { 128 bool isInRole = false; 129 var users = db.aspnet_UsersInRoles.Where(x => x.RoleId == roleId && x.UserId == userId).ToList<aspnet_UsersInRole>(); 130 foreach (aspnet_UsersInRole u in users) { 131 isInRole = true; 132 } 133 return isInRole; 134 } 135 } 136 137 138 public void RemoveUserFromRole(Guid roleId, Guid userId) { 139 using (UserManagementDataContext db = new UserManagementDataContext()) { 140 var role = db.aspnet_UsersInRoles.Where(x => x.RoleId == roleId && x.UserId == userId).FirstOrDefault(); 141 db.aspnet_UsersInRoles.DeleteOnSubmit(role); 142 db.SubmitChanges(); 143 } 144 } 145 146 147 // TODO !!! 148 public User ResetPassword(string applicationName, string userName, string password) { 149 150 string salt = ""; 151 string answer = ""; 152 153 using (UserManagementDataContext db = new UserManagementDataContext()) { 154 db.aspnet_Membership_ResetPassword(applicationName, userName, password, null, null, salt, null, null, answer); 155 return null; 156 } 157 } 158 159 #endregion 160 161 #region Role 162 163 public Role GetRole(Guid id) { 164 using (UserManagementDataContext db = new UserManagementDataContext()) { 165 var role = db.aspnet_Roles.Where(x => x.RoleId == id).FirstOrDefault(); 166 return Convert.ToDto(role); 167 } 168 } 169 170 public IEnumerable<Role> GetRoles() { 171 172 using (UserManagementDataContext db = new UserManagementDataContext()) { 173 List<DataTransfer.Role> roleList = new List<DataTransfer.Role>(); 174 var roles = db.aspnet_Roles.ToList<aspnet_Role>(); 175 foreach (aspnet_Role role in roles) { 176 roleList.Add(Convert.ToDto(role)); 177 } 178 return roleList; 179 } 180 181 } 182 183 public IEnumerable<Role> GetRolesForApplication(Guid applicationId) { 184 using (UserManagementDataContext db = new UserManagementDataContext()) { 185 List<Role> roleList = new List<Role>(); 186 var roles = db.aspnet_Roles.Where(x => x.ApplicationId == applicationId).ToList<aspnet_Role>(); 187 foreach (aspnet_Role role in roles) { 188 roleList.Add(Convert.ToDto(role)); 189 } 190 return roleList; 191 } 192 } 193 194 public Guid AddRole(Role role) { 195 if (role != null) { 196 using (UserManagementDataContext db = new UserManagementDataContext()) { 197 aspnet_Role eRole = new aspnet_Role(); 198 role.Id = Guid.NewGuid(); 199 Convert.ToEntity(role, eRole); 200 db.aspnet_Roles.InsertOnSubmit(eRole); 201 db.SubmitChanges(); 202 return role.Id; 203 } 204 } 205 return Guid.Empty; 206 } 207 208 public void DeleteRole(Guid id) { 209 using (UserManagementDataContext db = new UserManagementDataContext()) { 210 var role = db.aspnet_Roles.Where(x => x.RoleId == id).FirstOrDefault(); 211 var userinroles = db.aspnet_UsersInRoles.Where(x => x.RoleId == id).ToList<aspnet_UsersInRole>(); 212 db.aspnet_UsersInRoles.DeleteAllOnSubmit(userinroles); 213 db.aspnet_Roles.DeleteOnSubmit(role); 214 db.SubmitChanges(); 215 } 216 } 217 218 public IEnumerable<Guid> GetRolesForUser(Guid userId) { 219 using (UserManagementDataContext db = new UserManagementDataContext()) { 220 List<Guid> roleList = new List<Guid>(); 221 var roles = db.aspnet_UsersInRoles.Where(x => x.UserId == userId).ToList<aspnet_UsersInRole>(); 222 foreach (aspnet_UsersInRole r in roles) { 223 roleList.Add(GetRole(r.RoleId).Id); 224 } 225 return roleList; 226 } 227 } 228 229 public void UpdateRole(Role role) { 230 using (UserManagementDataContext db = new UserManagementDataContext()) { 231 var eRole = db.aspnet_Roles.Where(x => x.RoleId == role.Id).FirstOrDefault(); 232 Convert.ToEntity(role, eRole); 233 db.SubmitChanges(); 234 } 235 } 236 237 #endregion 238 239 #region Application 240 241 public Guid AddApplication(Application application) { 242 if (application != null) { 243 using (UserManagementDataContext db = new UserManagementDataContext()) { 244 aspnet_Application eApplication = new aspnet_Application(); 245 application.Id = Guid.NewGuid(); 246 Convert.ToEntity(application, eApplication); 247 db.aspnet_Applications.InsertOnSubmit(eApplication); 248 db.SubmitChanges(); 249 return application.Id; 250 } 251 } 252 return Guid.Empty; 253 } 254 255 public void UpdateApplication(Application application) { 256 using (UserManagementDataContext db = new UserManagementDataContext()) { 257 var eApplication = db.aspnet_Applications.Where(x => x.ApplicationId == application.Id).FirstOrDefault(); 258 Convert.ToEntity(application, eApplication); 259 db.SubmitChanges(); 260 } 261 } 262 263 264 public void DeleteApplication(Guid applicationId) { 265 using (UserManagementDataContext db = new UserManagementDataContext()) { 266 var users = db.aspnet_Users.Where(x => x.ApplicationId == applicationId).ToList<aspnet_User>(); 267 var memberships = db.aspnet_Memberships.Where(x => x.ApplicationId == applicationId).ToList<aspnet_Membership>(); 268 var roles = db.aspnet_Roles.Where(x => x.ApplicationId == applicationId).ToList<aspnet_Role>(); 269 var application = db.aspnet_Applications.Where(x => x.ApplicationId == applicationId).FirstOrDefault(); 270 foreach (aspnet_User u in users) { 271 var userinroles = db.aspnet_UsersInRoles.Where(x => x.UserId == u.UserId).ToList<aspnet_UsersInRole>(); 272 db.aspnet_UsersInRoles.DeleteAllOnSubmit(userinroles); 273 } 274 db.aspnet_Memberships.DeleteAllOnSubmit(memberships); 275 db.aspnet_Users.DeleteAllOnSubmit(users); 276 db.aspnet_Roles.DeleteAllOnSubmit(roles); 277 db.aspnet_Applications.DeleteOnSubmit(application); 278 db.SubmitChanges(); 279 } 280 } 281 282 public Application GetApplication(Guid id) { 283 using (UserManagementDataContext db = new UserManagementDataContext()) { 284 var application = db.aspnet_Applications.Where(x => x.ApplicationId == id).FirstOrDefault(); 285 return Convert.ToDto(application); 286 } 287 } 288 289 public IEnumerable<Application> GetApplications() { 290 List<Application> applicationList = new List<Application>(); 291 using (UserManagementDataContext db = new UserManagementDataContext()) { 292 var apps = db.aspnet_Applications.ToList<aspnet_Application>(); 293 foreach (aspnet_Application app in apps) { 294 applicationList.Add(Convert.ToDto(app)); 295 } 296 } 297 return applicationList; 298 } 299 #endregion 300 } 546 301 } 547 -
branches/UserManagement/HeuristicLab.Services.Authentication/Convert.cs
r4979 r5257 1 using HeuristicLab.Services.Authentication.DataAccess; 1 #region License Information 2 /* HeuristicLab 3 * Copyright (C) 2002-2010 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 using HeuristicLab.Services.Authentication.DataAccess; 2 22 using HeuristicLab.Services.Authentication.DataTransfer; 3 23 4 namespace HeuristicLab.Services.Authentication 5 { 6 public class Convert 7 { 24 namespace HeuristicLab.Services.Authentication { 25 public class Convert { 8 26 9 27 #region User 10 28 11 /// <summary> 12 /// converts data access object to data transfer object 13 /// </summary> 14 /// <param name="source">data access object</param> 15 /// <returns>data transfer object</returns> 16 public static User ToDataTransfer(aspnet_User userSource, aspnet_Membership membershipSource) 17 { 18 if (userSource == null || membershipSource == null) return null; 19 return new User() 20 { 21 ApplicationId = userSource.ApplicationId, 22 Id = userSource.UserId, 23 Name = userSource.UserName, 24 //LoweredUserName = userSource.LoweredUserName, 25 //IsAnonymous = userSource.IsAnonymous, 26 LastActivityDate = userSource.LastActivityDate, 27 Password = membershipSource.Password, 28 PasswordSalt = membershipSource.PasswordSalt, 29 Email = membershipSource.Email, 30 IsApproved = membershipSource.IsApproved, 31 IsLookedOut = membershipSource.IsApproved, 32 CreateDate = membershipSource.CreateDate, 33 LastLoginDate = membershipSource.LastLoginDate, 34 LastPasswordChangeDate = membershipSource.LastPasswordChangedDate, 35 LastLockoutDate = membershipSource.LastLockoutDate, 36 Comment = membershipSource.Comment 37 }; 38 } 29 public static User ToDto(aspnet_User sourceUser, aspnet_Membership sourceMembership) { 30 if (sourceUser == null || sourceMembership == null) return null; 31 return new User() { 32 ApplicationId = sourceUser.ApplicationId, 33 Id = sourceUser.UserId, 34 Name = sourceUser.UserName, 35 LastActivityDate = sourceUser.LastActivityDate, 36 Email = sourceMembership.Email, 37 IsApproved = sourceMembership.IsApproved, 38 IsLookedOut = sourceMembership.IsApproved, 39 CreateDate = sourceMembership.CreateDate, 40 LastLoginDate = sourceMembership.LastLoginDate, 41 LastPasswordChangeDate = sourceMembership.LastPasswordChangedDate, 42 LastLockoutDate = sourceMembership.LastLockoutDate, 43 Description = sourceMembership.Comment 44 }; 45 } 39 46 40 /// <summary> 41 /// converts data transfer object to data access object 42 /// </summary> 43 /// <param name="source">data transfer object</param> 44 /// <param name="target">data access object</param> 45 public static void ToEntity(User source, aspnet_User userTarget, aspnet_Membership membershipTarget) 46 { 47 if ((source != null) && (userTarget != null) && (membershipTarget != null)) 48 { 49 userTarget.ApplicationId = source.ApplicationId; 50 membershipTarget.ApplicationId = source.ApplicationId; 51 userTarget.UserId = source.Id; 52 membershipTarget.UserId = source.Id; 53 userTarget.UserName = source.Name; 54 userTarget.LoweredUserName = source.Name; 55 userTarget.IsAnonymous = false; 56 userTarget.LastActivityDate = source.LastActivityDate; 57 membershipTarget.Password = source.Password; 58 membershipTarget.PasswordFormat = 1; 59 membershipTarget.PasswordSalt = source.PasswordSalt; 60 membershipTarget.Email = source.Email; 61 membershipTarget.IsApproved = source.IsApproved; 62 membershipTarget.IsLockedOut = source.IsLookedOut; 63 membershipTarget.CreateDate = source.CreateDate; 64 membershipTarget.LastLoginDate = source.LastLoginDate; 65 membershipTarget.LastPasswordChangedDate = source.LastPasswordChangeDate; 66 membershipTarget.LastLockoutDate = source.LastLockoutDate; 67 membershipTarget.FailedPasswordAttemptCount = 0; 68 membershipTarget.FailedPasswordAttemptWindowStart = new System.DateTime(1900, 01, 01); 69 membershipTarget.FailedPasswordAnswerAttemptCount = 0; 70 membershipTarget.FailedPasswordAnswerAttemptWindowStart = new System.DateTime(1900, 01, 01); 71 membershipTarget.Comment = source.Comment; 47 public static void ToEntity(User sourceUser, aspnet_User targetUser, aspnet_Membership targetMembership) { 48 if ((sourceUser != null) && (targetUser != null) && (targetMembership != null)) { 49 targetUser.ApplicationId = sourceUser.ApplicationId; 50 targetUser.UserId = sourceUser.Id; 51 targetUser.UserName = sourceUser.Name; 52 targetUser.LoweredUserName = sourceUser.Name; 53 targetUser.IsAnonymous = false; 54 targetUser.LastActivityDate = sourceUser.LastActivityDate; 55 targetMembership.UserId = sourceUser.Id; 56 targetMembership.ApplicationId = sourceUser.ApplicationId; 57 targetMembership.PasswordFormat = 1; 58 targetMembership.Email = sourceUser.Email; 59 targetMembership.IsApproved = sourceUser.IsApproved; 60 targetMembership.IsLockedOut = sourceUser.IsLookedOut; 61 targetMembership.CreateDate = sourceUser.CreateDate; 62 targetMembership.LastLoginDate = sourceUser.LastLoginDate; 63 targetMembership.LastPasswordChangedDate = sourceUser.LastPasswordChangeDate; 64 targetMembership.LastLockoutDate = sourceUser.LastLockoutDate; 65 targetMembership.FailedPasswordAttemptCount = 0; 66 targetMembership.FailedPasswordAttemptWindowStart = new System.DateTime(1900, 01, 01); 67 targetMembership.FailedPasswordAnswerAttemptCount = 0; 68 targetMembership.FailedPasswordAnswerAttemptWindowStart = new System.DateTime(1900, 01, 01); 69 targetMembership.Comment = sourceUser.Description; 70 } 71 } 72 72 73 } 73 public static void ToEntity(User source, out aspnet_User userTarget, out aspnet_Membership membershipTarget) { 74 userTarget = new aspnet_User(); 75 membershipTarget = new aspnet_Membership(); 74 76 75 } 77 if ((source != null)) { 78 userTarget.ApplicationId = source.ApplicationId; 79 userTarget.UserId = source.Id; 80 userTarget.UserName = source.Name; 81 userTarget.LoweredUserName = source.Name; 82 userTarget.IsAnonymous = false; 83 userTarget.LastActivityDate = source.LastActivityDate; 84 membershipTarget.UserId = source.Id; 85 membershipTarget.ApplicationId = source.ApplicationId; 86 membershipTarget.PasswordFormat = 1; 87 membershipTarget.Email = source.Email; 88 membershipTarget.IsApproved = source.IsApproved; 89 membershipTarget.IsLockedOut = source.IsLookedOut; 90 membershipTarget.CreateDate = source.CreateDate; 91 membershipTarget.LastLoginDate = source.LastLoginDate; 92 membershipTarget.LastPasswordChangedDate = source.LastPasswordChangeDate; 93 membershipTarget.LastLockoutDate = source.LastLockoutDate; 94 membershipTarget.FailedPasswordAttemptCount = 0; 95 membershipTarget.FailedPasswordAttemptWindowStart = new System.DateTime(1900, 01, 01); 96 membershipTarget.FailedPasswordAnswerAttemptCount = 0; 97 membershipTarget.FailedPasswordAnswerAttemptWindowStart = new System.DateTime(1900, 01, 01); 98 membershipTarget.Comment = source.Description; 99 } 100 } 76 101 102 #endregion 77 103 78 #endregion104 #region Application 79 105 80 #region Application 106 public static Application ToDto(aspnet_Application sourceApplication) { 107 if (sourceApplication == null) return null; 108 return new Application() { 109 Id = sourceApplication.ApplicationId, 110 Name = sourceApplication.ApplicationName, 111 Description = sourceApplication.Description 112 }; 113 } 81 114 82 /// <summary> 83 /// converts data access object to data transfer object 84 /// </summary> 85 /// <param name="source">data access object</param> 86 /// <returns>data transfer object</returns> 87 public static Application ToDataTransfer(aspnet_Application source) 88 { 89 if (source == null) return null; 90 return new Application() 91 { 92 Id = source.ApplicationId, 93 Name = source.ApplicationName, 94 //LoweredApplicationName = source.LoweredApplicationName, 95 Description = source.Description 96 }; 97 } 115 public static void ToEntity(Application sourceApplication, aspnet_Application targetApplication) { 116 if ((sourceApplication != null) && (targetApplication != null)) { 117 targetApplication.ApplicationId = sourceApplication.Id; 118 targetApplication.ApplicationName = sourceApplication.Name; 119 targetApplication.LoweredApplicationName = sourceApplication.Name.ToLower(); 120 targetApplication.Description = sourceApplication.Description; 121 } 122 } 98 123 99 /// <summary> 100 /// converts data transfer object to data access object 101 /// </summary> 102 /// <param name="source">data transfer object</param> 103 /// <param name="target">data access object</param> 104 public static void ToEntity(Application source, aspnet_Application target) 105 { 106 if ((source != null) && (target != null)) 107 { 108 target.ApplicationId = source.Id; 109 target.ApplicationName = source.Name; 110 target.LoweredApplicationName = source.Name.ToLower(); 111 target.Description = source.Description; 112 } 124 public static aspnet_Application ToEntity(Application sourceApplication) { 125 if (sourceApplication == null) return null; 126 return new aspnet_Application() { 127 ApplicationId = sourceApplication.Id, 128 ApplicationName = sourceApplication.Name, 129 LoweredApplicationName = sourceApplication.Name.ToLower(), 130 Description = sourceApplication.Description 131 }; 132 } 113 133 114 }134 #endregion 115 135 116 #endregion136 #region Role 117 137 118 #region Role 138 public static Role ToDto(aspnet_Role sourceRole) { 139 if (sourceRole == null) return null; 140 return new Role() { 141 ApplicationId = sourceRole.ApplicationId, 142 Id = sourceRole.RoleId, 143 Name = sourceRole.RoleName, 144 Description = sourceRole.Description, 145 }; 146 } 119 147 120 /// <summary> 121 /// converts data access object to data transfer object 122 /// </summary> 123 /// <param name="source">data access object</param> 124 /// <returns>data transfer object</returns> 125 public static Role ToDataTransfer(aspnet_Role source) 126 { 127 if (source == null) return null; 128 return new Role() 129 { 130 ApplicationId = source.ApplicationId, 131 Id = source.RoleId, 132 Name = source.RoleName, 133 Description = source.Description, 134 //LoweredRoleName = source.LoweredRoleName, 135 }; 136 } 148 public static void ToEntity(Role sourceRole, aspnet_Role targetRole) { 149 if ((sourceRole != null) && (targetRole != null)) { 150 targetRole.ApplicationId = sourceRole.ApplicationId; 151 targetRole.RoleId = sourceRole.Id; 152 targetRole.RoleName = sourceRole.Name; 153 targetRole.LoweredRoleName = sourceRole.Name.ToLower(); 154 targetRole.Description = sourceRole.Description; 155 } 156 } 137 157 138 /// <summary> 139 /// converts data transfer object to data access object 140 /// </summary> 141 /// <param name="source">data transfer object</param> 142 /// <param name="target">data access object</param> 143 public static void ToEntity(Role source, aspnet_Role target) 144 { 145 if ((source != null) && (target != null)) 146 { 147 target.ApplicationId = source.ApplicationId; 148 target.RoleId = source.Id; 149 target.RoleName = source.Name; 150 target.LoweredRoleName = source.Name.ToLower(); 151 target.Description = source.Description; 152 } 158 public static aspnet_Role ToEntity(Role sourceRole) { 159 if (sourceRole == null) return null; 160 return new aspnet_Role() { 161 ApplicationId = sourceRole.ApplicationId, 162 RoleId = sourceRole.Id, 163 RoleName = sourceRole.Name, 164 LoweredRoleName = sourceRole.Name.ToLower(), 165 Description = sourceRole.Description, 166 }; 167 } 153 168 154 } 155 156 #endregion 157 158 } 169 #endregion 170 } 159 171 } -
branches/UserManagement/HeuristicLab.Services.Authentication/Interfaces/IAuthenticationService.cs
r4979 r5257 1 using System; 1 #region License Information 2 /* HeuristicLab 3 * Copyright (C) 2002-2010 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 using System; 2 22 using System.Collections.Generic; 3 23 using HeuristicLab.Services.Authentication.DataTransfer; … … 5 25 using System.Net.Security; 6 26 7 namespace HeuristicLab.Services.Authentication 8 { 9 [ServiceContract(ProtectionLevel = ProtectionLevel.EncryptAndSign)] 10 public interface IAuthenticationService 11 { 12 #region User 27 namespace HeuristicLab.Services.Authentication { 28 [ServiceContract(ProtectionLevel = ProtectionLevel.EncryptAndSign)] 29 public interface IAuthenticationService { 13 30 14 [OperationContract] 15 User GetUser(Guid id); 31 #region User 16 32 17 18 IEnumerable<User> GetAllUsers();33 [OperationContract] 34 User GetUser(Guid id); 19 35 20 [OperationContract]21 IEnumerable<User> GetUsers(Guid applicationId);36 [OperationContract()] 37 IEnumerable<User> GetUsers(); 22 38 23 [OperationContract]24 Guid AddUser(User user);39 [OperationContract()] 40 IEnumerable<User> GetUsersForApplication(Guid applicationId); 25 41 26 27 bool DeleteUser(Guid id);42 [OperationContract] 43 Guid AddUser(User user); 28 44 29 30 bool UpdateUser(User user);45 [OperationContract] 46 void DeleteUser(Guid id); 31 47 32 33 bool AddUserToRole(Guid roleId, Guid userId);48 [OperationContract] 49 void UpdateUser(User user); 34 50 35 36 IEnumerable<Guid> GetUsersInRole(Guid roleId);51 [OperationContract] 52 void AddUserToRole(Guid roleId, Guid userId); 37 53 38 //[OperationContract]39 //IEnumerable<User> GetUsersInRole(Guid roleId); // return = Guid54 [OperationContract] 55 IEnumerable<Guid> GetUsersInRole(Guid roleId); 40 56 41 42 boolRemoveUserFromRole(Guid roleId, Guid userId);57 [OperationContract] 58 void RemoveUserFromRole(Guid roleId, Guid userId); 43 59 44 45 60 [OperationContract] 61 bool IsUserInRole(Guid userId, Guid roleId); 46 62 47 #endregion 63 [OperationContract] 64 User ResetPassword(string applicationName, string userName, string password); 48 65 49 #region Role66 #endregion 50 67 51 [OperationContract] 52 Role GetRole(Guid id); 68 #region Role 53 69 54 55 IEnumerable<Role> GetAllRoles();70 [OperationContract] 71 Role GetRole(Guid id); 56 72 57 [OperationContract]58 IEnumerable<Role> GetRoles(Guid applicationId);73 [OperationContract()] 74 IEnumerable<Role> GetRoles(); 59 75 60 [OperationContract]61 Guid AddRole(Role role);76 [OperationContract()] 77 IEnumerable<Role> GetRolesForApplication(Guid applicationId); 62 78 63 64 bool UpdateRole(Role role);79 [OperationContract] 80 Guid AddRole(Role role); 65 81 66 67 bool DeleteRole(Guid id);82 [OperationContract] 83 void UpdateRole(Role role); 68 84 85 [OperationContract] 86 void DeleteRole(Guid id); 69 87 70 71 88 [OperationContract] 89 IEnumerable<Guid> GetRolesForUser(Guid userId); 72 90 91 #endregion 73 92 74 //[OperationContract] 75 //IEnumerable<Role> GetRolesForUser(Guid userId); // returnvalue = GUID 93 #region Application 76 94 77 #endregion 95 [OperationContract] 96 Application GetApplication(Guid id); 78 97 79 #region Application 98 [OperationContract] 99 Guid AddApplication(Application application); 80 100 81 82 Application GetApplication(Guid id);101 [OperationContract] 102 void DeleteApplication(Guid id); 83 103 84 85 Guid AddApplication(Application application); // ADD104 [OperationContract] 105 IEnumerable<Application> GetApplications(); 86 106 87 88 bool DeleteApplication(Guid id);107 [OperationContract] 108 void UpdateApplication(Application application); 89 109 90 [OperationContract] 91 IEnumerable<DataTransfer.Application> GetApplications(); 92 93 94 [OperationContract] 95 bool UpdateApplication(Application application); 96 97 #endregion 98 99 100 } 110 #endregion 111 } 101 112 }
Note: See TracChangeset
for help on using the changeset viewer.