Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
norelationpermission.php
1
<?php
2
namespace
Bitrix\Im\Disk
;
3
4
use
Bitrix\Im\Access\ChatAuthProvider
;
5
use \Bitrix\Main\Type\DateTime,
6
\
Bitrix\Im\Model\NoRelationPermissionDiskTable
;
7
8
use \Bitrix\Im\Model\RelationTable;
9
10
class
NoRelationPermission
11
{
12
const
ACCESS_TIME
= 86400;
13
const
CACHE_TIME
= 864000;
14
15
public
static
function
add
($chatId, $userId)
16
{
17
$result =
false
;
18
19
$rowRelation = RelationTable::getRow(array(
20
'select'
=> array(
'ID'
),
21
'filter'
=> array(
22
'=CHAT_ID'
=> $chatId,
23
'=USER_ID'
=> $userId
24
),
25
'cache'
=>array(
'ttl'
=>self::CACHE_TIME)
26
));
27
if
(empty($rowRelation))
28
{
29
$provider =
new
ChatAuthProvider
();
30
if
($provider->isCodeAlreadyExists($chatId, $userId))
31
{
32
return
$result;
33
}
34
35
if
(\CIMDisk::ChangeFolderMembers($chatId, $userId))
36
{
37
$raw =
NoRelationPermissionDiskTable::getList
(array(
38
'select'
=> array(
'ID'
),
39
'filter'
=> array(
'=CHAT_ID'
=> $chatId,
'=USER_ID'
=> $userId),
40
'cache'
=>array(
'ttl'
=>self::CACHE_TIME)
41
));
42
43
$count = 0;
44
while
($row = $raw->fetch())
45
{
46
$count++;
47
48
if
($count>1)
49
{
50
NoRelationPermissionDiskTable::delete
($row[
'ID'
]);
51
}
52
else
53
{
54
$updateRaw =
NoRelationPermissionDiskTable::update
($row[
'ID'
], array(
55
'ACTIVE_TO'
=>
DateTime::createFromTimestamp
(time() + self::ACCESS_TIME)
56
));
57
58
if
($updateRaw->isSuccess())
59
$result =
true
;
60
}
61
}
62
63
if
($count === 0)
64
{
65
$addRaw =
NoRelationPermissionDiskTable::add
(array(
66
'CHAT_ID'
=> $chatId,
67
'USER_ID'
=> $userId,
68
'ACTIVE_TO'
=>
DateTime::createFromTimestamp
(time() + self::ACCESS_TIME)
69
));
70
71
if
($addRaw->isSuccess())
72
$result =
true
;
73
}
74
}
75
}
76
77
return
$result;
78
}
79
80
public
static
function
delete
($chatId, $userId, $permissionDisk =
true
)
81
{
82
$result =
false
;
83
84
if
($permissionDisk)
85
{
86
$rowRelation = RelationTable::getRow(array(
87
'select'
=> array(
'ID'
),
88
'filter'
=> array(
89
'=CHAT_ID'
=> $chatId,
90
'=USER_ID'
=> $userId
91
),
92
'cache'
=>array(
'ttl'
=>self::CACHE_TIME)
93
));
94
if
(empty($rowRelation))
95
{
96
if
(\CIMDisk::ChangeFolderMembers($chatId, $userId,
false
))
97
$result =
true
;
98
}
99
}
100
101
$raw =
NoRelationPermissionDiskTable::getList
(array(
102
'select'
=> array(
'ID'
),
103
'filter'
=> array(
'=CHAT_ID'
=> $chatId,
'=USER_ID'
=> $userId),
104
'cache'
=>array(
'ttl'
=>self::CACHE_TIME)
105
));
106
107
while
($row = $raw->fetch())
108
{
109
if
(
NoRelationPermissionDiskTable::delete
($row[
'ID'
])->isSuccess())
110
$result =
true
;
111
}
112
113
return
$result;
114
}
115
116
public
static
function
cleaningAgent
(): string
117
{
118
if
(!\
Bitrix
\Main\
ModuleManager::isModuleInstalled
(
'disk'
))
119
{
120
return
__METHOD__.
'();'
;
121
}
122
123
$connection = \Bitrix\Main\Application::getInstance()->getConnection();
124
125
$relationTbl = RelationTable::getTableName();
126
$noRelationPermTbl =
NoRelationPermissionDiskTable::getTableName
();
127
128
$connection->queryExecute(
"
129
DELETE
130
FROM
131
{$noRelationPermTbl}
132
WHERE (CHAT_ID, USER_ID) in (
133
select CHAT_ID, USER_ID FROM {$relationTbl}
134
)
135
"
);
136
137
$result =
NoRelationPermissionDiskTable::getList
([
138
'select'
=> [
'CHAT_ID'
,
'USER_ID'
],
139
'filter'
=> [
140
'<=ACTIVE_TO'
=>
DateTime::createFromTimestamp
(time())
141
]
142
]);
143
$pseudoRelation = [];
144
while
($row = $result->fetch())
145
{
146
$pseudoRelation[$row[
'CHAT_ID'
]][$row[
'USER_ID'
]] = $row[
'USER_ID'
];
147
}
148
149
$connection->queryExecute(
"DELETE FROM {$noRelationPermTbl} WHERE ACTIVE_TO <= now()"
);
150
151
foreach
($pseudoRelation as $chatId => $userDelete)
152
{
153
\CIMDisk::changeFolderMembers($chatId, $userDelete,
false
);
154
}
155
156
return
__METHOD__.
'();'
;
157
}
158
}
Bitrix\Im\Access\ChatAuthProvider
Definition
chatauthprovider.php:18
Bitrix\Im\Disk\NoRelationPermission
Definition
norelationpermission.php:11
Bitrix\Im\Disk\NoRelationPermission\CACHE_TIME
const CACHE_TIME
Definition
norelationpermission.php:13
Bitrix\Im\Disk\NoRelationPermission\add
static add($chatId, $userId)
Definition
norelationpermission.php:15
Bitrix\Im\Disk\NoRelationPermission\cleaningAgent
static cleaningAgent()
Definition
norelationpermission.php:116
Bitrix\Im\Disk\NoRelationPermission\ACCESS_TIME
const ACCESS_TIME
Definition
norelationpermission.php:12
Bitrix\Im\Model\NoRelationPermissionDiskTable
Definition
norelationpermissiondisk.php:36
Bitrix\Im\Model\NoRelationPermissionDiskTable\getTableName
static getTableName()
Definition
norelationpermissiondisk.php:42
Bitrix\Main\ModuleManager\isModuleInstalled
static isModuleInstalled($moduleName)
Definition
modulemanager.php:67
Bitrix\Main\ORM\Data\DataManager\getList
static getList(array $parameters=array())
Definition
datamanager.php:441
Bitrix\Main\ORM\Data\DataManager\delete
static delete($primary)
Definition
datamanager.php:1632
Bitrix\Main\ORM\Data\DataManager\add
static add(array $data)
Definition
datamanager.php:874
Bitrix\Main\ORM\Data\DataManager\update
static update($primary, array $data)
Definition
datamanager.php:1246
Bitrix\Main\Type\DateTime\createFromTimestamp
static createFromTimestamp($timestamp)
Definition
datetime.php:246
Bitrix\Im\Disk
Definition
norelationpermission.php:2
Bitrix
modules
im
lib
disk
norelationpermission.php
Создано системой
1.10.0