Ejemplo n.º 1
0
        public ActionResult Create([ModelBinder(typeof(RoleViewModelBinder))] RoleView RoleView)//[ModelBinder(typeof(RoleViewModelBinder))],string[] selectedAvailableFunctions
        {
            UserAccessRepository rep = new UserAccessRepository();

            // Get the list of roles in the system
            List <AvailableFunction> allFunctions = rep.GetAllAvailableFunctions();

            RoleView.Roles = rep.GetAllRoles();



            if (ModelState.IsValid)
            {
                Role newRole = new Role(Guid.NewGuid(), RoleView.Name, RoleView.Description);
                newRole.AvailableFunctions = new List <AvailableFunction>();

                for (int i = 0; i < allFunctions.Count(); i++)
                {
                    foreach (var item in RoleView.AvailableFunctions)
                    {
                        if (Guid.Parse(item.Value).Equals(allFunctions[i].ID))
                        {
                            newRole.AvailableFunctions.Add(new AvailableFunction(Guid.Parse(allFunctions[i].ID.ToString()), allFunctions[i].FunctionName));
                        }
                    }
                }

                if (rep.CreateRole(newRole) == true)
                {
                    return(RedirectToAction("Create", "Roles", new { message = "Your role '" + RoleView.Name + "' was created successfully!" }));
                }
                else
                {
                    return(RedirectToAction("Create", "Roles", new { message = "There was an error creating your role. Please contact your administrator." }));
                }
            }

            RoleView.Roles = rep.GetAllRoles();

            if (RoleView.AvailableFunctions == null)
            {
                RoleView.AvailableFunctions = new List <CheckBoxListInfo>();
            }

            RoleView.AvailableFunctions.Clear();

            foreach (var item in allFunctions)
            {
                RoleView.AvailableFunctions.Add(new CheckBoxListInfo(item.ID.ToString(), item.FunctionName, false));
            }

            return(View("Create", RoleView));
        }
Ejemplo n.º 2
0
        public ActionResult Edit(string id)
        {
            UserAccessRepository rep         = new UserAccessRepository();
            UserAccountView      userAccView = new UserAccountView();

            userAccView.UserAccount = rep.GetUserAccount(id);

            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()
                });
            }


            if (userAccView.UserAccount != null)
            {
                View("Edit", userAccView);
            }
            else
            {
                ViewData["Message"] = "User could not be found!";
            }

            return(View("Edit", userAccView));
        }
Ejemplo n.º 3
0
        public ActionResult Edit(UserAccountView UserAccountView, Guid Roles)
        {
            UserAccessRepository rep = new UserAccessRepository();

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

            UserAccountView.Roles            = new List <SelectListItem>();
            UserAccountView.UserAccount.Role = rep.GetRole(Roles);

            ModelState.Remove("Roles"); //We remove any model state error on Roles because we manually load it from the database as drop-down list only returns the selected role

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

            if (ModelState.IsValid)
            {
                if (UserAccountView.UserAccount != null)
                {
                    ////////////////////
                    MembershipUser user = Membership.GetUser(UserAccountView.UserAccount.Username);

                    UserAccount existingAccount = rep.GetUserAccount(UserAccountView.UserAccount.Username);

                    foreach (AvailableFunction func in existingAccount.Role.AvailableFunctions)
                    {
                        System.Web.Security.Roles.RemoveUserFromRole(existingAccount.Username, func.FunctionName);
                    }

                    foreach (var func in UserAccountView.UserAccount.Role.AvailableFunctions)
                    {
                        System.Web.Security.Roles.AddUserToRole(UserAccountView.UserAccount.Username, func.FunctionName);
                    }


                    ////////////////////
                    bool status = rep.UpdateUserAccount(UserAccountView.UserAccount);

                    if (status)
                    {
                        ViewData["Message"] = "User updated successfully!";
                    }
                    else
                    {
                        ViewData["Message"] = "There was an error updating this user. Please contact your administrator!";
                    }

                    return(View("Edit", null));
                }
                else
                {
                    ViewData["Message"] = "User could not be found!";
                }
            }

            return(View(UserAccountView));
        }
Ejemplo n.º 4
0
        public ActionResult Create(string message)
        {
            RoleView             roleView = new RoleView();
            UserAccessRepository rep      = new UserAccessRepository();


            roleView.AvailableFunctions = new List <CheckBoxListInfo>();
            roleView.Roles = rep.GetAllRoles();

            // Get the list of roles in the system
            List <AvailableFunction> allFunctions = rep.GetAllAvailableFunctions();

            if (allFunctions.Count > 0) //It is possible that database might not have any available functions initially
            {
                for (int i = 0; i < allFunctions.Count(); i++)
                {
                    roleView.AvailableFunctions.Add(new CheckBoxListInfo(allFunctions.ElementAt(i).ID.ToString(), allFunctions.ElementAt(i).FunctionName, false));
                }
            }

            if (message != null)
            {
                ViewData["Message"] = message;
            }

            return(View("Create", roleView));
        }
Ejemplo n.º 5
0
        public ActionResult Create(string message)
        {
            UserAccessRepository rep         = new UserAccessRepository();
            UserAccountView      userAccView = new UserAccountView();

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

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

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

            if (message != null)
            {
                ViewData["Message"] = message;
            }

            return(View(userAccView));
        }
Ejemplo n.º 6
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));
        }