Beispiel #1
0
        /// <summary>Gets the users.</summary>
        /// <returns>A JSON encoded collection of users</returns>
        public JsonResult Get()
        {
            try
            {
                var dbContext = new PicolEntities();
                var users     = (from l in dbContext.Users
                                 select new
                {
                    Id = l.Id,
                    Logon = l.Logon,
                    FirstName = l.FirstName,
                    LastName = l.LastName,
                    Email = l.Email,
                    Verified = l.Verified,
                    Active = l.Active,
                    Admin = l.Admin,
                    PasswordLastSet = l.Password.LastSet.ToString(),
                    LastLogin = l.LastLogin.ToString()
                }).ToList();

                return(new JsonNetResult {
                    Data = new { Error = false, Users = users.ToList() }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
            catch (Exception e)
            {
                // Signal the error to be logged by elmah
                ErrorSignal.FromCurrentContext().Raise(e);
                return(new JsonNetResult {
                    Data = new { Error = true, ErrorMessage = "Failed to retrieve the users." }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
        }
Beispiel #2
0
        /// <summary>Resets the password.</summary>
        /// <returns>A base view</returns>
        public ActionResult ResetPassword()
        {
            try
            {
                int id = Convert.ToInt32(Encryption.UnprotectString(this.Request.Cookies[Convert.ToString(WebConfigurationManager.AppSettings["ApplicationName"]) + ".User"]["Id"], new string[] { "Cookie" }, true));

                var picolContext = new PicolEntities();
                var user         = (from u in picolContext.Users
                                    where u.Id == id
                                    select u).Single();

                this.ViewData["WsuReset"] = false;
                if (user.Email.ToLower().Contains("@wsu.edu"))
                {
                    this.ViewData["WsuReset"] = true;
                }

                this.ViewBag.SelectedLink = "Account.ResetPassword";
                return(this.View());
            }
            catch (Exception e)
            {
                // Signal the error to be logged by elmah
                ErrorSignal.FromCurrentContext().Raise(e);
                return(this.Redirect("~/Message/Error/LoadFailed"));
            }
        }
Beispiel #3
0
        /// <summary>Clears the history.</summary>
        /// <returns>A view confirming the clearing on the users history.</returns>
        public ActionResult ClearHistory()
        {
            try
            {
                int id = Convert.ToInt32(Encryption.UnprotectString(this.Request.Cookies[Convert.ToString(WebConfigurationManager.AppSettings["ApplicationName"]) + ".User"]["Id"], new string[] { "Cookie" }, true));

                var picolContext   = new PicolEntities();
                var utilityContext = new UtilityEntities();

                var user = (from u in picolContext.Users
                            where u.Id == id
                            select u).Single();

                var histories = from h in utilityContext.ApplicationHistories
                                where h.UserName == user.Email
                                select h;

                utilityContext.ApplicationHistories.RemoveRange(histories);
                utilityContext.SaveChanges();

                this.ViewBag.SelectedLink = "Account.ClearHistory";
                return(this.View(histories));
            }
            catch (Exception e)
            {
                // Signal the error to be logged by elmah
                ErrorSignal.FromCurrentContext().Raise(e);
                return(this.Redirect("~/Message/Error/LoadFailed"));
            }
        }
Beispiel #4
0
        /// <summary>Replaces the file.</summary>
        /// <param name="fileId">The file identifier.</param>
        /// <param name="fileData">The file data.</param>
        /// <returns>A JSON encoded success indicator</returns>
        public JsonResult ReplaceFile(int fileId, HttpPostedFileBase fileData)
        {
            try
            {
                var farmContext = new PicolEntities();
                var file        = (from f in farmContext.UploadedFiles
                                   where f.Id == fileId
                                   select f).Single();

                if (fileData != null && file != null)
                {
                    if (fileData.ContentLength > 0)
                    {
                        byte[] buffer = new byte[fileData.ContentLength];
                        fileData.InputStream.Read(buffer, 0, fileData.ContentLength);

                        file.Data    = buffer;
                        file.Updated = DateTime.Now;
                        farmContext.SaveChanges();
                    }
                }

                return(new JsonNetResult {
                    Data = new { Error = false }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
            catch (Exception e)
            {
                // Signal the error to be logged by elmah
                ErrorSignal.FromCurrentContext().Raise(e);
                return(new JsonNetResult {
                    Data = new { Error = true, ErrorMessage = "Failed to update file." }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
        }
Beispiel #5
0
        /// <summary>Updates the last name.</summary>
        /// <param name="value">The value.</param>
        /// <returns>A JSON encoded success indicator</returns>
        public JsonResult UpdateLastName(string value)
        {
            try
            {
                int id = Convert.ToInt32(Encryption.UnprotectString(this.Request.Cookies[Convert.ToString(WebConfigurationManager.AppSettings["ApplicationName"]) + ".User"]["Id"], new string[] { "Cookie" }, true));

                var dbContext = new PicolEntities();
                var user      = (from u in dbContext.Users
                                 where u.Id == id
                                 select u).SingleOrDefault();

                user.LastName = value;
                dbContext.SaveChanges();

                return(new JsonNetResult {
                    Data = new { Error = false }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
            catch (Exception e)
            {
                // Signal the error to be logged by elmah
                ErrorSignal.FromCurrentContext().Raise(e);
                return(new JsonNetResult {
                    Data = new { Error = true, ErrorMessage = "Failed to save name." }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
        }
Beispiel #6
0
        /// <summary>Gets the labels.</summary>
        /// <returns>A JSON encoded collection of labels</returns>
        public JsonResult Get()
        {
            try
            {
                var farmContext = new PicolEntities();
                var labels      = from l in farmContext.Labels
                                  select new
                {
                    Id             = l.Id,
                    Name           = l.Name,
                    Formulation    = l.Formulation.Name,
                    FormulationId  = l.FormulationId,
                    IntendedUser   = l.IntendedUser.Name,
                    IntendedUserId = l.IntendedUserId,
                    Registrant     = l.Registrant.Name,
                    RegistrantId   = l.RegistrantId,
                    SignalWord     = l.SignalWord.Name,
                    SignalWordId   = l.SignalWordId,
                    Usage          = l.Usage.Name,
                    UsageId        = l.Usage.Name
                };

                return(new JsonNetResult {
                    Data = new { Error = false, Labels = labels.ToList() }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
            catch (Exception e)
            {
                // Signal the error to be logged by elmah
                ErrorSignal.FromCurrentContext().Raise(e);
                return(new JsonNetResult {
                    Data = new { Error = true, ErrorMessage = "Failed to retrieve the labels." }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
        }
        /// <summary>Gets the registrants.</summary>
        /// <returns>A JSON encoded collection of registrants</returns>
        public JsonResult GetRegistrants()
        {
            try
            {
                var dbContext   = new PicolEntities();
                var registrants = (from r in dbContext.Registrants
                                   select new
                {
                    Id = r.Id,
                    Name = r.Name,
                    Website = r.Url
                }).OrderBy(x => x.Name).ToList();

                return(new JsonNetResult {
                    Data = new { Error = false, Registrants = registrants }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
            catch (Exception e)
            {
                // Signal the error to be logged by elmah
                ErrorSignal.FromCurrentContext().Raise(e);
                return(new JsonNetResult {
                    Data = new { Error = true, ErrorMessage = "Failed to retrieve results." }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
        }
        public JsonResult UpdateSetting(int id, string value)
        {
            try
            {
                var accountContext = new PicolEntities();
                var setting        = (from l in accountContext.Settings
                                      where l.Id == id
                                      select l).Single();

                setting.Value = value;
                accountContext.SaveChanges();

                return(new JsonNetResult {
                    Data = new { Error = false }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
            catch (Exception e)
            {
                // Signal the error to be logged by elmah
                ErrorSignal.FromCurrentContext().Raise(e);
                return(new JsonNetResult {
                    Data = new { Error = true, ErrorMessage = "Failed to retrieve the users." }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
        }
        /// <summary>Deletes the image.</summary>
        /// <param name="id">The identifier.</param>
        /// <returns>A JSON encoded success indicator</returns>
        public JsonResult DeleteFile(int id)
        {
            try
            {
                var dataContext = new PicolEntities();
                var file        = (from i in dataContext.BinarySettings
                                   where i.Id == id
                                   select i).Single();

                dataContext.BinarySettings.Remove(file);
                dataContext.SaveChanges();

                return(new JsonNetResult {
                    Data = new { Error = false }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
            catch (Exception e)
            {
                // Signal the error to be logged by elmah
                ErrorSignal.FromCurrentContext().Raise(e);
                return(new JsonNetResult {
                    Data = new { Error = true, ErrorMessage = "Failed to retrieve the file." }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
        }
Beispiel #10
0
        /// <summary>Updates the API key notes.</summary>
        /// <param name="id">The identifier.</param>
        /// <param name="notes">The notes.</param>
        /// <returns>A JSON encoded success indicator</returns>
        public JsonResult UpdateApiKeyNotes(int id, string notes)
        {
            try
            {
                var dataContext = new PicolEntities();
                var key         = (from l in dataContext.ApiKeys
                                   where l.Id == id
                                   select l).Single();

                key.Notes = notes;
                dataContext.SaveChanges();

                return(new JsonNetResult {
                    Data = new { Error = false }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
            catch (Exception e)
            {
                // Signal the error to be logged by elmah
                ErrorSignal.FromCurrentContext().Raise(e);
                return(new JsonNetResult {
                    Data = new { Error = true, ErrorMessage = "Failed to toggle approved" }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
        }
Beispiel #11
0
        /// <summary>Gets the settings.</summary>
        /// <returns>A JSON encoded collection of settings</returns>
        public JsonResult GetSettings()
        {
            try
            {
                var accountContext = new PicolEntities();
                var settings       = from l in accountContext.Settings
                                     select new
                {
                    Id    = l.Id,
                    Name  = l.Name,
                    Value = l.Value
                };

                return(new JsonNetResult {
                    Data = new { Error = false, Settings = settings.ToList() }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
            catch (Exception e)
            {
                // Signal the error to be logged by elmah
                ErrorSignal.FromCurrentContext().Raise(e);
                return(new JsonNetResult {
                    Data = new { Error = true, ErrorMessage = "Failed to retrieve the users." }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
        }
Beispiel #12
0
        /// <summary>Gets the API keys.</summary>
        /// <returns>A JSON encoded collection of API keys</returns>
        public JsonResult GetApiKeys()
        {
            try
            {
                var accountContext = new PicolEntities();
                var apiKeys        = from l in accountContext.ApiKeys
                                     select new
                {
                    Id       = l.Id,
                    Value    = l.Value,
                    User     = l.User.Email,
                    Active   = l.Active,
                    Approved = l.Approved
                };

                return(new JsonNetResult {
                    Data = new { Error = false, ApiKeys = apiKeys.ToList() }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
            catch (Exception e)
            {
                // Signal the error to be logged by elmah
                ErrorSignal.FromCurrentContext().Raise(e);
                return(new JsonNetResult {
                    Data = new { Error = true, ErrorMessage = "Failed to retrieve the users." }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
        }
Beispiel #13
0
        /// <summary>Verifies the specified identifier.</summary>
        /// <param name="id">The identifier.</param>
        /// <param name="code">The code.</param>
        /// <returns>A base view</returns>
        public ActionResult Verify(int id, string code)
        {
            try
            {
                this.ViewBag.SelectedLink = "NewAccount.Create";
                var picolContext = new PicolEntities();
                var user         = (from u in picolContext.Users
                                    where u.Id == id && !u.Verified
                                    select u).First();

                if (Encryption.UnprotectString(code, new string[] { "Verify" }, true) == user.Email)
                {
                    this.ViewBag.Verified = true;
                    this.ViewBag.Message  = "Your account has been successfully verified.";
                    user.Verified         = true;
                    user.LastLogin        = DateTime.Now;
                    picolContext.SaveChanges();
                }
                else
                {
                    this.ViewBag.Verified = false;
                    this.ViewBag.Message  = "We could not properly verify your account.";
                }

                return(this.View());
            }
            catch (Exception e)
            {
                // Signal the error to be logged by elmah
                ErrorSignal.FromCurrentContext().Raise(e);
                return(this.Redirect("~/Message/Error/LoadFailed"));
            }
        }
Beispiel #14
0
        /// <summary>Sends the verification.</summary>
        /// <param name="id">The identifier of the user</param>
        /// <returns>A base view</returns>
        public ActionResult SendVerification(int id)
        {
            try
            {
                this.ViewBag.SelectedLink = "NewAccount.Create";
                var picolContext = new PicolEntities();
                var user         = (from u in picolContext.Users
                                    where u.Id == id && !u.Verified
                                    select u).FirstOrDefault();

                if (user == null)
                {
                    return(this.RedirectToAction("Create", "Session", new { Id = user.Id }));
                }

                string url = Convert.ToString(WebConfigurationManager.AppSettings["VerificationUrl"]);
                url += "?id=" + id;
                url += "&code=" + HttpUtility.UrlEncode(Encryption.ProtectString(user.Email, new string[] { "Verify" }, true));

                string body = "<html><head></head><body><h2>Verify your new PICOL account</h2><p>We have received a request for a new PICOL account using this email address as the login.  If you did not request this account you can safely discard this email.  If you did request this account, please verify your email address by opening the following confirmation URL: <a href='url_token'>url_token</a>.  This verification email will expire at midnight on " + DateTime.Now.ToShortDateString() + "</p></body></html>";
                body = body.Replace("url_token", url);

                EmailHelper.SendEmail(user.Email, string.Empty, string.Empty, "*****@*****.**", "Verification link for PICOL account", body, true);

                this.ViewBag.Id = id;
                return(this.View());
            }
            catch (Exception e)
            {
                // Signal the error to be logged by elmah
                ErrorSignal.FromCurrentContext().Raise(e);
                return(this.Redirect("~/Message/Error/LoadFailed"));
            }
        }
Beispiel #15
0
        /// <summary>Updates the name of the search.</summary>
        /// <param name="id">The identifier.</param>
        /// <param name="name">The name.</param>
        /// <returns>JSON encoded success indicator</returns>
        public JsonResult UpdateSearchName(int id, string name)
        {
            try
            {
                int userId = Convert.ToInt32(Encryption.UnprotectString(this.Request.Cookies[Convert.ToString(WebConfigurationManager.AppSettings["ApplicationName"]) + ".User"]["Id"], new string[] { "Cookie" }, true));

                var picolContext = new PicolEntities();
                var search       = (from l in picolContext.Searches
                                    where l.Id == id && l.UserId == userId
                                    select l).Single();

                search.Name = name;
                picolContext.SaveChanges();

                return(new JsonNetResult {
                    Data = new { Error = false }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
            catch (Exception e)
            {
                // Signal the error to be logged by elmah
                ErrorSignal.FromCurrentContext().Raise(e);
                return(new JsonNetResult {
                    Data = new { Error = true, ErrorMessage = "Failed to submit the request." }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
        }
Beispiel #16
0
        /// <summary>Loads images for the Froala image manager</summary>
        /// <returns>JSON encoded Froala image manager array</returns>
        public ActionResult LoadImages()
        {
            try
            {
                List <string> extensions = new List <string>();
                extensions.Add(".jpg");
                extensions.Add(".jpeg");
                extensions.Add(".png");

                var dataContext = new PicolEntities();
                var files       = from i in dataContext.BinarySettings
                                  where extensions.Contains(i.Extension) &&
                                  i.Manageable
                                  select new
                {
                    id    = i.Id,
                    url   = "/File/Download/" + i.Id,
                    thumb = "/File/Download/" + i.Id,
                    tag   = i.Name,
                    name  = i.Name
                };

                return(new JsonNetResult {
                    Data = files, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
            catch (Exception e)
            {
                // Signal the error to be logged by elmah
                ErrorSignal.FromCurrentContext().Raise(e);
                return(new JsonNetResult {
                    Data = new { Error = true, ErrorMessage = "Failed to retrieve the file." }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
        }
Beispiel #17
0
        /// <summary>Updates integer typed fields.</summary>
        /// <param name="id">The identifier.</param>
        /// <param name="field">The field.</param>
        /// <param name="value">The value.</param>
        /// <returns>A JSON encoded success indicator</returns>
        public ActionResult UpdateIntegerField(int id, string field, int value)
        {
            try
            {
                var dataContext = new PicolEntities();
                var label       = (from l in dataContext.Labels
                                   where l.Id == id
                                   select l).SingleOrDefault();

                System.Reflection.PropertyInfo propertyInfo = label.GetType().GetProperty(field);

                propertyInfo.SetValue(label, value, null);
                dataContext.SaveChanges();

                return(new JsonNetResult {
                    Data = new { Error = false }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
            catch (Exception e)
            {
                // Signal the error to be logged by elmah
                ErrorSignal.FromCurrentContext().Raise(e);
                return(new JsonNetResult {
                    Data = new { Error = true, ErrorMessage = "Failed to update user in database." }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
        }
Beispiel #18
0
        /// <summary>Edits the home page contents</summary>
        /// <returns>A base view</returns>
        public ActionResult EditHome()
        {
            try
            {
                this.ViewBag.SelectedLink = "Admin.EditHome";

                var dataContext = new PicolEntities();
                var source      = (from l in dataContext.Settings
                                   where l.Name == "HomePage"
                                   select l).SingleOrDefault();

                if (source == null)
                {
                    var setting = new Setting();
                    setting.Value = string.Empty;
                    setting.Name  = "HomePage";
                    dataContext.Settings.Add(setting);
                    dataContext.SaveChanges();
                    source = (from l in dataContext.Settings
                              where l.Name == "HomePage"
                              select l).Single();
                }

                this.ViewData["Source"]    = source.Value;
                this.ViewData["SettingId"] = source.Id;

                return(this.View());
            }
            catch (Exception e)
            {
                // Signal the error to be logged by elmah
                ErrorSignal.FromCurrentContext().Raise(e);
                return(this.Redirect("~/Message/Error/LoadFailed"));
            }
        }
        /// <summary>Gets the resistance.</summary>
        /// <returns>A JSON encoded collection of resistances</returns>
        public JsonResult GetResistanceCodes()
        {
            try
            {
                var dbContext       = new PicolEntities();
                var resistanceCodes = (from r in dbContext.Resistances
                                       select new
                {
                    Id = r.Id,
                    Source = r.Source,
                    Code = r.Code,
                    Moa = r.MethodOfAction
                }).OrderBy(x => x.Source).ToList();

                return(new JsonNetResult {
                    Data = new { Error = false, ResistanceCodes = resistanceCodes }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
            catch (Exception e)
            {
                // Signal the error to be logged by elmah
                ErrorSignal.FromCurrentContext().Raise(e);
                return(new JsonNetResult {
                    Data = new { Error = true, ErrorMessage = "Failed to retrieve results." }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
        }
Beispiel #20
0
        /// <summary>Gets the preference.</summary>
        /// <param name="name">The name.</param>
        /// <returns>A JSON encoded value and success indicator</returns>
        public JsonResult GetPreference(string name)
        {
            try
            {
                HttpCookie authorizationCookie = this.Request.Cookies[Convert.ToString(WebConfigurationManager.AppSettings["ApplicationName"]) + ".Authorization"];
                string     value = string.Empty;

                if (authorizationCookie != null)
                {
                    if (Convert.ToBoolean(Encryption.UnprotectString(authorizationCookie["User"], new string[] { "Cookie" }, true)))
                    {
                        int id        = Convert.ToInt32(Encryption.UnprotectString(this.Request.Cookies[Convert.ToString(WebConfigurationManager.AppSettings["ApplicationName"]) + ".User"]["Id"], new string[] { "Cookie" }, true));
                        var dbContext = new PicolEntities();

                        value = (from s in dbContext.UserPreferences
                                 where s.UserId == id &&
                                 s.Name == name
                                 select s.Value).SingleOrDefault();
                    }
                }

                return(new JsonNetResult {
                    Data = new { Error = false, Value = value }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
            catch (Exception e)
            {
                // Signal the error to be logged by elmah
                ErrorSignal.FromCurrentContext().Raise(e);
                return(new JsonNetResult {
                    Data = new { Error = true, ErrorMessage = "Failed to retrieve results." }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
        }
Beispiel #21
0
        /// <summary>Resets the specified password.</summary>
        /// <param name="id">The identifier.</param>
        /// <param name="code">The code.</param>
        /// <returns>A success indicator</returns>
        public ActionResult Reset(int id, string code)
        {
            try
            {
                this.ViewBag.SelectedLink = "NewSession.Create";
                var picolContext = new PicolEntities();
                var user         = (from u in picolContext.Users
                                    where u.Id == id
                                    select u).First();

                if (Encryption.UnprotectString(code, new string[] { "Reset" }, true) == user.Email)
                {
                    string password = Encryption.GetUniqueKey(16);
                    this.ViewBag.Reset    = true;
                    this.ViewBag.Message  = "Your password was successfully reset to: " + password;
                    user.Password.Hash    = Encryption.HashString(password);
                    user.Password.LastSet = DateTime.Now;
                    picolContext.SaveChanges();
                }
                else
                {
                    this.ViewBag.Reset   = false;
                    this.ViewBag.Message = "We could not properly verify your password reset request.";
                }

                return(this.View());
            }
            catch (Exception e)
            {
                // Signal the error to be logged by elmah
                ErrorSignal.FromCurrentContext().Raise(e);
                return(this.Redirect("~/Message/Error/LoadFailed"));
            }
        }
Beispiel #22
0
        /// <summary>Requests the API key.</summary>
        /// <returns>ToS and user data</returns>
        public ActionResult RequestApiKey()
        {
            try
            {
                this.ViewBag.SelectedLink = "Account.RequestApiKey";
                int id = Convert.ToInt32(Encryption.UnprotectString(this.Request.Cookies[Convert.ToString(WebConfigurationManager.AppSettings["ApplicationName"]) + ".User"]["Id"], new string[] { "Cookie" }, true));

                var picolContext = new PicolEntities();
                var user         = (from u in picolContext.Users
                                    where u.Id == id
                                    select u).Single();

                var tosSetting = (from l in picolContext.Settings
                                  where l.Name == "ApiTos"
                                  select l).Single();

                var useSetting = (from l in picolContext.Settings
                                  where l.Name == "ApiUse"
                                  select l).Single();

                this.ViewData["ApiTos"]  = tosSetting.Value;
                this.ViewData["ApiUse"]  = useSetting.Value;
                this.ViewData["Account"] = user.Email;
                return(this.View());
            }
            catch (Exception e)
            {
                // Signal the error to be logged by elmah
                ErrorSignal.FromCurrentContext().Raise(e);
                return(this.Redirect("~/Message/Error/LoadFailed"));
            }
        }
Beispiel #23
0
        /// <summary>Gets the resistances.</summary>
        /// <returns>A JSON encoded collection of resistances</returns>
        public JsonResult Get()
        {
            try
            {
                var farmContext = new PicolEntities();
                var resistances = from l in farmContext.Resistances
                                  select new
                {
                    Id             = l.Id,
                    Source         = l.Source,
                    Code           = l.Code,
                    MethodOfAction = l.MethodOfAction
                };

                return(new JsonNetResult {
                    Data = new { Error = false, Resistances = resistances.ToList() }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
            catch (Exception e)
            {
                // Signal the error to be logged by elmah
                ErrorSignal.FromCurrentContext().Raise(e);
                return(new JsonNetResult {
                    Data = new { Error = true, ErrorMessage = "Failed to retrieve the resistances." }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
        }
Beispiel #24
0
        /// <summary>Deletes the resistance.</summary>
        /// <param name="id">The identifier.</param>
        /// <returns>A JSON encoded success indicator</returns>
        public JsonResult Delete(int id)
        {
            try
            {
                var farmContext = new PicolEntities();
                var resistance  = (from l in farmContext.Resistances
                                   where l.Id == id
                                   select l).Single();

                farmContext.Resistances.Remove(resistance);
                farmContext.SaveChanges();

                return(new JsonNetResult {
                    Data = new { Error = false }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
            catch (Exception e)
            {
                // Signal the error to be logged by elmah
                ErrorSignal.FromCurrentContext().Raise(e);
                return(new JsonNetResult {
                    Data = new { Error = true, ErrorMessage = "Failed to delete resistance." }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
        }
Beispiel #25
0
        /// <summary>Sends the recovery email.</summary>
        /// <param name="id">The identifier.</param>
        /// <returns>A JSON encoded success indicator</returns>
        public JsonResult SendRecoveryEmail(int id)
        {
            try
            {
                var picolContext = new PicolEntities();
                var user         = (from l in picolContext.Users
                                    where l.Id == id
                                    select l).Single();

                string url = Convert.ToString(WebConfigurationManager.AppSettings["ResetUrl"]);
                url += "?id=" + id;
                url += "&code=" + HttpUtility.UrlEncode(Encryption.ProtectString(user.Email, new string[] { "Reset" }, true));

                string body = "<html><head></head><body><h2>Recover your PICOL account</h2><p>We have received a request to reset the password to your PICOL account.  If you did not request this you can safely discard this email.  If you did request this, please verify your email address by opening the following recovery URL: <a href='url_token'>url_token</a></p></body></html>";
                body = body.Replace("url_token", url);

                EmailHelper.SendEmail(user.Email, string.Empty, string.Empty, "*****@*****.**", "Recovery link for PICOL account", body, true);

                return(new JsonNetResult {
                    Data = new { Error = false }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
            catch (Exception e)
            {
                // Signal the error to be logged by elmah
                ErrorSignal.FromCurrentContext().Raise(e);
                return(new JsonNetResult {
                    Data = new { Error = true, ErrorMessage = "Failed to delete user." }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
        }
Beispiel #26
0
        /// <summary>Gets the formulations.</summary>
        /// <returns>A JSON encoded collection of formulations</returns>
        public JsonResult Get()
        {
            try
            {
                var farmContext  = new PicolEntities();
                var formulations = from l in farmContext.Formulations
                                   select new
                {
                    Id    = l.Id,
                    Name  = l.Name,
                    Code  = l.Code,
                    Notes = l.Notes
                };

                return(new JsonNetResult {
                    Data = new { Error = false, Formulations = formulations.ToList() }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
            catch (Exception e)
            {
                // Signal the error to be logged by elmah
                ErrorSignal.FromCurrentContext().Raise(e);
                return(new JsonNetResult {
                    Data = new { Error = true, ErrorMessage = "Failed to retrieve the formulations." }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
        }
Beispiel #27
0
        /// <summary>Gets the state of the database.</summary>
        /// <returns>A JSON encoded state indicator</returns>
        public JsonResult GetDatabaseState()
        {
            try
            {
                var dataContext = new PicolEntities();
                var loadState   = (from l in dataContext.Settings
                                   where l.Name == "LoadState"
                                   select l.Value).SingleOrDefault();

                // Default to offline, then check for online
                bool online = false;

                if (string.IsNullOrEmpty(loadState))
                {
                    // Counterintuitive, but if no state variable we assume it is online
                    online = true;
                }
                else if (loadState == "Complete")
                {
                    online = true;
                }

                return(new JsonNetResult {
                    Data = new { Error = false, Online = online }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
            catch (Exception e)
            {
                // Signal the error to be logged by elmah
                ErrorSignal.FromCurrentContext().Raise(e);
                return(new JsonNetResult {
                    Data = new { Error = true, ErrorMessage = "Failed to retrieve the database load state." }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
        }
Beispiel #28
0
        /// <summary>Registers the label.</summary>
        /// <param name="id">The identifier.</param>
        /// <param name="state">The state.</param>
        /// <param name="year">The year.</param>
        /// <returns>A JSON encoded success indicator</returns>
        public JsonResult RegisterLabel(int id, int state, int year)
        {
            try
            {
                var picolContext = new PicolEntities();

                StateRecord record = new StateRecord();
                record.LabelId  = id;
                record.StateId  = state;
                record.Year     = year;
                record.AgencyId = string.Empty;
                record.Version  = "-1";
                record.Pdf      = DateTime.Now;
                picolContext.StateRecords.Add(record);
                picolContext.SaveChanges();

                return(new JsonNetResult {
                    Data = new { Error = false }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
            catch (Exception e)
            {
                // Signal the error to be logged by elmah
                ErrorSignal.FromCurrentContext().Raise(e);
                return(new JsonNetResult {
                    Data = new { Error = true, ErrorMessage = "Failed to update file." }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
        }
Beispiel #29
0
        /// <summary>Gets the uploads.</summary>
        /// <returns>A JSON encoded collection of uploaded files</returns>
        public JsonResult GetReviewed()
        {
            try
            {
                var farmContext = new PicolEntities();
                var files       = from f in farmContext.UploadedFiles
                                  where f.Reviewed
                                  select new
                {
                    Id   = f.Id,
                    Name = f.Name
                };

                return(new JsonNetResult {
                    Data = new { Error = false, Files = files }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
            catch (Exception e)
            {
                // Signal the error to be logged by elmah
                ErrorSignal.FromCurrentContext().Raise(e);
                return(new JsonNetResult {
                    Data = new { Error = true, ErrorMessage = "Failed to retrieve the labels." }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
        }
Beispiel #30
0
        /// <summary>Gets the uploads.</summary>
        /// <returns>A JSON encoded collection of uploaded files</returns>
        public JsonResult GetUploaded()
        {
            try
            {
                var farmContext     = new PicolEntities();
                var washingtonFiles = new List <Certificate>();

                string        path  = this.Server.MapPath("../Files/Washington");
                List <string> files = Directory.GetFiles(path).ToList();

                foreach (var f in files)
                {
                    var         nameParts = f.Split('\\').Last().Split('_');
                    Certificate file      = new Certificate();
                    file.Format = nameParts.Count() == 4;

                    if (file.Format)
                    {
                        file.Name       = nameParts[0];
                        file.Code       = nameParts[1].PadLeft(5, '0');
                        file.Date       = nameParts[2];
                        file.ThirdParty = nameParts[3].Contains("Wagn") ? true : false;

                        if (file.Code == "Corres")
                        {
                            continue;
                        }

                        int n;
                        if (!int.TryParse(file.Code, out n))
                        {
                            file.Format = false;
                        }

                        bool matching = (from l in farmContext.Registrants
                                         where l.Code == file.Code
                                         select l).Count() == 1;

                        file.Match    = matching;
                        file.FileName = f.Split('\\').Last();
                        washingtonFiles.Add(file);
                    }
                }

                return(new JsonNetResult {
                    Data = new { Error = false, Certificates = washingtonFiles.ToList() }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
            catch (Exception e)
            {
                // Signal the error to be logged by elmah
                ErrorSignal.FromCurrentContext().Raise(e);
                return(new JsonNetResult {
                    Data = new { Error = true, ErrorMessage = "Failed to retrieve the labels." }, MaxJsonLength = int.MaxValue, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
        }