fix(yunohostField) : run commands in background

This commit is contained in:
Florian Schmitt 2024-02-05 14:12:59 +03:00
parent 9d7f33d5c9
commit bd3a126572

View file

@ -50,7 +50,7 @@ class YunohostUserField extends BazarField
$this->usernameField = $values[self::FIELD_USERNAME_FIELD] ?: ''; // empty usernameField will use nameField as username $this->usernameField = $values[self::FIELD_USERNAME_FIELD] ?: ''; // empty usernameField will use nameField as username
$this->userMailDomain = $values[self::FIELD_USER_MAIL_DOMAIN_FIELD] ?: $this->yunohostOptions['mail_domain']; $this->userMailDomain = $values[self::FIELD_USER_MAIL_DOMAIN_FIELD] ?: $this->yunohostOptions['mail_domain'];
$this->mailingList = $values[self::FIELD_MAILING_LIST]; $this->mailingList = $values[self::FIELD_MAILING_LIST];
$this->autoUpdateMail = in_array($values[self::FIELD_AUTO_UPDATE_MAIL], [true,"1",1], true); $this->autoUpdateMail = in_array($values[self::FIELD_AUTO_UPDATE_MAIL], [true, "1", 1], true);
$this->autoAddToGroup = trim(strval($values[self::FIELD_AUTO_ADD_TO_GROUP])); $this->autoAddToGroup = trim(strval($values[self::FIELD_AUTO_ADD_TO_GROUP]));
$this->cmdPrefix = $this->yunohostOptions['cmd_prefix'] ?? 'sudo'; $this->cmdPrefix = $this->yunohostOptions['cmd_prefix'] ?? 'sudo';
@ -71,7 +71,7 @@ class YunohostUserField extends BazarField
protected function getYunohostUsernames() protected function getYunohostUsernames()
{ {
$output = $retval = null; $output = $retval = null;
$cmd = $this->cmdPrefix.' yunohost user list --fields username 2>&1'; $cmd = $this->cmdPrefix . ' yunohost user list --fields username 2>&1';
exec($cmd, $output, $retval); exec($cmd, $output, $retval);
$users = []; $users = [];
foreach ($output as $outputline) { foreach ($output as $outputline) {
@ -93,11 +93,11 @@ class YunohostUserField extends BazarField
// you need to add in /etc/sudoers (with visudo) something like // you need to add in /etc/sudoers (with visudo) something like
// <myunixuser> ALL=(root) NOPASSWD: /usr/bin/yunohost // <myunixuser> ALL=(root) NOPASSWD: /usr/bin/yunohost
// utiliser proc_open avec des arguments plutot que de echaper / concatener // utiliser proc_open avec des arguments plutot que de echaper / concatener
$cmd = $this->cmdPrefix.' yunohost user create '.$userValues['name'] $cmd = $this->cmdPrefix . ' yunohost user create ' . $userValues['name']
.' -F \''.$userValues['fullname'].'\'' . ' -F \'' . $userValues['fullname'] . '\''
.' -d '.$this->userMailDomain . ' -d ' . $this->userMailDomain
.' -p \''.$userValues['password'].'\'' . ' -p \'' . $userValues['password'] . '\''
.' 2>&1'; . ' 2>&1 &';
exec($cmd, $output, $retval); exec($cmd, $output, $retval);
// handle errors // handle errors
if ($retval == 1) { if ($retval == 1) {
@ -110,9 +110,9 @@ class YunohostUserField extends BazarField
throw new Exception($output[0]); throw new Exception($output[0]);
} }
} }
$cmd = $this->cmdPrefix.' yunohost user update '.$userValues['name'] $cmd = $this->cmdPrefix . ' yunohost user update ' . $userValues['name']
.' --add-mailforward '.$userValues[$this->emailField] . ' --add-mailforward ' . $userValues[$this->emailField]
.' 2>&1'; . ' 2>&1 &';
exec($cmd, $output, $retval); exec($cmd, $output, $retval);
//dump($output); //dump($output);
// handle errors // handle errors
@ -144,8 +144,8 @@ class YunohostUserField extends BazarField
if (empty($value) || !$this->isUserByName($value)) { if (empty($value) || !$this->isUserByName($value)) {
$value = $associatedUser['name']; $value = $associatedUser['name'];
$message = str_replace( $message = str_replace(
['{wikiname}','{email}'], ['{wikiname}', '{email}'],
[$value,$associatedUser['email']], [$value, $associatedUser['email']],
_t('BAZ_USER_FIELD_ALREADY_CONNECTED') _t('BAZ_USER_FIELD_ALREADY_CONNECTED')
); );
} }
@ -153,11 +153,11 @@ class YunohostUserField extends BazarField
$associatedUser = $userManager->getOneByName($value); $associatedUser = $userManager->getOneByName($value);
} }
if ($value === $loggedUser['name'] || ($this->getWiki()->UserIsAdmin() && !empty($associatedUser['email']))) { if ($value === $loggedUser['name'] || ($this->getWiki()->UserIsAdmin() && !empty($associatedUser['email']))) {
$message = (!empty($message) ? $message."\n" : '').($this->autoUpdateMail ? str_replace( $message = (!empty($message) ? $message . "\n" : '') . ($this->autoUpdateMail ? str_replace(
'{email}', '{email}',
$associatedUser['email'], $associatedUser['email'],
_t('BAZ_USER_FIELD_ALREADY_CONNECTED_AUTOUPDATE') _t('BAZ_USER_FIELD_ALREADY_CONNECTED_AUTOUPDATE')
): ''); ) : '');
} }
} }
} }
@ -168,8 +168,8 @@ class YunohostUserField extends BazarField
'userIsAdmin' => $this->getWiki()->UserIsAdmin(), 'userIsAdmin' => $this->getWiki()->UserIsAdmin(),
'userName' => $loggedUser['name'] ?? null, 'userName' => $loggedUser['name'] ?? null,
'userEmail' => $loggedUser['email'] ?? null, 'userEmail' => $loggedUser['email'] ?? null,
'forceLabel' => $this->propertyName.self::FORCE_LABEL, 'forceLabel' => $this->propertyName . self::FORCE_LABEL,
'forceLabelChecked' => $_POST[$this->propertyName.self::FORCE_LABEL] ?? false, 'forceLabelChecked' => $_POST[$this->propertyName . self::FORCE_LABEL] ?? false,
]); ]);
} }
@ -184,8 +184,10 @@ class YunohostUserField extends BazarField
$wiki = $this->getWiki(); $wiki = $this->getWiki();
if ($this->getWiki()->UserIsAdmin() if (
&& in_array($_POST[$this->propertyName.self::FORCE_LABEL] ?? false, [true,"true",1,"1"], true)) { $this->getWiki()->UserIsAdmin()
&& in_array($_POST[$this->propertyName . self::FORCE_LABEL] ?? false, [true, "true", 1, "1"], true)
) {
// force entry creation but do not create user if existing for this email // force entry creation but do not create user if existing for this email
$userManager = $this->getService(UserManager::class); $userManager = $this->getService(UserManager::class);
$existingUser = $userManager->getOneByEmail($entry[$this->emailField]); $existingUser = $userManager->getOneByEmail($entry[$this->emailField]);
@ -214,15 +216,16 @@ class YunohostUserField extends BazarField
if ($this->isUserByName($wikiName)) { if ($this->isUserByName($wikiName)) {
$currentWikiName = strtolower($wikiName); $currentWikiName = strtolower($wikiName);
$wikiName = $this->findANewNotExistingUserName($currentWikiName); $wikiName = $this->findANewNotExistingUserName($currentWikiName);
if (!$isImport if (
!$isImport
&& ( && (
!isset($_POST[$this->propertyName.self::CONFIRM_NAME_SUFFIX]) !isset($_POST[$this->propertyName . self::CONFIRM_NAME_SUFFIX])
|| !in_array($_POST[$this->propertyName.self::CONFIRM_NAME_SUFFIX], [true,1,"1"], true) || !in_array($_POST[$this->propertyName . self::CONFIRM_NAME_SUFFIX], [true, 1, "1"], true)
) )
) { ) {
throw new UserFieldException( throw new UserFieldException(
$this->render("@bazar/inputs/user-confirm.twig", [ $this->render("@bazar/inputs/user-confirm.twig", [
'confirmName' => $this->propertyName.self::CONFIRM_NAME_SUFFIX, 'confirmName' => $this->propertyName . self::CONFIRM_NAME_SUFFIX,
'wikiName' => $currentWikiName, 'wikiName' => $currentWikiName,
'newWikiName' => $wikiName, 'newWikiName' => $wikiName,
]) ])
@ -254,12 +257,12 @@ class YunohostUserField extends BazarField
'password' => $entry['mot_de_passe_wikini'] 'password' => $entry['mot_de_passe_wikini']
]); ]);
} catch (UserNameAlreadyUsedException $ex) { } catch (UserNameAlreadyUsedException $ex) {
throw new UserFieldException('yeswiki : '._t('BAZ_USER_FIELD_EXISTING_USER_BY_EMAIL')); throw new UserFieldException('yeswiki : ' . _t('BAZ_USER_FIELD_EXISTING_USER_BY_EMAIL'));
} catch (Exception $ex) { } catch (Exception $ex) {
throw new UserFieldException('yeswiki : '.$ex->getMessage(), $ex->getCode(), $ex); throw new UserFieldException('yeswiki : ' . $ex->getMessage(), $ex->getCode(), $ex);
} }
} catch (UserNameAlreadyUsedException $ex) { } catch (UserNameAlreadyUsedException $ex) {
throw new UserFieldException(_t('L\'utilisateur.ice '.$wikiName.' est déjà utilisé, veuillez vous connecter si c\'est vous, ou utiliser un autre nom d\'utilisateur.ice.')); throw new UserFieldException(_t('L\'utilisateur.ice ' . $wikiName . ' est déjà utilisé, veuillez vous connecter si c\'est vous, ou utiliser un autre nom d\'utilisateur.ice.'));
} catch (Exception $ex) { } catch (Exception $ex) {
throw new UserFieldException($ex->getMessage(), $ex->getCode(), $ex); throw new UserFieldException($ex->getMessage(), $ex->getCode(), $ex);
} }
@ -287,8 +290,8 @@ class YunohostUserField extends BazarField
'fields-to-remove' => [ 'fields-to-remove' => [
'mot_de_passe_wikini', 'mot_de_passe_wikini',
'mot_de_passe_repete_wikini', 'mot_de_passe_repete_wikini',
$this->propertyName.self::CONFIRM_NAME_SUFFIX, $this->propertyName . self::CONFIRM_NAME_SUFFIX,
$this->propertyName.self::FORCE_LABEL, $this->propertyName . self::FORCE_LABEL,
] ]
]; ];
} }
@ -372,7 +375,8 @@ class YunohostUserField extends BazarField
$userManager = $this->getService(UserManager::class); $userManager = $this->getService(UserManager::class);
$user = $userManager->getOneByName($userName); $user = $userManager->getOneByName($userName);
$loggedUser = $authController->getLoggedUser(); $loggedUser = $authController->getLoggedUser();
if (!empty($user) if (
!empty($user)
&& ( && (
$this->getWiki()->UserIsAdmin() $this->getWiki()->UserIsAdmin()
|| ( || (
@ -383,7 +387,7 @@ class YunohostUserField extends BazarField
&& $user['email'] !== $email && $user['email'] !== $email
) { ) {
try { try {
$userController->update($user, ['email'=>$email]); $userController->update($user, ['email' => $email]);
} catch (UserNameAlreadyUsedException $ex) { } catch (UserNameAlreadyUsedException $ex) {
throw new UserFieldException(_t('BAZ_USER_FIELD_EXISTING_USER_BY_EMAIL')); throw new UserFieldException(_t('BAZ_USER_FIELD_EXISTING_USER_BY_EMAIL'));
} catch (Exception $ex) { } catch (Exception $ex) {
@ -430,8 +434,8 @@ class YunohostUserField extends BazarField
foreach ($groupsNames as $groupName) { foreach ($groupsNames as $groupName) {
$previousACL = !in_array($groupName, $existingsGroups, true) $previousACL = !in_array($groupName, $existingsGroups, true)
? '' ? ''
: $wiki->GetGroupACL($groupName)."\n"; : $wiki->GetGroupACL($groupName) . "\n";
$wiki->SetGroupACL($groupName, $previousACL.$wikiName); $wiki->SetGroupACL($groupName, $previousACL . $wikiName);
} }
} }
} }
@ -463,7 +467,7 @@ class YunohostUserField extends BazarField
$baseWikiName = preg_replace("/[0-9]*$/", "", $firstWikiName); $baseWikiName = preg_replace("/[0-9]*$/", "", $firstWikiName);
// a loop 1000 should be enough // a loop 1000 should be enough
for ($i=1; $i < 1000; $i++) { for ($i = 1; $i < 1000; $i++) {
$newName = "$baseWikiName$i"; $newName = "$baseWikiName$i";
if (!$this->isUserByName($newName)) { if (!$this->isUserByName($newName)) {
return $newName; return $newName;
@ -475,4 +479,3 @@ class YunohostUserField extends BazarField
throw new UserFieldException('Impossible to find a new user name !'); throw new UserFieldException('Impossible to find a new user name !');
} }
} }