public static ApplicationUserExtended ToCoreModel(this ApplicationUser applicationUser, AccountEntity dbEntity, IPermissionScopeService scopeService) { var retVal = new ApplicationUserExtended(); retVal = new ApplicationUserExtended(); retVal.InjectFrom(applicationUser); retVal.InjectFrom(dbEntity); retVal.UserState = EnumUtility.SafeParse<Core.Security.AccountState>( dbEntity.AccountState, Core.Security.AccountState.Approved); retVal.Roles = dbEntity.RoleAssignments.Select(x => x.Role.ToCoreModel(scopeService)).ToArray(); retVal.Permissions = retVal.Roles.SelectMany(x => x.Permissions).SelectMany(x=> x.GetPermissionWithScopeCombinationNames()).Distinct().ToArray(); retVal.ApiAccounts = dbEntity.ApiAccounts.Select(x => x.ToCoreModel()).ToArray(); return retVal; }
public async Task<SecurityResult> CreateAsync(ApplicationUserExtended user) { IdentityResult result = null; if (user != null) { var dbUser = user.ToDataModel(); using (var userManager = _userManagerFactory()) { if (string.IsNullOrEmpty(user.Password)) { result = await userManager.CreateAsync(dbUser); } else { result = await userManager.CreateAsync(dbUser, user.Password); } } if (result.Succeeded) { using (var repository = _platformRepository()) { var account = new AccountEntity { Id = dbUser.Id, UserName = user.UserName, MemberId = user.MemberId, AccountState = AccountState.Approved, RegisterType = (RegisterType)user.UserType, StoreId = user.StoreId }; if (user.Roles != null) { foreach (var role in user.Roles) { account.RoleAssignments.Add(new RoleAssignmentEntity { RoleId = role.Id, AccountId = account.Id }); } } repository.Add(account); repository.UnitOfWork.Commit(); } } } return result == null ? null : result.ToCoreModel(); }
public async Task<SecurityResult> CreateAsync(ApplicationUserExtended user) { IdentityResult result; if (user == null) { throw new ArgumentNullException("user"); } NormalizeUser(user); //Update ASP.NET indentity user using (var userManager = _userManagerFactory()) { var dbUser = user.ToIdentityModel(); user.Id = dbUser.Id; if (string.IsNullOrEmpty(user.Password)) { result = await userManager.CreateAsync(dbUser); } else { result = await userManager.CreateAsync(dbUser, user.Password); } } if (result.Succeeded) { using (var repository = _platformRepository()) { var dbAcount = user.ToDataModel(); if(string.IsNullOrEmpty(user.MemberId)) { //Use for memberId same account id if its not set (Our current case Contact member 1 - 1 Account workaround). But client may use memberId as for any outer id. dbAcount.MemberId = dbAcount.Id; } dbAcount.AccountState = AccountState.Approved.ToString(); repository.Add(dbAcount); repository.UnitOfWork.Commit(); } } return result.ToCoreModel(); }
public static void CopyFrom(this ApplicationUser dbUser, ApplicationUserExtended user) { // Backup old values var id = dbUser.Id; var passwordHash = dbUser.PasswordHash; var securityStamp = dbUser.SecurityStamp; dbUser.InjectFrom(user); // Restore old values if (user.Id == null) dbUser.Id = id; if (user.PasswordHash == null) dbUser.PasswordHash = passwordHash; if (user.SecurityStamp == null) dbUser.SecurityStamp = securityStamp; // Copy logins if (user.Logins != null) { foreach (var login in user.Logins) { var userLogin = dbUser.Logins.FirstOrDefault(l => l.LoginProvider == login.LoginProvider); if (userLogin != null) { userLogin.ProviderKey = login.ProviderKey; } else { dbUser.Logins.Add(new IdentityUserLogin { LoginProvider = login.LoginProvider, ProviderKey = login.ProviderKey, UserId = dbUser.Id }); } } } }
public async Task<SecurityResult> CreateAsync(ApplicationUserExtended user) { IdentityResult result; if (user == null) { throw new ArgumentNullException("user"); } NormalizeUser(user); //Update ASP.NET indentity user using (var userManager = _userManagerFactory()) { var dbUser = user.ToIdentityModel(); user.Id = dbUser.Id; if (string.IsNullOrEmpty(user.Password)) { result = await userManager.CreateAsync(dbUser); } else { result = await userManager.CreateAsync(dbUser, user.Password); } } if (result.Succeeded) { using (var repository = _platformRepository()) { var dbAcount = user.ToDataModel(); dbAcount.AccountState = AccountState.Approved.ToString(); repository.Add(dbAcount); repository.UnitOfWork.Commit(); } } return result.ToCoreModel(); }
private static void NormalizeUser(ApplicationUserExtended user) { if (user.UserName != null) user.UserName = user.UserName.Trim(); if (user.Email != null) user.Email = user.Email.Trim(); if (user.PhoneNumber != null) user.PhoneNumber = user.PhoneNumber.Trim(); }
public async Task<SecurityResult> UpdateAsync(ApplicationUserExtended user) { SecurityResult result; if (user == null) { throw new ArgumentNullException("user"); } NormalizeUser(user); //Update ASP.NET indentity user using (var userManager = _userManagerFactory()) { var dbUser = await userManager.FindByIdAsync(user.Id); result = ValidateUser(dbUser); if (result.Succeeded) { var userName = dbUser.UserName; //Update ASP.NET indentity user user.Patch(dbUser); var identityResult = await userManager.UpdateAsync(dbUser); result = identityResult.ToCoreModel(); //clear cache RemoveUserFromCache(user.Id, userName); } } if (result.Succeeded) { //Update platform security user using (var repository = _platformRepository()) { var targetDbAcount = repository.GetAccountByName(user.UserName, UserDetails.Full); if (targetDbAcount == null) { result = new SecurityResult { Errors = new[] { "Account not found." } }; } else { var changedDbAccount = user.ToDataModel(); using (var changeTracker = GetChangeTracker(repository)) { changeTracker.Attach(targetDbAcount); changedDbAccount.Patch(targetDbAcount); repository.UnitOfWork.Commit(); } } } } return result; }
public async Task<IHttpActionResult> Create(ApplicationUserExtended user) { if (user != null) { user.PasswordHash = null; user.SecurityStamp = null; } var result = await _securityService.CreateAsync(user); if (result == null) return BadRequest(); return Ok(result); }
private ApplicationUserExtended GetUserExtended(ApplicationUser applicationUser, UserDetails detailsLevel) { ApplicationUserExtended result = null; if (applicationUser != null) { result = new ApplicationUserExtended(); result.InjectFrom(applicationUser); using (var repository = _platformRepository()) { var user = repository.GetAccountByName(applicationUser.UserName, detailsLevel); if (user != null) { result.InjectFrom(user); result.UserState = (UserState)user.AccountState; result.UserType = (UserType)user.RegisterType; if (detailsLevel == UserDetails.Full) { var roles = user.RoleAssignments.Select(x => x.Role).ToArray(); result.Roles = roles.Select(r => r.ToCoreModel(false)).ToArray(); var permissionIds = roles .SelectMany(x => x.RolePermissions) .Select(x => x.PermissionId) .Distinct() .ToArray(); result.Permissions = permissionIds; result.ApiAcounts = user.ApiAccounts.Select(x => x.ToCoreModel()).ToArray(); } } } } return result; }
public async Task<SecurityResult> UpdateAsync(ApplicationUserExtended user) { SecurityResult result = null; if (user != null) { var dbUser = await _userManager.FindByIdAsync(user.Id); result = ValidateUser(dbUser); if (result.Succeeded) { dbUser.InjectFrom(user); if (user.Logins != null) { foreach (var login in user.Logins) { var userLogin = dbUser.Logins.FirstOrDefault(l => l.LoginProvider == login.LoginProvider); if (userLogin != null) { userLogin.ProviderKey = login.ProviderKey; } else { dbUser.Logins.Add(new IdentityUserLogin { LoginProvider = login.LoginProvider, ProviderKey = login.ProviderKey, UserId = dbUser.Id }); } } } var identityResult = await _userManager.UpdateAsync(dbUser); result = identityResult.ToCoreModel(); if (result.Succeeded) { using (var repository = _platformRepository()) { var acount = repository.GetAccountByName(user.UserName, UserDetails.Full); if (acount == null) { result = new SecurityResult { Errors = new[] { "Acount not found." } }; } else { acount.RegisterType = (RegisterType)user.UserType; acount.AccountState = (AccountState)user.UserState; acount.MemberId = user.MemberId; acount.StoreId = user.StoreId; if (user.ApiAcounts != null) { var sourceCollection = new ObservableCollection<ApiAccountEntity>(user.ApiAcounts.Select(x => x.ToEntity())); var comparer = AnonymousComparer.Create((ApiAccountEntity x) => x.Id); acount.ApiAccounts.ObserveCollection(x => repository.Add(x), x => repository.Remove(x)); sourceCollection.Patch(acount.ApiAccounts, comparer, (sourceItem, targetItem) => sourceItem.Patch(targetItem)); } if (user.Roles != null) { var sourceCollection = new ObservableCollection<RoleAssignmentEntity>(user.Roles.Select(r => new RoleAssignmentEntity { RoleId = r.Id })); var comparer = AnonymousComparer.Create((RoleAssignmentEntity x) => x.RoleId); acount.RoleAssignments.ObserveCollection(x => repository.Add(x), ra => repository.Remove(ra)); sourceCollection.Patch(acount.RoleAssignments, comparer, (sourceItem, targetItem) => sourceItem.Patch(targetItem)); } repository.UnitOfWork.Commit(); } } } } } return result; }
public async Task<IHttpActionResult> Create(ApplicationUserExtended user) { var result = await _securityService.CreateAsync(user); if (result == null) return BadRequest(); return Ok(result); }
/// <summary> /// Returns list of stores ids which passed user can signIn /// </summary> /// <param name="userId"></param> /// <returns></returns> public IEnumerable<string> GetUserAllowedStoreIds(ApplicationUserExtended user) { if(user == null) { throw new ArgumentNullException("user"); } var retVal = new List<string>(); if(user.StoreId != null) { var store = GetById(user.StoreId); if(store != null) { retVal.Add(store.Id); if(!store.TrustedGroups.IsNullOrEmpty()) { retVal.AddRange(store.TrustedGroups); } } } return retVal; }