コード例 #1
0
ファイル: UsersController.cs プロジェクト: jcaubin/pm
 public ActionResult Create()
 {
     CreateUserVM newUser = new CreateUserVM();
     foreach (string rolName in _rp.GetAllRoles())
     {
         newUser.InRoles.Add(new RoleUser() { RoleName = rolName, IsInRole = false });
     }
     return View(newUser);
 }
コード例 #2
0
ファイル: UsersController.cs プロジェクト: jcaubin/pm
        public ActionResult Create(CreateUserVM u)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    //Mapeo objView->ObjDominio
                    GuUser newUser = new GuUser(u.UserName, null, u.Email, u.PasswordQuestion, true)
                    {
                        NombrePropio = u.Nombre,
                        Apellido1 = u.Apellido1,
                        Apellido2 = u.Apellido2
                    };
                    string[] inroles = u.InRoles.Where(i => i.IsInRole).Select(r => r.RoleName).ToArray();
                    string[] outroles = u.InRoles.Where(i => !i.IsInRole).Select(r => r.RoleName).ToArray();

                    MembershipCreateStatus status = default(MembershipCreateStatus);
                    newUser = _mp.CreateUser(newUser, u.Password, u.PasswordAnswer, out status);
                    _rp.AddUsersToRoles(new string[] { u.UserName }, inroles);
                    _rp.RemoveUsersFromRoles(new string[] { u.UserName }, outroles);

                    if (status == MembershipCreateStatus.Success)
                    {
                        ViewBag.Status = status;
                        return RedirectToAction("Details", new { id =newUser.GuUserID });
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, status.ToString());
                        return View(u);
                    }
                }
                else
                {
                    return View(u);
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex);
                return View(u);
            }
        }