Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
chatauthprovider.php
1
<?php
2
3
namespace
Bitrix\Im\Access
;
4
5
use \Bitrix\Main\Localization\Loc;
6
use
Bitrix\Main\UserAccessTable
;
7
17
class
ChatAuthProvider
extends
\CAuthProvider
18
{
19
protected
const
PROVIDER_ID
=
'imchat'
;
20
protected
const
ACCESS_CODE_PREFIX
=
'CHAT'
;
21
22
public
function
__construct
()
23
{
24
$this->
id
=
self::PROVIDER_ID
;
25
}
26
32
public
static
function
getProviders
(): array
33
{
34
return
[
35
[
36
'ID'
=>
self::PROVIDER_ID
,
37
'CLASS'
=> self::class,
38
'PROVIDER_NAME'
=>
Loc::getMessage
(
'chat_auth_provider'
),
39
'NAME'
=>
Loc::getMessage
(
'chat_auth_provider_name'
),
40
'SORT'
=> 400,
41
]
42
];
43
}
44
50
public
function
generateAccessCode
(
int
$chatId): string
51
{
52
return
self::ACCESS_CODE_PREFIX. $chatId;
53
}
54
64
public
function
getNames
($codes): array
65
{
66
$chatIds = [];
67
$accessCodePrefix =
self::ACCESS_CODE_PREFIX
;
68
foreach
($codes as $code)
69
{
70
if
(preg_match(
"/^{$accessCodePrefix}([0-9]+)$/i"
, $code, $match))
71
{
72
$chatIds[] = (int)$match[1];
73
}
74
}
75
76
$result = [];
77
if
(count($chatIds) > 0)
78
{
79
$resChatData = \Bitrix\Im\Model\ChatTable::getList([
80
'select'
=> [
'ID'
,
'TITLE'
],
81
'filter'
=> [
'=ID'
=> $chatIds],
82
]);
83
while
($chat = $resChatData->fetch())
84
{
85
$accessCode = $this->
generateAccessCode
($chat[
'ID'
]);
86
$result[$accessCode] = [
87
'provider'
=>
Loc::getMessage
(
'chat_auth_provider'
),
88
];
89
if
(!empty($chat[
'TITLE'
]))
90
{
91
$result[$accessCode][
'name'
] = $chat[
'TITLE'
];
92
}
93
else
94
{
95
$result[$accessCode][
'name'
] =
Loc::getMessage
(
'chat_auth_title'
, [
'#CHAT_ID#'
=> $chat[
'ID'
]]);
96
}
97
}
98
}
99
100
return
$result;
101
}
102
109
public
function
deleteByUser
($userId): void
110
{
111
$userId = (int)$userId;
112
if
($userId > 0)
113
{
114
$connection = \Bitrix\Main\Application::getConnection();
115
$helper = $connection->getSqlHelper();
116
$providerId = $helper->forSql($this->
id
);
117
$connection->queryExecute(
"
118
DELETE FROM b_user_access
119
WHERE PROVIDER_ID = '{$providerId}' AND USER_ID = {$userId}
120
"
);
121
}
122
123
parent::deleteByUser($userId);
124
}
125
133
public
function
addChatCodes
(
int
$chatId, array $userIds): void
134
{
135
$userIds = array_filter(array_map(
'intVal'
, $userIds));
136
if
($chatId > 0 && !empty($userIds))
137
{
138
$connection = \Bitrix\Main\Application::getConnection();
139
$helper = $connection->getSqlHelper();
140
$providerId = $helper->forSql($this->
id
);
141
$accessCode = $helper->forSql($this->
generateAccessCode
($chatId));
142
143
$users = implode(
','
, $userIds);
144
145
$sql = $helper->getInsertIgnore(
146
'b_user_access'
,
147
'(USER_ID, PROVIDER_ID, ACCESS_CODE)'
,
148
"SELECT ID, '{$providerId}', '{$accessCode}'
149
FROM b_user
150
WHERE ID IN({$users})"
151
);
152
153
$connection->queryExecute($sql);
154
155
foreach
($userIds as $uid)
156
{
157
\CAccess::ClearCache($uid);
158
}
159
}
160
}
161
169
public
function
deleteChatCodes
(
int
$chatId, ?array $userIds =
null
): void
170
{
171
if
($chatId > 0)
172
{
173
$connection = \Bitrix\Main\Application::getConnection();
174
$helper = $connection->getSqlHelper();
175
$providerId = $helper->forSql($this->
id
);
176
$accessCode = $helper->forSql($this->
generateAccessCode
($chatId));
177
178
if
($userIds ===
null
)
179
{
180
$res = \Bitrix\Main\UserAccessTable::getList([
181
'filter'
=> [
'=ACCESS_CODE'
=> $accessCode],
182
'select'
=> [
'USER_ID'
]
183
]);
184
$userIds = [];
185
while
($row = $res->fetch())
186
{
187
$userIds[] = (int)$row[
'USER_ID'
];
188
}
189
190
$connection->queryExecute(
"
191
DELETE FROM b_user_access
192
WHERE PROVIDER_ID = '{$providerId}' AND ACCESS_CODE = '{$accessCode}'
193
"
);
194
}
195
else
196
{
197
$userIds = array_filter(array_map(
'intVal'
, $userIds));
198
if
(count($userIds) > 0)
199
{
200
$users = implode(
','
, $userIds);
201
$connection->queryExecute(
"
202
DELETE FROM b_user_access
203
WHERE PROVIDER_ID = '{$providerId}'
204
AND ACCESS_CODE = '{$accessCode}'
205
AND USER_ID IN({$users})
206
"
);
207
}
208
}
209
210
foreach
($userIds as $uid)
211
{
212
\CAccess::ClearCache($uid);
213
}
214
}
215
}
216
217
public
function
isCodeAlreadyExists
(
int
$chatId,
int
$userId): bool
218
{
219
$result = UserAccessTable::query()
220
->setSelect([
'USER_ID'
])
221
->where(
'USER_ID'
, $userId)
222
->where(
'ACCESS_CODE'
,
"CHAT{$chatId}"
)
223
->where(
'PROVIDER_ID'
, $this->
id
)
224
->setLimit(1)
225
->fetch()
226
;
227
228
return
$result !==
false
;
229
}
230
237
public
function
updateChatCodesByRelations
(
int
$chatId): void
238
{
239
if
($chatId > 0)
240
{
241
$connection = \Bitrix\Main\Application::getConnection();
242
$helper = $connection->getSqlHelper();
243
$providerId = $helper->forSql($this->
id
);
244
$accessCode = $helper->forSql($this->
generateAccessCode
($chatId));
245
246
$sql = $helper->getInsertIgnore(
247
'b_user_access'
,
248
'(USER_ID, PROVIDER_ID, ACCESS_CODE)'
,
249
"SELECT R.USER_ID, '{$providerId}', '{$accessCode}'
250
FROM b_im_relation R
251
INNER JOIN b_user U ON R.USER_ID = U.ID
252
LEFT JOIN b_user_access A
253
ON U.ID = A.USER_ID
254
AND A.PROVIDER_ID = '{$providerId}'
255
AND A.ACCESS_CODE = '{$accessCode}'
256
WHERE
257
R.CHAT_ID = {$chatId}
258
AND A.ID IS NULL
259
AND (CASE
260
WHEN U.EXTERNAL_AUTH_ID = 'imconnector' AND POSITION('livechat|' in U.XML_Id) = 1 THEN 1
261
WHEN U.EXTERNAL_AUTH_ID = 'imconnector' THEN 0
262
ELSE 1
263
END) = 1"
264
);
265
266
$connection->queryExecute($sql);
267
268
$connection->queryExecute(
"
269
DELETE FROM b_user_access
270
WHERE PROVIDER_ID = '{$providerId}'
271
AND ACCESS_CODE = '{$accessCode}'
272
AND USER_ID NOT IN(
273
SELECT R.USER_ID
274
FROM b_im_relation R
275
WHERE R.CHAT_ID = {$chatId}
276
)
277
"
);
278
279
$res = \Bitrix\Main\UserAccessTable::getList([
280
'filter'
=> [
'=ACCESS_CODE'
=> $accessCode],
281
'select'
=> [
'USER_ID'
]
282
]);
283
while
($row = $res->fetch())
284
{
285
\CAccess::ClearCache($row[
'USER_ID'
]);
286
}
287
}
288
}
289
297
public
function
addUserCode
(
int
$chatId,
int
$userId): void
298
{
299
\CAccess::AddCode($userId, $this->
id
, $this->
generateAccessCode
($chatId));
300
}
301
309
public
function
removeUserCode
(
int
$chatId,
int
$userId): void
310
{
311
\CAccess::RemoveCode($userId, $this->
id
, $this->
generateAccessCode
($chatId));
312
}
313
}
Bitrix\Im\Access\ChatAuthProvider
Definition
chatauthprovider.php:18
Bitrix\Im\Access\ChatAuthProvider\__construct
__construct()
Definition
chatauthprovider.php:22
Bitrix\Im\Access\ChatAuthProvider\generateAccessCode
generateAccessCode(int $chatId)
Definition
chatauthprovider.php:50
Bitrix\Im\Access\ChatAuthProvider\updateChatCodesByRelations
updateChatCodesByRelations(int $chatId)
Definition
chatauthprovider.php:237
Bitrix\Im\Access\ChatAuthProvider\getProviders
static getProviders()
Definition
chatauthprovider.php:32
Bitrix\Im\Access\ChatAuthProvider\isCodeAlreadyExists
isCodeAlreadyExists(int $chatId, int $userId)
Definition
chatauthprovider.php:217
Bitrix\Im\Access\ChatAuthProvider\removeUserCode
removeUserCode(int $chatId, int $userId)
Definition
chatauthprovider.php:309
Bitrix\Im\Access\ChatAuthProvider\getNames
getNames($codes)
Definition
chatauthprovider.php:64
Bitrix\Im\Access\ChatAuthProvider\deleteChatCodes
deleteChatCodes(int $chatId, ?array $userIds=null)
Definition
chatauthprovider.php:169
Bitrix\Im\Access\ChatAuthProvider\deleteByUser
deleteByUser($userId)
Definition
chatauthprovider.php:109
Bitrix\Im\Access\ChatAuthProvider\addChatCodes
addChatCodes(int $chatId, array $userIds)
Definition
chatauthprovider.php:133
Bitrix\Im\Access\ChatAuthProvider\addUserCode
addUserCode(int $chatId, int $userId)
Definition
chatauthprovider.php:297
Bitrix\Im\Access\ChatAuthProvider\ACCESS_CODE_PREFIX
const ACCESS_CODE_PREFIX
Definition
chatauthprovider.php:20
Bitrix\Im\Access\ChatAuthProvider\PROVIDER_ID
const PROVIDER_ID
Definition
chatauthprovider.php:19
Bitrix\Main\Localization\Loc\getMessage
static getMessage($code, $replace=null, $language=null)
Definition
loc.php:29
Bitrix\Main\UserAccessTable
Definition
useraccess.php:24
Bitrix\Im\Access
Definition
chatauthprovider.php:3
modules
im
lib
access
chatauthprovider.php
Создано системой
1.10.0