Administrators should be able to enable OTP

Created on 6 January 2022, almost 3 years ago
Updated 27 March 2023, over 1 year ago

So. Now that OTP is working, we really need it turned on for a bunch of people. It's not really reasonable to rely on them to all agree to it. It seemed like we could just go to the user edit page and turn it on for each, but unfortunately the module had a bunch of currentUser checks.
So, I added an override if one had the administrator role.

I don't know if this is something everyone would want, but it seems like a thing administrators should be able to do.

Note. Patch below is on top of the prior issue fixing paths.

diff -Nr email_login_otp.fix/email_login_otp.links.menu.yml email_login_otp.admin/email_login_otp.links.menu.yml
0a1,4
> email_login_otp.otp_settings:
> route_name: email_login_otp.otp_settings_form
> title: '2FA Settings'
> menu_name: user
diff -Nr email_login_otp.fix/src/Controller/GeneralController.php email_login_otp.admin/src/Controller/GeneralController.php
85c85
< if ($this->currentUser->id() == $params[2]) {
---
> if (User::load(\Drupal::currentUser()->id())->hasRole('administrator') || $this->currentUser->id() == $params[2]) {
diff -Nr email_login_otp.fix/src/Form/OtpSettingsForm.php email_login_otp.admin/src/Form/OtpSettingsForm.php
4a5
> use Drupal\user\Entity\User;
70a72,73
> $instance->currentPath = $container->get('path.current');
> $instance->currentUserLoad = User::load(\Drupal::currentUser()->id());
86c89,94
< $expirationTime = $this->otp->getExpirationTime($this->currentUser->id());
---
> $uid = $this->currentUser->id();
> $path = $this->currentPath->getPath();
> $params = explode('/', $path);
> if ($this->currentUserLoad->hasRole('administrator') && $this->currentUser->id() != $params[2])
> $uid = $params[2];
> $expirationTime = $this->otp->getExpirationTime($uid);
169a178,183
> $uid = $this->currentUser->id();
> $path = $this->currentPath->getPath();
> $params = explode('/', $path);
> if ($this->currentUserLoad->hasRole('administrator') && $this->currentUser->id() != $params[2])
> $uid = $params[2];
>
173,174c187,188
< $this->otp->storeSettings(['uid' => $this->currentUser->id(), 'email' => $email, 'enabled' => $enabled]);
< $this->otp->expire($this->currentUser->id());
---
> $this->otp->storeSettings(['uid' => $uid, 'email' => $email, 'enabled' => $enabled]);
> $this->otp->expire($uid);
181a196,202
> $uid = $this->currentUser->id();
> $path = $this->currentPath->getPath();
> $params = explode('/', $path);
> if ($this->currentUserLoad->hasRole('administrator') && $this->currentUser->id() != $params[2])
> $uid = $params[2];
>
> $user = User::load($uid);
195c216
< $otp_code = $this->otp->generate($this->currentUser->getDisplayName());
---
> $otp_code = $this->otp->generate($user->getDisplayName());
222c243,248
< $uid = $this->currentUser->id();
---
> $uid = $this->currentUser->id();
> $path = $this->currentPath->getPath();
> $params = explode('/', $path);
> if ($this->currentUserLoad->hasRole('administrator') && $this->currentUser->id() != $params[2])
> $uid = $params[2];
> $user = User::load($uid);
232c258
< return $this->currentUser->getEmail();
---
> return $user->getEmail();

Feature request
Status

Fixed

Version

1.0

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024