Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow any user to manage two-factor #16130

Merged
merged 33 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
0cdf97d
Allow any user to manage two-factor
MikeAlhayek May 21, 2024
b2666ce
Merge branch 'main' into ma/fix-2fa-admin-attribute
MikeAlhayek May 22, 2024
f82fe05
Use email confirmation for 2FA
MikeAlhayek May 22, 2024
9a860a2
Merge branch 'ma/fix-2fa-admin-attribute' of https://github.com/Orcha…
MikeAlhayek May 22, 2024
84be641
cleanup
MikeAlhayek May 22, 2024
aa62415
document the breaking change
MikeAlhayek May 22, 2024
a0ea4fd
Fix tests
MikeAlhayek May 22, 2024
c15e1d5
address feedback
MikeAlhayek May 24, 2024
3dddeb0
Merge branch 'main' into ma/fix-2fa-admin-attribute
MikeAlhayek May 24, 2024
5b91f72
Fix build
MikeAlhayek May 24, 2024
ac57bc4
Merge branch 'ma/fix-2fa-admin-attribute' of https://github.com/Orcha…
MikeAlhayek May 24, 2024
4415a69
docs
MikeAlhayek May 24, 2024
a8810a6
Use Layout-TwoFactor instead of changing the AdminAttribute
MikeAlhayek May 24, 2024
f04b3f4
fix settings driver
MikeAlhayek May 24, 2024
bdc5b42
Merge branch 'main' into ma/fix-2fa-admin-attribute
MikeAlhayek May 24, 2024
5bc5bbe
Prevent a breaking change
MikeAlhayek May 25, 2024
9189352
Merge branch 'main' into ma/fix-2fa-admin-attribute
MikeAlhayek May 25, 2024
3b35ba9
address feedback
MikeAlhayek May 26, 2024
d9a83f9
Merge branch 'main' into ma/fix-2fa-admin-attribute
MikeAlhayek May 26, 2024
9fb580c
Merge branch 'main' into ma/fix-2fa-admin-attribute
MikeAlhayek May 27, 2024
7c6a0c5
remove email authenticator controller
MikeAlhayek May 27, 2024
56256c4
Merge branch 'ma/fix-2fa-admin-attribute' of https://github.com/Orcha…
MikeAlhayek May 27, 2024
ed5fd2e
Merge remote-tracking branch 'origin/main' into ma/fix-2fa-admin-attr…
MikeAlhayek May 27, 2024
846537a
cleanup
MikeAlhayek May 27, 2024
ea8204f
Merge branch 'main' into ma/fix-2fa-admin-attribute
MikeAlhayek May 27, 2024
28635b6
Fix filter
MikeAlhayek May 27, 2024
b6a6925
Merge branch 'main' into ma/fix-2fa-admin-attribute
MikeAlhayek May 27, 2024
7542d66
cleanup
MikeAlhayek May 27, 2024
97bba26
Merge branch 'ma/fix-2fa-admin-attribute' of https://github.com/Orcha…
MikeAlhayek May 27, 2024
7b42e70
Update src/OrchardCore/OrchardCore.Admin.Abstractions/AdminPermission…
MikeAlhayek May 27, 2024
e98a6d3
change fontawesome placement
MikeAlhayek May 27, 2024
3f9c1d2
Merge branch 'ma/fix-2fa-admin-attribute' of https://github.com/Orcha…
MikeAlhayek May 27, 2024
06e6cec
fix layout
MikeAlhayek May 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
@using OrchardCore.DisplayManagement.ModelBinding
@using Microsoft.AspNetCore.Authorization
@using OrchardCore.Admin
@using OrchardCore.Notifications.Endpoints.Management

@inject IDisplayManager<Notification> NotificationDisplayDriver
@inject IUpdateModelAccessor UpdateModelAccessor
Expand Down Expand Up @@ -52,10 +53,13 @@
{
<li class="text-center">@T["You have no unread notifications."]</li>
}
<li>
<hr class="dropdown-divider">
</li>
@if (await AuthorizationService.AuthorizeAsync(ViewContext.HttpContext.User, AdminPermissions.AccessAdminPanel))
{
<li>
<a class="dropdown-item fw-bold" asp-action="List" asp-controller="Admin" asp-area="OrchardCore.Notifications">@T["Notification Center"]</a>
<a class="dropdown-item fw-bold" asp-action="List" asp-controller="Admin" asp-area="@NotificationConstants.Features.Notifications">@T["Notification Center"]</a>
</li>
}
</ul>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ public async Task<IActionResult> IndexPost(RequestCodeSmsAuthenticatorViewModel

var currentPhoneNumber = await UserManager.GetPhoneNumberAsync(user);

var canSetNewPhone = settings.AllowChangingPhoneNumber
|| string.IsNullOrEmpty(currentPhoneNumber)
|| !_phoneFormatValidator.IsValid(currentPhoneNumber);
var canSetNewPhone = settings.AllowChangingPhoneNumber ||
string.IsNullOrEmpty(currentPhoneNumber) ||
!_phoneFormatValidator.IsValid(currentPhoneNumber);

model.AllowChangingPhoneNumber = canSetNewPhone;

Expand Down Expand Up @@ -206,43 +206,6 @@ public async Task<IActionResult> ValidateCode(EnableSmsAuthenticatorViewModel mo
return View(model);
}

// TODO: move this action into minimal API.
[HttpPost]
[Produces("application/json")]
[AllowAnonymous]
public async Task<IActionResult> SendCode()
{
var user = await SignInManager.GetTwoFactorAuthenticationUserAsync();
var errorMessage = S["The SMS message could not be sent. Please attempt to request the code at a later time."];

if (user == null)
{
return BadRequest(new
{
success = false,
message = errorMessage.Value,
});
}

var settings = (await SiteService.GetSiteSettingsAsync()).As<SmsAuthenticatorLoginSettings>();
var code = await UserManager.GenerateTwoFactorTokenAsync(user, _identityOptions.Tokens.ChangePhoneNumberTokenProvider);

var message = new SmsMessage()
{
To = await UserManager.GetPhoneNumberAsync(user),
Body = await GetBodyAsync(settings, user, code),
};

var result = await _smsService.SendAsync(message);

return Ok(new
{
success = result.Succeeded,
message = result.Succeeded ? S["A verification code has been sent to your phone number. Please check your device for the code."].Value
: errorMessage.Value,
});
}

private Task<string> GetBodyAsync(SmsAuthenticatorLoginSettings settings, IUser user, string code)
{
var message = string.IsNullOrWhiteSpace(settings.Body)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ internal static async Task<IUser> RegisterUser(this Controller controller, Regis
UserName = model.UserName,
Email = model.Email,
EmailConfirmed = !settings.UsersMustValidateEmail,
IsEnabled = !settings.UsersAreModerated
IsEnabled = !settings.UsersAreModerated,
}, model.Password, controller.ModelState.AddModelError) as User;

if (user != null && controller.ModelState.IsValid)
Expand Down Expand Up @@ -114,11 +114,15 @@ internal static async Task<string> SendEmailConfirmationTokenAsync(this Controll
new
{
userId = user.UserId,
code
code,
},
protocol: controller.HttpContext.Request.Scheme);

await SendEmailAsync(controller, user.Email, subject, new ConfirmEmailViewModel() { User = user, ConfirmEmailUrl = callbackUrl });
await SendEmailAsync(controller, user.Email, subject, new ConfirmEmailViewModel()
{
User = user,
ConfirmEmailUrl = callbackUrl,
});

return callbackUrl;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public class TwoFactorAuthenticationAuthorizationFilter : IAsyncAuthorizationFil
[
typeof(EmailConfirmationController).ControllerName(),
typeof(TwoFactorAuthenticationController).ControllerName(),
typeof(EmailAuthenticatorController).ControllerName(),
typeof(AuthenticatorAppController).ControllerName(),
typeof(SmsAuthenticatorController).ControllerName(),
];
Expand All @@ -42,7 +41,8 @@ public async Task OnAuthorizationAsync(AuthorizationFilterContext context)

if (context.HttpContext?.User?.Identity?.IsAuthenticated == false ||
context.HttpContext.Request.Path.Equals("/" + _userOptions.LogoffPath, StringComparison.OrdinalIgnoreCase) ||
context.HttpContext.Request.Path.Equals("/" + _userOptions.TwoFactorAuthenticationPath, StringComparison.OrdinalIgnoreCase))
context.HttpContext.Request.Path.Equals("/" + _userOptions.TwoFactorAuthenticationPath, StringComparison.OrdinalIgnoreCase) ||
context.HttpContext.Request.Path.Equals("/TwoFactor-Authenticator/", StringComparison.OrdinalIgnoreCase))
{
return;
}
Expand Down
Piedone marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ namespace OrchardCore.Users.Services
{
/// <summary>
/// Provides the theme defined in the site configuration for the current scope (request).
/// This selector provides AdminTheme as default or fallback for Account|Registration|ResetPassword
/// This selector provides AdminTheme as default or fallback for Account for Registration,
/// ResetPassword, TwoFactorAuthentication, SmsAuthenticator and AuthenticatorApp
/// controllers based on SiteSettings.
/// The same <see cref="ThemeSelectorResult"/> is returned if called multiple times
/// during the same scope.
Expand Down
Loading