Esempio n. 1
0
        public IActionResult Update(int id)
        {
            Role role = roleService.GetByIdWithDetails(id);

            RoleWithPrivilegesViewModel model = new RoleWithPrivilegesViewModel();

            model.Id   = role.Id;
            model.Name = role.Name;

            List <Privilege> privileges = privilegeService.GetAll();

            model.PrivilegesWithSelection = new List <PrivilegeSelectionViewModel>();

            if (privileges?.Count > 0)
            {
                foreach (var item in privileges)
                {
                    model.PrivilegesWithSelection.Add(new PrivilegeSelectionViewModel
                    {
                        Description = item.Description,
                        Id          = item.Id,
                        Name        = item.Name,
                        isSelected  = role.RolePrivileges?.FirstOrDefault(x => x.PrivilegeId == item.Id) != null
                    });
                }
            }

            return(View(model));
        }
Esempio n. 2
0
        public IActionResult Update(RoleWithPrivilegesViewModel model)
        {
            if (ModelState.IsValid)
            {
                Role searchingRole = roleService.GetByName(model.Name);
                if (model.Name == searchingRole?.Name && model.Id != searchingRole?.Id)
                {
                    ModelState.AddModelError("", "Bu isimde başka bir rol zaten var");
                    return(View(model));
                }
                else if (model.Name == ConstRoles.ADMIN)
                {
                    ModelState.AddModelError("", "Admin rolü değiştirelemez");
                    return(View(model));
                }

                Role role = new Role
                {
                    Id   = model.Id,
                    Name = model.Name
                };

                List <PrivilegeSelectionViewModel> selectedPrivilegeModels =
                    model.PrivilegesWithSelection.FindAll(x => x.isSelected);

                if (selectedPrivilegeModels?.Count > 0)
                {
                    List <RolePrivilege> rolePrivileges = new List <RolePrivilege>();

                    foreach (var item in selectedPrivilegeModels)
                    {
                        rolePrivileges.Add(new RolePrivilege {
                            PrivilegeId = item.Id
                        });
                    }

                    role.RolePrivileges = rolePrivileges;
                }

                roleService.Update(role);

                return(RedirectToAction("Index"));
            }
            return(View(model));
        }
Esempio n. 3
0
        public IActionResult Add(RoleWithPrivilegesViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (roleService.GetByName(model.Name) == null)
                {
                    Role role = new Role
                    {
                        Name = model.Name
                    };

                    List <PrivilegeSelectionViewModel> selectedPrivilegeModels =
                        model.PrivilegesWithSelection.FindAll(x => x.isSelected);

                    if (selectedPrivilegeModels?.Count > 0)
                    {
                        List <RolePrivilege> rolePrivileges = new List <RolePrivilege>();

                        foreach (var item in selectedPrivilegeModels)
                        {
                            rolePrivileges.Add(new RolePrivilege {
                                PrivilegeId = item.Id
                            });
                        }

                        role.RolePrivileges = rolePrivileges;
                    }

                    roleService.Add(role);

                    return(RedirectToAction("Index"));
                }
                else
                {
                    ModelState.AddModelError("", "Bu isimde bir rol zaten var.");
                }
            }

            return(View(model));
        }
Esempio n. 4
0
        public IActionResult Add()
        {
            RoleWithPrivilegesViewModel model      = new RoleWithPrivilegesViewModel();
            List <Privilege>            privileges = privilegeService.GetAll();

            model.PrivilegesWithSelection = new List <PrivilegeSelectionViewModel>();

            if (privileges?.Count > 0)
            {
                foreach (var item in privileges)
                {
                    model.PrivilegesWithSelection.Add(new PrivilegeSelectionViewModel
                    {
                        Description = item.Description,
                        Id          = item.Id,
                        Name        = item.Name,
                        isSelected  = false
                    });
                }
            }

            return(View(model));
        }