Exemple #1
0
        public ActionResult Index()
        {
            ViewBag.IsHomePage = true;
            ViewBag.SearchInfo = cacheService.GetOrSetCache(
                "geonetworkRecordsCount",
                () =>
            {
                try
                {
                    using (var client = restApiService.GetClient())
                    {
                        return(restApiService.PostRequest <JObject>(
                                   client,
                                   "search/records/_search",
                                   new StringContent(
                                       ApplicationData.ReadGeonetworkConfigFile("SearchInfo.json"),
                                       Encoding.UTF8,
                                       "application/json")));
                    }
                }
                catch (Exception exc)
                {
                    Logger.Error(exc);
                    return(null);
                }
            });

            using (ContextManager.NewConnection())
            {
                ViewBag.Providers = providerService.Search(
                    new ProviderQueryModel {
                    StatusId = EnumHelper.ProviderStatuses[ProviderStatus.Valid]
                });
                ViewBag.BackgroundImages = adminService.GetHomeImages();
            }

            return(View());
        }
Exemple #2
0
        public ActionResult ChangePassword(ChangePasswordViewModel model)
        {
            ModelState.Clear();

            // Validate captcha control
            if (!captchaService.Validate(Request["g-recaptcha-response"]))
            {
                ModelState.AddModelError(string.Empty, Resource.WrongCaptchaMessage);
            }

            User user = null;

            if (model.Token.IsNotNullOrEmpty())
            {
                user = GetUserByToken(model.Token);
            }
            else if (User?.Id != null)
            {
                using (ContextManager.NewConnection())
                {
                    user = accountService.GetByUserName(User.UserName);
                }

                // Validate old password - if is same as login user password
                if (model.OldPassword.IsNullOrEmpty() ||
                    !PasswordTools.ValidatePassword(model.OldPassword, user.Password))
                {
                    ModelState.AddModelError("OldPassword", Resource.PasswordsDoesNotMatch);
                }
            }

            if (user == null)
            {
                throw new WarningException(Resource.InvalidToken);
            }

            model.IsAdmin = user.IsAdmin;
            model.UserId  = user.Id.Value;

            if (!TryValidateModel(model) || !ModelState.IsValid)
            {
                return(View("ChangePassword", model));
            }

            using (var client = restApiService.GetClient(new UserPrincipal {
                UserName = ConfigurationReader.GeoNetworkAdminUser, Password = ConfigurationReader.GeoNetworkAdminPass
            }))
            {
                restApiService.PostRequest(
                    client,
                    $"users/{user.GeoNetworkId}/actions/forget-password?password={model.Password}&password2={model.ConfirmPassword}");
            }

            model.Password = PasswordTools.CreateHash(model.Password);

            var userPrincipal = Mapper.Map <IUser, UserPrincipal>(user);

            using (var transaction = ContextManager.NewTransaction(new RequestData(userPrincipal)))
            {
                userService.ChangePassword(Mapper.Map <ChangePasswordModel>(model));

                // If user is not active - activate it
                if (model.Token.IsNotNullOrEmpty() && user.Status.Id == EnumHelper.GetStatusIdByEnum(UserStatus.InActive))
                {
                    userService.ChangeStatus(
                        EnumHelper.GetStatusIdByEnum(UserStatus.Active),
                        user.Id.Value,
                        ConfigurationReader.AutomationUserId);
                }

                transaction.Commit();
            }

            return(RedirectToAction("Login"));
        }