Esempio n. 1
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));
        }
Esempio n. 2
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));
        }