Skip to content

Commit

Permalink
Merge pull request #586 from City-of-Helsinki/UHF-8875-randomize-admi…
Browse files Browse the repository at this point in the history
…n-password

UHF-8875: generate random password for uid 1 user
  • Loading branch information
hyrsky authored Sep 25, 2023
2 parents 1b07d5f + 1385701 commit f3a9eff
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions modules/helfi_user_roles/helfi_user_roles.install
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
declare(strict_types = 1);

use Drupal\user\Entity\Role;
use Drupal\user\Entity\User;

/**
* Grants required permissions.
Expand Down Expand Up @@ -71,3 +72,30 @@ function helfi_user_roles_update_9001() : void {
])
->save();
}

/**
* Randomize password for uid 1 user.
*/
function helfi_user_roles_update_9002() : void {
try {
// Attempt to resolve active environment. If this throws an exception, this
// is not running in the main instances, and we don't want to change the
// password.
\Drupal::service('helfi_api_base.environment_resolver')->getActiveEnvironment();
}
catch (\InvalidArgumentException) {
return;
}

$user = User::load(1);
if (empty($user)) {
return;
}

// Random password, 192 bits of entropy.
$password = base64_encode(random_bytes(24));

$user
->setPassword($password)
->save();
}

0 comments on commit f3a9eff

Please sign in to comment.