public static ApplicationUser ToDataModel(this ApplicationUserExtended user)
        {
            var dbUser = new ApplicationUser();
            var id = dbUser.Id;
            dbUser.InjectFrom(user);
            dbUser.Id = id;

            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
                        });
                    }
                }
            }

            return dbUser;
        }
        public static void Patch(this ApplicationUserExtended user, ApplicationUser dbUser)
        {
            var patchInjection = new PatchInjection<ApplicationUser>(x => x.Id, x => x.PasswordHash, x => x.SecurityStamp,
                                                                     x => x.UserName, x => x.Email, x => x.PhoneNumber);
            dbUser.InjectFrom(patchInjection, user);

            // Copy logins
            if (user.Logins != null)
            {
                var changedLogins = user.Logins.Select(x => new IdentityUserLogin
                {
                    LoginProvider = x.LoginProvider,
                    ProviderKey = x.ProviderKey,
                    UserId = dbUser.Id
                }).ToList();

                var comparer = AnonymousComparer.Create((IdentityUserLogin x) => x.LoginProvider);
                dbUser.Logins.Patch(changedLogins, comparer, (sourceItem, targetItem) => { targetItem.ProviderKey = sourceItem.ProviderKey; });
            }
        
        }
 public static ApplicationUser ToDataModel(this ApplicationUserExtended user)
 {
     var dbUser = new ApplicationUser();
     dbUser.CopyFrom(user);
     return dbUser;
 }
        private ApplicationUserExtended GetUserExtended(ApplicationUser applicationUser, UserDetails detailsLevel)
        {
            ApplicationUserExtended result = null;
            if (applicationUser != null)
            {
                var cacheRegion = GetUserCacheRegion(applicationUser.Id);
                result = _cacheManager.Get(cacheRegion + ":" + detailsLevel, cacheRegion, () =>
                {
                    ApplicationUserExtended retVal;
                    using (var repository = _platformRepository())
                    {
                        var user = repository.GetAccountByName(applicationUser.UserName, detailsLevel);
                        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;
        }
        private SecurityResult ValidateUser(ApplicationUser dbUser)
        {
            var result = new SecurityResult { Succeeded = true };

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

            return result;
        }
        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 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.ToWebModel()).ToList();
                        }
                    }
                }
            }

            return result;
        }
        public async Task<IHttpActionResult> CreateAsync(ApplicationUserExtended user)
        {
            var dbUser = new ApplicationUser();

            dbUser.InjectFrom(user);

            IdentityResult result;
            if (!string.IsNullOrEmpty(user.Password))
            {
                result = await UserManager.CreateAsync(dbUser, user.Password);
            }
            else
            {
                result = await UserManager.CreateAsync(dbUser);
            }

            if (result.Succeeded)
            {
                using (var repository = _platformRepository())
                {
                    var account = new AccountEntity()
                    {
                        Id = user.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 Ok();
            }
            else
            {
                return BadRequest(String.Join(" ", result.Errors));
            }
        }
 public static ApplicationUser ToIdentityModel(this ApplicationUserExtended user)
 {
     var retVal = new ApplicationUser();
     user.Patch(retVal);
     return retVal;
 }
        public async Task<IHttpActionResult> Register(ApplicationUserExtended user)
        {
            if (user == null)
            {
                return BadRequest();
            }

            var dbUser = new ApplicationUser();
            dbUser.InjectFrom(user);

            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 Microsoft.AspNet.Identity.EntityFramework.IdentityUserLogin
                    {
                        LoginProvider = login.LoginProvider,
                        ProviderKey = login.ProviderKey,
                        UserId = dbUser.Id
                    });
                }
            }

            IdentityResult result = null;

            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 = user.Id,
                        UserName = user.UserName,
                        MemberId = user.MemberId,
                        AccountState = AccountState.Approved,
                        RegisterType = (RegisterType)user.UserType,
                        StoreId = user.StoreId,
                    };

                    repository.Add(account);
                    repository.UnitOfWork.Commit();
                }
            }

            return Ok(result);
        }