[4584] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.ServiceModel;
|
---|
| 5 | using HeuristicLab.Services.Authentication.DataTransfer;
|
---|
| 6 | using HeuristicLab.Services.Authentication.DataAccess;
|
---|
| 7 | using System.Data.Linq;
|
---|
[4647] | 8 | using System.Diagnostics;
|
---|
[4584] | 9 |
|
---|
| 10 | namespace HeuristicLab.Services.Authentication
|
---|
| 11 | {
|
---|
| 12 | [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
|
---|
| 13 | public class AuthenticationService : IAuthenticationService
|
---|
| 14 | {
|
---|
[4926] | 15 |
|
---|
[4588] | 16 | #region User
|
---|
[4590] | 17 |
|
---|
[4926] | 18 |
|
---|
[4590] | 19 | public DataTransfer.User GetUser(Guid id)
|
---|
[4584] | 20 | {
|
---|
| 21 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
| 22 | {
|
---|
[4926] | 23 | var user = db.aspnet_Users.Where(x => x.UserId == id).FirstOrDefault();
|
---|
| 24 | var membership = db.aspnet_Memberships.Where(x => x.UserId == id).FirstOrDefault();
|
---|
[4584] | 25 |
|
---|
[4647] | 26 |
|
---|
[4926] | 27 | return Convert.ToDataTransfer(user, membership);
|
---|
[4726] | 28 |
|
---|
[4647] | 29 |
|
---|
[4584] | 30 | }
|
---|
[4926] | 31 | }
|
---|
[4647] | 32 |
|
---|
[4926] | 33 | public IEnumerable<DataTransfer.User> GetUsers(Guid applicationId)
|
---|
| 34 | {
|
---|
| 35 | List<DataTransfer.User> userList = new List<DataTransfer.User>();
|
---|
| 36 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
| 37 | {
|
---|
| 38 | var users = db.aspnet_Users.Where(x => x.ApplicationId == applicationId).ToList<aspnet_User>();
|
---|
[4647] | 39 |
|
---|
[4926] | 40 | foreach (aspnet_User user in users)
|
---|
| 41 | {
|
---|
| 42 | var membership = db.aspnet_Memberships.Where(x => x.UserId == user.UserId).FirstOrDefault();
|
---|
[4647] | 43 |
|
---|
[4926] | 44 | userList.Add(Convert.ToDataTransfer(user, membership));
|
---|
[4647] | 45 |
|
---|
[4926] | 46 | }
|
---|
[4647] | 47 |
|
---|
| 48 |
|
---|
| 49 |
|
---|
[4926] | 50 | }
|
---|
| 51 | return userList;
|
---|
| 52 | }
|
---|
[4647] | 53 |
|
---|
[4962] | 54 | public Guid InsertUser(User user)
|
---|
[4926] | 55 | {
|
---|
| 56 | if (user != null)
|
---|
| 57 | {
|
---|
| 58 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
| 59 | {
|
---|
[4647] | 60 |
|
---|
[4926] | 61 | aspnet_User eUser = new aspnet_User();
|
---|
| 62 | aspnet_Membership eMembership = new aspnet_Membership();
|
---|
[4647] | 63 |
|
---|
[4926] | 64 | Convert.ToEntity(user, eUser, eMembership);
|
---|
| 65 | try
|
---|
| 66 | {
|
---|
| 67 | db.aspnet_Users.InsertOnSubmit(eUser);
|
---|
| 68 | db.aspnet_Memberships.InsertOnSubmit(eMembership);
|
---|
| 69 | db.SubmitChanges();
|
---|
| 70 | }
|
---|
| 71 | catch (Exception ex)
|
---|
| 72 | {
|
---|
[4962] | 73 | return Guid.Empty;
|
---|
[4926] | 74 | }
|
---|
[4647] | 75 |
|
---|
[4962] | 76 | return user.Id;
|
---|
[4647] | 77 |
|
---|
| 78 |
|
---|
[4926] | 79 | }
|
---|
[4647] | 80 |
|
---|
[4926] | 81 | }
|
---|
[4962] | 82 | return Guid.Empty;
|
---|
[4926] | 83 | }
|
---|
[4584] | 84 |
|
---|
[4926] | 85 | public bool DeleteUser(Guid id)
|
---|
| 86 | {
|
---|
[4647] | 87 |
|
---|
[4926] | 88 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
| 89 | {
|
---|
| 90 | var user = db.aspnet_Users.Where(x => x.UserId == id).FirstOrDefault();
|
---|
| 91 | var membership = db.aspnet_Memberships.Where(x => x.UserId == id).FirstOrDefault();
|
---|
| 92 | var userinroles = db.aspnet_UsersInRoles.Where(x => x.UserId == id).ToList<aspnet_UsersInRole>();
|
---|
| 93 | try
|
---|
| 94 | {
|
---|
| 95 | db.aspnet_UsersInRoles.DeleteAllOnSubmit(userinroles);
|
---|
| 96 | db.aspnet_Memberships.DeleteOnSubmit(membership);
|
---|
| 97 | db.aspnet_Users.DeleteOnSubmit(user);
|
---|
| 98 | db.SubmitChanges();
|
---|
| 99 | }
|
---|
| 100 | catch (Exception ex)
|
---|
| 101 | {
|
---|
| 102 | return false;
|
---|
| 103 | }
|
---|
| 104 | return true;
|
---|
[4647] | 105 |
|
---|
[4926] | 106 | }
|
---|
| 107 | return false;
|
---|
| 108 | }
|
---|
[4647] | 109 |
|
---|
| 110 |
|
---|
[4926] | 111 | public bool UpdateUser(User user)
|
---|
| 112 | {
|
---|
| 113 | if (user != null)
|
---|
| 114 | {
|
---|
| 115 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
| 116 | {
|
---|
[4647] | 117 |
|
---|
[4926] | 118 | var eUser = db.aspnet_Users.Where(x => x.UserId == user.Id).FirstOrDefault();
|
---|
| 119 | var eMembership = db.aspnet_Memberships.Where(x => x.UserId == user.Id).FirstOrDefault();
|
---|
| 120 | Convert.ToEntity(user, eUser, eMembership);
|
---|
| 121 | try
|
---|
| 122 | {
|
---|
[4647] | 123 |
|
---|
[4926] | 124 | db.SubmitChanges();
|
---|
| 125 | }
|
---|
| 126 | catch (Exception ex)
|
---|
| 127 | {
|
---|
| 128 | return false;
|
---|
| 129 | }
|
---|
[4584] | 130 |
|
---|
[4926] | 131 | return true;
|
---|
[4647] | 132 |
|
---|
[4926] | 133 | }
|
---|
[4740] | 134 |
|
---|
[4926] | 135 | }
|
---|
| 136 | return false;
|
---|
| 137 | }
|
---|
[4740] | 138 |
|
---|
[4647] | 139 |
|
---|
[4588] | 140 |
|
---|
[4926] | 141 | #endregion
|
---|
[4647] | 142 |
|
---|
| 143 |
|
---|
| 144 |
|
---|
[4926] | 145 | #region Role
|
---|
[4647] | 146 |
|
---|
[4926] | 147 | public Role GetRole(Guid id)
|
---|
| 148 | {
|
---|
| 149 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
| 150 | {
|
---|
| 151 | var role = db.aspnet_Roles.Where(x => x.RoleId == id).FirstOrDefault();
|
---|
| 152 | return Convert.ToDataTransfer(role);
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
[4647] | 155 |
|
---|
[4926] | 156 | public IEnumerable<Role> GetRoles(Guid applicationId)
|
---|
| 157 | {
|
---|
| 158 | List<DataTransfer.Role> roleList = new List<DataTransfer.Role>();
|
---|
| 159 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
| 160 | {
|
---|
| 161 | var roles = db.aspnet_Roles.Where(x => x.ApplicationId == applicationId).ToList<aspnet_Role>();
|
---|
[4647] | 162 |
|
---|
[4926] | 163 | foreach (aspnet_Role role in roles)
|
---|
| 164 | {
|
---|
| 165 | roleList.Add(Convert.ToDataTransfer(role));
|
---|
| 166 | }
|
---|
[4647] | 167 |
|
---|
[4926] | 168 | }
|
---|
| 169 | return roleList;
|
---|
| 170 | }
|
---|
[4647] | 171 |
|
---|
[4962] | 172 | public Guid InsertRole(Role role)
|
---|
[4926] | 173 | {
|
---|
[4647] | 174 |
|
---|
[4926] | 175 | if (role != null)
|
---|
| 176 | {
|
---|
| 177 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
| 178 | {
|
---|
[4647] | 179 |
|
---|
[4926] | 180 | aspnet_Role eRole = new aspnet_Role();
|
---|
[4647] | 181 |
|
---|
[4926] | 182 | Convert.ToEntity(role, eRole);
|
---|
| 183 | try
|
---|
| 184 | {
|
---|
| 185 | db.aspnet_Roles.InsertOnSubmit(eRole);
|
---|
| 186 | db.SubmitChanges();
|
---|
| 187 | }
|
---|
| 188 | catch (Exception ex)
|
---|
| 189 | {
|
---|
[4962] | 190 | return Guid.Empty;
|
---|
[4926] | 191 | }
|
---|
[4588] | 192 |
|
---|
[4962] | 193 | return role.Id;
|
---|
[4926] | 194 | }
|
---|
[4588] | 195 |
|
---|
[4926] | 196 | }
|
---|
[4962] | 197 | return Guid.Empty;
|
---|
[4926] | 198 | }
|
---|
[4588] | 199 |
|
---|
[4926] | 200 | public bool DeleteRole(Guid id)
|
---|
| 201 | {
|
---|
| 202 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
| 203 | {
|
---|
| 204 | var role = db.aspnet_Roles.Where(x => x.RoleId == id).FirstOrDefault();
|
---|
[4588] | 205 |
|
---|
| 206 |
|
---|
[4926] | 207 | try
|
---|
| 208 | {
|
---|
| 209 | db.aspnet_Roles.DeleteOnSubmit(role);
|
---|
| 210 | db.SubmitChanges();
|
---|
| 211 | }
|
---|
| 212 | catch (Exception ex)
|
---|
| 213 | {
|
---|
| 214 | return false;
|
---|
| 215 | }
|
---|
| 216 | return true;
|
---|
[4647] | 217 |
|
---|
| 218 |
|
---|
[4926] | 219 | }
|
---|
| 220 | }
|
---|
[4647] | 221 |
|
---|
[4926] | 222 | public bool AddUserToRole(Guid roleId, Guid userId)
|
---|
| 223 | {
|
---|
| 224 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
| 225 | {
|
---|
| 226 | try
|
---|
| 227 | {
|
---|
| 228 | aspnet_UsersInRole r = new aspnet_UsersInRole();
|
---|
| 229 | r.RoleId = roleId;
|
---|
| 230 | r.UserId = userId;
|
---|
| 231 | db.aspnet_UsersInRoles.InsertOnSubmit(r);
|
---|
| 232 | db.SubmitChanges();
|
---|
| 233 | return true;
|
---|
| 234 | }
|
---|
| 235 | catch (Exception ex)
|
---|
| 236 | {
|
---|
| 237 | Debug.WriteLine(ex.InnerException);
|
---|
| 238 | return false;
|
---|
| 239 | }
|
---|
| 240 | }
|
---|
| 241 | }
|
---|
[4647] | 242 |
|
---|
[4926] | 243 | public bool RemoveUserFromRole(Guid roleId, Guid userId)
|
---|
| 244 | {
|
---|
| 245 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
| 246 | {
|
---|
| 247 | try
|
---|
| 248 | {
|
---|
| 249 | var role = db.aspnet_UsersInRoles.Where(x => x.RoleId == roleId && x.UserId == userId).FirstOrDefault();
|
---|
[4647] | 250 |
|
---|
[4926] | 251 | db.aspnet_UsersInRoles.DeleteOnSubmit(role);
|
---|
| 252 | db.SubmitChanges();
|
---|
| 253 | return true;
|
---|
[4588] | 254 |
|
---|
[4926] | 255 | }
|
---|
| 256 | catch (Exception ex)
|
---|
| 257 | {
|
---|
| 258 | Debug.WriteLine(ex.InnerException);
|
---|
| 259 | return false;
|
---|
| 260 | }
|
---|
| 261 | }
|
---|
[4590] | 262 |
|
---|
[4926] | 263 | }
|
---|
[4588] | 264 |
|
---|
[4926] | 265 | public IEnumerable<Role> GetRolesForUser(Guid userId)
|
---|
| 266 | {
|
---|
| 267 | List<Role> roleList = new List<Role>();
|
---|
| 268 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
| 269 | {
|
---|
| 270 | try
|
---|
| 271 | {
|
---|
| 272 | var roles = db.aspnet_UsersInRoles.Where(x => x.UserId == userId).ToList<aspnet_UsersInRole>();
|
---|
[4647] | 273 |
|
---|
[4926] | 274 | foreach (aspnet_UsersInRole r in roles)
|
---|
| 275 | {
|
---|
| 276 | roleList.Add(GetRole(r.RoleId));
|
---|
[4647] | 277 |
|
---|
[4926] | 278 | }
|
---|
[4647] | 279 |
|
---|
[4926] | 280 | }
|
---|
| 281 | catch (Exception ex)
|
---|
| 282 | {
|
---|
| 283 | Debug.WriteLine(ex.InnerException);
|
---|
| 284 | return new List<Role>();
|
---|
| 285 | }
|
---|
| 286 | }
|
---|
[4647] | 287 |
|
---|
[4926] | 288 | return roleList;
|
---|
| 289 | }
|
---|
[4647] | 290 |
|
---|
[4926] | 291 | public IEnumerable<User> GetUsersInRole(Guid roleId)
|
---|
| 292 | {
|
---|
| 293 | List<User> userList = new List<User>();
|
---|
| 294 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
| 295 | {
|
---|
| 296 | try
|
---|
| 297 | {
|
---|
| 298 | var users = db.aspnet_UsersInRoles.Where(x => x.RoleId == roleId).ToList<aspnet_UsersInRole>();
|
---|
[4647] | 299 |
|
---|
[4926] | 300 | foreach (aspnet_UsersInRole u in users)
|
---|
| 301 | {
|
---|
| 302 | userList.Add(GetUser(u.UserId));
|
---|
[4647] | 303 |
|
---|
[4926] | 304 | }
|
---|
[4647] | 305 |
|
---|
[4926] | 306 | }
|
---|
| 307 | catch (Exception ex)
|
---|
| 308 | {
|
---|
| 309 | Debug.WriteLine(ex.InnerException);
|
---|
| 310 | return new List<User>();
|
---|
| 311 | }
|
---|
| 312 | }
|
---|
[4647] | 313 |
|
---|
[4926] | 314 | return userList;
|
---|
| 315 | }
|
---|
[4647] | 316 |
|
---|
| 317 |
|
---|
| 318 |
|
---|
[4926] | 319 | public bool UpdateRole(Role role)
|
---|
| 320 | {
|
---|
| 321 | if (role != null)
|
---|
| 322 | {
|
---|
| 323 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
| 324 | {
|
---|
[4647] | 325 |
|
---|
[4926] | 326 | var eRole = db.aspnet_Roles.Where(x => x.RoleId == role.Id).FirstOrDefault();
|
---|
[4647] | 327 |
|
---|
[4926] | 328 | Convert.ToEntity(role, eRole);
|
---|
| 329 | try
|
---|
| 330 | {
|
---|
[4647] | 331 |
|
---|
[4926] | 332 | db.SubmitChanges();
|
---|
| 333 | }
|
---|
| 334 | catch (Exception ex)
|
---|
| 335 | {
|
---|
| 336 | return false;
|
---|
| 337 | }
|
---|
[4647] | 338 |
|
---|
[4926] | 339 | return true;
|
---|
[4647] | 340 |
|
---|
[4926] | 341 | }
|
---|
[4647] | 342 |
|
---|
[4926] | 343 | }
|
---|
| 344 | return false;
|
---|
| 345 | }
|
---|
[4647] | 346 |
|
---|
| 347 |
|
---|
| 348 |
|
---|
[4926] | 349 | #endregion
|
---|
[4647] | 350 |
|
---|
| 351 |
|
---|
[4588] | 352 |
|
---|
[4926] | 353 | #region Application
|
---|
[4647] | 354 |
|
---|
[4962] | 355 | public Guid InsertApplication(Application application)
|
---|
[4926] | 356 | {
|
---|
| 357 | if (application != null)
|
---|
| 358 | {
|
---|
| 359 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
| 360 | {
|
---|
[4647] | 361 |
|
---|
[4926] | 362 | aspnet_Application eApplication = new aspnet_Application();
|
---|
[4647] | 363 |
|
---|
[4926] | 364 | Convert.ToEntity(application, eApplication);
|
---|
| 365 | try
|
---|
| 366 | {
|
---|
| 367 | db.aspnet_Applications.InsertOnSubmit(eApplication);
|
---|
| 368 | db.SubmitChanges();
|
---|
| 369 | }
|
---|
| 370 | catch (Exception ex)
|
---|
| 371 | {
|
---|
[4962] | 372 | return Guid.Empty;
|
---|
[4926] | 373 | }
|
---|
[4647] | 374 |
|
---|
[4962] | 375 | return application.Id;
|
---|
[4647] | 376 |
|
---|
[4926] | 377 | }
|
---|
[4647] | 378 |
|
---|
[4926] | 379 | }
|
---|
[4962] | 380 | return Guid.Empty;
|
---|
[4926] | 381 | }
|
---|
[4647] | 382 |
|
---|
[4926] | 383 | public bool UpdateApplication(Application application)
|
---|
| 384 | {
|
---|
| 385 | if (application != null)
|
---|
| 386 | {
|
---|
| 387 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
| 388 | {
|
---|
[4647] | 389 |
|
---|
[4926] | 390 | var eApplication = db.aspnet_Applications.Where(x => x.ApplicationId == application.Id).FirstOrDefault();
|
---|
[4647] | 391 |
|
---|
[4926] | 392 | Convert.ToEntity(application, eApplication);
|
---|
| 393 | try
|
---|
| 394 | {
|
---|
[4647] | 395 |
|
---|
[4926] | 396 | db.SubmitChanges();
|
---|
| 397 | }
|
---|
| 398 | catch (Exception ex)
|
---|
| 399 | {
|
---|
| 400 | return false;
|
---|
| 401 | }
|
---|
[4647] | 402 |
|
---|
[4926] | 403 | return true;
|
---|
[4647] | 404 |
|
---|
[4926] | 405 | }
|
---|
[4647] | 406 |
|
---|
[4926] | 407 | }
|
---|
| 408 | return false;
|
---|
| 409 | }
|
---|
[4726] | 410 |
|
---|
| 411 |
|
---|
[4926] | 412 | public bool DeleteApplication(Guid applicationId)
|
---|
| 413 | {
|
---|
| 414 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
| 415 | {
|
---|
| 416 | var users = db.aspnet_Users.Where(x => x.ApplicationId == applicationId).ToList<aspnet_User>();
|
---|
| 417 | var memberships = db.aspnet_Memberships.Where(x => x.ApplicationId == applicationId).ToList<aspnet_Membership>();
|
---|
[4726] | 418 |
|
---|
[4926] | 419 | var roles = db.aspnet_Roles.Where(x => x.ApplicationId == applicationId).ToList<aspnet_Role>();
|
---|
| 420 | var application = db.aspnet_Applications.Where(x => x.ApplicationId == applicationId).FirstOrDefault();
|
---|
[4726] | 421 |
|
---|
[4926] | 422 | try
|
---|
| 423 | {
|
---|
| 424 | foreach (aspnet_User u in users)
|
---|
| 425 | {
|
---|
| 426 | var userinroles = db.aspnet_UsersInRoles.Where(x => x.UserId == u.UserId).ToList<aspnet_UsersInRole>();
|
---|
| 427 | db.aspnet_UsersInRoles.DeleteAllOnSubmit(userinroles);
|
---|
| 428 | }
|
---|
[4726] | 429 |
|
---|
[4926] | 430 | db.aspnet_Memberships.DeleteAllOnSubmit(memberships);
|
---|
| 431 | db.aspnet_Users.DeleteAllOnSubmit(users);
|
---|
| 432 | db.aspnet_Roles.DeleteAllOnSubmit(roles);
|
---|
| 433 | db.aspnet_Applications.DeleteOnSubmit(application);
|
---|
| 434 | db.SubmitChanges();
|
---|
| 435 | }
|
---|
| 436 | catch (Exception ex)
|
---|
| 437 | {
|
---|
| 438 | return false;
|
---|
| 439 | }
|
---|
| 440 | return true;
|
---|
[4726] | 441 |
|
---|
| 442 |
|
---|
[4926] | 443 | }
|
---|
| 444 | return false;
|
---|
| 445 | }
|
---|
[4726] | 446 |
|
---|
[4926] | 447 | public Application GetApplication(Guid id)
|
---|
| 448 | {
|
---|
| 449 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
| 450 | {
|
---|
| 451 | var application = db.aspnet_Applications.Where(x => x.ApplicationId == id).FirstOrDefault();
|
---|
| 452 | return Convert.ToDataTransfer(application);
|
---|
| 453 | }
|
---|
| 454 | }
|
---|
[4647] | 455 |
|
---|
[4926] | 456 | public IEnumerable<DataTransfer.Application> GetApplications()
|
---|
| 457 | {
|
---|
| 458 | List<DataTransfer.Application> applicationList = new List<DataTransfer.Application>();
|
---|
| 459 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
| 460 | {
|
---|
| 461 | var apps = db.aspnet_Applications.ToList<aspnet_Application>();
|
---|
[4647] | 462 |
|
---|
[4926] | 463 | foreach (aspnet_Application app in apps)
|
---|
| 464 | {
|
---|
| 465 | applicationList.Add(Convert.ToDataTransfer(app));
|
---|
| 466 | }
|
---|
[4647] | 467 |
|
---|
[4926] | 468 | }
|
---|
| 469 | return applicationList;
|
---|
| 470 | }
|
---|
[4647] | 471 |
|
---|
[4926] | 472 | #endregion
|
---|
| 473 | }
|
---|
[4647] | 474 |
|
---|
[4584] | 475 | }
|
---|
[4590] | 476 |
|
---|