Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
messageaccess.php
1<?php
2
3namespace Bitrix\Mail;
4
6use \Bitrix\Crm\ActivityTable;
7
12{
13 // supported entity types
14 public const ENTITY_TYPE_IM_CHAT = MessageAccessTable::ENTITY_TYPE_IM_CHAT;
15 public const ENTITY_TYPE_CALENDAR_EVENT = MessageAccessTable::ENTITY_TYPE_CALENDAR_EVENT;
16
18 private $userId;
19
21 private $message;
22
24 private $storage;
25
26 protected function __construct(int $userId, \Bitrix\Mail\Item\Message $message, IMessageAccessStorage $storage)
27 {
28 $this->userId = $userId;
29 $this->message = $message;
30 $this->storage = $storage;
31 }
32
33 public static function createForMessage(\Bitrix\Mail\Item\Message $message, int $userId): self
34 {
35 $storage = new \Bitrix\Mail\Storage\MessageAccess();
36 return new self($userId, $message, $storage);
37 }
38
39 public static function createByMessageId(int $messageId, int $userId): self
40 {
41 $messageStorage = new \Bitrix\Mail\Storage\Message();
42 $message = $messageStorage->getMessage($messageId);
43 return self::createForMessage($message, $userId);
44 }
45
51 public static function createForCurrentUser($message): self
52 {
53 global $USER;
54 $userId = $USER->GetID();
55
56 if (!$userId)
57 {
58 throw new \Bitrix\Main\SystemException('message access: user id error');
59 }
60
61 return $message instanceof \Bitrix\Mail\Item\Message
62 ? self::createForMessage($message, $userId)
63 : self::createByMessageId((int)$message, $userId);
64 }
65
66 public function isOwner(): bool
67 {
68 return (bool)self::getUserMailbox($this->getMessage()->getMailboxId(), $this->getUserId());
69 }
70
71 public static function getCrmEntityOwner($activityId): array
72 {
73 return ActivityTable::getList([
74 'select' => [
75 'OWNER_ID',
76 'OWNER_TYPE_ID',
77 ],
78 'filter' => [
79 '==ID' => $activityId,
80 ],
81 'limit' => 1,
82 ])->fetch();
83 }
84
85 public function getEntitiesForType($entityType): array
86 {
87 $collection = $this->getCollection($this->getMessage());
88
89 $bindings = [];
90
92 foreach ($collection as $item)
93 {
94 if ($item->getEntityType() === $entityType)
95 {
96 $bindings[] = $item->getEntityId();
97 }
98 }
99
100 return $bindings;
101 }
102
107 public function canViewMessage(): bool
108 {
109 if ($this->isOwner())
110 {
111 return true;
112 }
113
114 $collection = $this->getCollection($this->getMessage());
115
117 foreach ($collection as $item)
118 {
119 switch ($item->getEntityType())
120 {
122 if (\Bitrix\Mail\Helper\MessageAccess::checkAccessForChat($item->getEntityId(), $this->getUserId()))
123 {
124 return true;
125 }
126 break;
128 if (\Bitrix\Mail\Helper\MessageAccess::checkAccessForCalendarEvent($item->getEntityId(), $this->getUserId()))
129 {
130 return true;
131 }
132 break;
133 }
134 }
135
136 return false;
137 }
138
139 public function canModifyMessage(): bool
140 {
141 return $this->isOwner();
142 }
143
144 private function getMessage(): \Bitrix\Mail\Item\Message
145 {
146 return $this->message;
147 }
148
149 private function getUserId(): int
150 {
151 return $this->userId;
152 }
153
154 private function getStorage(): IMessageAccessStorage
155 {
156 return $this->storage;
157 }
158
159 private function getCollection(\Bitrix\Mail\Item\Message $item): \Bitrix\Mail\Collection\MessageAccess
160 {
161 return $this->getStorage()->getCollectionForMessage($item);
162 }
163
164 private static function getUserMailbox(int $mailboxId, int $userId)
165 {
166 return MailboxTable::getUserMailbox($mailboxId, $userId);
167 }
168
169}
static getUserMailbox($mailboxId, $userId=null)
Definition mailbox.php:110
static createForMessage(\Bitrix\Mail\Item\Message $message, int $userId)
__construct(int $userId, \Bitrix\Mail\Item\Message $message, IMessageAccessStorage $storage)
static createForCurrentUser($message)
static getCrmEntityOwner($activityId)
static createByMessageId(int $messageId, int $userId)