Beispiel #1
0
        public ActionResult Create(UserForm form)
        {
            if (ModelState.IsValid)
            {

                //We must check for passwords here, since they are not always required in the form.
                if (form.Password == null)
                {
                    ModelState.AddModelError("Password", "Please provide a Password.");
                    ViewBag.RoleList = GetRoleList();
                    return View(form);
                }

                if (form.SelectedRoleIds.Count == 0)
                {
                    ModelState.AddModelError("SelectedRoleIds", "Users must have at least one role assigned.");
                    ViewBag.RoleList = GetRoleList();
                    return View(form);
                }

                if (db.Users.Where(x => x.Username == form.Username.ToLower()).Count() != 0)
                {
                    ModelState.AddModelError("Username", "Username taken!");
                    ViewBag.RoleList = GetRoleList();
                    return View(form);
                }

                form.ID = Guid.NewGuid();

                if (form.Password.Equals(form.ConfirmedPassword))
                {
                    User user = formToUser(form);
                    user.Created = DateTime.Now;
                    user.LastModified = DateTime.Now;
                    db.Users.Add(user);
                    db.SaveChanges();
                }
                else
                {
                    ModelState.AddModelError("ConfirmedPassword", "Passwords do not match!");
                    ViewBag.RoleList = GetRoleList();
                    return View(form);
                }

                return RedirectToAction("Index");
            }
            ViewBag.RoleList = GetRoleList();
            return View(form);
        }
Beispiel #2
0
 private User formToUser(UserForm form)
 {
     IList<Role> roleList = new List<Role>();
     foreach (Guid g in form.SelectedRoleIds)
     {
         roleList.Add(db.Roles.Single(x => x.ID == g));
     }
     return new User
     {
         ID = form.ID,
         FirstName = form.FirstName,
         LastName = form.LastName,
         Username = form.Username,
         Password = Core.PasswordHashing.GetHash(form.Password),
         Roles = roleList,
         Created = form.Created,
         LastModified = form.LastModified
     };
 }
Beispiel #3
0
        public ActionResult Edit(UserForm form)
        {
            if (ModelState.IsValid)
            {
                User user = db.Users.Single(x => x.ID == form.ID);

                //Check for new passwords
                if (form.Password != null && form.ConfirmedPassword != null)
                {
                    if (form.ConfirmedPassword.Equals(form.Password))
                    {
                        user.Password = Core.PasswordHashing.GetHash(form.Password);
                    }
                    else
                    {
                        ModelState.AddModelError("ConfirmedPassword", "The new Passwords do not match.");
                        ViewBag.RoleList = GetRoleList();
                        return View(form);
                    }
                }

                if (form.SelectedRoleIds.Count == 0)
                {
                    ModelState.AddModelError("SelectedRoleIds", "Users must have at least one role assigned.");
                    ViewBag.RoleList = GetRoleList();
                    return View(form);
                }

                user.FirstName = form.FirstName;
                user.LastName = form.LastName;
                user.Username = form.Username;

                user.Roles.Clear();

                List<Role> userRoles = new List<Role>();
                foreach (Guid g in form.SelectedRoleIds)
                {
                    user.Roles.Add(db.Roles.Single(x => x.ID == g));
                }

                user.LastModified = DateTime.Now;
                db.Entry(user).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.RoleList = GetRoleList();
            return View(form);
        }