コード例 #1
0
        public ActionResult Create(UserAccount UserAccount, Guid Roles)
        {
            UserAccessRepository rep = new UserAccessRepository();

            if (ModelState.IsValid)
            {
                UserAccount.Role = rep.GetRole(Roles);
                UserAccount.Person.Address.Country = "Canada";
                UserAccount.Person.Organization    = new Organization(Guid.Parse(ConfigurationManager.AppSettings["HostingOrganizationID"].ToString()));

                MembershipCreateStatus createStatus;
                MembershipUser         newUser = Membership.CreateUser(UserAccount.Username, UserAccount.Password, UserAccount.Person.Email, "Q", "P", true, out createStatus);
                UserAccount.Person.Type = PersonType.Employee;

                switch (createStatus)
                {
                case MembershipCreateStatus.DuplicateUserName:
                    ViewData["Message"] = "Duplicate username! This username already exists in the system.";

                    break;

                case MembershipCreateStatus.InvalidPassword:
                    ViewData["Message"] = "Invalid password! Minimum password length is six characters.";

                    break;

                case MembershipCreateStatus.Success:
                    foreach (AvailableFunction func in UserAccount.Role.AvailableFunctions)
                    {
                        System.Web.Security.Roles.AddUserToRole(UserAccount.Username, func.FunctionName);
                    }

                    if (rep.CreateUserAccount(UserAccount))
                    {
                        ViewData["Message"] = "User account created succesfully!";
                        UserAccount         = null;
                        ModelState.Clear();     //Will clear out the form

                        return(RedirectToAction("Create", "UserAccount", new { message = "User account created succesfully!" }));
                    }
                    else
                    {
                        ViewData["Message"] = "There was an error creating this user. Please contact your administrator.";
                    }

                    break;

                default:
                    ViewData["Message"] = "There was an error creating this user. Please contact your administrator.";
                    break;
                }
            }

            UserAccountView userAccView = new UserAccountView();

            List <Role> roles = rep.GetAllRoles();

            userAccView.Roles = new List <SelectListItem>();

            foreach (Role role in roles)
            {
                userAccView.Roles.Add(new SelectListItem {
                    Text = role.RoleName, Value = role.ID.ToString()
                });
            }

            userAccView.ExistingUserAccounts = rep.GetUserAccounts();
            userAccView.UserAccount          = UserAccount;

            return(View("Create", userAccView));
        }