16 private const CACHE_PREFIX =
'sonet_user2group_U';
17 private const CACHE_DIR =
'/sonet/userregistry';
18 private const CACHE_TTL = 3 * 60 * 60;
27 private static array $instance = [];
30 private array $userGroups = [];
31 private array $userProjects = [];
32 private array $userWorkgroups = [];
33 private array $userScrum = [];
34 private array $userCollabs = [];
38 if (!array_key_exists($userId, self::$instance))
40 self::$instance[$userId] =
new self($userId);
42 return self::$instance[$userId];
49 case self::MODE_GROUP:
50 $groups = $this->userWorkgroups;
52 case self::MODE_PROJECT:
55 case self::MODE_SCRUM:
58 case self::MODE_COLLAB:
61 case self::MODE_EXCLUDE_SCRAM:
62 $groups = array_replace($this->userProjects, $this->userWorkgroups);
73 Cache::createInstance()->cleanDir($this->getCacheDir($userId));
75 $this->loadGroupInfo();
80 private function __construct(
int $userId)
83 $this->loadGroupInfo();
86 private function loadGroupInfo(): void
88 $cache = Cache::createInstance();
90 if ($cache->initCache(self::CACHE_TTL, $this->getCacheTag(), $this->getCacheDir()))
92 $res = $cache->getVars();
96 $res = UserToGroupTable::query()
97 ->addSelect(
'GROUP_ID')
99 ->addSelect(
'WORKGROUP.PROJECT',
'PROJECT')
100 ->addSelect(
'WORKGROUP.TYPE',
'TYPE')
101 ->addSelect(
'WORKGROUP.SCRUM_MASTER_ID',
'SCRUM_MASTER')
102 ->registerRuntimeField(
105 WorkgroupTable::class,
106 Join::on(
'this.GROUP_ID',
'ref.ID'),
107 [
'join_type' =>
'LEFT']
111 '=USER_ID' => $this->userId,
116 $taggedCache = Application::getInstance()->getTaggedCache();
117 $taggedCache->StartTagCache($this->getCacheDir());
118 $taggedCache->RegisterTag($this->getCacheTag());
120 $cache->startDataCache();
121 $cache->endDataCache(
$res);
122 $taggedCache->EndTagCache();
125 foreach (
$res as $row)
127 $this->userGroups[$row[
'GROUP_ID']] = $row[
'ROLE'];
128 if ((
int)$row[
'SCRUM_MASTER'] > 0)
130 $this->userScrum[$row[
'GROUP_ID']] = $row[
'ROLE'];
132 elseif ($row[
'PROJECT'] ===
'Y')
134 $this->userProjects[$row[
'GROUP_ID']] = $row[
'ROLE'];
136 elseif ($row[
'TYPE'] === Type::Collab->value)
138 $this->userCollabs[$row[
'GROUP_ID']] = $row[
'ROLE'];
142 $this->userWorkgroups[$row[
'GROUP_ID']] = $row[
'ROLE'];
147 private function getCacheTag(): string
152 private function getCacheDir(?
int $userId =
null): string
154 return self::CACHE_DIR .
'/' . substr(md5($userId ?? $this->userId),2,2) .
'/';
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)