Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
CheckAvatarIdInFields.php
1<?php
2
4
5use Bitrix\Disk\Driver;
6use Bitrix\Disk\File;
7use Bitrix\Disk\Security\DiskSecurityContext;
14
16{
17 public function onBeforeAction(Event $event)
18 {
19 $fields = $this->getAction()->getArguments()['fields'];
20 $avatarId = $fields['avatar'] ?? null;
21 if (!is_numeric($avatarId))
22 {
23 return null;
24 }
25
26 $avatarResult = \CFile::GetByID($avatarId);
27 $avatar = (isset($avatarResult) && $avatarResult) ? $avatarResult->Fetch() : null;
28 $info = (new Image($_SERVER["DOCUMENT_ROOT"] . $avatar['SRC']))->getInfo();
29 if (!$info)
30 {
31 $this->addError(new Error(
32 'Wrong file type',
34 ));
35 return new EventResult(EventResult::ERROR, null, null, $this);
36 }
37
38 $currentUser = $this->getAction()->getCurrentUser();
39 $userId = isset($currentUser) ? $currentUser->getId() : null;
40 $securityContext = new DiskSecurityContext((int)$userId);
41 $parameters = [
42 'filter' => ['FILE_ID' => $avatarId],
43 'with' => ['CREATE_USER']
44 ];
45 $parameters = Driver::getInstance()->getRightsManager()->addRightsCheck($securityContext, $parameters, ['ID', 'CREATED_BY']);
46
47 $fileCollection = File::getModelList($parameters);
48 if (!$fileCollection)
49 {
50 $this->addError(new Error(
51 'File is not accessible',
53 ));
54 return new EventResult(EventResult::ERROR, null, null, $this);
55 }
56
57 return null;
58 }
59}