2828 * -------------------------------------------------------------------------
2929 */
3030
31-
31+ use function Safe \ preg_split ;
3232
3333class PluginDatainjectionUserInjection extends User implements PluginDatainjectionInjectionInterface
3434{
@@ -55,14 +55,20 @@ public function connectedTo()
5555
5656 public function isNullable ($ field )
5757 {
58- return in_array ($ field , [
59- 'begin_date ' ,
60- 'date_sync ' ,
61- 'end_date ' ,
62- 'last_login ' ,
63- 'substitution_end_date ' ,
64- 'substitution_start_date ' ,
65- ]);
58+ // Fields that cannot accept empty string values and should use their default values instead
59+ $ non_nullable_fields = [
60+ 'use_mode ' ,
61+ 'highcontrast_css ' ,
62+ 'default_central_tab ' ,
63+ 'entities_id ' ,
64+ 'profiles_id ' ,
65+ 'is_active ' ,
66+ 'is_deleted ' ,
67+ 'authtype ' ,
68+ 'auths_id ' ,
69+ ];
70+
71+ return !in_array ($ field , $ non_nullable_fields );
6672 }
6773
6874
@@ -86,6 +92,8 @@ public function getOptions($primary_type = '')
8692 $ tab [4 ]['displaytype ' ] = 'password ' ;
8793
8894 $ tab [5 ]['displaytype ' ] = 'text ' ;
95+ // Add email option to make it importable with the correct linkfield
96+ $ tab [5 ]['linkfield ' ] = 'useremails_id ' ; // Map email field to useremails_id for injection
8997
9098 //To manage groups : relies on a CommonDBRelation object !
9199 $ tab [100 ]['name ' ] = __s ('Group ' );
@@ -153,6 +161,20 @@ public function addSpecificNeededFields($primary_type, $values)
153161 }
154162
155163
164+ /**
165+ * @param array $values
166+ */
167+ public function reformat (&$ values )
168+ {
169+ // Remove token fields to avoid double encryption and length issues
170+ $ tokens = ['password_forget_token ' , 'personal_token ' , 'api_token ' , 'cookie_token ' ];
171+ foreach ($ tokens as $ token ) {
172+ if (isset ($ values ['User ' ][$ token ])) {
173+ unset($ values ['User ' ][$ token ]);
174+ }
175+ }
176+ }
177+
156178 /**
157179 * @param array $values
158180 * @param boolean $add (true by default)
@@ -164,17 +186,30 @@ public function processAfterInsertOrUpdate($values, $add = true, $rights = [])
164186 global $ DB ;
165187
166188 //Manage user emails
167- if (isset ($ values ['User ' ]['useremails_id ' ]) && $ rights ['add_dropdown ' ] && Session::haveRight ('user ' , UPDATE ) && !countElementsInTable (
168- "glpi_useremails " ,
169- [
170- 'users_id ' => $ values ['User ' ]['id ' ],
171- 'email ' => $ values ['User ' ]['useremails_id ' ],
172- ],
173- )) {
174- $ useremail = new UserEmail ();
175- $ tmp ['users_id ' ] = $ values ['User ' ]['id ' ];
176- $ tmp ['email ' ] = $ values ['User ' ]['useremails_id ' ];
177- $ useremail ->add ($ tmp );
189+ if (isset ($ values ['User ' ]['useremails_id ' ]) && $ rights ['add_dropdown ' ] && Session::haveRight ('user ' , UPDATE )) {
190+ $ emails = preg_split ('/[\s,;]+/ ' , $ values ['User ' ]['useremails_id ' ], -1 , PREG_SPLIT_NO_EMPTY );
191+ foreach ($ emails as $ email ) {
192+ $ email = trim ($ email );
193+ if (filter_var ($ email , FILTER_VALIDATE_EMAIL ) && !countElementsInTable (
194+ "glpi_useremails " ,
195+ [
196+ 'users_id ' => $ values ['User ' ]['id ' ],
197+ 'email ' => $ email ,
198+ ],
199+ )) {
200+ $ useremail = new UserEmail ();
201+ $ tmp = [
202+ 'users_id ' => $ values ['User ' ]['id ' ],
203+ 'email ' => $ email ,
204+ 'is_default ' => 0 ,
205+ ];
206+ // If user has no emails, set this one as default
207+ if (!countElementsInTable ("glpi_useremails " , ['users_id ' => $ values ['User ' ]['id ' ]])) {
208+ $ tmp ['is_default ' ] = 1 ;
209+ }
210+ $ useremail ->add ($ tmp );
211+ }
212+ }
178213 }
179214
180215 if (isset ($ values ['User ' ]['password ' ]) && ($ values ['User ' ]['password ' ] != '' )) {
0 commit comments