public ActionResult ChangePassword(AccountModel model)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageAccounts))
                return AccessDeniedView();

            var account = _accountService.GetAccountById(model.Id);
            if (account == null)
                //No account found with the specified id
                return RedirectToAction("List");

            if (ModelState.IsValid)
            {
                var changePassRequest = new ChangePasswordRequest(model.Email,
                    false, _accountSettings.DefaultPasswordFormat, model.Password);
                var changePassResult = _accountRegistrationService.ChangePassword(changePassRequest);
                if (changePassResult.Success)
                    SuccessNotification(_localizationService.GetResource("Admin.Accounts.Accounts.PasswordChanged"));
                else
                    foreach (var error in changePassResult.Errors)
                        ErrorNotification(error);
            }

            return RedirectToAction("Edit", account.Id);
        }
        public virtual ActionResult Create()
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageAccounts))
                return AccessDeniedView();

            var model = new AccountModel();
            model.AllowManagingAccountRoles = _permissionService.Authorize(StandardPermissionProvider.ManageAccountRoles);
            model.AvailableAccountRoles = _accountService
                .GetAllAccountRoles(true)
                .Select(entity => new AccountRoleModel()
                {
                    Id = entity.Id,
                    Active = entity.Active,
                    IsSystemRole = entity.IsSystemRole,
                    Name = entity.Name,
                    SystemName = entity.SystemName
                })
                .ToList();
            model.Active = true;

            return View(model);
        }
        public virtual ActionResult Edit(int id)
        {
            var account = _accountService.GetAccountById(id);
            if (account == null || account.Deleted)
                return RedirectToAction("List");

            if (!_permissionService.Authorize(StandardPermissionProvider.ManageAccounts) &&
                (!_permissionService.Authorize(StandardPermissionProvider.EditAccounts) ||
                !_authenticationService.GetAuthenticatedAccount().Equals(account)))
                return AccessDeniedView();

            var model = new AccountModel();
            model.Id = account.Id;
            model.Email = account.Email;
            model.Active = account.Active;
            model.AllowManagingAccountRoles = _permissionService.Authorize(StandardPermissionProvider.ManageAccountRoles);
            model.AvailableAccountRoles = _accountService
                .GetAllAccountRoles(true)
                .Select(entity => new AccountRoleModel()
                {
                    Id = entity.Id,
                    Active = entity.Active,
                    IsSystemRole = entity.IsSystemRole,
                    Name = entity.Name,
                    SystemName = entity.SystemName
                })
                .ToList();
            model.SelectedAccountRoleIds = account.AccountRoles.Select(ar => ar.Id).ToArray();

            return View(model);
        }
        public virtual ActionResult Edit(AccountModel model)
        {
            var account = _accountService.GetAccountById(model.Id);
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageAccounts) &&
                (!_permissionService.Authorize(StandardPermissionProvider.EditAccounts) ||
                !_authenticationService.GetAuthenticatedAccount().Equals(account)))
                return AccessDeniedView();

            var allAccountRoles = _accountService.GetAllAccountRoles(true);
            var newAccountRoles = new List<AccountRole>();
            foreach (var accountRole in allAccountRoles)
            {
                if (model.SelectedAccountRoleIds != null && model.SelectedAccountRoleIds.Contains(accountRole.Id))
                    newAccountRoles.Add(accountRole);
            }
            var allowManagingAccountRoles = _permissionService.Authorize(StandardPermissionProvider.ManageAccountRoles);
            if (ModelState.IsValid)
            {

                    account.Active = model.Active;
                    account.LastActivityDateUtc = DateTime.UtcNow;

                    if (allowManagingAccountRoles)
                    {
                        foreach (var accountRole in allAccountRoles)
                        {
                            if (model.SelectedAccountRoleIds != null && model.SelectedAccountRoleIds.Contains(accountRole.Id))
                            {
                                //new role
                                if (account.AccountRoles.Where(cr => cr.Id == accountRole.Id).Count() == 0)
                                    account.AccountRoles.Add(accountRole);
                            }
                            else
                            {
                                //removed role
                                if (account.AccountRoles.Where(cr => cr.Id == accountRole.Id).Count() > 0)
                                    account.AccountRoles.Remove(accountRole);
                            }
                        }
                        _accountService.UpdateAccount(account);
                    }
                return RedirectToAction("List");
            }
            model.AvailableAccountRoles = _accountService
               .GetAllAccountRoles(true)
               .Select(entity => new AccountRoleModel()
               {
                   Id = entity.Id,
                   Active = entity.Active,
                   IsSystemRole = entity.IsSystemRole,
                   Name = entity.Name,
                   SystemName = entity.SystemName
               })
               .ToList();
            return View(model);
        }
        public virtual ActionResult Create(AccountModel model)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageAccounts))
                return AccessDeniedView();

            if (!String.IsNullOrWhiteSpace(model.Email))
            {
                var acc = _accountService.GetAccountByEmail(model.Email);
                if (acc != null)
                    ModelState.AddModelError("", "Email đã được đăng ký!");
            }
            else
                ModelState.AddModelError("", "Email không được bỏ trống!");

            if (!CommonHelper.IsValidEmail(model.Email))
            {
                ModelState.AddModelError("", "Email không hợp lệ!");
            }
            if(String.IsNullOrWhiteSpace(model.Password))
                ModelState.AddModelError("", "Mật khẩu không được bỏ trống!");

            var allAccountRoles = _accountService.GetAllAccountRoles(true);
            var newAccountRoles = new List<AccountRole>();
            foreach (var accountRole in allAccountRoles)
                if (model.SelectedAccountRoleIds != null && model.SelectedAccountRoleIds.Contains(accountRole.Id))
                    newAccountRoles.Add(accountRole);
            bool allowManagingAccountRole = _permissionService.Authorize(StandardPermissionProvider.ManageAccountRoles);

            if (ModelState.IsValid)
            {
                var account = new Account()
                {
                    AccountGuid = Guid.NewGuid(),
                    Email = model.Email,
                    Username = model.Email,
                    Active = model.Active,
                    CreatedOnUtc = DateTime.UtcNow,
                    LastActivityDateUtc = DateTime.UtcNow,
                };

                _accountService.InsertAccount(account);

                if (!String.IsNullOrWhiteSpace(model.Password))
                {
                    var changePasswordRequest = new ChangePasswordRequest(model.Email, false, PasswordFormat.Hashed, model.Password);
                    var changPasswordResult = _accountRegistrationService.ChangePassword(changePasswordRequest);

                }

                if (allowManagingAccountRole)
                {
                    foreach (var accountRole in newAccountRoles)
                        account.AccountRoles.Add(accountRole);
                    _accountService.UpdateAccount(account);
                }
                return RedirectToAction("List");
            }

            model.AvailableAccountRoles = _accountService
               .GetAllAccountRoles(true)
               .Select(entity => new AccountRoleModel()
               {
                   Id = entity.Id,
                   Active = entity.Active,
                   IsSystemRole = entity.IsSystemRole,
                   Name = entity.Name,
                   SystemName = entity.SystemName
               })
               .ToList();
            return View(model);
        }