Пример #1
0
        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 });
        }
Пример #2
0
        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 });
        }
Пример #3
0
        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");
        }
Пример #4
0
        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 });
        }