Beispiel #1
0
        public ActionResult Profile(FormCollection _vars)
        {
            var user = Membership.GetUser(Guid.Parse(_vars["UserGuid"]));
            var prfMgr = new UserProfileManager(user);

            UserProfileViewModel viewModel = new UserProfileViewModel() {
                AccountProperties = new UserProfileManager(user).UserProfile.Properties,
                UserId = (Guid)user.ProviderUserKey,
                Email = user.Email,
            };

            user.Email = _vars["Email"];

            string currPwd = _vars["CurrentPassword"];
            string newPwd = _vars["NewPassword"];
            string newPwdConfirmation = _vars["NewPasswordConfirmation"];

            if (!string.IsNullOrWhiteSpace(currPwd)) {
                // attempt to change the user's password
                if (string.IsNullOrWhiteSpace(newPwd)) {
                    ModelState.AddModelError("NewPassword", "Você deve digitar a nova senha.");
                    return View(viewModel);
                }
                if (string.IsNullOrWhiteSpace(newPwdConfirmation)) {
                    ModelState.AddModelError("NewPassword", "Você deve digitar a confirmação da nova senha.");
                    return View(viewModel);
                }
                if (newPwdConfirmation != newPwd) {
                    ModelState.AddModelError("NewPassword", "A nova senha e a confirmação devem ser iguais.");
                    ModelState.AddModelError("NewPasswordConfirmation", "A nova senha e a confirmação devem ser iguais.");
                    return View(viewModel);
                }

                if (!user.ChangePassword(currPwd, newPwd)) {
                    ModelState.AddModelError("CurrentPassword", "Não foi possível trocar sua senha. Verifique sua senha atual e tente novamente.");
                    return View(viewModel);
                }
            }

            if (_vars["Administrator"] != null) {
                if (!Roles.IsUserInRole(user.UserName, "administrators")) {
                    Roles.AddUserToRole(user.UserName, "administrators");
                }
            }
            else if (Roles.IsUserInRole(user.UserName, "administrators")) {
                Roles.RemoveUserFromRole(user.UserName, "administrators");
            }

            _vars.Remove("UserGuid");
            _vars.Remove("Email");
            _vars.Remove("CurrentPassword");
            _vars.Remove("NewPassword");
            _vars.Remove("NewPasswordConfirmation");
            _vars.Remove("Administrator");

            foreach (string key in _vars.Keys) {
                prfMgr.SetUserProfileProperty(key, _vars[key]);
            }

            try {
                Membership.UpdateUser(user);
            }
            catch (ProviderException e) {
                ModelState.AddModelError("Email", "Ocorreu um erro atualizar seus dados. " + e.Message);
                return View(viewModel);
            }

            TempData["Message"] = "Dados salvos com sucesso.";
            return RedirectToAction("Index", "Home");
        }
Beispiel #2
0
        public ActionResult Edit(string id)
        {
            MembershipUser user = Membership.GetUser(id);
            UserProfileManager pMgr = new UserProfileManager(user);

            var userModules = ModRepository.GetUserModules((Guid)user.ProviderUserKey);
            var allModules = new List<IModule>();//ModRepository.GetAllStaticModules();

            EditUserViewModel viewModel = new EditUserViewModel() {
                UserId = (Guid)user.ProviderUserKey,
                Email = user.Email,
                UserProfile = pMgr.UserProfile,
                UserModules = userModules,
                AllModules = allModules.Where(module => userModules.FirstOrDefault(mod => mod.Id == module.Id) == null)
            };

            // add the list with all available database types
            // TODO: Move this code to the profile-related namespace
            List<object> databaseTypes = new List<object>();

            return View(viewModel);
        }