Ejemplo n.º 1
0
        internal string ProcessGet(SettingsClassHelper helper, HttpCookieCollection responseCookies)
        {
            var assembly = Assembly.GetExecutingAssembly();

            var fileName = "{0}.Page.html".FormatWith(GetType().Namespace);

            string fileContent;
            using (var stream = assembly.GetManifestResourceStream(fileName))
            {
                if (stream == null)
                    throw new Exception("The UI can't be found.");

                using (var reader = new StreamReader(stream))
                    fileContent = reader.ReadToEnd();
            }

            var model = new ViewModel
                {
                    Settings = InflateSettingsViewModel(helper),
                    Token = CreateCsrfToken(responseCookies)
                };

            foreach (var item in Configuration.SettingsProvider.GetAllValues())
            {
                model.Settings.First(x => x.Name == item.Key).Value = item.Value;
            }

            var outputJson = new JavaScriptSerializer().Serialize(model);

            fileContent = fileContent.Replace("{data}", outputJson);

            return fileContent;
        }
Ejemplo n.º 2
0
        public void TestGetRequest()
        {
            var helper = new SettingsClassHelper(new TestSettings());
            var cookies = new HttpCookieCollection();
            var response = new EasySettingsHandler().ProcessGet(helper, cookies);

            Assert.IsTrue(cookies.Count == 1);
            Assert.IsTrue(response.Contains(cookies[0].Value));
        }
Ejemplo n.º 3
0
        public void TestPostRequest()
        {
            var helper = new SettingsClassHelper(new TestSettings());
            var cookies = new HttpCookieCollection();
            cookies.Add(new HttpCookie("easysettings-token", "6d5f6e66bf0e4a638490abfb073d0e68"));

            byte[] byteArray = Encoding.UTF8.GetBytes(@"{""settings"":[{""type"":0,""name"":""IntegerValue"",""value"":""0"",""desc"":""This is an integer value with a really long description that should wrap eventually"",""possibleValues"":null},{""type"":1,""name"":""MiniProfiler"",""value"":true,""desc"":""Allow MiniProfiler to run"",""possibleValues"":null},{""type"":1,""name"":""MyTest"",""value"":false,""desc"":"""",""possibleValues"":null},{""type"":2,""name"":""MyTestEnum"",""value"":""ThisIs0"",""desc"":"""",""possibleValues"":[""ThisIs0"",""ThisIs1""]}],""token"":""6d5f6e66bf0e4a638490abfb073d0e68""}");
            var stream = new MemoryStream(byteArray);
            var response = new EasySettingsHandler().ProcessPost(stream, helper, cookies);
        }
Ejemplo n.º 4
0
 internal IEnumerable<SettingViewModel> InflateSettingsViewModel(SettingsClassHelper helper)
 {
     return
         helper.GetProperties().Select(
                     x =>
                         new SettingViewModel
                             {
                                 Name = x.Name,
                                 Value = (x.GetValue(helper.TheClass) ?? "").ToString(),
                                 Description = x.GetDescription(),
                                 Type = DetermineSettingType(x),
                                 PossibleValues = DetermineSettingType(x) == SettingType.Enum ? Enum.GetNames(x.PropertyType) : null
                             })
                 .ToList();
 }
Ejemplo n.º 5
0
        public void ProcessRequest(HttpContext context)
        {
            var outputStream = context.Response.Output;

            var settingsClass = new SettingsClassHelper(context);

            switch (context.Request.HttpMethod)
            {
                case "POST":
                    outputStream.Write(ProcessPost(context.Request.InputStream, settingsClass, context.Request.Cookies));
                    break;
                default:
                    outputStream.Write(ProcessGet(settingsClass, context.Response.Cookies));
                    break;
            }
        }
Ejemplo n.º 6
0
        internal string ProcessPost(Stream inputStream, SettingsClassHelper helper, HttpCookieCollection requestCookies)
        {
            string jsonString;

            var jsonSerializer = new JavaScriptSerializer();
            inputStream.Position = 0;
            using (var stream = new StreamReader(inputStream))
            {
                jsonString = stream.ReadToEnd();
            }

            var newJson = jsonSerializer.Deserialize<ViewModel>(jsonString);

            if (!ValidateCsrfToken(requestCookies, newJson.Token))
                throw new HttpRequestValidationException("Token was missing, or invalid");

            foreach (var item in newJson.Settings)
            {
                if (helper.IsValidValue(item.Name, item.Value))
                {
                    Configuration.SettingsProvider.SaveSetting(item.Name, item.Value);
                }

                //TODO: create return json array to tell which properties didn't save with invalid values
            }

            return "";
        }