[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 | {
|
---|
[4588] | 15 | #region User
|
---|
[4590] | 16 |
|
---|
| 17 | public DataTransfer.User GetUser(Guid id)
|
---|
[4584] | 18 | {
|
---|
| 19 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
| 20 | {
|
---|
[4647] | 21 | try
|
---|
| 22 | {
|
---|
| 23 | Table<aspnet_User> UserTable = db.GetTable<aspnet_User>();
|
---|
[4584] | 24 |
|
---|
[4647] | 25 | var User = from item in UserTable
|
---|
| 26 | where item.UserId == id
|
---|
| 27 | select item;
|
---|
| 28 | User user = Convert.ToDataTransfer((aspnet_User)User.Single());
|
---|
| 29 |
|
---|
| 30 | if (user == null) { return null; }
|
---|
| 31 |
|
---|
| 32 | Table<aspnet_Membership> MembershipTable = db.GetTable<aspnet_Membership>();
|
---|
| 33 |
|
---|
| 34 | var Membership = from item in MembershipTable
|
---|
| 35 | where item.UserId == id
|
---|
| 36 | select item;
|
---|
| 37 |
|
---|
| 38 | Membership membership = Convert.ToDataTransfer((aspnet_Membership)Membership.Single());
|
---|
| 39 |
|
---|
| 40 | if (membership == null) { return null; }
|
---|
| 41 | user.Membership = membership;
|
---|
| 42 |
|
---|
| 43 | return user;
|
---|
| 44 |
|
---|
| 45 |
|
---|
| 46 | }
|
---|
| 47 | catch (Exception ex)
|
---|
| 48 | {
|
---|
| 49 | Debug.WriteLine(ex.InnerException);
|
---|
| 50 | return null;
|
---|
| 51 | }
|
---|
[4584] | 52 | }
|
---|
| 53 | }
|
---|
[4647] | 54 |
|
---|
| 55 | public DataTransfer.User GetUser(Guid applicationId, string userName)
|
---|
[4584] | 56 | {
|
---|
[4647] | 57 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
| 58 | {
|
---|
| 59 | try
|
---|
| 60 | {
|
---|
| 61 | Table<aspnet_User> UserTable = db.GetTable<aspnet_User>();
|
---|
| 62 |
|
---|
| 63 | var User = from item in UserTable
|
---|
| 64 | where item.UserName == userName && item.ApplicationId == applicationId
|
---|
| 65 | select item;
|
---|
| 66 |
|
---|
| 67 | User user = Convert.ToDataTransfer((aspnet_User)User.Single());
|
---|
| 68 |
|
---|
| 69 | if (user == null) { return null; }
|
---|
| 70 |
|
---|
| 71 | Table<aspnet_Membership> MembershipTable = db.GetTable<aspnet_Membership>();
|
---|
| 72 |
|
---|
| 73 | var Membership = from item in MembershipTable
|
---|
| 74 | where item.UserId == user.UserId
|
---|
| 75 | select item;
|
---|
| 76 |
|
---|
| 77 | Membership membership = Convert.ToDataTransfer((aspnet_Membership)Membership.Single());
|
---|
| 78 |
|
---|
| 79 | if (membership == null) { return null; }
|
---|
| 80 | user.Membership = membership;
|
---|
| 81 |
|
---|
| 82 | return user;
|
---|
| 83 |
|
---|
| 84 |
|
---|
| 85 |
|
---|
| 86 |
|
---|
| 87 | }
|
---|
| 88 | catch (Exception ex)
|
---|
| 89 | {
|
---|
| 90 | Debug.WriteLine(ex.InnerException);
|
---|
| 91 | return null;
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | public IEnumerable<DataTransfer.User> GetUsers(Guid applicationId)
|
---|
| 97 | {
|
---|
[4584] | 98 | List<DataTransfer.User> UserList = new List<DataTransfer.User>();
|
---|
| 99 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
| 100 | {
|
---|
[4647] | 101 | try
|
---|
| 102 | {
|
---|
| 103 | Table<aspnet_User> UserTable = db.GetTable<aspnet_User>();
|
---|
| 104 | var Users = from item in UserTable
|
---|
| 105 | where item.ApplicationId == applicationId
|
---|
| 106 | select item;
|
---|
| 107 | foreach (var eUser in Users)
|
---|
| 108 | {
|
---|
[4584] | 109 |
|
---|
[4647] | 110 | User user = Convert.ToDataTransfer(eUser);
|
---|
| 111 |
|
---|
| 112 | if (user== null) { return null; }
|
---|
| 113 |
|
---|
| 114 | Table<aspnet_Membership> MembershipTable = db.GetTable<aspnet_Membership>();
|
---|
| 115 |
|
---|
| 116 | var Membership = from item in MembershipTable
|
---|
| 117 | where item.UserId == user.UserId
|
---|
| 118 | select item;
|
---|
| 119 |
|
---|
| 120 | Membership membership = Convert.ToDataTransfer((aspnet_Membership)Membership.Single());
|
---|
| 121 |
|
---|
| 122 | if (membership == null) { return null; }
|
---|
| 123 | user.Membership = membership;
|
---|
| 124 |
|
---|
| 125 | UserList.Add(user);
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 | catch (Exception ex)
|
---|
[4584] | 129 | {
|
---|
[4647] | 130 | Debug.WriteLine(ex.InnerException);
|
---|
| 131 | return new List<User>();
|
---|
[4584] | 132 | }
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | return UserList;
|
---|
| 136 | }
|
---|
[4590] | 137 | public bool InsertUser(User user)
|
---|
[4588] | 138 | {
|
---|
| 139 | if (user != null)
|
---|
| 140 | {
|
---|
| 141 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
| 142 | {
|
---|
[4590] | 143 | try
|
---|
| 144 | {
|
---|
[4647] | 145 |
|
---|
| 146 | Guid? userId = null;
|
---|
| 147 | Application application = GetApplication(user.ApplicationId);
|
---|
| 148 | if(application == null){return false;}
|
---|
| 149 | if (user.Membership == null) { return false; }
|
---|
| 150 |
|
---|
| 151 | int? passwordFormat = 1;
|
---|
| 152 | int? uniqueEmail = null;
|
---|
| 153 | int result = db.aspnet_Membership_CreateUser(application.ApplicationName, user.UserName, user.Membership.Password, user.Membership.PasswordSalt, user.Membership.Email, user.Membership.PasswordQuestion, user.Membership.PasswordAnswer, user.Membership.IsApproved, DateTime.UtcNow, DateTime.Now, uniqueEmail, passwordFormat, ref userId);
|
---|
| 154 |
|
---|
| 155 | if (result != 0)
|
---|
| 156 | {
|
---|
| 157 | return false;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | if (userId != null)
|
---|
| 161 | {
|
---|
| 162 | return true;
|
---|
| 163 | }
|
---|
| 164 | else
|
---|
| 165 | {
|
---|
| 166 | return false;
|
---|
| 167 | }
|
---|
[4590] | 168 | }
|
---|
[4647] | 169 | catch (Exception ex)
|
---|
[4590] | 170 | {
|
---|
[4647] | 171 | Debug.WriteLine(ex.InnerException);
|
---|
[4590] | 172 | return false;
|
---|
| 173 | }
|
---|
[4588] | 174 | }
|
---|
[4647] | 175 |
|
---|
[4588] | 176 | }
|
---|
[4590] | 177 | return false;
|
---|
[4588] | 178 | }
|
---|
[4590] | 179 | public bool DeleteUser(Guid id)
|
---|
| 180 | {
|
---|
[4588] | 181 |
|
---|
[4647] | 182 | User user = GetUser(id);
|
---|
| 183 | if (user != null)
|
---|
| 184 | {
|
---|
| 185 |
|
---|
| 186 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
| 187 | {
|
---|
| 188 | try
|
---|
| 189 | {
|
---|
| 190 | Application application = GetApplication(user.ApplicationId);
|
---|
| 191 | if (application == null)
|
---|
| 192 | {
|
---|
| 193 | return false;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | int? tablesToDeleteFrom = 99;
|
---|
| 197 | int? numTablesDeletedFrom = null;
|
---|
| 198 | db.aspnet_Users_DeleteUser(application.ApplicationName, user.UserName, tablesToDeleteFrom, ref numTablesDeletedFrom);
|
---|
| 199 |
|
---|
| 200 | if (numTablesDeletedFrom != null)
|
---|
| 201 | {
|
---|
| 202 | return true;
|
---|
| 203 | }
|
---|
| 204 | else
|
---|
| 205 | {
|
---|
| 206 | return false;
|
---|
| 207 | }
|
---|
| 208 | }
|
---|
| 209 | catch (Exception ex)
|
---|
| 210 | {
|
---|
| 211 | Debug.WriteLine(ex.InnerException);
|
---|
| 212 | return false;
|
---|
| 213 | }
|
---|
| 214 | }
|
---|
| 215 | }
|
---|
[4590] | 216 | return false;
|
---|
| 217 | }
|
---|
[4647] | 218 |
|
---|
[4590] | 219 | public bool UpdateUser(User user)
|
---|
[4588] | 220 | {
|
---|
[4647] | 221 |
|
---|
| 222 | if (user != null)
|
---|
| 223 | {
|
---|
| 224 |
|
---|
| 225 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
| 226 | {
|
---|
| 227 | try
|
---|
| 228 | {
|
---|
| 229 | if (user.Membership == null)
|
---|
| 230 | {
|
---|
| 231 | return false;
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | Table<aspnet_User> UserTable = db.GetTable<aspnet_User>();
|
---|
| 235 |
|
---|
| 236 | var User = from item in UserTable
|
---|
| 237 | where item.UserId == user.UserId
|
---|
| 238 | select item;
|
---|
| 239 | aspnet_User eUser = ((aspnet_User)User.Single());
|
---|
| 240 |
|
---|
| 241 |
|
---|
| 242 | Table<aspnet_Membership> MembershipTable = db.GetTable<aspnet_Membership>();
|
---|
| 243 |
|
---|
| 244 | var Membership = from item in MembershipTable
|
---|
| 245 | where item.UserId == user.UserId
|
---|
| 246 | select item;
|
---|
| 247 |
|
---|
| 248 | aspnet_Membership eMembership = ((aspnet_Membership)Membership.Single());
|
---|
| 249 |
|
---|
| 250 | Convert.ToEntity(user, eUser);
|
---|
| 251 |
|
---|
| 252 | Convert.ToEntity(user.Membership, eMembership);
|
---|
| 253 | if (eUser == null)
|
---|
| 254 | {
|
---|
| 255 | return false;
|
---|
| 256 | }
|
---|
| 257 | if (eMembership == null)
|
---|
| 258 | {
|
---|
| 259 | return false;
|
---|
| 260 | }
|
---|
| 261 | db.SubmitChanges();
|
---|
| 262 | }
|
---|
| 263 | catch (Exception ex)
|
---|
| 264 | {
|
---|
| 265 | Debug.WriteLine(ex.InnerException);
|
---|
| 266 | return false;
|
---|
| 267 | }
|
---|
| 268 | }
|
---|
| 269 | } return false;
|
---|
[4590] | 270 | }
|
---|
[4588] | 271 |
|
---|
[4590] | 272 | #endregion
|
---|
[4588] | 273 |
|
---|
| 274 |
|
---|
[4590] | 275 | #region Role
|
---|
[4588] | 276 |
|
---|
[4590] | 277 | public Role GetRole(Guid id)
|
---|
| 278 | {
|
---|
[4647] | 279 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
| 280 | {
|
---|
| 281 | try
|
---|
| 282 | {
|
---|
| 283 | Table<aspnet_Role> RoleTable = db.GetTable<aspnet_Role>();
|
---|
[4588] | 284 |
|
---|
[4647] | 285 | var Role = from item in RoleTable
|
---|
| 286 | where item.RoleId == id
|
---|
| 287 | select item;
|
---|
| 288 | return Convert.ToDataTransfer((aspnet_Role)Role.Single());
|
---|
| 289 | }
|
---|
| 290 | catch (Exception ex)
|
---|
| 291 | {
|
---|
| 292 | Debug.WriteLine(ex.InnerException);
|
---|
| 293 | return null;
|
---|
| 294 | }
|
---|
| 295 | }
|
---|
[4590] | 296 | }
|
---|
[4647] | 297 |
|
---|
| 298 | public Role GetRole(Guid applicationId, string roleName)
|
---|
[4590] | 299 | {
|
---|
[4647] | 300 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
| 301 | {
|
---|
| 302 | try
|
---|
| 303 | {
|
---|
| 304 | Table<aspnet_Role> RoleTable = db.GetTable<aspnet_Role>();
|
---|
| 305 |
|
---|
| 306 | var Role = from item in RoleTable
|
---|
| 307 | where item.RoleName == roleName && item.ApplicationId == applicationId
|
---|
| 308 | select item;
|
---|
| 309 | return Convert.ToDataTransfer((aspnet_Role)Role.Single());
|
---|
| 310 | }
|
---|
| 311 | catch (Exception ex)
|
---|
| 312 | {
|
---|
| 313 | Debug.WriteLine(ex.InnerException);
|
---|
| 314 | return null;
|
---|
| 315 | }
|
---|
| 316 | }
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 | public bool RoleExists(Guid roleId)
|
---|
| 320 | {
|
---|
| 321 | if (roleId != null)
|
---|
| 322 | {
|
---|
| 323 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
| 324 | {
|
---|
| 325 | try
|
---|
| 326 | {
|
---|
| 327 | Role role = GetRole(roleId);
|
---|
| 328 | if (role == null)
|
---|
| 329 | {
|
---|
| 330 | return false;
|
---|
| 331 | }
|
---|
| 332 | Application application = GetApplication(role.ApplicationId);
|
---|
| 333 | if (application == null)
|
---|
| 334 | {
|
---|
| 335 | return false;
|
---|
| 336 | }
|
---|
| 337 |
|
---|
| 338 | int result = db.aspnet_Roles_RoleExists(application.ApplicationName, role.RoleName);
|
---|
| 339 |
|
---|
| 340 | return (result == 0);
|
---|
| 341 | }
|
---|
| 342 | catch (Exception ex)
|
---|
| 343 | {
|
---|
| 344 | Debug.Write(ex.InnerException);
|
---|
| 345 | return false;
|
---|
| 346 | }
|
---|
| 347 | }
|
---|
| 348 |
|
---|
| 349 | } return false;
|
---|
| 350 | }
|
---|
| 351 |
|
---|
| 352 | public IEnumerable<Role> GetRoles(Guid applicationId)
|
---|
| 353 | {
|
---|
[4590] | 354 | List<Role> RoleList = new List<Role>();
|
---|
[4588] | 355 |
|
---|
[4590] | 356 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
| 357 | {
|
---|
[4647] | 358 | try
|
---|
| 359 | {
|
---|
| 360 | // db.aspnet_Roles_GetAllRoles(applicationName);
|
---|
| 361 | Table<aspnet_Role> RoleTable = db.GetTable<aspnet_Role>();
|
---|
[4590] | 362 |
|
---|
[4647] | 363 | var Roles = from item in RoleTable
|
---|
| 364 | where item.ApplicationId == applicationId
|
---|
| 365 | select item;
|
---|
| 366 | foreach (var Role in Roles)
|
---|
| 367 | {
|
---|
| 368 | RoleList.Add(Convert.ToDataTransfer((aspnet_Role)Role));
|
---|
| 369 | }
|
---|
| 370 | }
|
---|
| 371 | catch (Exception ex)
|
---|
[4590] | 372 | {
|
---|
[4647] | 373 | Debug.WriteLine(ex.InnerException);
|
---|
| 374 | return new List<Role>();
|
---|
[4588] | 375 | }
|
---|
[4590] | 376 | }
|
---|
[4588] | 377 |
|
---|
[4590] | 378 | return RoleList;
|
---|
| 379 | }
|
---|
[4647] | 380 |
|
---|
[4590] | 381 | public bool InsertRole(Role role)
|
---|
| 382 | {
|
---|
| 383 | if (role != null)
|
---|
| 384 | {
|
---|
| 385 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
| 386 | {
|
---|
| 387 | try
|
---|
| 388 | {
|
---|
[4647] | 389 | Application application = GetApplication(role.ApplicationId);
|
---|
| 390 | if (application == null)
|
---|
| 391 | {
|
---|
| 392 | return false;
|
---|
| 393 | }
|
---|
| 394 | int result = db.aspnet_Roles_CreateRole(application.ApplicationName, role.RoleName);
|
---|
| 395 |
|
---|
| 396 | return (result == 0); // checken, welchen rückgabewert (in db, procedure)
|
---|
| 397 |
|
---|
[4590] | 398 | }
|
---|
[4647] | 399 | catch (Exception ex)
|
---|
[4590] | 400 | {
|
---|
[4647] | 401 | Debug.WriteLine(ex.InnerException);
|
---|
[4590] | 402 | return false;
|
---|
| 403 | }
|
---|
| 404 | }
|
---|
[4647] | 405 |
|
---|
[4588] | 406 | }
|
---|
[4590] | 407 | return false;
|
---|
[4588] | 408 | }
|
---|
[4590] | 409 | public bool UpdateRole(Role role)
|
---|
| 410 | {
|
---|
[4647] | 411 |
|
---|
| 412 | if (role != null)
|
---|
| 413 | {
|
---|
| 414 |
|
---|
| 415 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
| 416 | {
|
---|
| 417 | try
|
---|
| 418 | {
|
---|
| 419 |
|
---|
| 420 | Table<aspnet_Role> RoleTable = db.GetTable<aspnet_Role>();
|
---|
| 421 |
|
---|
| 422 | var Role = from item in RoleTable
|
---|
| 423 | where item.RoleId == role.RoleId
|
---|
| 424 | select item;
|
---|
| 425 | aspnet_Role eRole = ((aspnet_Role)Role.Single());
|
---|
| 426 |
|
---|
| 427 |
|
---|
| 428 | Convert.ToEntity(role, eRole);
|
---|
| 429 |
|
---|
| 430 | if (eRole != null)
|
---|
| 431 | {
|
---|
| 432 | db.SubmitChanges();
|
---|
| 433 | }
|
---|
| 434 | else
|
---|
| 435 | {
|
---|
| 436 | return false;
|
---|
| 437 | }
|
---|
| 438 | }
|
---|
| 439 | catch (Exception ex)
|
---|
| 440 | {
|
---|
| 441 | Debug.WriteLine(ex.InnerException);
|
---|
| 442 | return false;
|
---|
| 443 | }
|
---|
| 444 | }
|
---|
| 445 | } return false;
|
---|
[4590] | 446 | }
|
---|
[4647] | 447 |
|
---|
[4590] | 448 | public bool DeleteRole(Guid id)
|
---|
| 449 | {
|
---|
[4647] | 450 | if (id != null)
|
---|
| 451 | {
|
---|
| 452 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
| 453 | {
|
---|
| 454 | try
|
---|
| 455 | {
|
---|
| 456 | Role role = GetRole(id);
|
---|
| 457 |
|
---|
| 458 | bool deleteOnlyIfRoleIsEmpty = true;
|
---|
| 459 | if (role == null)
|
---|
| 460 | {
|
---|
| 461 | return false;
|
---|
| 462 | }
|
---|
| 463 | Application application = GetApplication(role.ApplicationId);
|
---|
| 464 | if (application == null)
|
---|
| 465 | {
|
---|
| 466 | return false;
|
---|
| 467 | }
|
---|
| 468 | db.aspnet_Roles_DeleteRole(application.ApplicationName, role.RoleName, deleteOnlyIfRoleIsEmpty);
|
---|
| 469 |
|
---|
| 470 |
|
---|
| 471 |
|
---|
| 472 | return true;
|
---|
| 473 | }
|
---|
| 474 | catch (Exception ex)
|
---|
| 475 | {
|
---|
| 476 | Debug.WriteLine(ex.InnerException);
|
---|
| 477 | return false;
|
---|
| 478 | }
|
---|
| 479 | }
|
---|
| 480 |
|
---|
| 481 | }
|
---|
[4590] | 482 | return false;
|
---|
| 483 | }
|
---|
[4647] | 484 |
|
---|
| 485 | public bool IsUserInRole(Guid roleId, Guid userId)
|
---|
[4590] | 486 | {
|
---|
[4647] | 487 | if (roleId != null && userId != null)
|
---|
| 488 | {
|
---|
| 489 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
| 490 | {
|
---|
| 491 |
|
---|
| 492 | try
|
---|
| 493 | {
|
---|
| 494 | User user = GetUser(userId);
|
---|
| 495 | if (user == null) { return false; }
|
---|
| 496 |
|
---|
| 497 | Application application = GetApplication(user.ApplicationId);
|
---|
| 498 | if (application == null) { return false; }
|
---|
| 499 |
|
---|
| 500 | Role role = GetRole(roleId);
|
---|
| 501 | if (role == null) { return false; }
|
---|
| 502 |
|
---|
| 503 | int result = db.aspnet_UsersInRoles_IsUserInRole(application.ApplicationName, user.UserName, role.RoleName);
|
---|
| 504 |
|
---|
| 505 | return (result == 0);
|
---|
| 506 | }
|
---|
| 507 | catch (Exception ex)
|
---|
| 508 | {
|
---|
| 509 | Debug.WriteLine(ex.InnerException);
|
---|
| 510 | return false;
|
---|
| 511 |
|
---|
| 512 | }
|
---|
| 513 |
|
---|
| 514 | }
|
---|
| 515 |
|
---|
| 516 | } return false;
|
---|
[4590] | 517 | }
|
---|
[4647] | 518 |
|
---|
| 519 | public bool AddUserToRole(Guid roleId, Guid userId)
|
---|
[4590] | 520 | {
|
---|
[4647] | 521 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
| 522 | {
|
---|
| 523 | try
|
---|
| 524 | {
|
---|
| 525 | Role role = GetRole(roleId);
|
---|
| 526 | if (role == null)
|
---|
| 527 | {
|
---|
| 528 | return false;
|
---|
| 529 | }
|
---|
| 530 | Application application = GetApplication(role.ApplicationId);
|
---|
| 531 | if (application == null)
|
---|
| 532 | {
|
---|
| 533 | return false;
|
---|
| 534 | }
|
---|
| 535 | User user = GetUser(userId);
|
---|
| 536 | if (user == null)
|
---|
| 537 | {
|
---|
| 538 | return false;
|
---|
| 539 | }
|
---|
| 540 |
|
---|
| 541 | db.aspnet_UsersInRoles_AddUsersToRoles(application.ApplicationName, user.UserName, role.RoleName,DateTime.Now);
|
---|
| 542 | return true;
|
---|
| 543 | }
|
---|
| 544 | catch (Exception ex)
|
---|
| 545 | {
|
---|
| 546 | Debug.WriteLine(ex.InnerException);
|
---|
| 547 | return false;
|
---|
| 548 | }
|
---|
| 549 | }
|
---|
| 550 |
|
---|
[4590] | 551 | }
|
---|
[4647] | 552 | public bool RemoveUserFromRole(Guid roleId, Guid userId)
|
---|
| 553 | {
|
---|
| 554 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
| 555 | {
|
---|
| 556 | try
|
---|
| 557 | {
|
---|
| 558 | Role role = GetRole(roleId);
|
---|
| 559 | if (role == null)
|
---|
| 560 | {
|
---|
| 561 | return false;
|
---|
| 562 | }
|
---|
| 563 | Application application = GetApplication(role.ApplicationId);
|
---|
| 564 | if (application == null)
|
---|
| 565 | {
|
---|
| 566 | return false;
|
---|
| 567 | }
|
---|
| 568 | User user = GetUser(userId);
|
---|
| 569 | if (user == null)
|
---|
| 570 | {
|
---|
| 571 | return false;
|
---|
| 572 | }
|
---|
| 573 |
|
---|
| 574 | db.aspnet_UsersInRoles_RemoveUsersFromRoles(application.ApplicationName, user.UserName, role.RoleName);
|
---|
| 575 | return true;
|
---|
| 576 |
|
---|
| 577 | }
|
---|
| 578 | catch (Exception ex)
|
---|
| 579 | {
|
---|
| 580 | Debug.WriteLine(ex.InnerException);
|
---|
| 581 | return false;
|
---|
| 582 | }
|
---|
| 583 | }
|
---|
| 584 |
|
---|
| 585 | }
|
---|
[4590] | 586 | public IEnumerable<Role> GetRolesForUser(Guid userId)
|
---|
| 587 | {
|
---|
| 588 | List<Role> roles = new List<Role>();
|
---|
[4647] | 589 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
| 590 | {
|
---|
| 591 | try
|
---|
| 592 | {
|
---|
| 593 | User user = GetUser(userId);
|
---|
| 594 | if (user == null)
|
---|
| 595 | {
|
---|
| 596 | return roles;
|
---|
| 597 | }
|
---|
| 598 | Application application = GetApplication(user.ApplicationId);
|
---|
| 599 | if (application == null)
|
---|
| 600 | {
|
---|
| 601 | return roles;
|
---|
| 602 | }
|
---|
| 603 |
|
---|
| 604 |
|
---|
| 605 | ISingleResult<aspnet_UsersInRoles_GetRolesForUserResult> results = db.aspnet_UsersInRoles_GetRolesForUser(application.ApplicationName,user.UserName);
|
---|
| 606 | foreach (aspnet_UsersInRoles_GetRolesForUserResult userInRole in results)
|
---|
| 607 | {
|
---|
| 608 | roles.Add(GetRole(application.ApplicationId,userInRole.RoleName));
|
---|
| 609 | }
|
---|
| 610 | }
|
---|
| 611 | catch (Exception ex)
|
---|
| 612 | {
|
---|
| 613 | Debug.WriteLine(ex.InnerException);
|
---|
| 614 | return new List<Role>();
|
---|
| 615 | }
|
---|
| 616 | }
|
---|
| 617 |
|
---|
[4590] | 618 | return roles;
|
---|
| 619 | }
|
---|
[4588] | 620 |
|
---|
[4647] | 621 | public IEnumerable<User> GetUsersInRole(Guid roleId)
|
---|
| 622 | {
|
---|
| 623 | List<User> users = new List<User>();
|
---|
| 624 |
|
---|
| 625 | using(UserManagementDataContext db = new UserManagementDataContext()){
|
---|
| 626 | try
|
---|
| 627 | {
|
---|
| 628 | Role role = GetRole(roleId);
|
---|
| 629 |
|
---|
| 630 | if (role != null)
|
---|
| 631 | {
|
---|
| 632 | Application application = GetApplication(role.ApplicationId);
|
---|
| 633 |
|
---|
| 634 | if (application != null)
|
---|
| 635 | {
|
---|
| 636 |
|
---|
| 637 |
|
---|
| 638 | ISingleResult<aspnet_UsersInRoles_GetUsersInRolesResult> result = db.aspnet_UsersInRoles_GetUsersInRoles(application.ApplicationName, role.RoleName);
|
---|
| 639 | foreach (aspnet_UsersInRoles_GetUsersInRolesResult usersInRole in result)
|
---|
| 640 | {
|
---|
| 641 | users.Add(GetUser(application.ApplicationId,usersInRole.UserName));
|
---|
| 642 | }
|
---|
| 643 | }
|
---|
| 644 | }
|
---|
| 645 | }
|
---|
| 646 | catch (Exception ex)
|
---|
| 647 | {
|
---|
| 648 | Debug.WriteLine(ex.InnerException);
|
---|
| 649 | return new List<User>();
|
---|
| 650 | }
|
---|
| 651 | }
|
---|
| 652 |
|
---|
| 653 | return users;
|
---|
| 654 | }
|
---|
| 655 |
|
---|
[4588] | 656 | #endregion
|
---|
[4647] | 657 |
|
---|
| 658 | #region Application
|
---|
| 659 |
|
---|
| 660 | public Application InsertApplication(Application application)
|
---|
| 661 | {
|
---|
| 662 | if (application != null)
|
---|
| 663 | {
|
---|
| 664 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
| 665 | {
|
---|
| 666 | try
|
---|
| 667 | {
|
---|
| 668 | Guid? applicationId = null;
|
---|
| 669 | int result = db.aspnet_Applications_CreateApplication(application.ApplicationName, ref applicationId);
|
---|
| 670 | Console.WriteLine("result=" + result);
|
---|
| 671 | if (applicationId != null)
|
---|
| 672 | {
|
---|
| 673 | application.ApplicationId = (Guid)applicationId;
|
---|
| 674 | return application;
|
---|
| 675 | }
|
---|
| 676 | else
|
---|
| 677 | {
|
---|
| 678 | return null;
|
---|
| 679 | }
|
---|
| 680 | }
|
---|
| 681 | catch (Exception ex)
|
---|
| 682 | {
|
---|
| 683 | Debug.WriteLine(ex.InnerException);
|
---|
| 684 | return null;
|
---|
| 685 | }
|
---|
| 686 | }
|
---|
| 687 |
|
---|
| 688 | }
|
---|
| 689 | return null;
|
---|
| 690 | }
|
---|
| 691 |
|
---|
| 692 | public bool DeleteApplication(Application application)
|
---|
| 693 | {
|
---|
| 694 | return false;
|
---|
| 695 | }
|
---|
| 696 |
|
---|
| 697 | public Application GetApplication(Guid id)
|
---|
| 698 | {
|
---|
| 699 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
| 700 | {
|
---|
| 701 | try
|
---|
| 702 | {
|
---|
| 703 | Table<aspnet_Application> ApplicationTable = db.GetTable<aspnet_Application>();
|
---|
| 704 |
|
---|
| 705 | var Application = from item in ApplicationTable
|
---|
| 706 | where item.ApplicationId == id
|
---|
| 707 | select item;
|
---|
| 708 | return Convert.ToDataTransfer((aspnet_Application)Application.Single());
|
---|
| 709 | }
|
---|
| 710 | catch (Exception ex)
|
---|
| 711 | {
|
---|
| 712 | Debug.WriteLine(ex.InnerException);
|
---|
| 713 | return null;
|
---|
| 714 | }
|
---|
| 715 | }
|
---|
| 716 | }
|
---|
| 717 |
|
---|
| 718 | #endregion
|
---|
| 719 |
|
---|
| 720 | #region Membership
|
---|
| 721 |
|
---|
| 722 | public Membership InsertMembership(Membership membership)
|
---|
| 723 | {
|
---|
| 724 |
|
---|
| 725 | if (membership != null)
|
---|
| 726 | {
|
---|
| 727 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
| 728 | {
|
---|
| 729 | try
|
---|
| 730 | {
|
---|
| 731 | Application application = GetApplication(membership.ApplicationId);
|
---|
| 732 | if (application == null) { return null; }
|
---|
| 733 |
|
---|
| 734 | }
|
---|
| 735 | catch (Exception ex)
|
---|
| 736 | {
|
---|
| 737 | Debug.WriteLine(ex.InnerException);
|
---|
| 738 | return null;
|
---|
| 739 | }
|
---|
| 740 | }
|
---|
| 741 | }
|
---|
| 742 | return null;
|
---|
| 743 | }
|
---|
| 744 |
|
---|
| 745 | #endregion
|
---|
[4584] | 746 | }
|
---|
| 747 | }
|
---|
[4590] | 748 |
|
---|