@@ -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