MandatoryTwoFactor.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Authentication\TwoFactorAuth;
  8. use OCP\IConfig;
  9. use OCP\IGroupManager;
  10. use OCP\IUser;
  11. class MandatoryTwoFactor {
  12. /** @var IConfig */
  13. private $config;
  14. /** @var IGroupManager */
  15. private $groupManager;
  16. public function __construct(IConfig $config, IGroupManager $groupManager) {
  17. $this->config = $config;
  18. $this->groupManager = $groupManager;
  19. }
  20. /**
  21. * Get the state of enforced two-factor auth
  22. */
  23. public function getState(): EnforcementState {
  24. return new EnforcementState(
  25. $this->config->getSystemValue('twofactor_enforced', 'false') === 'true',
  26. $this->config->getSystemValue('twofactor_enforced_groups', []),
  27. $this->config->getSystemValue('twofactor_enforced_excluded_groups', [])
  28. );
  29. }
  30. /**
  31. * Set the state of enforced two-factor auth
  32. */
  33. public function setState(EnforcementState $state) {
  34. $this->config->setSystemValue('twofactor_enforced', $state->isEnforced() ? 'true' : 'false');
  35. $this->config->setSystemValue('twofactor_enforced_groups', $state->getEnforcedGroups());
  36. $this->config->setSystemValue('twofactor_enforced_excluded_groups', $state->getExcludedGroups());
  37. }
  38. /**
  39. * Check if two-factor auth is enforced for a specific user
  40. *
  41. * The admin(s) can enforce two-factor auth system-wide, for certain groups only
  42. * and also have the option to exclude users of certain groups. This method will
  43. * check their membership of those groups.
  44. *
  45. * @param IUser $user
  46. *
  47. * @return bool
  48. */
  49. public function isEnforcedFor(IUser $user): bool {
  50. $state = $this->getState();
  51. if (!$state->isEnforced()) {
  52. return false;
  53. }
  54. $uid = $user->getUID();
  55. /*
  56. * If there is a list of enforced groups, we only enforce 2FA for members of those groups.
  57. * For all the other users it is not enforced (overruling the excluded groups list).
  58. */
  59. if (!empty($state->getEnforcedGroups())) {
  60. foreach ($state->getEnforcedGroups() as $group) {
  61. if ($this->groupManager->isInGroup($uid, $group)) {
  62. return true;
  63. }
  64. }
  65. // Not a member of any of these groups -> no 2FA enforced
  66. return false;
  67. }
  68. /**
  69. * If the user is member of an excluded group, 2FA won't be enforced.
  70. */
  71. foreach ($state->getExcludedGroups() as $group) {
  72. if ($this->groupManager->isInGroup($uid, $group)) {
  73. return false;
  74. }
  75. }
  76. /**
  77. * No enforced groups configured and user not member of an excluded groups,
  78. * so 2FA is enforced.
  79. */
  80. return true;
  81. }
  82. }