6521 |
efrain |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
declare(strict_types=1);
|
|
|
4 |
|
|
|
5 |
namespace LeadersLinked\Library;
|
|
|
6 |
|
|
|
7 |
use Laminas\Db\Adapter\AdapterInterface;
|
|
|
8 |
use LeadersLinked\Mapper\MyCoachCategoryMapper;
|
|
|
9 |
use LeadersLinked\Mapper\MyCoachCategoryUserMapper;
|
|
|
10 |
use LeadersLinked\Mapper\QueryMapper;
|
|
|
11 |
use LeadersLinked\Model\MyCoachCategory;
|
|
|
12 |
use LeadersLinked\Mapper\MyCoachQuestionMapper;
|
|
|
13 |
use LeadersLinked\Mapper\MyCoachQuestionCategoryMapper;
|
|
|
14 |
use LeadersLinked\Model\MyCoachCategoryUser;
|
|
|
15 |
use LeadersLinked\Mapper\MyCoachAnswerMapper;
|
7134 |
efrain |
16 |
use LeadersLinked\Mapper\MyCoachCategoryJobDescriptionMapper;
|
|
|
17 |
use LeadersLinked\Mapper\OrganizationPositionMapper;
|
6521 |
efrain |
18 |
|
|
|
19 |
class MyCoachAccessControl
|
|
|
20 |
{
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
*
|
|
|
24 |
* @var MyCoachAccessControl
|
|
|
25 |
*/
|
|
|
26 |
private static $_instance;
|
|
|
27 |
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
*
|
|
|
31 |
* @var AdapterInterface
|
|
|
32 |
*/
|
|
|
33 |
private $adapter;
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
*
|
|
|
37 |
* @param AdapterInterface $adapter
|
|
|
38 |
*/
|
|
|
39 |
public function __construct($adapter)
|
|
|
40 |
{
|
|
|
41 |
$this->adapter = $adapter;
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
*
|
|
|
46 |
* @param AdapterInterface $adapter
|
|
|
47 |
* @return MyCoachAccessControl
|
|
|
48 |
*/
|
|
|
49 |
public static function getInstance($adapter)
|
|
|
50 |
{
|
|
|
51 |
if(self::$_instance == null) {
|
|
|
52 |
self::$_instance = new MyCoachAccessControl($adapter);
|
|
|
53 |
}
|
|
|
54 |
return self::$_instance;
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
/**
|
7134 |
efrain |
58 |
*
|
|
|
59 |
* @param int $user_id
|
|
|
60 |
* @return array
|
|
|
61 |
*/
|
|
|
62 |
private function getCategoryIdsWithAccessJobDescription($user_id)
|
|
|
63 |
{
|
|
|
64 |
$category_ids = [];
|
|
|
65 |
$job_description_ids = [];
|
|
|
66 |
$organizationPositionMapper = OrganizationPositionMapper::getInstance($this->adapter);
|
|
|
67 |
$records = $organizationPositionMapper->fetchAllByEmployeeId($user_id);
|
|
|
68 |
foreach($records as $record)
|
|
|
69 |
{
|
|
|
70 |
array_push($job_description_ids, $record->job_description_id);
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
$myCoachCategoryJobDescriptionMapper = MyCoachCategoryJobDescriptionMapper::getInstance($this->adapter);
|
|
|
74 |
$records = $myCoachCategoryJobDescriptionMapper->fetchAllByJobDescriptionIds($job_description_ids);
|
|
|
75 |
foreach ($records as $record)
|
|
|
76 |
{
|
|
|
77 |
|
|
|
78 |
if(!in_array($record->category_id, $category_ids)) {
|
|
|
79 |
array_push($category_ids, $record->category_id);
|
|
|
80 |
}
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
return $category_ids;
|
|
|
84 |
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
/**
|
6521 |
efrain |
88 |
*
|
|
|
89 |
* @param int $user_id
|
|
|
90 |
* @param int $network_id
|
|
|
91 |
* @return int[]
|
|
|
92 |
*/
|
|
|
93 |
public function getCategoryIdsWithAccess($user_id, $network_id)
|
|
|
94 |
{
|
7134 |
efrain |
95 |
$category_filter_ids = $this->getCategoryIdsWithAccessJobDescription($user_id);
|
6521 |
efrain |
96 |
|
7134 |
efrain |
97 |
|
6521 |
efrain |
98 |
$queryMapper = QueryMapper::getInstance($this->adapter);
|
|
|
99 |
|
7134 |
efrain |
100 |
if($category_filter_ids) {
|
|
|
101 |
$selectJobDescription = $queryMapper->getSql()->select();
|
|
|
102 |
$selectJobDescription->columns(['id']);
|
|
|
103 |
$selectJobDescription->from(['c' => MyCoachCategoryMapper::_TABLE]);
|
|
|
104 |
$selectJobDescription->where->equalTo('c.network_id', $network_id);
|
|
|
105 |
$selectJobDescription->where->equalTo('c.status', MyCoachCategory::STATUS_ACTIVE);
|
|
|
106 |
$selectJobDescription->where->in('id', $category_filter_ids);
|
|
|
107 |
} else {
|
|
|
108 |
$selectJobDescription = null;
|
|
|
109 |
}
|
6521 |
efrain |
110 |
|
7134 |
efrain |
111 |
|
|
|
112 |
|
|
|
113 |
|
|
|
114 |
$category_ids = [];
|
|
|
115 |
|
6521 |
efrain |
116 |
$selectPublic = $queryMapper->getSql()->select();
|
|
|
117 |
$selectPublic->columns(['id']);
|
|
|
118 |
$selectPublic->from(['c' => MyCoachCategoryMapper::_TABLE]);
|
|
|
119 |
$selectPublic->where->equalTo('c.network_id', $network_id);
|
|
|
120 |
$selectPublic->where->equalTo('c.privacy', MyCoachCategory::PRIVACY_PUBLIC);
|
|
|
121 |
$selectPublic->where->equalTo('c.status', MyCoachCategory::STATUS_ACTIVE);
|
7134 |
efrain |
122 |
|
6521 |
efrain |
123 |
|
|
|
124 |
$select = $queryMapper->getSql()->select();
|
|
|
125 |
$select->columns(['id']);
|
|
|
126 |
$select->from(['c' => MyCoachCategoryMapper::_TABLE]);
|
|
|
127 |
$select->join(['cu' => MyCoachCategoryUserMapper::_TABLE], 'c.id = cu.category_id', []);
|
|
|
128 |
$select->where->equalTo('cu.user_id', $user_id);
|
|
|
129 |
$select->where->equalTo('c.privacy', MyCoachCategory::PRIVACY_COMPANY);
|
|
|
130 |
$select->where->equalTo('c.status', MyCoachCategory::STATUS_ACTIVE);
|
|
|
131 |
$select->combine( $selectPublic );
|
7134 |
efrain |
132 |
if($selectJobDescription) {
|
|
|
133 |
$select->combine( $selectJobDescription );
|
|
|
134 |
}
|
6521 |
efrain |
135 |
|
|
|
136 |
// echo $select->getSqlString($this->adapter->platform); exit;
|
|
|
137 |
|
|
|
138 |
|
|
|
139 |
$records = $queryMapper->fetchAll($select);
|
|
|
140 |
foreach($records as $record)
|
|
|
141 |
{
|
7134 |
efrain |
142 |
if(!in_array($record['id'], $category_ids)) {
|
|
|
143 |
array_push($category_ids, $record['id']);
|
|
|
144 |
}
|
6521 |
efrain |
145 |
}
|
|
|
146 |
|
|
|
147 |
return array_unique($category_ids);
|
|
|
148 |
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
/**
|
|
|
152 |
*
|
|
|
153 |
* @param int $user_id
|
|
|
154 |
* @param int $network_id
|
|
|
155 |
* @return array
|
|
|
156 |
*/
|
|
|
157 |
public function getCategoriesWithAccessToFormSelect($user_id, $network_id)
|
|
|
158 |
{
|
7134 |
efrain |
159 |
$category_filter_ids = $this->getCategoryIdsWithAccessJobDescription($user_id);
|
|
|
160 |
|
|
|
161 |
|
|
|
162 |
$queryMapper = QueryMapper::getInstance($this->adapter);
|
|
|
163 |
|
|
|
164 |
if($category_filter_ids) {
|
|
|
165 |
$selectJobDescription = $queryMapper->getSql()->select();
|
|
|
166 |
$selectJobDescription->columns(['uuid', 'name']);
|
|
|
167 |
$selectJobDescription->from(['c' => MyCoachCategoryMapper::_TABLE]);
|
|
|
168 |
$selectJobDescription->where->equalTo('c.network_id', $network_id);
|
|
|
169 |
$selectJobDescription->where->equalTo('c.status', MyCoachCategory::STATUS_ACTIVE);
|
|
|
170 |
$selectJobDescription->where->in('id', $category_filter_ids);
|
|
|
171 |
} else {
|
|
|
172 |
$selectJobDescription = null;
|
|
|
173 |
}
|
|
|
174 |
|
6521 |
efrain |
175 |
$categories = [];
|
|
|
176 |
|
|
|
177 |
|
|
|
178 |
|
|
|
179 |
$selectPublic = $queryMapper->getSql()->select();
|
|
|
180 |
$selectPublic->columns(['uuid', 'name']);
|
|
|
181 |
$selectPublic->from(['c' => MyCoachCategoryMapper::_TABLE]);
|
|
|
182 |
$selectPublic->where->equalTo('c.network_id', $network_id);
|
|
|
183 |
$selectPublic->where->equalTo('c.privacy', MyCoachCategory::PRIVACY_PUBLIC);
|
|
|
184 |
$selectPublic->where->equalTo('c.status', MyCoachCategory::STATUS_ACTIVE);
|
|
|
185 |
|
|
|
186 |
$select = $queryMapper->getSql()->select();
|
|
|
187 |
$select->columns(['uuid', 'name']);
|
|
|
188 |
$select->from(['c' => MyCoachCategoryMapper::_TABLE]);
|
|
|
189 |
$select->join(['cu' => MyCoachCategoryUserMapper::_TABLE], 'c.id = cu.category_id', []);
|
|
|
190 |
$select->where->equalTo('cu.user_id', $user_id);
|
|
|
191 |
$select->combine( $selectPublic );
|
|
|
192 |
|
|
|
193 |
// echo $select->getSqlString($this->adapter->platform); exit;
|
|
|
194 |
|
|
|
195 |
|
|
|
196 |
$records = $queryMapper->fetchAll($select);
|
|
|
197 |
foreach($records as $record)
|
|
|
198 |
{
|
7134 |
efrain |
199 |
if(!isset( $categories[ $record['uuid'] ] )) {
|
|
|
200 |
$categories[ $record['uuid'] ] = $record['name'];
|
|
|
201 |
}
|
6521 |
efrain |
202 |
}
|
|
|
203 |
|
|
|
204 |
|
|
|
205 |
uksort($categories, function ($a, $b) {
|
|
|
206 |
return $a <=> $b;
|
|
|
207 |
});
|
|
|
208 |
|
|
|
209 |
|
|
|
210 |
return $categories;
|
|
|
211 |
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
|
|
|
215 |
/**
|
|
|
216 |
*
|
|
|
217 |
* @param int $user_id
|
|
|
218 |
* @return int[]
|
|
|
219 |
*/
|
|
|
220 |
public function getCategoryIdsWithEdition($user_id)
|
|
|
221 |
{
|
|
|
222 |
$category_ids = [];
|
|
|
223 |
|
|
|
224 |
$queryMapper = QueryMapper::getInstance($this->adapter);
|
|
|
225 |
|
|
|
226 |
|
|
|
227 |
$select = $queryMapper->getSql()->select();
|
|
|
228 |
$select->columns(['id']);
|
|
|
229 |
$select->from(['c' => MyCoachCategoryMapper::_TABLE]);
|
|
|
230 |
$select->join(['cu' => MyCoachCategoryUserMapper::_TABLE], 'c.id = cu.category_id', []);
|
|
|
231 |
$select->where->equalTo('cu.user_id', $user_id);
|
6547 |
efrain |
232 |
//$select->where->equalTo('c.privacy', MyCoachCategory::PRIVACY_COMPANY);
|
6521 |
efrain |
233 |
$select->where->equalTo('c.status', MyCoachCategory::STATUS_ACTIVE);
|
|
|
234 |
$select->where->in('cu.role', [MyCoachCategoryUser::ROLE_EDITOR, MyCoachCategoryUser::ROLE_ADMINISTRATOR]);
|
|
|
235 |
|
|
|
236 |
|
6547 |
efrain |
237 |
//echo $select->getSqlString($this->adapter->platform); exit;
|
6521 |
efrain |
238 |
|
|
|
239 |
|
|
|
240 |
$records = $queryMapper->fetchAll($select);
|
|
|
241 |
foreach($records as $record)
|
|
|
242 |
{
|
|
|
243 |
array_push($category_ids, $record['id']);
|
|
|
244 |
}
|
|
|
245 |
|
|
|
246 |
return array_unique($category_ids);
|
|
|
247 |
|
|
|
248 |
}
|
|
|
249 |
|
|
|
250 |
/**
|
|
|
251 |
*
|
|
|
252 |
* @param int $user_id
|
|
|
253 |
* @param int|string $category_id
|
|
|
254 |
* @param int $network_id
|
|
|
255 |
* @param string $message_error
|
|
|
256 |
* return boolean
|
|
|
257 |
*/
|
|
|
258 |
public function hasAccessForCategory($user_id, $category_id, $network_id, &$message_error)
|
|
|
259 |
{
|
|
|
260 |
$myCoachCategoryMapper = MyCoachCategoryMapper::getInstance($this->adapter);
|
|
|
261 |
|
|
|
262 |
if(is_numeric($category_id)) {
|
|
|
263 |
$myCoachCategory = $myCoachCategoryMapper->fetchOneByIdAndNetworkId($category_id, $network_id);
|
|
|
264 |
} else {
|
|
|
265 |
$myCoachCategory = $myCoachCategoryMapper->fetchOneByUuidAndNetworkId($category_id, $network_id);
|
|
|
266 |
}
|
|
|
267 |
|
|
|
268 |
if(!$myCoachCategory) {
|
|
|
269 |
$message_error ='ERROR_MY_COACH_CATEGORY_NOT_FOUND';
|
|
|
270 |
return false;
|
|
|
271 |
}
|
|
|
272 |
|
|
|
273 |
if($myCoachCategory->status == MyCoachCategory::STATUS_INACTIVE) {
|
|
|
274 |
$message_error = 'ERROR_MY_COACH_CATEGORY_IS_INACTIVE';
|
|
|
275 |
return false;
|
|
|
276 |
}
|
|
|
277 |
|
|
|
278 |
if($myCoachCategory->privacy == MyCoachCategory::PRIVACY_COMPANY) {
|
|
|
279 |
|
|
|
280 |
$myCoachCategoryUserMapper = MyCoachCategoryUserMapper::getInstance($this->adapter);
|
|
|
281 |
$myCoachCategoryUser = $myCoachCategoryUserMapper->fetchOneByCategoryIdAndUserId($myCoachCategory->id, $user_id);
|
|
|
282 |
|
|
|
283 |
if(!$myCoachCategoryUser) {
|
|
|
284 |
$message_error = 'ERROR_MY_COACH_CATEGORY_UNAUTHORIZED';
|
|
|
285 |
return false;
|
|
|
286 |
}
|
|
|
287 |
}
|
|
|
288 |
|
|
|
289 |
return true;
|
|
|
290 |
}
|
|
|
291 |
|
|
|
292 |
/**
|
|
|
293 |
*
|
|
|
294 |
* @param int $user_id
|
|
|
295 |
* @param int|string $question_id
|
|
|
296 |
* @param int $network_id
|
|
|
297 |
* @param string $message_error
|
|
|
298 |
* return boolean
|
|
|
299 |
*/
|
|
|
300 |
public function hasAccessViewQuestion($user_id, $question_id, $network_id, &$message_error)
|
|
|
301 |
{
|
|
|
302 |
|
|
|
303 |
$myCoachQuestionMapper = MyCoachQuestionMapper::getInstance($this->adapter);
|
|
|
304 |
|
|
|
305 |
if(is_numeric($question_id)) {
|
|
|
306 |
$myCoachQuestion = $myCoachQuestionMapper->fetchOneByIdAndNetworkId($question_id, $network_id);
|
|
|
307 |
|
|
|
308 |
} else {
|
|
|
309 |
$myCoachQuestion = $myCoachQuestionMapper->fetchOneByUuidAndNetworkId($question_id, $network_id);
|
|
|
310 |
|
|
|
311 |
}
|
|
|
312 |
|
|
|
313 |
|
|
|
314 |
|
|
|
315 |
if(!$myCoachQuestion) {
|
|
|
316 |
$message_error = 'ERROR_MY_COACH_QUESTION_NOT_FOUND';
|
|
|
317 |
return false;
|
|
|
318 |
}
|
|
|
319 |
|
|
|
320 |
|
|
|
321 |
$category_ids = $this->getCategoryIdsWithAccess($user_id, $network_id);
|
|
|
322 |
|
|
|
323 |
$myCoachQuestionCategoryMapper = MyCoachQuestionCategoryMapper::getInstance($this->adapter);
|
|
|
324 |
$records = $myCoachQuestionCategoryMapper->fetchAllByQuestionId($myCoachQuestion->id);
|
|
|
325 |
|
|
|
326 |
foreach($records as $record)
|
|
|
327 |
{
|
|
|
328 |
if(in_array($record->category_id, $category_ids)) {
|
|
|
329 |
return true;
|
|
|
330 |
}
|
|
|
331 |
}
|
|
|
332 |
|
|
|
333 |
|
|
|
334 |
$message_error = 'ERROR_MY_COACH_QUESTION_UNAUTHORIZED';
|
|
|
335 |
return false;
|
|
|
336 |
}
|
|
|
337 |
|
|
|
338 |
|
|
|
339 |
/**
|
|
|
340 |
*
|
|
|
341 |
* @param int $user_id
|
|
|
342 |
* @param int|string $question_id
|
|
|
343 |
* @param int $network_id
|
|
|
344 |
* @param string $message_error
|
|
|
345 |
* return boolean
|
|
|
346 |
*/
|
|
|
347 |
public function hasAccessEditQuestion($user_id, $question_id, $network_id, &$message_error)
|
|
|
348 |
{
|
|
|
349 |
|
|
|
350 |
$myCoachQuestionMapper = MyCoachQuestionMapper::getInstance($this->adapter);
|
|
|
351 |
|
|
|
352 |
if(is_numeric($question_id)) {
|
|
|
353 |
$myCoachQuestion = $myCoachQuestionMapper->fetchOneByIdAndNetworkId($question_id, $network_id);
|
|
|
354 |
} else {
|
|
|
355 |
$myCoachQuestion = $myCoachQuestionMapper->fetchOneByUuidAndNetworkId($question_id, $network_id);
|
|
|
356 |
}
|
|
|
357 |
|
|
|
358 |
|
|
|
359 |
|
|
|
360 |
|
|
|
361 |
if(!$myCoachQuestion) {
|
|
|
362 |
$message_error = 'ERROR_MY_COACH_QUESTION_NOT_FOUND';
|
|
|
363 |
return false;
|
|
|
364 |
}
|
|
|
365 |
|
|
|
366 |
|
|
|
367 |
if($myCoachQuestion->user_id == $user_id) {
|
|
|
368 |
|
|
|
369 |
$myCoachAnswerMapper = MyCoachAnswerMapper::getInstance($this->adapter);
|
|
|
370 |
$total = $myCoachAnswerMapper->fetchCountByMyCoachQuestionId($myCoachQuestion->id);
|
|
|
371 |
|
|
|
372 |
return $total == 0 ? true : false;
|
|
|
373 |
}
|
|
|
374 |
|
|
|
375 |
$message_error = 'ERROR_MY_COACH_QUESTION_UNAUTHORIZED';
|
|
|
376 |
return false;
|
|
|
377 |
}
|
|
|
378 |
|
|
|
379 |
|
|
|
380 |
/**
|
|
|
381 |
*
|
|
|
382 |
* @param int $user_id
|
|
|
383 |
* @param int|string $question_id
|
|
|
384 |
* @param int $network_id
|
|
|
385 |
* @param string $message_error
|
|
|
386 |
* return boolean
|
|
|
387 |
*/
|
|
|
388 |
public function hasAccessDeleteQuestion($user_id, $question_id, $network_id, &$message_error)
|
|
|
389 |
{
|
|
|
390 |
|
|
|
391 |
$myCoachQuestionMapper = MyCoachQuestionMapper::getInstance($this->adapter);
|
|
|
392 |
|
|
|
393 |
if(is_numeric($question_id)) {
|
|
|
394 |
$myCoachQuestion = $myCoachQuestionMapper->fetchOneByIdAndNetworkId($question_id, $network_id);
|
|
|
395 |
} else {
|
|
|
396 |
$myCoachQuestion = $myCoachQuestionMapper->fetchOneByUuidAndNetworkId($question_id, $network_id);
|
|
|
397 |
}
|
|
|
398 |
|
|
|
399 |
|
|
|
400 |
|
|
|
401 |
|
|
|
402 |
if(!$myCoachQuestion) {
|
|
|
403 |
$message_error = 'ERROR_MY_COACH_QUESTION_NOT_FOUND';
|
|
|
404 |
return false;
|
|
|
405 |
}
|
|
|
406 |
|
|
|
407 |
$category_ids = $this->getCategoryIdsWithEdition($user_id);
|
|
|
408 |
|
|
|
409 |
$myCoachQuestionCategoryMapper = MyCoachQuestionCategoryMapper::getInstance($this->adapter);
|
|
|
410 |
$records = $myCoachQuestionCategoryMapper->fetchAllByQuestionId($myCoachQuestion->id);
|
|
|
411 |
|
|
|
412 |
foreach($records as $record)
|
|
|
413 |
{
|
|
|
414 |
if(in_array($record->category_id, $category_ids)) {
|
|
|
415 |
return true;
|
|
|
416 |
}
|
|
|
417 |
}
|
|
|
418 |
|
|
|
419 |
if($myCoachQuestion->user_id == $user_id) {
|
|
|
420 |
$myCoachAnswerMapper = MyCoachAnswerMapper::getInstance($this->adapter);
|
|
|
421 |
$total = $myCoachAnswerMapper->fetchCountByMyCoachQuestionId($myCoachQuestion->id);
|
|
|
422 |
|
|
|
423 |
return $total == 0 ? true : false;
|
|
|
424 |
}
|
|
|
425 |
|
|
|
426 |
|
|
|
427 |
|
|
|
428 |
|
|
|
429 |
$message_error = 'ERROR_MY_COACH_QUESTION_UNAUTHORIZED';
|
|
|
430 |
return false;
|
|
|
431 |
}
|
6547 |
efrain |
432 |
|
6521 |
efrain |
433 |
|
6547 |
efrain |
434 |
|
|
|
435 |
|
6521 |
efrain |
436 |
/**
|
|
|
437 |
*
|
|
|
438 |
* @param int $user_id
|
|
|
439 |
* @param int|string $question_id
|
|
|
440 |
* @param int $network_id
|
|
|
441 |
* @param string $message_error
|
|
|
442 |
* return boolean
|
|
|
443 |
*/
|
|
|
444 |
public function hasAccessAnswerQuestion($user_id, $question_id, $network_id, &$message_error)
|
|
|
445 |
{
|
|
|
446 |
|
|
|
447 |
|
|
|
448 |
$myCoachQuestionMapper = MyCoachQuestionMapper::getInstance($this->adapter);
|
|
|
449 |
|
|
|
450 |
if(is_numeric($question_id)) {
|
|
|
451 |
$myCoachQuestion = $myCoachQuestionMapper->fetchOneByIdAndNetworkId($question_id, $network_id);
|
|
|
452 |
} else {
|
|
|
453 |
$myCoachQuestion = $myCoachQuestionMapper->fetchOneByUuidAndNetworkId($question_id, $network_id);
|
|
|
454 |
|
|
|
455 |
}
|
|
|
456 |
|
6547 |
efrain |
457 |
|
|
|
458 |
|
|
|
459 |
|
6521 |
efrain |
460 |
if(!$myCoachQuestion) {
|
|
|
461 |
$message_error = 'ERROR_MY_COACH_QUESTION_NOT_FOUND';
|
|
|
462 |
return false;
|
|
|
463 |
}
|
|
|
464 |
|
|
|
465 |
|
|
|
466 |
$category_ids = $this->getCategoryIdsWithEdition($user_id);
|
|
|
467 |
|
|
|
468 |
|
|
|
469 |
$myCoachQuestionCategoryMapper = MyCoachQuestionCategoryMapper::getInstance($this->adapter);
|
|
|
470 |
$records = $myCoachQuestionCategoryMapper->fetchAllByQuestionId($myCoachQuestion->id);
|
|
|
471 |
|
6547 |
efrain |
472 |
|
6521 |
efrain |
473 |
foreach($records as $record)
|
|
|
474 |
{
|
|
|
475 |
if(in_array($record->category_id, $category_ids)) {
|
|
|
476 |
return true;
|
|
|
477 |
}
|
|
|
478 |
}
|
|
|
479 |
|
|
|
480 |
|
|
|
481 |
$message_error = 'ERROR_MY_COACH_QUESTION_UNAUTHORIZED';
|
|
|
482 |
return false;
|
|
|
483 |
}
|
|
|
484 |
|
|
|
485 |
/**
|
|
|
486 |
*
|
|
|
487 |
* @param int $user_id
|
|
|
488 |
* @param int|string $answer_id
|
|
|
489 |
* @param int $network_id
|
|
|
490 |
* @param string $message_error
|
|
|
491 |
* return boolean
|
|
|
492 |
*/
|
|
|
493 |
public function hasAccessViewAnswer($user_id, $answer_id, $network_id, &$message_error)
|
|
|
494 |
{
|
|
|
495 |
|
|
|
496 |
$myCoachAnswerMapper = MyCoachAnswerMapper::getInstance($this->adapter);
|
|
|
497 |
if(is_numeric($answer_id)) {
|
|
|
498 |
$myCoachAnswer = $myCoachAnswerMapper->fetchOne($answer_id);
|
|
|
499 |
} else {
|
|
|
500 |
$myCoachAnswer = $myCoachAnswerMapper->fetchOneByUuid($answer_id);
|
|
|
501 |
|
|
|
502 |
}
|
|
|
503 |
|
|
|
504 |
|
|
|
505 |
|
|
|
506 |
if(!$myCoachAnswer) {
|
|
|
507 |
$message_error = 'ERROR_MY_COACH_ANSWER_NOT_FOUND';
|
|
|
508 |
return false;
|
|
|
509 |
}
|
|
|
510 |
|
|
|
511 |
$category_ids = $this->getCategoryIdsWithAccess($user_id, $network_id);
|
|
|
512 |
|
|
|
513 |
|
|
|
514 |
|
|
|
515 |
$myCoachQuestionCategoryMapper = MyCoachQuestionCategoryMapper::getInstance($this->adapter);
|
|
|
516 |
$records = $myCoachQuestionCategoryMapper->fetchAllByQuestionId($myCoachAnswer->question_id);
|
|
|
517 |
|
|
|
518 |
foreach($records as $record)
|
|
|
519 |
{
|
|
|
520 |
if(in_array($record->category_id, $category_ids)) {
|
|
|
521 |
return true;
|
|
|
522 |
}
|
|
|
523 |
}
|
|
|
524 |
|
|
|
525 |
|
|
|
526 |
$message_error = 'ERROR_MY_COACH_QUESTION_UNAUTHORIZED';
|
|
|
527 |
return false;
|
|
|
528 |
}
|
|
|
529 |
|
|
|
530 |
|
|
|
531 |
/**
|
|
|
532 |
*
|
|
|
533 |
* @param int $user_id
|
|
|
534 |
* @param int|string $answer_id
|
|
|
535 |
* @param int $network_id
|
|
|
536 |
* @param string $message_error
|
|
|
537 |
* return boolean
|
|
|
538 |
*/
|
|
|
539 |
public function hasAccessEditOrDeleteAnswer($user_id, $answer_id, $network_id, &$message_error)
|
|
|
540 |
{
|
|
|
541 |
$myCoachAnswerMapper = MyCoachAnswerMapper::getInstance($this->adapter);
|
|
|
542 |
|
|
|
543 |
if(is_numeric($answer_id)) {
|
|
|
544 |
$myCoachAnswer = $myCoachAnswerMapper->fetchOne($answer_id);
|
|
|
545 |
} else {
|
|
|
546 |
$myCoachAnswer = $myCoachAnswerMapper->fetchOneByUuid($answer_id);
|
|
|
547 |
}
|
6547 |
efrain |
548 |
|
6521 |
efrain |
549 |
|
|
|
550 |
if(!$myCoachAnswer) {
|
|
|
551 |
$message_error = 'ERROR_MY_COACH_ANSWER_NOT_FOUND';
|
|
|
552 |
return false;
|
|
|
553 |
}
|
|
|
554 |
|
|
|
555 |
|
|
|
556 |
$myCoachQuestionMapper = MyCoachQuestionMapper::getInstance($this->adapter);
|
6547 |
efrain |
557 |
$myCoachQuestion = $myCoachQuestionMapper->fetchOneByIdAndNetworkId($myCoachAnswer->question_id, $network_id);
|
6521 |
efrain |
558 |
|
|
|
559 |
|
|
|
560 |
$category_ids = $this->getCategoryIdsWithEdition($user_id);
|
|
|
561 |
|
|
|
562 |
$myCoachCategoryUserMapper = MyCoachCategoryUserMapper::getInstance($this->adapter);
|
|
|
563 |
$myCoachQuestionCategoryMapper = MyCoachQuestionCategoryMapper::getInstance($this->adapter);
|
|
|
564 |
$records = $myCoachQuestionCategoryMapper->fetchAllByQuestionId($myCoachQuestion->id);
|
6547 |
efrain |
565 |
|
6521 |
efrain |
566 |
$ok = false;
|
|
|
567 |
$role = MyCoachCategoryUser::ROLE_USER;
|
|
|
568 |
foreach($records as $record)
|
|
|
569 |
{
|
|
|
570 |
if(in_array($record->category_id, $category_ids)) {
|
|
|
571 |
$ok = true;
|
|
|
572 |
|
|
|
573 |
$myCoachCategoryUser = $myCoachCategoryUserMapper->fetchOneByCategoryIdAndUserId($record->category_id, $user_id);
|
|
|
574 |
if($myCoachCategoryUser) {
|
|
|
575 |
|
|
|
576 |
if($myCoachCategoryUser->role == MyCoachCategoryUser::ROLE_EDITOR && $role == MyCoachCategoryUser::ROLE_USER)
|
|
|
577 |
{
|
|
|
578 |
$role = MyCoachCategoryUser::ROLE_EDITOR;
|
|
|
579 |
}
|
|
|
580 |
if($myCoachCategoryUser->role == MyCoachCategoryUser::ROLE_ADMINISTRATOR && $role != MyCoachCategoryUser::ROLE_ADMINISTRATOR)
|
|
|
581 |
{
|
|
|
582 |
$role = MyCoachCategoryUser::ROLE_ADMINISTRATOR;
|
|
|
583 |
}
|
|
|
584 |
}
|
|
|
585 |
|
|
|
586 |
}
|
|
|
587 |
}
|
|
|
588 |
|
6547 |
efrain |
589 |
if(!$ok) {
|
6521 |
efrain |
590 |
$message_error = 'ERROR_MY_COACH_QUESTION_UNAUTHORIZED';
|
|
|
591 |
return false;
|
|
|
592 |
}
|
|
|
593 |
|
|
|
594 |
if($myCoachAnswer->user_id == $user_id || $role == MyCoachCategoryUser::ROLE_ADMINISTRATOR) {
|
|
|
595 |
return true;
|
|
|
596 |
} else {
|
|
|
597 |
$message_error = 'ERROR_MY_COACH_ANSWER_UNAUTHORIZED';
|
|
|
598 |
return false;
|
|
|
599 |
|
|
|
600 |
}
|
|
|
601 |
}
|
|
|
602 |
}
|