Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
userregistry.php
1<?php
2
4
5
12
14{
15 private const CACHE_PREFIX = 'sonet_user2group_U';
16 private const CACHE_DIR = '/sonet/userregistry';
17 private const CACHE_TTL = 3 * 60 * 60;
18
19 public const MODE_GROUP_ALL = 'all';
20 public const MODE_GROUP = 'group';
21 public const MODE_PROJECT = 'project';
22 public const MODE_SCRUM = 'scrum';
23 public const MODE_EXCLUDE_SCRAM = 'ex_scram';
24
25 private static array $instance = [];
26
27 private int $userId;
28 private array $userGroups = [];
29 private array $userProjects = [];
30 private array $userWorkgroups = [];
31 private array $userScrum = [];
32
33 public static function getInstance(int $userId): self
34 {
35 if (!array_key_exists($userId, self::$instance))
36 {
37 self::$instance[$userId] = new self($userId);
38 }
39 return self::$instance[$userId];
40 }
41
42 public function getUserGroups(string $mode = self::MODE_GROUP_ALL): array
43 {
44 switch ($mode)
45 {
47 $groups = $this->userWorkgroups;
48 break;
50 $groups = $this->userProjects;
51 break;
53 $groups = $this->userScrum;
54 break;
56 $groups = array_replace($this->userProjects, $this->userWorkgroups);
57 break;
58 default:
59 $groups = $this->userGroups;
60 }
61
62 return $groups;
63 }
64
65 private function __construct(int $userId)
66 {
67 $this->userId = $userId;
68 $this->loadInfo();
69 }
70
71 private function loadInfo(): void
72 {
73 $this->loadGroupInfo();
74 }
75
76 private function loadGroupInfo(): void
77 {
78 $cache = Cache::createInstance();
79
80 if ($cache->initCache(self::CACHE_TTL, $this->getCacheId(), $this->getCacheDir()))
81 {
82 $res = $cache->getVars();
83 }
84 else
85 {
86 $res = UserToGroupTable::query()
87 ->addSelect('GROUP_ID')
88 ->addSelect('ROLE')
89 ->addSelect('WORKGROUP.PROJECT', 'PROJECT')
90 ->addSelect('WORKGROUP.SCRUM_MASTER_ID', 'SCRUM_MASTER')
91 ->registerRuntimeField(
92 new Reference(
93 'WORKGROUP',
94 WorkgroupTable::class,
95 Join::on('this.GROUP_ID', 'ref.ID'),
96 ['join_type' => 'LEFT']
97 )
98 )
99 ->setFilter([
100 '=USER_ID' => $this->userId,
102 ])
103 ->fetchAll();
104
105 $taggedCache = Application::getInstance()->getTaggedCache();
106 $taggedCache->StartTagCache($this->getCacheDir());
107 $taggedCache->RegisterTag($this->getCacheTag());
108
109 $cache->startDataCache();
110 $cache->endDataCache($res);
111 $taggedCache->EndTagCache();
112 }
113
114 foreach ($res as $row)
115 {
116 $this->userGroups[$row['GROUP_ID']] = $row['ROLE'];
117 if ((int)$row['SCRUM_MASTER'] > 0)
118 {
119 $this->userScrum[$row['GROUP_ID']] = $row['ROLE'];
120 }
121 elseif ($row['PROJECT'] === 'Y')
122 {
123 $this->userProjects[$row['GROUP_ID']] = $row['ROLE'];
124 }
125 else
126 {
127 $this->userWorkgroups[$row['GROUP_ID']] = $row['ROLE'];
128 }
129 }
130 }
131
132 private function getCacheTag(): string
133 {
134 return self::CACHE_PREFIX . $this->userId;
135 }
136
137 private function getCacheDir(): string
138 {
139 return self::CACHE_DIR . '/' . substr(md5($this->userId),2,2) . '/';
140 }
141
142 private function getCacheId(): string
143 {
144 return $this->getCacheTag();
145 }
146}
getUserGroups(string $mode=self::MODE_GROUP_ALL)