Skip to content

Commit 3d3e7dc

Browse files
committed
Add checks
Add autoloader-suffix
1 parent 30f41b3 commit 3d3e7dc

5 files changed

Lines changed: 127 additions & 94 deletions

File tree

‎composer.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"phpunit/phpunit": "^11.5"
1111
},
1212
"config": {
13+
"autoloader-suffix": "PluginVip",
1314
"optimize-autoloader": true,
1415
"platform": {
1516
"php": "8.2.99"

‎phpstan-baseline.neon‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,5 +273,5 @@ parameters:
273273
-
274274
message: '#^Class GlpiPlugin\\Vip\\RuleVipCollection constructor invoked with 1 parameter, 0 required\.$#'
275275
identifier: arguments.count
276-
count: 2
276+
count: 1
277277
path: src/Vip.php

‎src/Group.php‎

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,16 @@ public static function install(Migration $migration)
9292
$migration->addField($table, "name", "varchar(100) DEFAULT 'VIP'");
9393
$migration->migrationOneTable($table);
9494
}
95+
96+
// Older versions stored the colour and the icon as free text: bring the rows they left
97+
// behind into the domain prepareInputForUpdate() now enforces.
98+
foreach ($DB->request(['SELECT' => ['id', 'vip_color', 'vip_icon'], 'FROM' => $table]) as $row) {
99+
$color = self::normalizeColor($row['vip_color']);
100+
$icon = self::normalizeIcon($row['vip_icon']);
101+
if ($color !== $row['vip_color'] || $icon !== $row['vip_icon']) {
102+
$DB->update($table, ['vip_color' => $color, 'vip_icon' => $icon], ['id' => $row['id']]);
103+
}
104+
}
95105
}
96106

97107
public static function uninstall()
@@ -323,6 +333,26 @@ private static function getVisibleVipGroup($id)
323333
return false;
324334
}
325335

336+
/**
337+
* Whether the group is flagged as a VIP group.
338+
*/
339+
public static function isVipGroup(int $groups_id): bool
340+
{
341+
$grp = new self();
342+
343+
return $groups_id > 0
344+
&& $grp->getFromDB($groups_id)
345+
&& (int) $grp->fields['isvip'] === 1;
346+
}
347+
348+
/**
349+
* @return int[] ids of the groups flagged as VIP
350+
*/
351+
public static function getVipGroupIds(): array
352+
{
353+
return array_map('intval', array_keys((new self())->find(['isvip' => 1])));
354+
}
355+
326356
public static function getVipName($id)
327357
{
328358
$grp = self::getVisibleVipGroup($id);
@@ -336,16 +366,16 @@ public static function getVipColor($id)
336366
{
337367
$grp = self::getVisibleVipGroup($id);
338368
if ($grp !== false) {
339-
return $grp->fields["vip_color"];
369+
return self::normalizeColor($grp->fields["vip_color"]);
340370
}
341-
return "darkred";
371+
return "#8b0000";
342372
}
343373

344374
public static function getVipIcon($id)
345375
{
346376
$grp = self::getVisibleVipGroup($id);
347377
if ($grp !== false) {
348-
return $grp->fields["vip_icon"];
378+
return self::normalizeIcon($grp->fields["vip_icon"]);
349379
}
350380
return "ti-vip";
351381
}
@@ -411,22 +441,37 @@ public function prepareInputForUpdate($input)
411441
$input['name'] = strip_tags(RichText::getTextFromHtml((string) $input['name']));
412442
}
413443
if (isset($input['vip_icon']) && $input['vip_icon']) {
414-
$icon = strip_tags(RichText::getTextFromHtml($input['vip_icon']));
415-
// Only allow Tabler-like icon class tokens; fall back to the default otherwise.
416-
if (!preg_match('/^[A-Za-z0-9 _-]+$/', $icon)) {
417-
$icon = 'ti-vip';
418-
}
419-
$input['vip_icon'] = $icon;
444+
$input['vip_icon'] = self::normalizeIcon(strip_tags(RichText::getTextFromHtml($input['vip_icon'])));
420445
}
421446
if (isset($input['vip_color'])) {
422-
// Only allow #rgb / #rrggbb hex colors; fall back to the default otherwise.
423-
if (!preg_match('/^#[0-9A-Fa-f]{3}(?:[0-9A-Fa-f]{3})?$/', (string) $input['vip_color'])) {
424-
$input['vip_color'] = '#ff0000';
425-
}
447+
$input['vip_color'] = self::normalizeColor($input['vip_color']);
426448
}
427449
return $input;
428450
}
429451

452+
/**
453+
* Only a #rgb / #rrggbb hex colour, the default otherwise. The value lands in a style
454+
* attribute, where Twig escaping stops a break-out but not an extra CSS declaration.
455+
*
456+
* @param mixed $color
457+
*/
458+
public static function normalizeColor($color): string
459+
{
460+
return preg_match('/^#[0-9A-Fa-f]{3}(?:[0-9A-Fa-f]{3})?$/', (string) $color)
461+
? (string) $color
462+
: '#ff0000';
463+
}
464+
465+
/**
466+
* Only Tabler-like icon class tokens, the default otherwise.
467+
*
468+
* @param mixed $icon
469+
*/
470+
public static function normalizeIcon($icon): string
471+
{
472+
return preg_match('/^[A-Za-z0-9 _-]+$/', (string) $icon) ? (string) $icon : 'ti-vip';
473+
}
474+
430475
/**
431476
* @since version 0.85
432477
*

‎src/RuleVip.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ public function getActions()
132132
$actions['groups_id']['name'] = __('Group');
133133
$actions['groups_id']['type'] = 'dropdown';
134134
$actions['groups_id']['table'] = 'glpi_groups';
135+
// A VIP rule only ever grants a VIP group: Vip::applyRules() enforces it on execution
136+
$vip_groups = Group::getVipGroupIds();
137+
$actions['groups_id']['condition'] = ['glpi_groups.id' => $vip_groups ?: [0]];
135138

136139
return $actions;
137140
}

‎src/Vip.php‎

Lines changed: 64 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -59,101 +59,85 @@ public static function getIcon()
5959

6060
public static function afterAdd(User $user)
6161
{
62-
$rulevip = new RuleVip();
63-
$criterias = $rulevip->getCriterias();
64-
65-
if (isset($user->fields["authtype"])
66-
&& (($user->fields["authtype"] == Auth::LDAP)
67-
|| Auth::isAlternateAuth($user->fields['authtype']))) {
68-
$config_ldap = new AuthLDAP();
69-
$ds = false;
70-
71-
if ($config_ldap->getFromDB($user->fields['auths_id'])) {
72-
$ds = $config_ldap->connect();
73-
}
74-
75-
if ($ds) {
76-
$info = AuthLdap::getUserByDn($ds, $user->fields['user_dn'], []);
77-
}
78-
79-
$input = [];
80-
foreach ($criterias as $criteria) {
81-
if (isset($criteria['field']) && isset($info[$criteria['field']]) && isset($info[$criteria['field']][0])) {
82-
$input[$criteria['field']] = $info[$criteria['field']][0];
83-
}
84-
if (isset($info["dn"])) {
85-
$input["dn"] = $info["dn"];
86-
}
87-
}
88-
89-
$ruleCollection = new RuleVipCollection($user->fields['entities_id']);
90-
$fields = [];
91-
92-
$fields = $ruleCollection->processAllRules($input, $fields, []);
93-
94-
//Store rule that matched
95-
if (isset($fields['groups_id'])) {
96-
$groupuser = new Group_User();
62+
self::applyRules($user);
63+
}
9764

98-
$result = $groupuser->find([
99-
'users_id' => $user->getID(),
100-
'groups_id' => $fields['groups_id'],
101-
]);
102-
if (!$result) {
103-
$groupuser->add(['users_id' => $user->getID(),
104-
'groups_id' => $fields['groups_id']]);
105-
}
106-
}
65+
public static function afterUpdate(User $user)
66+
{
67+
// The hook fires on every update of a user (last login, preferences, tokens...): only
68+
// the fields that change the directory entry the rules read are worth an LDAP bind.
69+
if (!array_intersect(['user_dn', 'authtype', 'auths_id', 'entities_id'], $user->updates)) {
70+
return;
10771
}
72+
73+
self::applyRules($user);
10874
}
10975

110-
public static function afterUpdate(User $user)
76+
/**
77+
* Run the VIP rules against the LDAP entry of a user, and add the user to the VIP group
78+
* the matching rule names.
79+
*/
80+
private static function applyRules(User $user): void
11181
{
112-
$rulevip = new RuleVip();
113-
$criterias = $rulevip->getCriterias();
82+
global $DB;
11483

115-
if (isset($user->fields["authtype"])
116-
&& (($user->fields["authtype"] == Auth::LDAP)
84+
if (!isset($user->fields["authtype"])
85+
|| !(($user->fields["authtype"] == Auth::LDAP)
11786
|| Auth::isAlternateAuth($user->fields['authtype']))) {
118-
$config_ldap = new AuthLDAP();
119-
$ds = false;
87+
return;
88+
}
12089

121-
if ($config_ldap->getFromDB($user->fields['auths_id'])) {
122-
$ds = $config_ldap->connect();
123-
}
90+
// No active VIP rule: skip the LDAP bind altogether
91+
if (count($DB->request([
92+
'COUNT' => 'cpt',
93+
'FROM' => 'glpi_rules',
94+
'WHERE' => ['sub_type' => RuleVip::class, 'is_active' => 1],
95+
])->current()['cpt'] ?? 0) === 0) {
96+
return;
97+
}
12498

125-
if ($ds) {
126-
$info = AuthLdap::getUserByDn($ds, $user->fields['user_dn'], []);
127-
}
99+
$config_ldap = new AuthLDAP();
100+
if (!$config_ldap->getFromDB($user->fields['auths_id'])) {
101+
return;
102+
}
103+
$ds = $config_ldap->connect();
104+
if (!$ds) {
105+
return;
106+
}
107+
$info = AuthLdap::getUserByDn($ds, $user->fields['user_dn'], []);
108+
if (!is_array($info)) {
109+
return;
110+
}
128111

129-
$input = [];
130-
foreach ($criterias as $criteria) {
131-
if (isset($criteria['field']) && isset($info[$criteria['field']]) && isset($info[$criteria['field']][0])) {
132-
$input[$criteria['field']] = $info[$criteria['field']][0];
133-
}
134-
if (isset($info["dn"])) {
135-
$input["dn"] = $info["dn"];
136-
}
112+
$input = [];
113+
foreach ((new RuleVip())->getCriterias() as $criteria) {
114+
if (isset($criteria['field'], $info[$criteria['field']][0])) {
115+
$input[$criteria['field']] = $info[$criteria['field']][0];
137116
}
117+
}
118+
if (isset($info["dn"])) {
119+
$input["dn"] = $info["dn"];
120+
}
138121

139-
$ruleCollection = new RuleVipCollection($user->fields['entities_id']);
140-
$fields = [];
122+
$ruleCollection = new RuleVipCollection($user->fields['entities_id']);
123+
$fields = $ruleCollection->processAllRules($input, [], []);
141124

142-
$fields = $ruleCollection->processAllRules($input, $fields, []);
125+
$groups_id = (int) ($fields['groups_id'] ?? 0);
126+
if ($groups_id <= 0) {
127+
return;
128+
}
143129

144-
//Store rule that matched
145-
if (isset($fields['groups_id'])) {
146-
$groupuser = new Group_User();
130+
// The group comes from a rule action, a value stored by whoever manages the VIP rules:
131+
// the add below runs without any right check, so it must only ever grant a VIP group.
132+
// A rule naming any other group would otherwise hand out membership of any group of
133+
// any entity to the users it matches.
134+
if (!Group::isVipGroup($groups_id)) {
135+
return;
136+
}
147137

148-
$result = $groupuser->find([
149-
'users_id' => $user->getID(),
150-
'groups_id' => $fields['groups_id'],
151-
]);
152-
if (!$result) {
153-
$groupuser->add(['users_id' => $user->getID(),
154-
'groups_id' => $fields['groups_id']]);
155-
}
156-
}
138+
$groupuser = new Group_User();
139+
if (!$groupuser->find(['users_id' => $user->getID(), 'groups_id' => $groups_id])) {
140+
$groupuser->add(['users_id' => $user->getID(), 'groups_id' => $groups_id]);
157141
}
158142
}
159143
}

0 commit comments

Comments
 (0)