Add: Site-wide Two-Factor enforcement per user role#845
Add: Site-wide Two-Factor enforcement per user role#845masteradhoc wants to merge 6 commits intoWordPress:masterfrom
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core SVNIf you're a Core Committer, use this list when committing to GitHub Merge commitsIf you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
There was a problem hiding this comment.
Pull request overview
Adds role-based Two-Factor enforcement to the plugin, allowing admins to mandate 2FA for selected user roles via the settings UI and automatically apply enforcement for existing users (at login) and new users (at registration).
Changes:
- Persist a new
two_factor_enforced_rolesoption and render a role-checkbox “Two-Factor Enforcement” section on the settings page. - Enforce 2FA for users in enforced roles by injecting a provider when the user has none enabled.
- Auto-enable a provider for newly registered users in enforced roles by writing 2FA user meta on
user_register.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
two-factor.php |
Adds enforcement filter for role-based 2FA and auto-enrollment on registration. |
settings/class-two-factor-settings.php |
Adds settings UI + saves enforced roles option. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Fixes #846
What?
Adds a Two-Factor Enforcement section to the plugin's settings page that lets administrators require 2FA for specific user roles.
Why?
There is no built-in way to mandate Two-Factor authentication for all users in a given role. Site owners — especially those running membership or multi-user sites — need a way to enforce 2FA for existing users and automatically enroll new users without requiring each user to opt in manually.
See: https://wordpress.org/support/topic/can-i-by-default-turn-on-this-feature-for-all-my-existing-and-for-new-user/ or #307
How?
Three files were changed:
settings/class-two-factor-settings.phptwo_factor_enforced_rolesoption (array of role slugs) alongside the existing provider option.two-factor.php
two_factor_enforce_for_user()— hooked ontwo_factor_enabled_providers_for_userat priority 20. If a user belongs to an enforced role but has no provider configured, the Email provider is injected at runtime so they are challenged on their next login without any manual setup. If the Email provider is disabled site-wide, the function returns unchanged (fails closed) rather than injecting a provider the user has not configured.two_factor_force_on_user_register()— hooked onuser_register. Writes_two_factor_enabled_providersmeta immediately for new users in an enforced role, so enforcement applies from their very first login. Skipped if the Email provider is disabled site-wide.class-two-factor-core.php
uninstall()now also deletestwo_factor_enabled_providersandtwo_factor_enforced_rolesso no orphaned options are left behind when the plugin is removed.Testing Instructions
_two_factor_enabled_providersuser meta is set to["Two_Factor_Email"]immediately after registration.two_factor_enabled_providersnortwo_factor_enforced_rolesremain in the options table.Screenshots or screencast
Changelog Entry