Exemplo n.º 1
0
        public ServiceResult <RoleEntity> Update(RoleEntity roleEntity, List <PermissionDescriptor> permissionDescriptors)
        {
            return(BeginTransaction(() =>
            {
                var permissions = _permissionService.Get(m => m.RoleId == roleEntity.ID);
                permissions.Each(m => m.ActionType = ActionType.Delete);
                permissionDescriptors.Where(m => m.Checked ?? false).Each(m =>
                {
                    bool exists = false;
                    foreach (var item in permissions)
                    {
                        if (item.PermissionKey == m.Key)
                        {
                            item.ActionType = ActionType.Update;
                            exists = true;
                        }
                    }
                    if (!exists)
                    {
                        permissions.Add(new Permission
                        {
                            RoleId = roleEntity.ID,
                            PermissionKey = m.Key,
                            Module = m.Module,
                            Title = m.Title,
                            ActionType = ActionType.Create
                        });
                    }
                });
                _permissionService.BeginBulkSave();
                permissions.Each(m =>
                {
                    switch (m.ActionType)
                    {
                    case ActionType.Create:
                        {
                            _permissionService.Add(m);
                            break;
                        }

                    case ActionType.Update:
                        {
                            _permissionService.Update(m);
                            break;
                        }

                    case ActionType.Delete:
                        {
                            _permissionService.Remove(m);
                            break;
                        }
                    }
                });
                _permissionService.SaveChanges();

                var old = Get(roleEntity.ID);
                roleEntity.CopyTo(old);
                return Update(old);
            }));
        }
Exemplo n.º 2
0
        public IActionResult Edit(RoleEntity entity, List <PermissionDescriptor> PermissionSet)
        {
            var permissions = _permissionService.Get(m => m.RoleId == entity.ID);

            permissions.Each(m => m.ActionType = ActionType.Delete);
            PermissionSet.Where(m => m.Checked ?? false).Each(m =>
            {
                bool exists = false;
                foreach (var item in permissions)
                {
                    if (item.PermissionKey == m.Key)
                    {
                        item.ActionType = ActionType.Update;
                        exists          = true;
                    }
                }
                if (!exists)
                {
                    permissions.Add(new Permission
                    {
                        RoleId        = entity.ID,
                        PermissionKey = m.Key,
                        Module        = m.Module,
                        Title         = m.Title,
                        ActionType    = ActionType.Create
                    });
                }
            });
            var old = Service.Get(entity.ID);

            entity.CopyTo(old);
            Service.Update(old);
            _permissionService.BeginBulkSave();
            permissions.Each(m =>
            {
                switch (m.ActionType)
                {
                case ActionType.Create:
                    {
                        _permissionService.Add(m);
                        break;
                    }

                case ActionType.Update:
                    {
                        _permissionService.Update(m);
                        break;
                    }

                case ActionType.Delete:
                    {
                        _permissionService.Remove(m);
                        break;
                    }
                }
            });
            _permissionService.SaveChanges();
            return(RedirectToAction("Index"));
        }