1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
user.php
См. документацию.
1<?php
2namespace Bitrix\Forum;
3
4use Bitrix\Forum;
5use Bitrix\Main;
6use Bitrix\Main\Error;
7use Bitrix\Main\Localization\Loc;
8use Bitrix\Main\ORM\EntityError;
9use Bitrix\Main\ORM\Event;
10use Bitrix\Main\ORM\Fields\FieldError;
11use Bitrix\Main\Result;
12use Bitrix\Main\Type\DateTime;
13
14
15Loc::loadMessages(__FILE__);
16
60class UserTable extends Main\Entity\DataManager
61{
67 public static function getTableName()
68 {
69 return 'b_forum_user';
70 }
71
77 public static function getMap()
78 {
79 return array(
80 'ID' => array(
81 'data_type' => 'integer',
82 'primary' => true,
83 'autocomplete' => true,
84 ),
85 'USER_ID' => array(
86 'data_type' => 'integer',
87 'required' => true
88 ),
89 'USER' => array(
90 'data_type' => 'Bitrix\Main\UserTable',
91 'reference' => array(
92 '=this.USER_ID' => 'ref.ID'
93 )
94 ),
95 'DESCRIPTION' => array(
96 'data_type' => 'string',
97 ),
98 'AVATAR' => array(
99 'data_type' => 'integer'
100 ),
101 'POINTS' => array(
102 'data_type' => 'integer'
103 ),
104 'RANK_ID' => array(
105 'data_type' => 'integer'
106 ),
107 'NUM_POSTS' => array(
108 'data_type' => 'integer'
109 ),
110 'INTERESTS' => array(
111 'data_type' => 'text'
112 ),
113 'LAST_POST' => array(
114 'data_type' => 'integer'
115 ),
116 'SIGNATURE' => array(
117 'data_type' => 'string'
118 ),
119 'IP_ADDRESS' => array(
120 'data_type' => 'string',
121 'size' => 255
122 ),
123 'REAL_IP_ADDRESS' => array(
124 'data_type' => 'string',
125 'size' => 255
126 ),
127 'DATE_REG' => array(
128 'data_type' => 'datetime',
129 'required' => true,
130 'default_value' => function(){return new DateTime();}
131 ),
132 'LAST_VISIT' => array(
133 'data_type' => 'datetime',
134 'required' => true,
135 'default_value' => function(){return new DateTime();}
136 ),
137 'ALLOW_POST' => array(
138 'data_type' => "boolean",
139 'values' => array("N", "Y"),
140 'default_value' => "Y"
141 ),
142 'SHOW_NAME' => array(
143 'data_type' => "boolean",
144 'values' => array("N", "Y"),
145 'default_value' => "Y"
146 ),
147 'HIDE_FROM_ONLINE' => array(
148 'data_type' => "boolean",
149 'values' => array("N", "Y"),
150 'default_value' => "N"
151 ),
152 'SUBSC_GROUP_MESSAGE' => array(
153 'data_type' => "boolean",
154 'values' => array("N", "Y"),
155 'default_value' => "N"
156 ),
157 'SUBSC_GET_MY_MESSAGE' => array(
158 'data_type' => "boolean",
159 'values' => array("N", "Y"),
160 'default_value' => "Y"
161 )
162 );
163 }
164 public static function onBeforeAdd(Event $event)
165 {
168 $data = $event->getParameter("fields");
170 "select" => ["ID"],
171 "filter" => ["USER_ID" => $data["USER_ID"]]
172 ])->fetch())
173 {
174 $result->addError(new EntityError("Error: User is already exists.", "event"));
175 return $result;
176 }
177
178 return self::modifyData($event, $result);
179 }
180
181 public static function onBeforeUpdate(Main\ORM\Event $event)
182 {
184
185 return self::modifyData($event, $result);
186 }
187
188 private static function modifyData(Main\ORM\Event $event, Main\ORM\EventResult $result)
189 {
190 $data = array_merge($event->getParameter("fields"), $result->getModified());
191 $fields = [];
192
193 //region check image
194 if (array_key_exists("AVATAR", $data))
195 {
196 \CFile::ResizeImage($data["AVATAR"], array(
197 "width" => Main\Config\Option::get("forum", "avatar_max_width", 100),
198 "height" => Main\Config\Option::get("forum", "avatar_max_height", 100)));
199 $maxSize = Main\Config\Option::get("forum", "file_max_size", 5242880);
200 if ($str = \CFile::CheckImageFile($data["AVATAR"], $maxSize))
201 {
202 $result->addError(new FieldError(static::getEntity()->getField("AVATAR"), $str));
203 }
204 else
205 {
206 $fields["AVATAR"] = $data["AVATAR"];
207 $fields["AVATAR"]["MODULE_ID"] = "forum";
208 if ($id = $event->getParameter("id"))
209 {
210 $id = is_integer($id) ? $id : $id["ID"];
211 if ($id > 0 && ($res = UserTable::getById($id)->fetch()) && ($res["AVATAR"] > 0))
212 {
213 $fields["AVATAR"]["old_file"] = $res["AVATAR"];
214 }
215 }
216 if (!\CFile::SaveForDB($fields, "AVATAR", "forum/avatar"))
217 {
218 $result->unsetField("AVATAR");
219 }
220 }
221 }
222 //endregion
223 if (!empty($fields))
224 {
225 $result->modifyFields(array_merge($result->getModified(), $fields));
226 }
227 return $result;
228 }
229
230 public static function onBeforeDelete(Main\ORM\Event $event)
231 {
233 $id = $event->getParameter("id");
234 $id = $id["ID"];
235 if (($events = GetModuleEvents("forum", "onBeforeUserDelete", true)) && !empty($events))
236 {
237 foreach ($events as $ev)
238 {
239 if (ExecuteModuleEventEx($ev, array($id)) === false)
240 {
241 $result->addError(new EntityError("Error: ".serialize($ev), "event"));
242 return $result;
243 }
244 }
245 }
246 if (($user = UserTable::getById($id)->fetch()) && $user["AVATAR"] > 0)
247 {
248 \CFile::Delete($user["AVATAR"]);
249 }
250 return $result;
251 }
252
253 public static function onAfterDelete(Main\ORM\Event $event)
254 {
255 $id = $event->getParameter("id");
256 $id = $id["ID"];
257 foreach(GetModuleEvents("forum", "onAfterUserDelete", true) as $arEvent)
258 {
259 ExecuteModuleEventEx($arEvent, [$id]);
260 }
261 }
262}
263
264class User implements \ArrayAccess {
266 use Internals\EntityBaseMethods;
268 protected $id = 0;
270 protected $data = [];
272 protected $forumUserId = null;
274 protected $locked = false;
276 protected $groups;
277
279 protected $permissions = [];
281 private $editOwn = false;
282
283 protected function __construct($id)
284 {
285 $this->data = [
286 'VISIBLE_NAME' => 'Guest',
287 'ALLOW_POST' => 'Y',
288 'SHOW_NAME' => 'Y',
289 ];
290 if ($id > 0)
291 {
292 $user = UserTable::getList([
293 'select' => [
294 'ID', 'USER_ID', 'POINTS', 'NUM_POSTS', 'LAST_POST', 'ALLOW_POST', 'SHOW_NAME',
295 'ACTIVE' => 'USER.ACTIVE',
296 'NAME' => 'USER.NAME',
297 'SECOND_NAME' => 'USER.SECOND_NAME',
298 'LAST_NAME' => 'USER.LAST_NAME',
299 'LOGIN' => 'USER.LOGIN'
300 ],
301 'filter' => ['USER_ID' => (int)$id],
302 'limit' => 1,
303 ])->fetch();
304 if ($user)
305 {
306 $this->forumUserId = $user['ID'];
307 $this->id = $user['USER_ID'];
308 $this->locked = ($user['ACTIVE'] !== 'Y' || $user['ALLOW_POST'] !== 'Y');
309 }
310 elseif ($user = Main\UserTable::getList([
311 'select' => ['ID', 'ACTIVE', 'NAME', 'SECOND_NAME', 'LAST_NAME', 'LOGIN'],
312 'filter' => ['ID' => (int)$id],
313 'limit' => 1,
314 ])->fetch())
315 {
316 $this->id = $user['ID'];
317 $this->locked = ($user['ACTIVE'] !== 'Y');
318
319 $user['ALLOW_POST'] = 'Y';
320 $user['SHOW_NAME'] = (\COption::GetOptionString('forum', 'USER_SHOW_NAME', 'Y') == 'Y' ? 'Y' : 'N');
321 }
322 else
323 {
324 throw new Main\ObjectNotFoundException('User was not found.');
325 }
326 $this->data = $user;
327 $this->data['ALLOW_POST'] = (($this->data['ALLOW_POST'] ?? 'Y') === 'N' ? 'N' : 'Y');
328 if (empty($this->data['SHOW_NAME']))
329 $this->data['SHOW_NAME'] = \COption::GetOptionString('forum', 'USER_SHOW_NAME', 'Y');
330 $this->data['SHOW_NAME'] = $this->data['SHOW_NAME'] == 'N' ? 'N' : 'Y';
331 $this->data['VISIBLE_NAME'] = ($this->data['SHOW_NAME'] === 'Y' ? \CUser::FormatName(\CSite::getNameFormat(false), $user, true, false) : $this->data['LOGIN']);
332 $this->editOwn = (\COption::GetOptionString('forum', 'USER_EDIT_OWN_POST', 'Y') == 'Y');
333 }
334 }
335
338 public function getName()
339 {
340 return $this->data["VISIBLE_NAME"];
341 }
342
343 public function setLastVisit(): static
344 {
345 if ($this->getId() <= 0)
346 {
347 return $this;
348 }
349
351 $helper = $connection->getSqlHelper();
352 $tableName = UserTable::getTableName();
353 $update = $helper->prepareUpdate($tableName, ['LAST_VISIT' => new Main\DB\SqlExpression($helper->getCurrentDateTimeFunction())]);
354 $where = $helper->prepareAssignment($tableName, 'USER_ID', $this->getId());
355 $sql = 'UPDATE '.$helper->quote($tableName).' SET '.$update[0].' WHERE '.$where;
356 $connection->queryExecute($sql, $update[1]);
357 if ($connection->getAffectedRowsCount() <= 0)
358 {
359 $merge = $helper->prepareMerge(
360 'b_forum_user',
361 array('USER_ID'),
362 array(
363 'SHOW_NAME' => ($this->data['SHOW_NAME'] === 'N' ? 'N' : 'Y'),
364 'ALLOW_POST' => ($this->data['ALLOW_POST'] === 'N' ? 'N' : 'Y'),
365 'USER_ID' => $this->getId(),
366 'DATE_REG' => new Main\DB\SqlExpression($helper->getCurrentDateTimeFunction()),
367 'LAST_VISIT' => new Main\DB\SqlExpression($helper->getCurrentDateTimeFunction())
368 ),
369 array(
370 'LAST_VISIT' => new Main\DB\SqlExpression($helper->getCurrentDateTimeFunction())
371 )
372 );
373 if ($merge[0] != '')
374 {
375 $connection->query($merge[0]);
376 }
377 }
378
379 unset($GLOBALS['FORUM_CACHE']['USER']);
380 unset($GLOBALS['FORUM_CACHE']['USER_ID']);
381
382 return $this;
383 }
384
385 public function setLocation(int $forumId = 0, int $topicId = 0)
386 {
387 global $USER;
388 if (!($USER instanceof \CUser && $this->getId() === $USER->GetID()))
389 {
390 return;
391 }
392
394 $helper = $connection->getSqlHelper();
395
396 $primaryFields = [
397 'USER_ID' => $this->getId(),
398 'PHPSESSID' => $this->getSessId()
399 ];
400 $fields = [
401 'SHOW_NAME' => $this->getName(),
403 'LAST_VISIT' => new Main\DB\SqlExpression($helper->getCurrentDateTimeFunction()),
404 'SITE_ID' => SITE_ID,
405 'FORUM_ID' => $forumId,
406 'TOPIC_ID' => $topicId,
407 ];
408
409 if ($this->getId() > 0)
410 {
411 $fields['PHPSESSID'] = $primaryFields['PHPSESSID'];
412 }
413
414 $merge = $helper->prepareMerge(
415 'b_forum_stat',
416 array_keys($primaryFields),
417 $primaryFields + $fields,
418 $fields
419 );
420 if ($merge[0] != '')
421 {
422 $connection->query($merge[0]);
423 }
424 }
425
426 public function isLocked()
427 {
428 return $this->locked;
429 }
430
431 public function isAdmin()
432 {
433 if ($this->isLocked())
434 return false;
435 return self::isUserAdmin($this->getGroups());
436 }
437
438 public function isGuest()
439 {
440 return ($this->getId() <= 0);
441 }
442
443 public function isAuthorized()
444 {
445 return ($this->getId() > 0);
446 }
447
448 public function edit(array $fields)
449 {
450 $result = new Result();
451
452 if (!$this->isAuthorized())
453 {
454 return $result;
455 }
456
457 foreach (GetModuleEvents("forum", "onBeforeUserEdit", true) as $arEvent)
458 {
459 if (ExecuteModuleEventEx($arEvent, [$this->forumUserId, $this->getId(), &$fields]) === false)
460 {
461 $result->addError(new Error("Event error"));
462 return $result;
463 }
464 }
465
466 $result = $this->save($fields);
467
468 if ($result->isSuccess())
469 {
470 foreach(GetModuleEvents("forum", "onAfterUserEdit", true) as $arEvent)
471 {
472 ExecuteModuleEventEx($arEvent, array($this->forumUserId, $this->getId(), $fields));
473 }
474 }
475
476 return $result;
477 }
478
479 public function calculateStatistic()
480 {
481 $result = new Result();
482
483 if (!$this->isAuthorized())
484 {
485 return $result;
486 }
487
488 $fields = [
489 "LAST_POST" => 0,
490 "NUM_POSTS" => 0,
491 "POINTS" => 0
492 ];
493 if ($res = MessageTable::getList([
494 "select" => ["CNT", "LAST_MESSAGE_ID"],
495 "filter" => ["AUTHOR_ID" => $this->getId(), "=APPROVED" => "Y"],
496 "runtime" => [
497 new Main\Entity\ExpressionField("CNT", "COUNT(*)"),
498 new Main\Entity\ExpressionField("LAST_MESSAGE_ID", "MAX(%s)", ["ID"])
499 ],
500 ])->fetch())
501 {
502 $fields = [
503 "LAST_POST" => $res["LAST_MESSAGE_ID"],
504 "NUM_POSTS" => $res["CNT"],
505 "POINTS" => \CForumUser::GetUserPoints($this->getId(), ["NUM_POSTS" => $res["CNT"]])
506 ];
507 }
508 return $this->save($fields);
509 }
510
512 {
513 if (!$this->isAuthorized() || $message["APPROVED"] != "Y")
514 {
515 return;
516 }
517
518 $this->data["NUM_POSTS"]++;
519 $this->data["POINTS"] = \CForumUser::GetUserPoints($this->getId(), array("INCREMENT" => $this->data["NUM_POSTS"]));
520 $this->data["LAST_POST"] = $message["ID"];
521 $this->save([
522 "NUM_POSTS" => new Main\DB\SqlExpression('?# + 1', "NUM_POSTS"),
523 "POINTS" => $this->data["POINTS"],
524 "LAST_POST" => $message["ID"]
525 ]);
526 }
527
528 public function decrementStatistic($message = null)
529 {
530 if (!$this->isAuthorized() || $message['APPROVED'] != 'Y')
531 {
532 return;
533 }
534
535 $this->data['NUM_POSTS']--;
536 $this->data['POINTS'] = \CForumUser::GetUserPoints($this->getId(), array('INCREMENT' => $this->data['NUM_POSTS']));
537 $fields = [
538 'NUM_POSTS' => new Main\DB\SqlExpression('?# - 1', 'NUM_POSTS'),
539 'POINTS' => $this->data['POINTS'],
540 ];
541 if ($message === null ||
542 $message['ID'] === $this->data['LAST_POST']
543 )
544 {
545 $message = MessageTable::getList([
546 'select' => ['ID'],
547 'filter' => ['AUTHOR_ID' => $this->getId(), 'APPROVED' => 'Y'],
548 'limit' => 1,
549 'order' => ['ID' => 'DESC']
550 ])->fetch();
551 $this->data['LAST_POST'] = $message['ID'];
552 $fields['LAST_POST'] = $message['ID'];
553 }
554 $this->save($fields);
555 }
556
560 public function getUnreadMessageId($topicId = 0): ?int
561 {
562 if ($topicId <= 0)
563 {
564 return null;
565 }
566
567 try
568 {
569 $topic = Topic::getById($topicId);
570 }
572 {
573 return null;
574 }
575
576 $query = MessageTable::query()
577 ->setSelect(['ID'])
578 ->where('TOPIC_ID', $topic->getId())
579 ->registerRuntimeField('FORCED_INT_ID', new Main\Entity\ExpressionField('FORCED_ID', '%s', ['ID']))
580 ->setOrder(['FORCED_INT_ID' => 'ASC'])
581 ->setLimit(1);
582 if ($this->isAuthorized())
583 {
584 $query
585 ->registerRuntimeField(
586 'USER_TOPIC',
588 'USER_TOPIC',
590 [
591 '=this.TOPIC_ID' => 'ref.TOPIC_ID',
592 '=ref.USER_ID' => ['?i', $this->getId()],
593 ],
594 ['join_type' => 'LEFT']
595 )
596 )
597 ->registerRuntimeField(
598 'USER_FORUM',
600 'USER_FORUM',
601 UserForumTable::getEntity(),
602 [
603 '=this.FORUM_ID' => 'ref.FORUM_ID',
604 '=ref.USER_ID' => ['?i', $this->getId()]
605 ],
606 ['join_type' => 'LEFT']
607 )
608 )
609 ->registerRuntimeField(
610 'USER_FORUM_0',
612 'FUF0',
613 UserForumTable::getEntity(),
614 [
615 '=this.FORUM_ID' => ['?i', 0],
616 '=ref.USER_ID' => ['?i', $this->getId()]
617 ],
618 ['join_type' => 'LEFT']
619 )
620 )
621 ->where(
622 Main\ORM\Query\Query::filter()
623 ->logic('or')
624 ->where(
625 Main\ORM\Query\Query::filter()
626 ->whereNotNull('USER_TOPIC.LAST_VISIT')
627 ->whereColumn('POST_DATE', '>', 'USER_TOPIC.LAST_VISIT')
628 )
629 ->where(
630 Main\ORM\Query\Query::filter()
631 ->whereNull('USER_TOPIC.LAST_VISIT')
632 ->whereColumn('POST_DATE', '>', 'USER_FORUM.LAST_VISIT')
633 )
634 ->where(
635 Main\ORM\Query\Query::filter()
636 ->whereNull('USER_TOPIC.LAST_VISIT')
637 ->whereNull('USER_FORUM.LAST_VISIT')
638 ->whereColumn('POST_DATE', '>', 'USER_FORUM_0.LAST_VISIT')
639 )
640 ->where(
641 Main\ORM\Query\Query::filter()
642 ->whereNull('USER_TOPIC.LAST_VISIT')
643 ->whereNull('USER_FORUM.LAST_VISIT')
644 ->whereNull('USER_FORUM_0.LAST_VISIT')
645 ->whereNotNull('ID')
646 )
647 );
648 }
649 else
650 {
651 $timestamps = $this->getFromSession('GUEST_TID');
652 $lastVisit = max(
653 $this->getFromSession('LAST_VISIT_FORUM_0'),
654 $this->getFromSession('LAST_VISIT_FORUM_'.$topic->getForumId()),
655 is_array($timestamps) && array_key_exists($topic->getId(), $timestamps) ? $timestamps[$topic->getId()] : 0
656 );
657 if ($lastVisit > 0)
658 {
659 $query->where('POST_DATE', '>', DateTime::createFromTimestamp($lastVisit));
660 }
661 else
662 {
663 return null;
664 }
665 }
666 if ($res = $query->fetch())
667 {
668 return $res['ID'];
669 }
670 return null;
671 }
672
677 public function readTopic($topicId = 0): void
678 {
679 if ($topicId <= 0
680 || !($topic = Topic::getById($topicId))
681 || !($topic instanceof Topic)
682 )
683 {
684 return;
685 }
686
687 $topic->incrementViews();
688
689 if ($this->isAuthorized())
690 {
692 $helper = $connection->getSqlHelper();
693
694 $primaryFields = [
695 'USER_ID' => $this->getId(),
696 'TOPIC_ID' => $topic->getId()
697 ];
698
699 $fields = [
700 'FORUM_ID' => $topic->getForumId(),
701 'LAST_VISIT' => new Main\DB\SqlExpression($helper->getCurrentDateTimeFunction())
702 ];
703
704 $result = UserTopicTable::update($primaryFields, $fields);
705 if ($result->getAffectedRowsCount() <= 0)
706 {
707 $merge = $helper->prepareMerge(
708 'b_forum_user_topic',
709 array_keys($primaryFields),
710 $primaryFields + $fields,
711 $fields
712 );
713 if ($merge[0] != '')
714 {
715 $connection->query($merge[0]);
716 }
717 }
718 }
719 else
720 {
721 $timestamp = new DateTime();
722 $this->saveInSession('GUEST_TID', null);
723
724 if (Main\Config\Option::set('forum', 'USE_COOKIE', 'N') == 'Y')
725 {
726 $GLOBALS['APPLICATION']->set_cookie('FORUM_GUEST_TID', '', false, '/', false, false, 'Y', false);
727 }
728 }
729 }
730
731 public function readTopicsOnForum(int $forumId = 0)
732 {
733 if ($this->isAuthorized())
734 {
736 $helper = $connection->getSqlHelper();
737
738 $primaryFields = [
739 'USER_ID' => $this->getId(),
740 'FORUM_ID' => $forumId
741 ];
742
743 $fields = [
744 'LAST_VISIT' => new Main\DB\SqlExpression($helper->getCurrentDateTimeFunction())
745 ];
746
747 $result = Forum\UserForumTable::update($primaryFields, $fields);
748 if ($result->getAffectedRowsCount() <= 0)
749 {
750 $merge = $helper->prepareMerge(
752 array_keys($primaryFields),
753 $primaryFields + $fields,
754 $fields
755 );
756 if ($merge[0] != '')
757 {
758 $connection->query($merge[0]);
759 }
760 }
761
762 if ($forumId > 0)
763 {
764 Forum\UserTopicTable::deleteBatch(['USER_ID' => $this->getId(), 'FORUM_ID' => $forumId]);
765 }
766 else
767 {
768 Forum\UserTopicTable::deleteBatch(['USER_ID' => $this->getId()]);
769 Forum\UserForumTable::deleteBatch(['USER_ID' => $this->getId(), '>FORUM_ID' => 0]);
770 }
771 }
772 else
773 {
774 $timestamp = new DateTime();
775 $topics = $this->saveInSession('GUEST_TID', [$topic->getId() => $timestamp->getTimestamp()]);
776
777 if (Main\Config\Option::set('forum', 'USE_COOKIE', 'N') == 'Y')
778 {
779 foreach ($topics as $topicId => $timestamp)
780 {
781 $topics[$topicId] = implode('-', [$topicId, $timestamp]);
782 }
783 $GLOBALS['APPLICATION']->set_cookie('FORUM_GUEST_TID', implode('/', $topics), false, '/', false, false, 'Y', false);
784 }
785 }
786 }
787
788 private function save(array $fields)
789 {
790 $result = new Result();
791
792 if (!$this->isAuthorized())
793 {
794 return $result;
795 }
796
797 if ($this->forumUserId > 0)
798 {
799 $result = User::update($this->forumUserId, $fields);
800 }
801 else
802 {
803 if (Main\Application::getConnection()->getType() === 'pgsql')
804 {
805 $fields['NUM_POSTS'] = 1;
806 }
807 $fields = ['USER_ID' => $this->getId()] + $fields + $this->data;
808 unset($fields['ID']);
810 if ($result->isSuccess())
811 {
812 $res = $result->getPrimary();
813 if (is_array($res))
814 {
815 $res = reset($res);
816 }
817 $this->forumUserId = $res;
818 }
819 }
820 return $result;
821 }
822
823 public function getGroups()
824 {
825 if (!$this->groups)
826 {
827 global $USER;
828 $this->groups = [2];
829 if ($this->getId() <= 0 || $this->isLocked())
830 {
831 if (Main\Config\Option::get("main", "new_user_registration", "") == "Y")
832 {
833 $def_group = Main\Config\Option::get("main", "new_user_registration_def_group", "");
834 if($def_group != "" && ($res = explode(",", $def_group)))
835 {
836 $this->groups = array_merge($this->groups, $res);
837 }
838 }
839 }
840 elseif ($USER instanceof \CUser && $this->getId() === $USER->GetID())
841 {
842 $this->groups = $USER->GetUserGroupArray();
843 }
844 else
845 {
846 $dbRes = Main\UserGroupTable::getList(array(
847 "select" => ["GROUP_ID"],
848 "filter" => ["USER_ID" => $this->getId()],
849 "order" => ["GROUP_ID" => "ASC"]
850 ));
851 while ($res = $dbRes->fetch())
852 {
853 $this->groups[] = $res["GROUP_ID"];
854 }
855 }
856 }
857 return $this->groups;
858 }
859
860 public function setPermissionOnForum($forum, $permission)
861 {
862 $forum = Forum\Forum::getInstance($forum);
863 $this->permissions[$forum->getId()] = $permission;
864 return $this;
865 }
866
867 public function getPermissionOnForum($forum)
868 {
869 $forum = Forum\Forum::getInstance($forum);
870 if (!array_key_exists($forum->getId(), $this->permissions))
871 {
872 $this->permissions[$forum->getId()] = $forum->getPermissionForUser($this);
873 }
874 return $this->permissions[$forum->getId()];
875 }
876
877 public function canModerate(Forum\Forum $forum)
878 {
879 return $this->getPermissionOnForum($forum->getId()) >= Permission::CAN_MODERATE;
880 }
881
882 public function canAddTopic(Forum\Forum $forum)
883 {
884 return $this->getPermissionOnForum($forum->getId()) >= Permission::CAN_ADD_TOPIC;
885 }
886
887 public function canAddMessage(Topic $topic)
888 {
889 if ($topic["STATE"] === Topic::STATE_OPENED && $topic["APPROVED"] === Topic::APPROVED_APPROVED)
890 {
892 }
893 return $this->getPermissionOnForum($topic->getForumId()) >= Permission::CAN_EDIT;
894 }
895
896 public function canEditTopic(Topic $topic)
897 {
899 {
900 return true;
901 }
902 if (!$this->isAuthorized())
903 {
904 return false;
905 }
906 if (
907 ($this->getId() == $topic->getAuthorId())
908 &&
909 ($this->editOwn || ($topic["POSTS"] <= 0 && $topic["POSTS_UNAPPROVED"] <= 0))
910 )
911 {
912 return true;
913 }
914 return false;
915 }
916
918 {
919 if ($this->getPermissionOnForum($message->getForumId()) >= Permission::CAN_EDIT)
920 {
921 return true;
922 }
923 if (!$this->isAuthorized())
924 {
925 return false;
926 }
927 if ($this->getId() == $message->getAuthorId())
928 {
929 if ($this->editOwn)
930 {
931 return true;
932 }
933 $topic = Topic::getById($message["TOPIC_ID"]);
934 if ($topic["ABS_LAST_MESSAGE_ID"] == $message->getId())
935 {
936 return true;
937 }
938 }
939 return false;
940 }
941
943 {
944 return $this->canEditMessage($message);
945 }
946
947 public function canEditForum(Forum\Forum $forum)
948 {
949 return $this->getPermissionOnForum($forum->getId()) >= Permission::FULL_ACCESS;
950 }
951
952 public function canDeleteForum(Forum\Forum $forum)
953 {
954 return $this->canEditForum($forum);
955 }
956
957 public function canReadForum(Forum\Forum $forum)
958 {
959 return $this->getPermissionOnForum($forum->getId()) >= Permission::CAN_READ;
960 }
961
962 public function canReadTopic(Topic $topic)
963 {
964 return $this->getPermissionOnForum($topic->getForumId()) >= Permission::CAN_READ;
965 }
966
967 public static function isUserAdmin(array $groups)
968 {
969 global $APPLICATION;
970 return (in_array(1, $groups) || $APPLICATION->GetGroupRight("forum", $groups) >= "W");
971 }
972
973 private function saveInSession(string $name, $value)
974 {
975 if (method_exists(Main\Application::getInstance(), 'getKernelSession'))
976 {
977 $forumSession = Main\Application::getInstance()->getKernelSession()->get('FORUM');
978 }
979 else
980 {
981 $forumSession = $_SESSION['FORUM'];
982 }
983 $forumSession = is_array($forumSession) ? $forumSession : [];
984 if (is_array($value) && array_key_exists($name, $forumSession) && is_array($forumSession[$name]))
985 {
986 $forumSession[$name] = $value + $forumSession[$name];
987 }
988 else
989 {
990 $forumSession[$name] = $value;
991 }
992 if (method_exists(Main\Application::getInstance(), 'getKernelSession'))
993 {
994 Main\Application::getInstance()->getKernelSession()->set('FORUM', $forumSession);
995 }
996 else
997 {
998 $_SESSION['FORUM'] = $forumSession;
999 }
1000 return $forumSession[$name];
1001 }
1002
1003 private function getFromSession(string $name)
1004 {
1005 if (method_exists(Main\Application::getInstance(), 'getKernelSession'))
1006 {
1007 $forumSession = Main\Application::getInstance()->getKernelSession()->get('FORUM');
1008 }
1009 else
1010 {
1011 $forumSession = $_SESSION['FORUM'];
1012 }
1013 if (is_array($forumSession) && array_key_exists($name, $forumSession))
1014 {
1015 return $forumSession[$name];
1016 }
1017 return null;
1018 }
1019
1020 private function getSessId()
1021 {
1022 if (method_exists(Main\Application::getInstance(), 'getKernelSession'))
1023 {
1024 return Main\Application::getInstance()->getKernelSession()->getId();
1025 }
1026 return bitrix_sessid();
1027 }
1028
1029 public static function add(array &$data)
1030 {
1031 $result = new \Bitrix\Main\ORM\Data\AddResult();
1032 if (($events = GetModuleEvents("forum", "onBeforeUserAdd", true)) && !empty($events))
1033 {
1034 global $APPLICATION;
1035 foreach ($events as $ev)
1036 {
1037 $APPLICATION->ResetException();
1038
1039 if (ExecuteModuleEventEx($ev, array(&$data)) === false)
1040 {
1041 $errorMessage = Loc::getMessage("FORUM_EVENT_BEFORE_USER_ADD");
1042 if (($ex = $APPLICATION->GetException()) && ($ex instanceof \CApplicationException))
1043 {
1044 $errorMessage = $ex->getString();
1045 }
1046
1047 $result->addError(new \Bitrix\Main\Error($errorMessage, "onBeforeUserAdd"));
1048 return $result;
1049 }
1050 }
1051 }
1052
1053 $dbResult = UserTable::add($data);
1054
1055 if (!$dbResult->isSuccess())
1056 {
1057 $result->addErrors($dbResult->getErrors());
1058 }
1059 else
1060 {
1061 $id = $dbResult->getId();
1062 $result->setId($id);
1063 foreach (GetModuleEvents("forum", "onAfterUserAdd", true) as $event)
1064 {
1066 }
1067 }
1068
1069 return $result;
1070 }
1071
1072 public static function update(int $id, array &$data)
1073 {
1074 unset($data["USER_ID"]);
1075
1077 $result->setPrimary(["ID" => $id]);
1078
1079 if (($events = GetModuleEvents("forum", "onBeforeUserUpdate", true)) && !empty($events))
1080 {
1081 global $APPLICATION;
1082 foreach ($events as $ev)
1083 {
1084 $APPLICATION->ResetException();
1085
1086 if (ExecuteModuleEventEx($ev, array($id, &$data)) === false)
1087 {
1088 $errorMessage = Loc::getMessage("FORUM_EVENT_BEFORE_USER_UPDATE_ERROR");
1089 if (($ex = $APPLICATION->GetException()) && ($ex instanceof \CApplicationException))
1090 {
1091 $errorMessage = $ex->getString();
1092 }
1093 $result->addError(new Main\Error($errorMessage, "onBeforeUserUpdate"));
1094 return $result;
1095 }
1096 }
1097 }
1098
1099 $dbResult = UserTable::update($id, $data);
1100
1101 if (!$dbResult->isSuccess())
1102 {
1103 $result->addErrors($dbResult->getErrors());
1104 }
1105 else
1106 {
1107 foreach (GetModuleEvents("forum", "onAfterUserUpdate", true) as $event)
1108 {
1110 }
1111 }
1112 return $result;
1113 }
1114}
$connection
Определения actionsdefinitions.php:38
global $APPLICATION
Определения include.php:80
getAuthorId()
Определения entity.php:57
const CAN_EDIT
Определения permission.php:72
const CAN_ADD_TOPIC
Определения permission.php:70
const FULL_ACCESS
Определения permission.php:73
const CAN_READ
Определения permission.php:68
const CAN_ADD_MESSAGE
Определения permission.php:69
const CAN_MODERATE
Определения permission.php:71
Определения topic.php:287
const APPROVED_APPROVED
Определения topic.php:293
const STATE_OPENED
Определения topic.php:292
static deleteBatch(array $filter)
Определения userforum.php:45
static getTableName()
Определения userforum.php:27
Определения user.php:264
canReadForum(Forum\Forum $forum)
Определения user.php:957
calculateStatistic()
Определения user.php:479
isGuest()
Определения user.php:438
canDeleteForum(Forum\Forum $forum)
Определения user.php:952
__construct($id)
Определения user.php:283
edit(array $fields)
Определения user.php:448
isLocked()
Определения user.php:426
setLastVisit()
Определения user.php:343
$groups
Определения user.php:276
decrementStatistic($message=null)
Определения user.php:528
readTopicsOnForum(int $forumId=0)
Определения user.php:731
incrementStatistic(array $message)
Определения user.php:511
getUnreadMessageId($topicId=0)
Определения user.php:560
setLocation(int $forumId=0, int $topicId=0)
Определения user.php:385
getName()
Определения user.php:338
canReadTopic(Topic $topic)
Определения user.php:962
getGroups()
Определения user.php:823
canAddTopic(Forum\Forum $forum)
Определения user.php:882
$locked
Определения user.php:274
$data
Определения user.php:270
canEditMessage(Message $message)
Определения user.php:917
canDeleteMessage(Message $message)
Определения user.php:942
canEditForum(Forum\Forum $forum)
Определения user.php:947
readTopic($topicId=0)
Определения user.php:677
static isUserAdmin(array $groups)
Определения user.php:967
setPermissionOnForum($forum, $permission)
Определения user.php:860
canAddMessage(Topic $topic)
Определения user.php:887
static update(int $id, array &$data)
Определения user.php:1072
canEditTopic(Topic $topic)
Определения user.php:896
isAdmin()
Определения user.php:431
$permissions
Определения user.php:279
canModerate(Forum\Forum $forum)
Определения user.php:877
getPermissionOnForum($forum)
Определения user.php:867
isAuthorized()
Определения user.php:443
static add(array &$data)
Определения user.php:1029
$id
Определения user.php:268
$forumUserId
Определения user.php:272
static getMap()
Определения user.php:77
static onAfterDelete(Main\ORM\Event $event)
Определения user.php:253
static onBeforeUpdate(Main\ORM\Event $event)
Определения user.php:181
static onBeforeDelete(Main\ORM\Event $event)
Определения user.php:230
static getTableName()
Определения user.php:67
static deleteBatch(array $filter)
Определения usertopic.php:72
static getInstance()
Определения application.php:98
static getConnection($name="")
Определения application.php:638
static get($moduleId, $name, $default="", $siteId=false)
Определения option.php:30
Определения error.php:15
Определения event.php:5
static getEntity()
Определения datamanager.php:65
static getById($id)
Определения datamanager.php:364
static getList(array $parameters=array())
Определения datamanager.php:431
static onBeforeAdd(Event $event)
Определения datamanager.php:2007
static update($primary, array $data)
Определения datamanager.php:1256
static getRealIp()
Определения manager.php:532
Определения user.php:48
static GetUserPoints($USER_ID, $arAddParams=array())
Определения user.php:766
Определения user.php:6037
$str
Определения commerceml2.php:63
$data['IS_AVAILABLE']
Определения .description.php:13
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$res
Определения filter_act.php:7
$result
Определения get_property_values.php:14
$query
Определения get_search.php:11
while($arParentIBlockProperty=$dbParentIBlockProperty->Fetch()) $errorMessage
global $USER
Определения csv_new_run.php:40
ExecuteModuleEventEx($arEvent, $arParams=[])
Определения tools.php:5214
bitrix_sessid()
Определения tools.php:4656
GetModuleEvents($MODULE_ID, $MESSAGE_ID, $bReturnArray=false)
Определения tools.php:5177
$name
Определения menu_edit.php:35
trait EntityFabric
Определения entityfabric.php:5
$value
Определения Param.php:39
Определения arrayresult.php:2
Определения ufield.php:9
Определения chain.php:3
$user
Определения mysql_to_pgsql.php:33
$GLOBALS['____1690880296']
Определения license.php:1
$message
Определения payment.php:8
$event
Определения prolog_after.php:141
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
const SITE_ID
Определения sonet_set_content_view.php:12
$dbResult
Определения updtr957.php:3
$dbRes
Определения yandex_detail.php:168
$fields
Определения yandex_run.php:501