-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.php
126 lines (116 loc) · 4.98 KB
/
controller.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
namespace Concrete\Package\MdVisibilityAttribute;
use Concrete\Core\Attribute\Category\CategoryService;
use Concrete\Core\Attribute\TypeFactory;
use Concrete\Core\Cache\Level\RequestCache;
use Concrete\Core\Entity\Attribute\Category;
use Concrete\Core\Package\Package;
use Concrete\Core\Page\Page;
use Concrete\Core\Page\Type\Event;
use Concrete\Core\Permission\Access\Entity\GroupEntity;
use Concrete\Core\Permission\Key\PageKey as PagePermissionKey;
use Concrete\Core\User\Group\Group;
use Macareux\VisibilityAttribute\Entity\VisibilityAttributeValue;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Concrete\Core\Permission\Access\Access as PermissionAccess;
class Controller extends Package
{
protected $pkgHandle = 'md_visibility_attribute';
protected $appVersionRequired = '8.5.5';
protected $pkgVersion = '0.1.1';
protected $pkgAutoloaderRegistries = [
'src/Entity' => '\Macareux\VisibilityAttribute\Entity',
];
public function getPackageName()
{
return t('Macareux Visibility Control Attribute');
}
public function getPackageDescription()
{
return t('Add an attribute type to control visibility of pages.');
}
public function install()
{
$pkg = parent::install();
/** @var TypeFactory $factory */
$factory = $this->app->make(TypeFactory::class);
$type = $factory->getByHandle('visibility');
if (!is_object($type)) {
$type = $factory->add('visibility', 'Visibility', $pkg);
/** @var CategoryService $service */
$service = $this->app->make(CategoryService::class);
$category = $service->getByHandle('collection')->getController();
$category->associateAttributeKeyType($type);
}
return $pkg;
}
public function on_start()
{
/** @var RequestCache $requestCache */
$requestCache = $this->app->make('cache/request');
/** @var EventDispatcherInterface $director */
$director = $this->app->make('director');
$director->addListener('on_page_type_publish', function ($event) use ($requestCache) {
$requestCache->disable();
/** @var Event $event */
$c = $event->getPageObject();
/** @var CategoryService $service */
$service = $this->app->make(CategoryService::class);
/** @var Category $category */
$category = $service->getByHandle('collection');
$attributes = $category->getController()->getList();
foreach ($attributes as $attribute) {
if ($attribute->getAttributeTypeHandle() === 'visibility') {
/** @var VisibilityAttributeValue $visibility */
$visibility = $c->getAttribute($attribute->getAttributeKeyHandle());
if ($visibility) {
$visibleGroups = $visibility->getVisibleGroups();
self::setVisibleGroups($c, $visibleGroups);
}
break;
}
}
});
}
protected static function setVisibleGroups(Page $page, array $groupIDs): void
{
if ($page->getCollectionInheritance() !== 'OVERRIDE') {
$page->setPermissionsToManualOverride();
}
$pk = PagePermissionKey::getByHandle('view_page');
if ($pk) {
$pk->setPermissionObject($page);
$pt = $pk->getPermissionAssignmentObject();
$pa = $pk->getPermissionAccessObject();
if (!$pa) {
$pa = PermissionAccess::create($pk);
} elseif ($pa->isPermissionAccessInUse()) {
$pa = $pa->duplicate();
}
foreach ($pa->getAccessListItems() as $accessListItem) {
$accessEntity = $accessListItem->getAccessEntityObject();
if ($accessEntity->getAccessEntityTypeHandle() === 'group') {
/** @var Group|null $group */
$group = $accessEntity->getGroupObject();
if ($group) {
$groupID = $group->getGroupID();
if ($groupID !== ADMIN_GROUP_ID && (!in_array($groupID, $groupIDs) || empty($groupIDs))) {
core_log(sprintf('Remove access: Page %s, Group %s', $page->getCollectionPath(), $group->getGroupDisplayName(false)));
$pa->removeListItem($accessEntity);
}
}
}
}
foreach ($groupIDs as $groupID) {
$group = Group::getByID($groupID);
if ($group) {
$pe = GroupEntity::getOrCreate($group);
core_log(sprintf('Add access: Page %s, Group %s', $page->getCollectionPath(), $group->getGroupDisplayName(false)));
$pa->addListItem($pe);
}
}
$pa->markAsInUse();
$pt->assignPermissionAccess($pa);
}
}
}