GenerateSecurePasswordEvent.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCP\Security\Events;
  8. use OCP\EventDispatcher\Event;
  9. use OCP\Security\PasswordContext;
  10. /**
  11. * Event to request a secure password to be generated
  12. * @since 18.0.0
  13. */
  14. class GenerateSecurePasswordEvent extends Event {
  15. private ?string $password;
  16. /**
  17. * Request a secure password to be generated.
  18. *
  19. * By default passwords are generated for the user account context,
  20. * this can be adjusted by passing another `PasswordContext`.
  21. * @since 31.0.0
  22. */
  23. public function __construct(
  24. private PasswordContext $context = PasswordContext::ACCOUNT,
  25. ) {
  26. parent::__construct();
  27. $this->password = null;
  28. }
  29. /**
  30. * Get the generated password.
  31. *
  32. * If a password generator is registered and successfully generated a password
  33. * that password can get read back. Otherwise `null` is returned.
  34. * @since 18.0.0
  35. */
  36. public function getPassword(): ?string {
  37. return $this->password;
  38. }
  39. /**
  40. * Set the generated password.
  41. *
  42. * This is used by password generators to set the generated password.
  43. * @since 18.0.0
  44. */
  45. public function setPassword(string $password): void {
  46. $this->password = $password;
  47. }
  48. /**
  49. * Get the context this password should generated for.
  50. * @since 31.0.0
  51. */
  52. public function getContext(): PasswordContext {
  53. return $this->context;
  54. }
  55. }