-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow Admin to disable Two-Factor Authentication (#16899)
- Loading branch information
1 parent
5417cbb
commit 50e9bba
Showing
16 changed files
with
208 additions
and
38 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
...chardCore.Modules/OrchardCore.Users/Controllers/AdminTwoFactorAuthenticationController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.AspNetCore.Mvc; | ||
using OrchardCore.Admin; | ||
using OrchardCore.Modules; | ||
using OrchardCore.Mvc.Core.Utilities; | ||
|
||
namespace OrchardCore.Users.Controllers; | ||
|
||
[Admin] | ||
[Feature(UserConstants.Features.TwoFactorAuthentication)] | ||
public sealed class AdminTwoFactorAuthenticationController : Controller | ||
{ | ||
private readonly UserManager<IUser> _userManager; | ||
private readonly IAuthorizationService _authorizationService; | ||
|
||
public AdminTwoFactorAuthenticationController( | ||
UserManager<IUser> userManager, | ||
IAuthorizationService authorizationService) | ||
{ | ||
_userManager = userManager; | ||
_authorizationService = authorizationService; | ||
} | ||
|
||
public async Task<IActionResult> Disable(string id) | ||
{ | ||
if (!await _authorizationService.AuthorizeAsync(User, CommonPermissions.DisableTwoFactorAuthenticationForUsers)) | ||
{ | ||
return Forbid(); | ||
} | ||
|
||
var user = await _userManager.FindByIdAsync(id); | ||
|
||
if (user == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
|
||
|
||
if (await _userManager.GetTwoFactorEnabledAsync(user)) | ||
{ | ||
await _userManager.SetTwoFactorEnabledAsync(user, false); | ||
} | ||
|
||
return RedirectToAction(nameof(AdminController.Index), typeof(AdminController).ControllerName()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/OrchardCore.Modules/OrchardCore.Users/Drivers/UserRegistrationAdminDisplayDriver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using OrchardCore.DisplayManagement.Handlers; | ||
using OrchardCore.DisplayManagement.Views; | ||
using OrchardCore.Users.Models; | ||
using OrchardCore.Users.ViewModels; | ||
|
||
namespace OrchardCore.Users.Drivers; | ||
|
||
public sealed class UserRegistrationAdminDisplayDriver : DisplayDriver<User> | ||
{ | ||
public override Task<IDisplayResult> DisplayAsync(User user, BuildDisplayContext context) | ||
{ | ||
return CombineAsync( | ||
Initialize<SummaryAdminUserViewModel>("UserSendConfirmationActionsMenu", model => model.User = user) | ||
.Location("SummaryAdmin", "ActionsMenu:15") | ||
); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/OrchardCore.Modules/OrchardCore.Users/Drivers/UserTwoFactorDisplayDriver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using OrchardCore.DisplayManagement.Handlers; | ||
using OrchardCore.DisplayManagement.Views; | ||
using OrchardCore.Users.Models; | ||
using OrchardCore.Users.ViewModels; | ||
|
||
namespace OrchardCore.Users.Drivers; | ||
|
||
public sealed class UserTwoFactorDisplayDriver : DisplayDriver<User> | ||
{ | ||
public override Task<IDisplayResult> DisplayAsync(User user, BuildDisplayContext context) | ||
{ | ||
return CombineAsync( | ||
Initialize<SummaryAdminUserViewModel>("UserTwoFactorActionsMenu", model => model.User = user) | ||
.Location("SummaryAdmin", "ActionsMenu:10") | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/OrchardCore.Modules/OrchardCore.Users/Services/TwoFactorPermissionProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using OrchardCore.Security.Permissions; | ||
|
||
namespace OrchardCore.Users.Services; | ||
|
||
public sealed class TwoFactorPermissionProvider : IPermissionProvider | ||
{ | ||
private readonly IEnumerable<Permission> _allPermissions = | ||
[ | ||
CommonPermissions.DisableTwoFactorAuthenticationForUsers, | ||
]; | ||
public Task<IEnumerable<Permission>> GetPermissionsAsync() | ||
=> Task.FromResult(_allPermissions); | ||
|
||
public IEnumerable<PermissionStereotype> GetDefaultStereotypes() => | ||
[ | ||
new PermissionStereotype | ||
{ | ||
Name = OrchardCoreConstants.Roles.Administrator, | ||
Permissions = _allPermissions, | ||
}, | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/OrchardCore.Modules/OrchardCore.Users/Views/Items/UserSendConfirmationActionsMenu.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
@using Microsoft.AspNetCore.Identity | ||
@using OrchardCore.Users.Models | ||
|
||
@model SummaryAdminUserViewModel | ||
|
||
@inject UserManager<IUser> UserManager | ||
@inject IAuthorizationService AuthorizationService | ||
|
||
@if (!Model.User.EmailConfirmed && | ||
Site.As<RegistrationSettings>().UsersMustValidateEmail && | ||
await AuthorizationService.AuthorizeAsync(User, CommonPermissions.EditUsers, Model.User)) | ||
{ | ||
<li> | ||
<form method="post" class="d-inline-block" class="no-multisubmit"> | ||
<input name="id" type="hidden" value="@Model.User.UserId" /> | ||
<button asp-action="SendVerificationEmail" asp-controller="EmailConfirmation" class="dropdown-item">@T["Send verification email"]</button> | ||
</form> | ||
</li> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/OrchardCore.Modules/OrchardCore.Users/Views/UserActionsMenu.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
@using Microsoft.AspNetCore.Identity | ||
|
||
@model SummaryAdminUserViewModel | ||
|
||
@inject UserManager<IUser> UserManager | ||
@inject IAuthorizationService AuthorizationService | ||
|
||
@{ | ||
var isCurrentUser = Model.User.UserName == User.Identity.Name; | ||
var canEdit = await AuthorizationService.AuthorizeAsync(User, CommonPermissions.EditUsers, Model.User); | ||
var isLockedOut = await UserManager.IsLockedOutAsync(Model.User); | ||
} | ||
|
||
@if (canEdit) | ||
{ | ||
<li> | ||
<a asp-action="EditPassword" asp-route-id="@Model.User.UserId" class="dropdown-item">@T["Change password"]</a> | ||
</li> | ||
if (isLockedOut) | ||
{ | ||
<li> | ||
<a asp-action="Unlock" asp-route-id="@Model.User.UserId" class="dropdown-item" data-url-af="RemoveUrl UnsafeUrl" data-title="@T["Unlock user"]" data-message="@T["Are you sure you want to unlock this user?"]">@T["Unlock"]</a> | ||
</li> | ||
} | ||
} | ||
|
||
@if (!isCurrentUser && await AuthorizationService.AuthorizeAsync(User, CommonPermissions.DeleteUsers, Model.User)) | ||
{ | ||
<li> | ||
<a asp-action="Delete" asp-route-id="@Model.User.UserId" class="dropdown-item text-danger" data-url-af="RemoveUrl UnsafeUrl">@T["Delete"]</a> | ||
</li> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/OrchardCore.Modules/OrchardCore.Users/Views/UserTwoFactorActionsMenu.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
@using Microsoft.AspNetCore.Identity | ||
@using OrchardCore.Users.Models | ||
|
||
@model SummaryAdminUserViewModel | ||
|
||
@inject UserManager<IUser> UserManager | ||
@inject IAuthorizationService AuthorizationService | ||
|
||
@if (await AuthorizationService.AuthorizeAsync(User, CommonPermissions.DisableTwoFactorAuthenticationForUsers, Model.User) && | ||
await UserManager.GetTwoFactorEnabledAsync(Model.User)) | ||
{ | ||
<li> | ||
<form method="post" class="d-inline-block" class="no-multisubmit"> | ||
<input name="id" type="hidden" value="@Model.User.UserId" /> | ||
<button asp-action="Disable" asp-controller="AdminTwoFactorAuthentication" class="dropdown-item">@T["Disable two-factor authentication"]</button> | ||
</form> | ||
</li> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters