15 private const CACHE_PREFIX =
'sonet_user2group_U';
16 private const CACHE_DIR =
'/sonet/userregistry';
17 private const CACHE_TTL = 3 * 60 * 60;
25 private static array $instance = [];
28 private array $userGroups = [];
29 private array $userProjects = [];
30 private array $userWorkgroups = [];
31 private array $userScrum = [];
35 if (!array_key_exists($userId, self::$instance))
37 self::$instance[$userId] =
new self($userId);
39 return self::$instance[$userId];
42 public function getUserGroups(
string $mode = self::MODE_GROUP_ALL): array
47 $groups = $this->userWorkgroups;
50 $groups = $this->userProjects;
53 $groups = $this->userScrum;
56 $groups = array_replace($this->userProjects, $this->userWorkgroups);
59 $groups = $this->userGroups;
65 private function __construct(
int $userId)
67 $this->userId = $userId;
71 private function loadInfo(): void
73 $this->loadGroupInfo();
76 private function loadGroupInfo(): void
78 $cache = Cache::createInstance();
80 if ($cache->initCache(self::CACHE_TTL, $this->getCacheId(), $this->getCacheDir()))
82 $res = $cache->getVars();
86 $res = UserToGroupTable::query()
87 ->addSelect(
'GROUP_ID')
89 ->addSelect(
'WORKGROUP.PROJECT',
'PROJECT')
90 ->addSelect(
'WORKGROUP.SCRUM_MASTER_ID',
'SCRUM_MASTER')
91 ->registerRuntimeField(
94 WorkgroupTable::class,
95 Join::on(
'this.GROUP_ID',
'ref.ID'),
96 [
'join_type' =>
'LEFT']
100 '=USER_ID' => $this->userId,
106 $taggedCache->StartTagCache($this->getCacheDir());
107 $taggedCache->RegisterTag($this->getCacheTag());
109 $cache->startDataCache();
110 $cache->endDataCache($res);
111 $taggedCache->EndTagCache();
114 foreach ($res as $row)
116 $this->userGroups[$row[
'GROUP_ID']] = $row[
'ROLE'];
117 if ((
int)$row[
'SCRUM_MASTER'] > 0)
119 $this->userScrum[$row[
'GROUP_ID']] = $row[
'ROLE'];
121 elseif ($row[
'PROJECT'] ===
'Y')
123 $this->userProjects[$row[
'GROUP_ID']] = $row[
'ROLE'];
127 $this->userWorkgroups[$row[
'GROUP_ID']] = $row[
'ROLE'];
132 private function getCacheTag(): string
134 return self::CACHE_PREFIX . $this->userId;
137 private function getCacheDir(): string
139 return self::CACHE_DIR .
'/' . substr(md5($this->userId),2,2) .
'/';
142 private function getCacheId(): string
144 return $this->getCacheTag();