Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
rightsmanager.php
1<?php
2namespace Bitrix\Report;
3
6use CPHPCache;
7
9{
10 const ACCESS_READ = 'access_read';
11 const ACCESS_EDIT = 'access_edit';
12 const ACCESS_FULL = 'access_full';
13
14 protected $userId;
15
16 public function __construct($userId)
17 {
18 $this->userId = intval($userId);
19 }
20
26 public function canRead($reportId)
27 {
28 if(!$this->isOwner($reportId))
29 {
30 $listSharing = Sharing::getEntityOfSharing($reportId);
31 $listEntity = $this->getGroupsAndDepartments();
32 foreach($listSharing as $sharingRow)
33 {
34 if(in_array($sharingRow['ENTITY'], $listEntity))
35 {
36 if($this->compareAccess($sharingRow['RIGHTS'], self::ACCESS_READ) >= 0)
37 return true;
38 }
39 }
40 return false;
41 }
42 return true;
43 }
44
50 public function canEdit($reportId)
51 {
52 if(!$this->isOwner($reportId))
53 {
54 $listSharing = Sharing::getEntityOfSharing($reportId);
55 $listEntity = $this->getGroupsAndDepartments();
56 foreach($listSharing as $sharingRow)
57 {
58 if(in_array($sharingRow['ENTITY'], $listEntity))
59 {
60 if($this->compareAccess($sharingRow['RIGHTS'], self::ACCESS_EDIT) >= 0)
61 return true;
62 }
63 }
64 return false;
65 }
66 return true;
67 }
68
74 public function canDelete($reportId)
75 {
76 return $this->isOwner($reportId);
77 }
78
84 public function canShare($reportId)
85 {
86 return $this->isOwner($reportId);
87 }
88
95 public function getGroupsAndDepartments()
96 {
97 $cacheTime = defined('BX_COMP_MANAGED_CACHE') ? 3153600 : 3600*4;
98 $cacheId = 'report-rights-'.$this->userId;
99 $cacheDir = '/report/rights/'.$this->userId;
100 $cache = new CPHPCache;
101 if($cache->initCache($cacheTime, $cacheId, $cacheDir))
102 {
103 $listEntity = $cache->getVars();
104 }
105 else
106 {
107 global $CACHE_MANAGER;
108 $cache->startDataCache();
109 $listEntity = array(Sharing::CODE_USER.$this->userId);
110 $userObject = \CUser::getByID($this->userId);
111 if($userData = $userObject->fetch())
112 {
113 if(Loader::includeModule('socialnetwork'))
114 {
115 $queryObject = UserToGroupTable::getList(array(
116 'select' => array('GROUP_ID'),
117 'filter' => array(
118 'USER_ID' => $userData['ID'],
119 'ROLE' => array(
120 UserToGroupTable::ROLE_USER,
121 UserToGroupTable::ROLE_MODERATOR,
122 UserToGroupTable::ROLE_OWNER
123 )
124 )
125 ));
126 while ($groupData = $queryObject->fetch())
127 $listEntity[] = Sharing::CODE_SOCNET_GROUP.$groupData['GROUP_ID'];
128 }
129
130 if(!empty($userData['UF_DEPARTMENT']))
131 {
132 $parentDepartmentList = array();
133 foreach($userData['UF_DEPARTMENT'] as $departmentId)
134 $parentDepartmentList[] = \CIntranetUtils::getIBlockTopSection($departmentId);
135
136 $childrenDepartmentList = \CIntranetUtils::getIBlockSectionChildren($parentDepartmentList);
137 foreach($childrenDepartmentList as $departmentId)
138 $listEntity[] = Sharing::CODE_DEPARTMENT.$departmentId;
139 }
140 }
141 $CACHE_MANAGER->startTagCache($cacheDir);
142 $CACHE_MANAGER->registerTag("sonet_user2group_U".$this->userId);
143 $CACHE_MANAGER->registerTag("USER_CARD_".intval($this->userId/TAGGED_user_card_size));
144 $CACHE_MANAGER->endTagCache();
145 $cache->endDataCache($listEntity);
146 }
147
148 return $listEntity;
149 }
150
151 private function isOwner($reportId)
152 {
153 $reportId = intval($reportId);
154
155 $hasReport = ReportTable::getCount(
156 array('=ID' => $reportId, '=CREATED_BY' => $this->userId));
157
158 if($hasReport)
159 return true;
160 else
161 return false;
162 }
163
171 private function compareAccess($access1, $access2)
172 {
173 switch($access1)
174 {
175 case 'access_read':
177 $access1Pos = 2;
178 break;
179 case 'access_edit':
181 $access1Pos = 3;
182 break;
183 case 'access_full':
185 $access1Pos = 4;
186 break;
187 default:
188 $access1Pos = -1;
189 }
190 switch($access2)
191 {
192 case 'access_read':
194 $access2Pos = 2;
195 break;
196 case 'access_edit':
198 $access2Pos = 3;
199 break;
200 case 'access_full':
202 $access2Pos = 4;
203 break;
204 default:
205 $access2Pos = -1;
206 }
207
208 if($access1Pos == $access2Pos)
209 return 0;
210
211 return $access1Pos > $access2Pos? 1 : -1;
212 }
213}
static getEntityOfSharing($reportId)
Definition sharing.php:113