public ActionResult CreateGroup(MultipleModel.CreateGradeVM vm) { if (ModelState.IsValid) { using (var db = new LibraryDbContext()) { if (db.Grades.Where(u => u.Grade == vm.CreateGrade.Grade).Any()) { vm.Error = true; var errorList = new List<string>(); string message = "Grade " + vm.CreateGrade.Grade + " is already existing"; errorList.Add(message); vm.Message = errorList; TempData["AddGroup"] = vm; return RedirectToAction("GradesIndex", "Admin"); } else { var newGroup = db.Grades.Create(); newGroup.Grade = vm.CreateGrade.Grade; newGroup.CreatedAt = DateTime.Now; if (Request.Files.Count > 0) { var img = Request.Files[0]; if ((img != null) && (img.FileName == null)) { string name = Guid.NewGuid().ToString() + "_" + Path.GetFileName(img.FileName); string path = Path.Combine(Server.MapPath("~/Image/group_image"), name); img.SaveAs(path); var image = newGroup.Image; image.Name = name; image.Path = "~/Image/group_image/" + name; image.CreatedAt = DateTime.Now; } } db.Grades.Add(newGroup); db.SaveChanges(); vm.Error = false; var errorList = new List<string>(); string message = "You have successfully added a group(" + newGroup.Grade + ")"; errorList.Add(message); vm.Message = errorList; TempData["AddGroup"] = vm; return RedirectToAction("GradesIndex", "Admin"); } } } vm.Error = true; vm.Message = CustomValidationMessage.GetErrorList(ViewData.ModelState); TempData["AddGroup"] = vm; return RedirectToAction("GradesIndex", "Admin"); }
public ActionResult ActivateAccount(MultipleModel.LoginModelVM login) { if(ModelState.IsValid) { using (var db = new LibraryDbContext()) { var emailCheck = db.Users.Where(u => u.Email == login.ActivationModel.Email).ToList(); if(emailCheck[0] !=null) { var email = db.Users.SingleOrDefault(u => u.Email == login.ActivationModel.Email); var crypto = new PBKDF2(); if ((email.Password != null) && (email.PasswordSalt != null)) { login.Error = true; ModelState.AddModelError("", "The account is already activated"); return View("Login", login); } else if ((email != null) && (email.Pincode == crypto.Compute(login.ActivationModel.PinCode, email.PincodeSalt))) { var ctx = Request.GetOwinContext(); var authManager = ctx.Authentication; var identity = new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, "acc_act"), new Claim(ClaimTypes.Role, "activation") }, "ApplicationCookie"); authManager.SignIn(identity); return RedirectToAction("ActivateAccount2", new { id = email.Id }); } else if ((email != null) && (email.Pincode != login.ActivationModel.PinCode)) { login.Error = true; ModelState.AddModelError("", "Incorrect pin entered"); return View("Login", login); } } else if (emailCheck[0] == null) { login.Error = true; ModelState.AddModelError("", "The account does not exist"); } } } login.Error = true; return View("Login", login); }
public ActionResult ActivateAccount2(Guid? id) { if (id.HasValue) { using (var db = new LibraryDbContext()) { MultipleModel.AuthModelVM vm = new MultipleModel.AuthModelVM(); var userActivation = TempData["UserActivation"] as MultipleModel.AuthModelVM; if(userActivation != null) { vm = userActivation; } vm.UserModel = db.Users.SingleOrDefault(u => u.Id == id); return View(vm); } } return RedirectToAction("Login"); }
public ActionResult Login(MultipleModel.LoginModelVM user) { if (ModelState.IsValid) { using (var db = new LibraryDbContext()) { var crypto = new SimpleCrypto.PBKDF2(); var emailCheck = db.Users.FirstOrDefault(u => u.Email == user.AuthModel.Email); var getPasswordSalt = db.Users.Where(u => u.Email == user.AuthModel.Email).Select(u => u.PasswordSalt); if ((emailCheck != null) && (getPasswordSalt != null) && (emailCheck.Deleted == false) && (emailCheck.Status == true)) { var materializePasswordSalt = getPasswordSalt.ToList(); var passwordSalt = materializePasswordSalt[0]; var encryptedPassword = crypto.Compute(user.AuthModel.Password, passwordSalt); if (user.AuthModel.Email != null && emailCheck.Password == encryptedPassword) { var name = emailCheck.Student.FirstName; var getEmail = db.Users.Where(u => u.Id == emailCheck.Id).Select(u => u.Email); var materializeEmail = getEmail.ToList(); var email = materializeEmail[0]; var getRole = db.Users.Where(u => u.Id == emailCheck.Id).Select(u => u.Role); var materializeRole = getRole.ToList(); var role = materializeRole[0]; var identity = new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, name), new Claim(ClaimTypes.Email, email), new Claim(ClaimTypes.Role, role) }, "ApplicationCookie"); var ctx = Request.GetOwinContext(); var authManager = ctx.Authentication; authManager.SignIn(identity); if (emailCheck.Role == "administrator") { return RedirectToAction("GradesIndex", "Admin"); } else { return RedirectToAction("Index", "Auth"); } } else { user.Error = true; ModelState.AddModelError("", "Invalid email or password"); } } else if((emailCheck != null) && (emailCheck.Status == false) && (emailCheck.Deleted == false) ) { user.Error = true; ModelState.AddModelError("", "Please activate the account"); } else if(emailCheck == null || ((emailCheck.Deleted == true) && (emailCheck.Status == false))) { user.Error = true; ModelState.AddModelError("", "Account does not exist"); } } } user.Error = true; return View(user); }
public ActionResult ActivateAccount2(MultipleModel.AuthModelVM request) { if(ModelState.IsValid) { using (var db = new LibraryDbContext()) { MultipleModel.AuthModelVM vm = new MultipleModel.AuthModelVM(); vm.UserModel = db.Users.SingleOrDefault(u => u.Id == request.UserModel.Id); var crypto = new SimpleCrypto.PBKDF2(); var encrypPass = crypto.Compute(request.ActivationModel1.Password); vm.UserModel.PasswordSalt = crypto.Salt; vm.UserModel.Password = encrypPass; vm.UserModel.SecretQuestion = request.ActivationModel1.SecretQuestion; vm.UserModel.SecretAnswer = request.ActivationModel1.SecretAnswer; vm.UserModel.Status = true; vm.UserModel.Deleted = false; vm.UserModel.UpdatedAt = DateTime.Now; vm.UserModel.Student.Birthday = request.ActivationModel1.Birthday; vm.UserAddressModel = db.UserAddresses.Create(); vm.UserAddressModel.UserId = vm.UserModel.Id; vm.UserAddressModel.ZipCode = request.ActivationModel1.ZipCode; vm.UserAddressModel.Address1 = request.ActivationModel1.Address1; vm.UserAddressModel.Address2 = request.ActivationModel1.Address2; vm.UserAddressModel.City = request.ActivationModel1.City; vm.UserAddressModel.Country = request.ActivationModel1.Country; vm.UserAddressModel.CreatedAt = DateTime.Now; db.UserAddresses.Add(vm.UserAddressModel); db.Entry(vm.UserModel).State = EntityState.Modified; db.SaveChanges(); var ctx = Request.GetOwinContext(); var authManager = ctx.Authentication; authManager.SignOut("ApplicationCookie"); var loginVM = new MultipleModel.LoginModelVM(); loginVM.Error = false; var errorList = new List<string>(); string message = "You have successfully activated your account. Please log in"; errorList.Add(message); loginVM.Message = errorList; TempData["LoginTD"] = loginVM; return RedirectToAction("Login"); } } request.Error = true; request.Message = CustomValidationMessage.GetErrorList(ViewData.ModelState); TempData["UserActivation"] = request; return RedirectToAction("ActivateAccount2", new { id = request.UserModel.Id }); }
public ActionResult CreateUser(MultipleModel.UserIndexVM reg) { if (ModelState.IsValid) { if (db.Users.Where(u => u.Email == reg.Register.EmailAddress).Any()) { reg.Error = true; var errorList = new List<string>(); string message = "Email " + reg.Register.EmailAddress + " is already existing"; errorList.Add(message); reg.Message = errorList; TempData["UserIndexTD"] = reg; return RedirectToAction("UserIndex", new { id = reg.SectionID }); } else { using (var db = new LibraryDbContext()) { var newUser = db.Users.Create(); string pin = RandomPassword.Generate(6, PasswordGroup.Lowercase, PasswordGroup.Lowercase, PasswordGroup.Numeric); var crypto = new PBKDF2(); var encrypPin = crypto.Compute(pin); newUser.Pincode = encrypPin; newUser.PincodeSalt = crypto.Salt; newUser.Email = reg.Register.EmailAddress; newUser.Role = "student"; newUser.CreatedAt = DateTime.Now; newUser.UpdatedAt = DateTime.Now; db.Users.Add(newUser); var newStudent = db.Students.Create(); var section = db.Sections.FirstOrDefault(s => s.Id == reg.SectionID); newStudent.SectionId = section.Id; newStudent.FirstName = reg.Register.FirstName; newStudent.MiddleInitial = reg.Register.MiddleInitial; newStudent.LastName = reg.Register.LastName; newStudent.Gender = reg.Register.Gender; newStudent.CreatedAt = DateTime.Now; newStudent.UpdatedAt = DateTime.Now; db.Students.Add(newStudent); db.SaveChanges(); SMTP smtp = new SMTP(); smtp.SendEmal(newUser.Email, pin); reg.Error = false; var errorList = new List<string>(); string message = "You have successfully added a user(" + newUser.Email + ")"; errorList.Add(message); reg.Message = errorList; TempData["UserIndexTD"] = reg; return RedirectToAction("UserIndex", new { id = reg.SectionID }); } } } else { reg.Error = true; reg.Message = CustomValidationMessage.GetErrorList(ViewData.ModelState); } TempData["UserIndexTD"] = reg; return RedirectToAction("UserIndex", new { id = reg.SectionID }); }
public ActionResult CreateSection(MultipleModel.CreateSectionVM vm) { if (ModelState.IsValid) { using (var db = new LibraryDbContext()) { if (db.Sections.Where(u => u.Section == vm.CreateSection.Section).Any()) { vm.Error = true; var errorList = new List<string>(); string message = "Section " + vm.CreateSection.Section + " is already existing"; errorList.Add(message); vm.Message = errorList; TempData["AddSection"] = vm; return RedirectToAction("SectionIndex", new { id = vm.GroupID }); } else { var grade = db.Grades.Find(vm.GroupID); var section = db.Sections.Create(); section.Section = vm.CreateSection.Section; section.CreatedAt = DateTime.Now; grade.Sections.Add(section); db.SaveChanges(); vm.Error = false; var errorList = new List<string>(); string message = "You have successfully added a section(" + vm.CreateSection.Section + ")"; errorList.Add(message); vm.Message = errorList; TempData["UserRegistration"] = vm; return RedirectToAction("SectionIndex", new { id = section.GradeId }); } } } vm.Error = true; vm.Message = CustomValidationMessage.GetErrorList(ViewData.ModelState); TempData["AddSection"] = vm; return RedirectToAction("SectionIndex", new { id = vm.GroupID }); }