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 = _userManager) { var dbUser = await userManager.FindByIdAsync(user.Id.ToString()); 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); }
private ApplicationUserExtended GetUserExtended(UserEntity applicationUser, UserDetails detailsLevel) { ApplicationUserExtended result = null; if (applicationUser != null) { var cacheRegion = GetUserCacheRegion(applicationUser.Id.ToString()); result = _cacheManager.Get(cacheRegion + ":" + detailsLevel, cacheRegion, () => { ApplicationUserExtended retVal; var user = _baseUnitOfWork.BaseWorkArea.User.Query().Include(x => x.Roles).First(x => x.UserName == applicationUser.UserName); var roleIds = user.Roles.Select(x => x.RoleId); //角色 var roles = _roleManager.Roles.Where(x => roleIds.Contains(x.Id)).Select(x => x.ToCoreModel()).ToArray(); //模块权限 var modules = _baseUnitOfWork.BaseWorkArea.RoleModule.QueryFilter(x => roleIds.Contains(x.RoleId)).Select(x => x.Module).Distinct(); //操作权限 var permissions = _baseUnitOfWork.BaseWorkArea.RolePermission.QueryFilter(x => roleIds.Contains(x.RoleId)).Select(x => x.Permission).Distinct(); retVal = user.ToCoreModel(); retVal.Roles = roles; retVal.Permissions = permissions.Select(rp => rp.Name).Distinct().ToArray(); retVal.Modules = modules.Select(x => x.EnCode).Distinct().ToArray(); if (detailsLevel != UserDetails.Export) { retVal.PasswordHash = null; retVal.SecurityStamp = null; } return(retVal); }); } return(result); }
private ApplicationUserExtended GetUserExtended(UserEntity applicationUser, UserDetails detailsLevel) { ApplicationUserExtended result = null; if (applicationUser != null) { var cacheRegion = GetUserCacheRegion(applicationUser.Id.ToString()); result = _cacheManager.Get(cacheRegion + ":" + detailsLevel, cacheRegion, () => { ApplicationUserExtended retVal; var user = _baseUnitOfWork.BaseWorkArea.User.Query().First(x => x.UserName == applicationUser.UserName); retVal = applicationUser.ToCoreModel(user, _permissionScopeService); //Populate available permission scopes if (retVal.Roles != null) { foreach (var permission in retVal.Roles.SelectMany(x => x.Permissions).Where(x => x != null)) { permission.AvailableScopes = _permissionScopeService.GetAvailablePermissionScopes(permission.Id).ToList(); } } if (detailsLevel != UserDetails.Export) { retVal.PasswordHash = null; retVal.SecurityStamp = null; } return(retVal); }); } return(result); }
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 = _userManager) { 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()); }
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(); } }