public ActionResult Create(Supervisor supervisor)
        {
            if (!AuthenticationController.IsUserAuthorized(HttpContext, AuthenticationController.UserRole.Admin))
            {
                return(RedirectToAction("NotAuthorized", "Authentication"));
            }
            ViewData["Layout"] = AuthenticationController.GetUserLayout(HttpContext);

            if (!ModelState.IsValid)
            {
                return(View());
            }

            supervisor.User.Password = AuthenticationController.GetSha256FromString(supervisor.User.Password);

            using (var transaction = _context.Database.BeginTransaction())
            {
                _context.Users.Add(supervisor.User);
                _context.SaveChanges();

                _context.Supervisors.Add(supervisor);
                _context.SaveChanges();

                transaction.Commit();
            }

            return(RedirectToAction(nameof(Index)));
        }
Пример #2
0
        public ActionResult Edit(StudentViewModel viewModel)
        {
            if (!AuthenticationController.IsUserAuthorized(HttpContext, AuthenticationController.UserRole.Admin))
            {
                return(RedirectToAction("NotAuthorized", "Authentication"));
            }
            ViewData["Layout"] = AuthenticationController.GetUserLayout(HttpContext);

            var enteredStudent = viewModel.Student;
            var enteredUser    = viewModel.Student.User;

            var student = _context.Students
                          .FirstOrDefault(s => s.Id == viewModel.Student.Id);
            var user = _context.Users
                       .FirstOrDefault(u => u.Id == student.UserId);

            if (!string.IsNullOrWhiteSpace(enteredUser.Email))
            {
                user.Email = enteredUser.Email;
            }
            if (!string.IsNullOrWhiteSpace(enteredUser.FirstName))
            {
                user.FirstName = enteredUser.FirstName;
            }
            if (!string.IsNullOrWhiteSpace(enteredUser.LastName))
            {
                user.LastName = enteredUser.LastName;
            }
            if (!string.IsNullOrWhiteSpace(enteredUser.Password))
            {
                if (enteredUser.Password != viewModel.ConfirmPassword)
                {
                    ViewData["Message"] = "Wpisane hasła nie są takie same";
                    return(View());
                }
                user.Password = AuthenticationController.GetSha256FromString(enteredUser.Password);
            }

            if (enteredStudent.SpecialtyId != student.SpecialtyId)
            {
                student.SpecialtyId = enteredStudent.SpecialtyId;
            }
            if (enteredStudent.DegreeCycle != student.DegreeCycle)
            {
                student.DegreeCycle = enteredStudent.DegreeCycle;
            }
            if (enteredStudent.StudentNo != student.StudentNo)
            {
                student.StudentNo = enteredStudent.StudentNo;
            }

            _context.SaveChanges();
            return(RedirectToAction(nameof(Index)));
        }
        public ActionResult Edit(Supervisor model)
        {
            if (!AuthenticationController.IsUserAuthorized(HttpContext, AuthenticationController.UserRole.Admin))
            {
                return(RedirectToAction("NotAuthorized", "Authentication"));
            }

            var supervisor = _context.Supervisors
                             .FirstOrDefault(s => s.Id == model.Id);
            var user = _context.Users
                       .FirstOrDefault(u => u.Id == model.UserId);

            if (model.User.FirstName != user.FirstName)
            {
                user.FirstName = model.User.FirstName;
            }
            if (model.User.LastName != user.LastName)
            {
                user.LastName = model.User.LastName;
            }
            if (model.User.Email != user.Email)
            {
                user.Email = model.User.Email;
            }
            if (!string.IsNullOrWhiteSpace(model.User.Password))
            {
                user.Password = AuthenticationController.GetSha256FromString(model.User.Password);
            }

            if (model.FacultyId != supervisor.FacultyId)
            {
                supervisor.FacultyId = model.FacultyId;
            }
            if (model.StudentLimit != supervisor.StudentLimit)
            {
                supervisor.StudentLimit = model.StudentLimit;
            }

            _context.SaveChanges();

            return(RedirectToAction(nameof(Index)));
        }
Пример #4
0
        public ActionResult Create(StudentViewModel model)
        {
            if (!AuthenticationController.IsUserAuthorized(HttpContext, AuthenticationController.UserRole.Admin))
            {
                return(RedirectToAction("NotAuthorized", "Authentication"));
            }
            ViewData["Layout"] = AuthenticationController.GetUserLayout(HttpContext);

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var enteredStudent = model.Student;
            var enteredUser    = model.Student.User;

            using (var transaction = _context.Database.BeginTransaction())
            {
                var matchedUser = _context.Users
                                  .FirstOrDefault(u => (u.Email == model.Student.User.Email));
                if (matchedUser != null)
                {
                    ViewData["Message"] = "Taki użytkownik istnieje już w systemie!";
                    return(View(model));
                }

                enteredUser.Password = AuthenticationController.GetSha256FromString(enteredUser.Password);

                _context.Users.Add(enteredUser);
                _context.SaveChanges();

                model.Student.UserId = enteredUser.Id;
                _context.Students.Add(enteredStudent);
                _context.SaveChanges();

                transaction.Commit();
            }
            return(RedirectToAction(nameof(Index)));
        }