| Total Complexity | 24 |
| Total Lines | 201 |
| Duplicated Lines | 0 % |
| Coverage | 78.31% |
| Changes | 11 | ||
| Bugs | 0 | Features | 1 |
| 1 | <?php |
||
| 32 | class DataObjectExtension extends DataExtension |
||
| 33 | { |
||
| 34 | public const WRITE = 'write'; |
||
| 35 | public const DELETE = 'delete'; |
||
| 36 | /** |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | public static $canViewClasses = []; |
||
| 40 | /** |
||
| 41 | * @var DataList |
||
| 42 | */ |
||
| 43 | protected static $members; |
||
| 44 | /** |
||
| 45 | * @var array |
||
| 46 | */ |
||
| 47 | protected static $excludedClasses = [ |
||
| 48 | DirtyClass::class, |
||
| 49 | ChangeSet::class, |
||
| 50 | ChangeSetItem::class, |
||
| 51 | ]; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @throws ValidationException |
||
| 55 | */ |
||
| 56 | 63 | public function onAfterWrite() |
|
| 57 | { |
||
| 58 | /** @var DataObject $owner */ |
||
| 59 | 63 | $owner = $this->owner; |
|
| 60 | 63 | if (in_array($owner->ClassName, static::$excludedClasses, true) || |
|
| 61 | 63 | (Controller::curr()->getRequest()->getURL() && |
|
| 62 | 63 | strpos('dev/build', Controller::curr()->getRequest()->getURL()) !== false) |
|
| 63 | ) { |
||
| 64 | 62 | return; |
|
| 65 | } |
||
| 66 | 63 | if (!$owner->hasExtension(Versioned::class)) { |
|
| 67 | 62 | $this->pushToSolr($owner); |
|
| 68 | } |
||
| 69 | 63 | } |
|
| 70 | |||
| 71 | /** |
||
| 72 | * @throws ValidationException |
||
| 73 | */ |
||
| 74 | 2 | public function onAfterPublish() |
|
| 75 | { |
||
| 76 | 2 | $owner = $this->owner; |
|
| 77 | 2 | $this->pushToSolr($owner); |
|
|
|
|||
| 78 | 2 | } |
|
| 79 | |||
| 80 | /** |
||
| 81 | * @param DataObject $owner |
||
| 82 | * @param string $type |
||
| 83 | * @return DirtyClass |
||
| 84 | * @throws ValidationException |
||
| 85 | */ |
||
| 86 | 63 | protected function getDirtyClass(DataObject $owner, $type) |
|
| 87 | { |
||
| 88 | // Get the DirtyClass object for this item |
||
| 89 | /** @var null|DirtyClass $record */ |
||
| 90 | 63 | $record = DirtyClass::get()->filter(['Class' => $owner->ClassName, 'Type' => $type])->first(); |
|
| 91 | 63 | if (!$record || !$record->exists()) { |
|
|
1 ignored issue
–
show
|
|||
| 92 | 62 | $record = DirtyClass::create([ |
|
| 93 | 62 | 'Class' => $owner->ClassName, |
|
| 94 | 62 | 'Type' => $type |
|
| 95 | ]); |
||
| 96 | 62 | $record->write(); |
|
| 97 | } |
||
| 98 | |||
| 99 | 63 | return $record; |
|
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param array $ids |
||
| 104 | * @param $record |
||
| 105 | * @param Exception $e |
||
| 106 | */ |
||
| 107 | protected function registerException(array $ids, $record, Exception $error): void |
||
| 108 | { |
||
| 109 | /** @var DataObject $owner */ |
||
| 110 | $owner = $this->owner; |
||
| 111 | $ids[] = $owner->ID; |
||
| 112 | // If we don't get an exception, mark the item as clean |
||
| 113 | $record->IDs = json_encode($ids); |
||
| 114 | $record->write(); |
||
| 115 | $logger = Injector::inst()->get(LoggerInterface::class); |
||
| 116 | $logger->warn( |
||
| 117 | sprintf( |
||
| 118 | 'Unable to alter %s with ID %s', |
||
| 119 | $owner->ClassName, |
||
| 120 | $owner->ID |
||
| 121 | ) |
||
| 122 | ); |
||
| 123 | $logger->error($error->getMessage()); |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @throws ValidationException |
||
| 128 | */ |
||
| 129 | 2 | public function onAfterDelete(): void |
|
| 149 | } |
||
| 150 | 2 | } |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Get the view status for each member in this object |
||
| 154 | * @return array |
||
| 155 | */ |
||
| 156 | 5 | public function getViewStatus(): array |
|
| 157 | { |
||
| 158 | // Return empty if it's not allowed to show in search |
||
| 159 | // The setting needs to be explicitly false, to avoid any possible collision |
||
| 160 | // with objects not having the setting, thus being `null` |
||
| 161 | /** @var DataObject|SiteTree $owner */ |
||
| 162 | 5 | $owner = $this->owner; |
|
| 163 | // Return immediately if the owner has ShowInSearch not being `null` |
||
| 164 | 5 | if ($owner->ShowInSearch === false || $owner->ShowInSearch === 0) { |
|
| 165 | 1 | return []; |
|
| 166 | } |
||
| 167 | |||
| 168 | 5 | return self::$canViewClasses[$owner->ClassName] ?? $this->getMemberPermissions($owner); |
|
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @param DataObject|SiteTree $owner |
||
| 173 | * @return array |
||
| 174 | */ |
||
| 175 | 5 | protected function getMemberPermissions($owner): array |
|
| 176 | { |
||
| 177 | // Log out the current user to avoid collisions in permissions |
||
| 178 | 5 | $currMember = Security::getCurrentUser(); |
|
| 179 | 5 | Security::setCurrentUser(null); |
|
| 180 | |||
| 181 | 5 | $return = []; |
|
| 182 | |||
| 183 | 5 | if ($owner->canView(null)) { |
|
| 184 | 3 | $return[] = '1-null'; |
|
| 185 | } else { |
||
| 186 | // Return a default '0-0' to basically say "noboday can view" |
||
| 187 | 3 | $return[] = '0-0'; |
|
| 188 | 3 | if (!self::$members) { |
|
| 189 | 1 | self::$members = Member::get(); |
|
| 190 | } |
||
| 191 | 3 | foreach (self::$members as $member) { |
|
| 192 | 3 | $return[] = sprintf('%s-%s', (int)$owner->canView($member), (int)$member->ID); |
|
| 193 | } |
||
| 194 | } |
||
| 195 | |||
| 196 | |||
| 197 | 5 | if (!$owner->hasField('ShowInSearch')) { |
|
| 198 | 1 | self::$canViewClasses[$owner->ClassName] = $return; |
|
| 199 | } |
||
| 200 | |||
| 201 | 5 | Security::setCurrentUser($currMember); |
|
| 202 | |||
| 203 | 5 | return $return; |
|
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @param DataObject $owner |
||
| 208 | * @throws ValidationException |
||
| 209 | */ |
||
| 210 | 62 | protected function pushToSolr(DataObject $owner): void |
|
| 233 | } |
||
| 234 | 62 | } |
|
| 235 | } |
||
| 236 |