Exemplo n.º 1
0
        public IActionResult Delete(int id)
        {
            var role = roleProvider.GetEntity(x => x.Id == id);

            if (role.IsNullOrDefault())
            {
                return(RedirectToAction("List", "Role"));
            }

            try
            {
                var rolePermissions = rolePermissionProvider.Where(x => x.Role_Id == role.Id);
                var userRoles       = userRoleProvider.Where(x => x.Role_Id == role.Id);

                using (var transaction = new TransactionScope(TransactionScopeOption.Required))
                {
                    rolePermissionService.DeleteRange(rolePermissions);
                    userRoleService.DeleteRange(userRoles);
                    roleService.Delete(role);

                    transaction.Complete();
                }

                return(RedirectToAction("List", "Role"));
            }
            catch (Exception)
            {
                var validationResult = new ValidationResult <UserViewModel>();
                var loader           = GetLoader <RoleViewModel, int>();
                var viewModel        = loader.Load(role.Id);

                validationResult.AddError(
                    m => m.Id,
                    translationManager.GetTranslationLabel(LanguageId, "Dashboard:Role:NotDeleted"));

                ModelState.Clear();
                ModelState.AddValidationResult(validationResult);

                return(View("Details", viewModel));
            }
        }
Exemplo n.º 2
0
        public override void Process(RoleViewModel viewModel)
        {
            if (viewModel.Permissions.IsNullOrDefault() || viewModel.Permissions.Count == default(int))
            {
                viewModel.Permissions = new List <PermissionViewModel>();
            }

            var permissions = permissionProvider.GetAllAsList();
            var role        = roleProvider.GetEntity(r => r.Id == viewModel.Id);

            IEnumerable <PermissionViewModel> selectedViewModelPermissions;
            List <Permission> selectedPermissions;

            if (role.IsNullOrDefault())
            {
                role = new Role
                {
                    Name   = viewModel.Name,
                    Active = viewModel.Active
                };

                selectedViewModelPermissions = viewModel.Permissions.Where(x => x.Active);
                selectedPermissions          = permissions
                                               .Where(x => selectedViewModelPermissions.Any(y => y.Id == x.Id))
                                               .ToList();

                roleService.Add(role);

                if (!selectedPermissions.Any())
                {
                    return;
                }

                var rolePermissions = selectedPermissions.Select(x =>
                                                                 new RolePermission
                {
                    Role          = role,
                    Role_Id       = role.Id,
                    Permission_Id = x.Id,
                    Permission    = x
                }).ToList();

                rolePermissionService.AddRange(rolePermissions);

                return;
            }

            var currentPermissions = rolePermissionProvider.Where(x => x.Role_Id == role.Id);

            selectedViewModelPermissions = viewModel.Permissions.Where(x => x.Active);
            selectedPermissions          = permissions
                                           .Where(x => selectedViewModelPermissions.Any(y => y.Id == x.Id))
                                           .ToList();
            var deletedRoles = currentPermissions
                               .Where(x => !selectedPermissions.Any(y => y.Id == x.Permission_Id))
                               .ToList();
            var newRoles = selectedPermissions
                           .Where(x => !currentPermissions.Any(y => x.Id == y.Permission_Id))
                           .Select(x => new RolePermission
            {
                Role          = role,
                Role_Id       = role.Id,
                Permission    = x,
                Permission_Id = x.Id
            }).ToList();

            role.Name   = viewModel.Name;
            role.Active = viewModel.Active;

            roleService.AddOrUpdate(role);

            if (deletedRoles.Any())
            {
                rolePermissionService.DeleteRange(deletedRoles);
            }

            if (newRoles.Any())
            {
                rolePermissionService.AddRange(newRoles);
            }
        }