public void TestEditBatchNotes() { UserProfile jon = TestUtils.createUser(111, "Jon", "Smith"); Batch batch = TestUtils.createBatch("Test", BatchType.Beer, jon); BatchNote note = TestUtils.createBatchNote(batch, "Test Note", "I am a note!", jon); BrewersBuddyContext db = new BrewersBuddyContext(); var id = note.NoteId; var note1 = db.BatchNotes.Find(id); Assert.IsTrue(batch.Notes.Contains(note)); Assert.AreEqual("Test Note", note1.Title); Assert.AreEqual("I am a note!", note1.Text); note.Title = "Test Title"; // save changes db = new BrewersBuddyContext(); db.Entry(note).State = System.Data.EntityState.Modified; db.SaveChanges(); //clear context and reload db = new BrewersBuddyContext(); note1 = db.BatchNotes.Find(id); // verify local context is not being used note.Text = "test text"; Assert.AreEqual("Test Title", note1.Title); Assert.AreEqual("I am a note!", note1.Text); }
// // GET: /Account/Delete/ public ActionResult Delete() { BrewersBuddyContext db = new BrewersBuddyContext(); var currentUser = User.Identity.Name; UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == currentUser.ToLower()); if (user == null) { // Some problem occured return HttpNotFound(); } return View(user); }
public static BatchComment createBatchComment(Batch batch, UserProfile user, string comment) { BrewersBuddyContext db = new BrewersBuddyContext(); BatchComment batchComment = new BatchComment(); batchComment.Batch = batch; batchComment.User = user; batchComment.Comment = comment; db.BatchComments.Add(batchComment); db.SaveChanges(); return batchComment; }
public static BatchRating createBatchRating(Batch batch, UserProfile user, int rating, string comment) { BrewersBuddyContext db = new BrewersBuddyContext(); BatchRating batchRating = new BatchRating(); batchRating.Batch = batch; batchRating.User = user; batchRating.Rating = rating; batchRating.Comment = comment; db.BatchRatings.Add(batchRating); db.SaveChanges(); return batchRating; }
public static BatchNote createBatchNote(Batch batch, String title, String text, UserProfile user) { BrewersBuddyContext db = new BrewersBuddyContext(); BatchNote note = new BatchNote(); note.Batch = batch; note.Text = text; note.Title = title; note.AuthorId = user.UserId; note.AuthorDate = DateTime.Now; db.BatchNotes.Add(note); db.SaveChanges(); return note; }
public static Batch createBatch(String name, BatchType type, UserProfile owner) { BrewersBuddyContext db = new BrewersBuddyContext(); Batch batch = new Batch(); batch.Name = name; batch.Type = type; batch.OwnerId = owner.UserId; batch.StartDate = DateTime.Now; db.Batches.Add(batch); db.SaveChanges(); return batch; }
public virtual void TestInitialize() { string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); AppDomain.CurrentDomain.SetData("DataDirectory", path); Database.SetInitializer(new DatabaseInitializer()); context = new BrewersBuddyContext(); context.Database.Initialize(true); if (!WebSecurity.Initialized) { WebSecurity.InitializeDatabaseConnection( connectionStringName: "DefaultConnection", userTableName: "UserProfile", userIdColumn: "UserID", userNameColumn: "UserName", autoCreateTables: true); } }
public SimpleMembershipInitializer() { Database.SetInitializer<BrewersBuddyContext>(null); try { using (var context = new BrewersBuddyContext()) { if (!context.Database.Exists()) { // Create the SimpleMembership database without Entity Framework migration schema ((IObjectContextAdapter)context).ObjectContext.CreateDatabase(); } } WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true); } catch (Exception ex) { throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex); } }
public ActionResult ResetPassword(string un, string rt) { BrewersBuddyContext db = new BrewersBuddyContext(); //TODO: Check the un and rt matching and then perform following //get userid of received username var userid = (from i in db.UserProfiles where i.UserName == un select i.UserId).FirstOrDefault(); //check userid and token matches bool any = (from j in db.webpages_Memberships where (j.UserId == userid) && (j.PasswordVerificationToken == rt) //&& (j.PasswordVerificationTokenExpirationDate < DateTime.Now) select j).Any(); if (any == true) { //generate random password string newpassword = GenerateRandomPassword(8); //reset password bool response = WebSecurity.ResetPassword(rt, newpassword); if (response == true) { //get user emailid to send password var emailid = (from i in db.UserProfiles where i.UserName == un select i.Email).FirstOrDefault(); //send email string subject = "New Password"; string body = "<b>Please find the New Password</b><br/>" + newpassword; //edit it try { SendEMail(emailid, subject, body); TempData["Message"] = "Success! Check email we sent for new password."; } catch (Exception ex) { TempData["Message"] = "Error occured while sending email." + ex.Message; } } else { TempData["Message"] = "Hey, avoid random request on this page."; } } else { TempData["Message"] = "Username and token not maching."; } return View(); }
public ActionResult ForgotPassword(string UserName) { //check user existance var user = Membership.GetUser(UserName); if (user == null) { TempData["Message"] = "User Not exist."; } else { //generate password token var token = WebSecurity.GeneratePasswordResetToken(UserName); //create url with above token var resetLink = "<a href='" + Url.Action("ResetPassword", "Account", new { un = UserName, rt = token }, "http") + "'>Reset Password</a>"; //get user emailid BrewersBuddyContext db = new BrewersBuddyContext(); var emailid = (from i in db.UserProfiles where i.UserName == UserName select i.Email).FirstOrDefault(); //send mail string subject = "Password Reset Token"; string body = "<b>Please find the Password Reset Token</b><br/>" + resetLink; //edit it try { SendEMail(emailid, subject, body); TempData["Message"] = "Mail Sent."; } catch (Exception ex) { TempData["Message"] = "Error occured while sending email." + ex.Message; } } return View(); }
public ActionResult ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl) { string provider = null; string providerUserId = null; if (User.Identity.IsAuthenticated || !OAuthWebSecurity.TryDeserializeProviderUserId(model.ExternalLoginData, out provider, out providerUserId)) { return RedirectToAction("Manage"); } if (ModelState.IsValid) { // Insert a new user into the database using (BrewersBuddyContext db = new BrewersBuddyContext()) { UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower()); // Check if user already exists if (user == null) { // Insert name into the profile table db.UserProfiles.Add(new UserProfile { UserName = model.UserName }); db.SaveChanges(); OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName); OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false); return RedirectToLocal(returnUrl); } else { ModelState.AddModelError("UserName", "User name already exists. Please enter a different user name."); } } } ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName; ViewBag.ReturnUrl = returnUrl; return View(model); }
public static Measurement createMeasurement(Batch batch, String name, String description, String measured, Double value) { BrewersBuddyContext db = new BrewersBuddyContext(); Measurement measurment = new Measurement(); measurment.Batch = batch; measurment.Name = name; measurment.Description = description; measurment.Measured = measured; measurment.MeasurementDate = DateTime.Now; measurment.Value = value; db.Measurements.Add(measurment); db.SaveChanges(); return measurment; }
public static UserProfile createUser(int userId, String firstName, String lastName) { BrewersBuddyContext db = new BrewersBuddyContext(); UserProfile user = new UserProfile(); user.UserId = userId; user.FirstName = firstName; user.LastName = lastName; //TODO should we register them somehow?? db.UserProfiles.Add(user); db.SaveChanges(); return user; }
public ActionResult EditNote(BatchNote note) { if (ModelState.IsValid) { BrewersBuddyContext db2 = new BrewersBuddyContext(); db2.Entry(note).State = EntityState.Modified; db2.SaveChanges(); //Associate the batch with the note int batchId = (int)Session["CurrentBatchId"]; return RedirectToAction("Details/" + batchId); } return View(note); }
public ActionResult EditNote(int idNote = 0, int idBatch = 0) { Batch batch = db.Batches.Find(idBatch); if (batch == null) { return HttpNotFound(); } Session["CurrentBatchId"] = batch.BatchId; BrewersBuddyContext db2 = new BrewersBuddyContext(); BatchNote note = db2.BatchNotes.Find(idNote); if (note == null) { return HttpNotFound(); } return View(note); }