// PUT api/AdminUser/5 (Update in CRUD) public HttpResponseMessage PutAdminUser(int id, AdminUser user) { if (!ModelState.IsValid) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } // email must be unique if (db.Admins.Where(adminUser => adminUser.Email.ToLower() == user.Email.ToLower() && adminUser.ID != id).Count() > 0) { ModelState.AddModelError("EmailNotUnique", "The email address provided is not unique."); return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } if (id != user.ID) { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } db.Entry(user).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex)); } return(Request.CreateResponse(HttpStatusCode.OK)); }
public HttpResponseMessage Put(int id, Event mitoEvent) { if (!ModelState.IsValid) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } if (id != mitoEvent.ID) { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } db.Entry(mitoEvent).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex)); } return(Request.CreateResponse(HttpStatusCode.OK)); }
public ActionResult Create([Bind(Include = "Id,Data,Nome,Tag,Texto")] Arquivo arquivo) { if (ModelState.IsValid) { db.Arquivos.Add(arquivo); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(arquivo)); }
// PUT api/BlogPost/5 (Update in CRUD) public HttpResponseMessage PutBlogPost(int id, BlogPost post) { if (!ModelState.IsValid) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } if (id != post.ID) { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } // figure out if the post was previously published // we have to use a new context to mess with the states bool bWasThePostPreviouslyPublished = new SiteDB().BlogPosts.Find(id).Published.GetValueOrDefault(false); // set the last updated date. post.UpdatedDate = DateTime.Now; // check to make sure the SEO link is unique if (db.BlogPosts.FirstOrDefault(currPost => currPost.SEOLink == post.SEOLink && currPost.ID != id) != null) { ModelState.AddModelError("SEOLink", "The SEO link must be unique and cannot match an existing blog post"); return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } db.Entry(post).State = EntityState.Modified; try { db.SaveChanges(); // if the post was not previously published, but now is, we need to send emails to those // that were signed up to receive emails. if (!bWasThePostPreviouslyPublished && post.Published.GetValueOrDefault(false)) { foreach (var notificationItem in db.NotifiedList) { SendPostedEmail(notificationItem.Email, post); } } } catch (DbUpdateConcurrencyException ex) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex)); } return(Request.CreateResponse(HttpStatusCode.OK)); }
public ActionResult Unsubscribe() { var id = RouteData.Values["id"]; if (id != null) { // we encrypted the email in the url so we need to grab it then decrypt it string sEncryptedEmail = id.ToString(); string sDecrypted = SimpleCrypto.Decrypt(sEncryptedEmail) ?? ""; SiteDB db = new SiteDB(); var itemToDelete = db.NotifiedList.FirstOrDefault(item => !string.IsNullOrEmpty(item.Email) && item.Email.ToLower() == sDecrypted.ToLower()); // we try to delete the item if its there. if (itemToDelete != null) { db.NotifiedList.Remove(itemToDelete); db.SaveChanges(); } } // no matter what we return a success. The user doesn't care if the unsubscribe doesn't work. return(View()); }
/// <summary> /// If userIsOnline is true it updates the LastLoginDate. /// </summary> /// <param name="db"></param> /// <param name="user"></param> /// <param name="userIsOnline"></param> /// <remarks>EDIT ON REUSE: User</remarks> private void UpdateLastLogin(SiteDB db, User user, bool userIsOnline) { if (userIsOnline && user != null) { user.LastLogin = DateTime.Now; db.SaveChanges(); } }
/// <summary> /// The login form for the admin site. Will auto redirect if logged in. /// </summary> /// <returns></returns> public ActionResult Index(string email, string password) { if (db.Admins.Count() == 0) { db.Admins.Add(new AdminUser() { Email = "*****@*****.**", FirstName = "Ali", LastName = "Khatami", Password = "******" }); db.SaveChanges(); } bool bAttemptedLogin = !string.IsNullOrEmpty(email) || !string.IsNullOrEmpty(password); // create the user instace AdminUser userFromCredentials = null; // try to find the user from credentials if (!string.IsNullOrEmpty(email) && !string.IsNullOrEmpty(password)) { userFromCredentials = db.Admins.FirstOrDefault(user => user.Email.ToLower() == email.ToLower() && user.Password == password); } // check if user is logged in as an admin already if (UserUtils.CurrentUser != null || userFromCredentials != null) { // create a session cookie for the user then redirect them UserUtils.CreateEncryptedUserCookie((userFromCredentials != null) ? userFromCredentials.ID : UserUtils.CurrentUser.ID); // redirect to the events page which is the first link in the navigation Response.Redirect("~/Admin/Events"); } // if we didn't redirect it means someone unsuccessfully tried to login if (bAttemptedLogin) { ViewBag.FailedLogin = true; } return(View()); }
public override string ResetPassword(string username, string answer) { //reset the users password to a temporary one. using (SiteDB db = new SiteDB()) { User user = UserRepository.GetUser(db, username); if (user != null) { //create a new password, hash it and save it. string sPassword = Membership.GeneratePassword(8, 1); user.PasswordHash = UserRepository.CreatePasswordHash(sPassword); db.SaveChanges(); return(sPassword); } else { throw new ProviderException("The user could not be found."); } } }
// POST api/NotifiedListItem (Create in CRUD) public HttpResponseMessage PostNotifiedListItem(NotifiedListItem item) { if (ModelState.IsValid) { // check to see if email is already added. We only need to add it once, but we // want to return a success because the request didn't fail if (db.NotifiedList.FirstOrDefault(currItem => currItem.Email == item.Email) != null) { return(Request.CreateResponse(HttpStatusCode.OK, ModelState)); } db.NotifiedList.Add(item); db.SaveChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, item); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = item.ID })); return(response); } else { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } }