public static SecurityResult ToCoreModel(this IdentityResult dataModel)
        {
            var result = new SecurityResult();
            result.InjectFrom(dataModel);

            if (dataModel.Errors != null)
                result.Errors = dataModel.Errors.ToArray();

            return result;
        }
        private SecurityResult ValidateUser(ApplicationUser dbUser)
        {
            var result = new SecurityResult { Succeeded = true };

            if (dbUser == null)
            {
                result = new SecurityResult { Errors = new[] { "User not found." } };
            }

            return result;
        }
        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;
        }
        private IHttpActionResult ProcessSecurityResult(SecurityResult securityResult)
        {
            IHttpActionResult result;

            if (securityResult == null)
            {
                result = BadRequest();
            }
            else
            {
                if (!securityResult.Succeeded)
                    result = BadRequest(securityResult.Errors != null ? string.Join(" ", securityResult.Errors) : "Unknown error.");
                else
                    result = Ok();
            }

            return result;
        }
Esempio n. 5
0
        private SecurityResult ValidateUser(ApplicationUser dbUser)
        {
            SecurityResult result;

            if (dbUser == null)
            {
                result = new SecurityResult { Errors = new[] { "User not found." } };
            }
            else
            {
                if (!IsEditableUser(dbUser.UserName))
                {
                    result = new SecurityResult { Errors = new[] { "It is forbidden to edit this user." } };
                }
                else
                {
                    result = new SecurityResult { Succeeded = true };
                }
            }

            return result;
        }
 private IHttpActionResult ProcessSecurityResult(SecurityResult result)
 {
     if (result == null)
     {
         return BadRequest();
     }
     else
     {
         if (!result.Succeeded)
             return BadRequest(result.Errors != null ? string.Join(" ", result.Errors) : "Unknown error.");
         else
             return Ok();
     }
 }
        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;
        }