Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
userfieldaccess.php
1<?php
2
4
10
11abstract class UserFieldAccess
12{
13 public const SETTINGS_USER_FIELD_KEY = 'userField';
14 public const SETTINGS_ACCESS_CLASS_KEY = 'access';
15
16 protected $userId;
17
18 public function __construct(int $userId = null)
19 {
20 if($userId === null)
21 {
22 $userId = $this->getDefaultUserId();
23 }
24
25 $this->userId = $userId;
26 }
27
28 public static function getInstance(string $moduleId, int $userId = null): UserFieldAccess
29 {
30 $configuration = Configuration::getInstance($moduleId);
31
32 $value = $configuration->get(static::SETTINGS_USER_FIELD_KEY);
33 if(
34 is_array($value)
35 && isset($value[static::SETTINGS_ACCESS_CLASS_KEY])
36 && Loader::includeModule($moduleId)
37 && is_a($value[static::SETTINGS_ACCESS_CLASS_KEY], self::class, true))
38 {
39 return new $value[static::SETTINGS_ACCESS_CLASS_KEY]($userId);
40 }
41
42 throw new ObjectNotFoundException('No settings for UserFieldAccess');
43 }
44
45 protected function getDefaultUserId(): int
46 {
47 global $USER;
48 if($USER instanceof \CUser)
49 {
50 return (int) CurrentUser::get()->getId();
51 }
52
53 return 0;
54 }
55
56 public function setUserId(int $userId): UserFieldAccess
57 {
58 $this->userId = $userId;
59
60 return $this;
61 }
62
63 public function getUserId(): int
64 {
65 return $this->userId;
66 }
67
68 abstract protected function getAvailableEntityIds(): array;
69
70 public function getRestrictedTypes(): array
71 {
72 return [
73 'resourcebooking', // available in crm only
74 'mail_message', // no way to edit
75 'hlblock', // the field is not implemented yet
76 ];
77 }
78
79 public function canReadWithFilter(array $filter): bool
80 {
81 return is_array($this->prepareFilter($filter));
82 }
83
84 public function prepareFilter(array $filter = []): ?array
85 {
86 $filterEntityIds = [];
87 foreach($filter as $name => $value)
88 {
89 if(strpos($name, 'ENTITY_ID') !== false)
90 {
91 if($name === 'ENTITY_ID')
92 {
93 if(is_array($value))
94 {
95 $filterEntityIds = $value;
96 }
97 else
98 {
99 $filterEntityIds = [$value];
100 }
101 }
102
103 unset($filter[$name]);
104 }
105 }
106 $availableEntityIds = $this->getAvailableEntityIds();
107 if(empty($filterEntityIds))
108 {
109 $filterEntityIds = $availableEntityIds;
110 }
111 else
112 {
113 foreach($filterEntityIds as $key => $entityId)
114 {
115 if(!in_array($entityId, $availableEntityIds, true))
116 {
117 unset($filterEntityIds[$key]);
118 }
119 }
120 }
121
122 if(empty($filterEntityIds))
123 {
124 return null;
125 }
126
127 $filter['=ENTITY_ID'] = $filterEntityIds;
128
129 return $filter;
130 }
131
132 public function canRead(int $id): bool
133 {
134 $filter = $this->prepareFilter([
135 '=ID' => $id,
136 ]);
137
138 if(empty($filter))
139 {
140 return false;
141 }
142
143 return (UserFieldTable::getCount($filter) > 0);
144 }
145
146 public function canAdd(array $field): bool
147 {
148 $availableEntityIds = $this->getAvailableEntityIds();
149
150 return (isset($field['ENTITY_ID']) && in_array($field['ENTITY_ID'], $availableEntityIds, true));
151 }
152
153 public function canUpdate(int $id): bool
154 {
155 return $this->canRead($id);
156 }
157
158 public function canDelete(int $id): bool
159 {
160 return $this->canUpdate($id);
161 }
162}
static includeModule($moduleName)
Definition loader.php:69
static getCount($filter=array(), array $cache=array())
static getInstance(string $moduleId, int $userId=null)