示例#1
0
        public async Task <IActionResult> Info(UserInfoModel model)
        {
            if (!_workContext.CurrentUser.IsRegistered())
            {
                return(Challenge());
            }

            var user = _workContext.CurrentUser;

            try
            {
                if (ModelState.IsValid)
                {
                    //username
                    if (_userSettings.UsernamesEnabled && _userSettings.AllowUsersToChangeUsernames)
                    {
                        if (!user.Username.Equals(model.Username.Trim(), StringComparison.InvariantCultureIgnoreCase))
                        {
                            //change username
                            await _userRegistrationService.SetUsernameAsync(user, model.Username.Trim());

                            //re-authenticate
                            await _authenticationService.SignInAsync(user, true);
                        }
                    }

                    //email
                    if (!user.Email.Equals(model.Email.Trim(), StringComparison.InvariantCultureIgnoreCase))
                    {
                        //change email
                        await _userRegistrationService.SetEmailAsync(user, model.Email.Trim());

                        //re-authenticate (if usernames are disabled)
                        if (!_userSettings.UsernamesEnabled)
                        {
                            await _authenticationService.SignInAsync(user, true);
                        }
                    }

                    //properties
                    if (_dateTimeSettings.AllowUsersToSetTimeZone)
                    {
                        await _genericAttributeService.SaveAttributeAsync(user, UserDefaults.TimeZoneIdAttribute, model.TimeZoneId);
                    }

                    //form fields
                    if (_userSettings.GenderEnabled)
                    {
                        await _genericAttributeService.SaveAttributeAsync(user, UserDefaults.GenderAttribute, model.Gender);
                    }

                    await _genericAttributeService.SaveAttributeAsync(user, UserDefaults.FirstNameAttribute, model.FirstName);

                    await _genericAttributeService.SaveAttributeAsync(user, UserDefaults.LastNameAttribute, model.LastName);

                    if (_userSettings.DateOfBirthEnabled)
                    {
                        var dateOfBirth = model.ParseDateOfBirth();
                        await _genericAttributeService.SaveAttributeAsync(user, UserDefaults.DateOfBirthAttribute, dateOfBirth);
                    }

                    if (_userSettings.PhoneEnabled)
                    {
                        await _genericAttributeService.SaveAttributeAsync(user, UserDefaults.PhoneAttribute, model.Phone);
                    }

                    return(RedirectToRoute("UserInfo"));
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
            }

            model = await _userAccountModelFactory.PrepareUserInfoModel(model, user, true);

            return(View(model));
        }
示例#2
0
        public async Task <IActionResult> Edit(UserModel model, bool continueEditing)
        {
            if (!await _permissionService.AuthorizeAsync(StandardPermissionProvider.ManageUsers))
            {
                return(AccessDeniedView());
            }

            var user = await _userService.GetUserByIdAsync(model.Id);

            if (user == null || user.Deleted)
            {
                return(RedirectToAction("Index"));
            }

            //validate user roles
            var allRoles = _userService.GetRoles(true);
            var newRoles = new List <Role>();

            foreach (var role in allRoles)
            {
                if (model.SelectedRoleIds.Contains(role.Id))
                {
                    newRoles.Add(role);
                }
            }

            var rolesError = ValidateRoles(newRoles);

            if (!string.IsNullOrEmpty(rolesError))
            {
                ModelState.AddModelError(string.Empty, rolesError);
                _notificationService.ErrorNotification(rolesError);
            }

            if (newRoles.Any() && newRoles.FirstOrDefault(c => c.SystemName == UserDefaults.RegisteredRoleName) != null &&
                !CommonHelper.IsValidEmail(model.Email))
            {
                ModelState.AddModelError(string.Empty, "Valid Email is required for user to be in 'Registered' role");
                _notificationService.ErrorNotification("Valid Email is required for user to be in 'Registered' role");
            }

            if (ModelState.IsValid)
            {
                try
                {
                    //prevent deactivation of the last active administrator
                    if (!user.IsAdmin() || model.Active || await SecondAdminAccountExists(user))
                    {
                        user.Active = model.Active;
                    }
                    else
                    {
                        _notificationService.ErrorNotification(
                            "You can't deactivate the last administrator. At least one administrator account should exists.");
                    }

                    //email
                    if (!string.IsNullOrWhiteSpace(model.Email))
                    {
                        await _userRegistrationService.SetEmailAsync(user, model.Email);
                    }
                    else
                    {
                        user.Email = model.Email;
                    }

                    //username
                    if (_userSettings.UsernamesEnabled)
                    {
                        if (!string.IsNullOrWhiteSpace(model.Username))
                        {
                            await _userRegistrationService.SetUsernameAsync(user, model.Username);
                        }
                        else
                        {
                            user.Username = model.Username;
                        }
                    }

                    //form fields
                    if (_dateTimeSettings.AllowUsersToSetTimeZone)
                    {
                        await _genericAttributeService.SaveAttributeAsync(user, UserDefaults.TimeZoneIdAttribute, model.TimeZoneId);
                    }
                    if (_userSettings.GenderEnabled)
                    {
                        await _genericAttributeService.SaveAttributeAsync(user, UserDefaults.GenderAttribute, model.Gender);
                    }
                    await _genericAttributeService.SaveAttributeAsync(user, UserDefaults.FirstNameAttribute, model.FirstName);

                    await _genericAttributeService.SaveAttributeAsync(user, UserDefaults.LastNameAttribute, model.LastName);

                    if (_userSettings.DateOfBirthEnabled)
                    {
                        await _genericAttributeService.SaveAttributeAsync(user, UserDefaults.DateOfBirthAttribute, model.DateOfBirth);
                    }
                    if (_userSettings.PhoneEnabled)
                    {
                        await _genericAttributeService.SaveAttributeAsync(user, UserDefaults.PhoneAttribute, model.Phone);
                    }

                    //roles
                    foreach (var role in allRoles)
                    {
                        if (role.SystemName == UserDefaults.AdministratorsRoleName && !_workContext.CurrentUser.IsAdmin())
                        {
                            continue;
                        }

                        if (model.SelectedRoleIds.Contains(role.Id))
                        {
                            if (user.UserRoles.Count(mapping => mapping.RoleId == role.Id) == 0)
                            {
                                user.AddUserRole(new UserRole {
                                    Role = role
                                });
                            }
                        }
                        else
                        {
                            //prevent attempts to delete the administrator role from the user, if the user is the last active administrator
                            if (role.SystemName == UserDefaults.AdministratorsRoleName && !await SecondAdminAccountExists(user))
                            {
                                _notificationService.ErrorNotification("You can't remove the Administrator role. At least one administrator account should exists.");
                                continue;
                            }

                            //remove role
                            if (user.UserRoles.Count(mapping => mapping.RoleId == role.Id) > 0)
                            {
                                user.RemoveUserRole(user.UserRoles.FirstOrDefault(mapping => mapping.RoleId == role.Id));
                            }
                        }
                    }

                    //stores
                    var stores = await _storeService.GetStores();

                    foreach (var store in stores)
                    {
                        if (model.SelectedStoreIds != null && model.SelectedStoreIds.Contains(store.P_BranchNo))
                        {
                            //new store
                            if (user.UserStores.Count(mapping => mapping.StoreId == store.P_BranchNo) == 0)
                            {
                                user.UserStores.Add(new UserStore {
                                    Store = store
                                });
                            }
                        }
                        else
                        {
                            //remove store
                            if (user.UserStores.Count(mapping => mapping.StoreId == store.P_BranchNo) > 0)
                            {
                                user.UserStores.Remove(user.UserStores.FirstOrDefault(mapping =>
                                                                                      mapping.StoreId == store.P_BranchNo));
                            }
                        }
                    }

                    await _userService.UpdateUserAsync(user);

                    //activity log
                    await _userActivityService.InsertActivityAsync("EditUser", $"Edited a user (ID = {user.Id})", user);

                    _notificationService.SuccessNotification("User has been updated successfully.");

                    if (!continueEditing)
                    {
                        return(RedirectToAction("Index"));
                    }

                    //selected tab
                    SaveSelectedTabName();

                    return(RedirectToAction("Edit", new { id = user.Id }));
                }
                catch (Exception e)
                {
                    _notificationService.ErrorNotification(e.Message);
                }
            }

            model = await _userModelFactory.PrepareUserModel(model, user, true);

            return(View(model));
        }