-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create authorization policies for OrganizationAdmin and ServerAdmin. …
…Use these within the routing system via Authorize attribute instead of directly checking on each page.
- Loading branch information
Showing
18 changed files
with
586 additions
and
485 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using Microsoft.AspNetCore.Authorization; | ||
|
||
namespace Remotely.Server.Auth; | ||
|
||
public class OrganizationAdminRequirement : IAuthorizationRequirement | ||
{ | ||
|
||
} |
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,34 @@ | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Identity; | ||
using Remotely.Shared.Models; | ||
using System.Threading.Tasks; | ||
|
||
namespace Remotely.Server.Auth; | ||
|
||
public class OrganizationAdminRequirementHandler : AuthorizationHandler<OrganizationAdminRequirement> | ||
{ | ||
private readonly UserManager<RemotelyUser> _userManager; | ||
|
||
public OrganizationAdminRequirementHandler(UserManager<RemotelyUser> userManager) | ||
{ | ||
_userManager = userManager; | ||
} | ||
|
||
protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, OrganizationAdminRequirement requirement) | ||
{ | ||
if (context.User.Identity?.IsAuthenticated != true) | ||
{ | ||
context.Fail(); | ||
return; | ||
} | ||
|
||
var user = await _userManager.GetUserAsync(context.User); | ||
if (user?.IsAdministrator != true) | ||
{ | ||
context.Fail(); | ||
return; | ||
} | ||
|
||
context.Succeed(requirement); | ||
} | ||
} |
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,8 @@ | ||
namespace Remotely.Server.Auth; | ||
|
||
public static class PolicyNames | ||
{ | ||
public const string TwoFactorRequired = nameof(TwoFactorRequired); | ||
public const string OrganizationAdminRequired = nameof(OrganizationAdminRequired); | ||
public const string ServerAdminRequired = nameof(ServerAdminRequired); | ||
} |
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,7 @@ | ||
using Microsoft.AspNetCore.Authorization; | ||
|
||
namespace Remotely.Server.Auth; | ||
|
||
public class ServerAdminRequirement : IAuthorizationRequirement | ||
{ | ||
} |
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,36 @@ | ||
#nullable enable | ||
|
||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Identity; | ||
using Remotely.Shared.Models; | ||
using System.Threading.Tasks; | ||
|
||
namespace Remotely.Server.Auth; | ||
|
||
public class ServerAdminRequirementHandler : AuthorizationHandler<ServerAdminRequirement> | ||
{ | ||
private readonly UserManager<RemotelyUser> _userManager; | ||
|
||
public ServerAdminRequirementHandler(UserManager<RemotelyUser> userManager) | ||
{ | ||
_userManager = userManager; | ||
} | ||
|
||
protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, ServerAdminRequirement requirement) | ||
{ | ||
if (context.User.Identity?.IsAuthenticated != true) | ||
{ | ||
context.Fail(); | ||
return; | ||
} | ||
|
||
var user = await _userManager.GetUserAsync(context.User); | ||
if (user?.IsServerAdmin != true) | ||
{ | ||
context.Fail(); | ||
return; | ||
} | ||
|
||
context.Succeed(requirement); | ||
} | ||
} |
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
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
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
Oops, something went wrong.