示例#1
0
        public ActionResult MySettings(MySettingsModel model)
        {
            var user = PermissionContext.LoggedUser;

            if (user == null || user.Id != model.Id)
            {
                return(new HttpStatusCodeResult(403));
            }

            if (!ModelState.IsValid)
            {
                return(View(new MySettingsModel(GetUserForMySettings())));
            }

            try {
                var newUser = Data.UpdateUserSettings(model.ToContract());
                LoginManager.SetLoggedUser(newUser);
                PermissionContext.LanguagePreferenceSetting.Value = model.DefaultLanguageSelection;
            } catch (InvalidPasswordException x) {
                ModelState.AddModelError("OldPass", x.Message);
                return(View(model));
            } catch (UserEmailAlreadyExistsException) {
                ModelState.AddModelError("Email", ViewRes.User.MySettingsStrings.EmailTaken);
                return(View(model));
            } catch (InvalidEmailFormatException) {
                ModelState.AddModelError("Email", ViewRes.User.MySettingsStrings.InvalidEmail);
                return(View(model));
            }

            TempData.SetSuccessMessage(ViewRes.User.MySettingsStrings.SettingsUpdated);

            return(RedirectToAction("Profile", new { id = user.Name }));
        }
示例#2
0
        public ActionResult MySettings(MySettingsModel model)
        {
            var user = PermissionContext.LoggedUser;

            if (user == null || user.Id != model.Id)
            {
                return(new HttpStatusCodeResult(403));
            }

            if (!ModelState.IsValid)
            {
                return(View(new MySettingsModel(GetUserForMySettings())));
            }

            UpdateUserSettingsContract contract;

            try {
                contract = model.ToContract();
            } catch (InvalidFormException x) {
                AddFormSubmissionError(x.Message);
                return(View(model));
            }

            UserWithPermissionsContract newUser;

            try {
                newUser = Data.UpdateUserSettings(contract);
                LoginManager.SetLoggedUser(newUser);
                PermissionContext.LanguagePreferenceSetting.Value = model.DefaultLanguageSelection;
            } catch (InvalidPasswordException x) {
                ModelState.AddModelError("OldPass", x.Message);
                return(View(model));
            } catch (UserEmailAlreadyExistsException) {
                ModelState.AddModelError("Email", ViewRes.User.MySettingsStrings.EmailTaken);
                return(View(model));
            } catch (InvalidEmailFormatException) {
                ModelState.AddModelError("Email", ViewRes.User.MySettingsStrings.InvalidEmail);
                return(View(model));
            } catch (InvalidUserNameException) {
                ModelState.AddModelError("Username", "Username is invalid. Username may contain alphanumeric characters and underscores.");
                return(View(model));
            } catch (UserNameAlreadyExistsException) {
                ModelState.AddModelError("Username", "Username is already in use.");
                return(View(model));
            } catch (UserNameTooSoonException) {
                ModelState.AddModelError("Username", "Username may only be changed once per year. If necessary, contact a staff member.");
                return(View(model));
            }

            // Updating username currently requires signing in again
            if (newUser.Name != user.Name)
            {
                FormsAuthentication.SignOut();
            }

            TempData.SetSuccessMessage(ViewRes.User.MySettingsStrings.SettingsUpdated);

            return(RedirectToAction("Profile", new { id = newUser.Name }));
        }
        public GeolocationModel GetGeolocation(MySettingsModel settings)
        {
            Task <GeolocationModel> location = ApiClientFactory.Instance.CallGeolocationApi(settings.GeolocationUrl);

            location.Wait();

            return(location.Result);
        }
示例#4
0
        public Forecast GetWeeklyForecastForZipCode(MySettingsModel settings, string zipCode)
        {
            settings.WeatherZip = zipCode;
            Task <Forecast> forecast = ApiClientFactory.Instance.CallWeatherApi(settings.WeatherUrl);

            forecast.Wait();

            return(forecast.Result);
        }
示例#5
0
 public HomeController(MySettingsModel app, ILocationService location,
                       IUserService user, IWeatherForecastService forecast)
 {
     appSettings = app;
     ApplicationSettings.WebApiUrl = appSettings.GeolocationUrl;
     _locationService = location;
     _userService     = user;
     _forecastService = forecast;
 }
示例#6
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure <CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded    = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });


            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            var url = new MySettingsModel();

            Configuration.GetSection("MySettings").Bind(url);
            services.AddScoped <IHttpClient>(s => new HttpClientWrapper(new HttpClient()));
            services.AddScoped <ITFLAPIClient>(s => new TflApiClient(s.GetService <IHttpClient>(), new Uri(url.WebApiBaseUrl)));
        }
示例#7
0
        public ActionResult MySettings(MySettingsModel model, [FromJson] IList <WebLinkDisplay> webLinksJson)
        {
            var user = LoginManager.LoggedUser;

            if (user.Id != model.Id)
            {
                return(new HttpStatusCodeResult(403));
            }

            model.WebLinks = webLinksJson;

            if (!ModelState.IsValid)
            {
                return(View(new MySettingsModel(GetUserForMySettings())));
            }

            if (!string.IsNullOrEmpty(model.Email))
            {
                try {
                    new MailAddress(model.Email);
                } catch (FormatException) {
                    ModelState.AddModelError("Email", ViewRes.User.MySettingsStrings.InvalidEmail);
                    return(View(model));
                }
            }

            try {
                var newUser = Service.UpdateUserSettings(model.ToContract());
                LoginManager.SetLoggedUser(newUser);
            } catch (InvalidPasswordException x) {
                ModelState.AddModelError("OldPass", x.Message);
                return(View(model));
            }

            TempData.SetSuccessMessage(ViewRes.User.MySettingsStrings.SettingsUpdated);

            return(RedirectToAction("Profile", new { id = user.Name }));
        }
 public UserService(IOptions <MySettingsModel> appSettings)
 {
     _appSettings = appSettings.Value;
 }