mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-01-23 05:12:09 +08:00
Applying change to support “Anonymous” and “Authenticated” role’s permissions as appropriate.
--HG-- extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4046025
This commit is contained in:
@@ -14,6 +14,8 @@ namespace Orchard.Roles.Services {
|
|||||||
public class RolesBasedAuthorizationService : IAuthorizationService {
|
public class RolesBasedAuthorizationService : IAuthorizationService {
|
||||||
private readonly IRoleService _roleService;
|
private readonly IRoleService _roleService;
|
||||||
private readonly IEnumerable<IAuthorizationServiceEvents> _events;
|
private readonly IEnumerable<IAuthorizationServiceEvents> _events;
|
||||||
|
private static readonly string[] AnonymousRole = new[] { "Anonymous" };
|
||||||
|
private static readonly string[] AuthenticatedRole = new[] { "Authenticated" };
|
||||||
|
|
||||||
public RolesBasedAuthorizationService(IRoleService roleService, IEnumerable<IAuthorizationServiceEvents> events) {
|
public RolesBasedAuthorizationService(IRoleService roleService, IEnumerable<IAuthorizationServiceEvents> events) {
|
||||||
_roleService = roleService;
|
_roleService = roleService;
|
||||||
@@ -24,7 +26,6 @@ namespace Orchard.Roles.Services {
|
|||||||
public ILogger Logger { get; set; }
|
public ILogger Logger { get; set; }
|
||||||
protected virtual ISite CurrentSite { get; [UsedImplicitly] private set; }
|
protected virtual ISite CurrentSite { get; [UsedImplicitly] private set; }
|
||||||
|
|
||||||
#region Implementation of IAuthorizationService
|
|
||||||
|
|
||||||
public void CheckAccess(Permission permission, IUser user, IContent content) {
|
public void CheckAccess(Permission permission, IUser user, IContent content) {
|
||||||
if (!TryCheckAccess(permission, user, content)) {
|
if (!TryCheckAccess(permission, user, content)) {
|
||||||
@@ -37,6 +38,7 @@ namespace Orchard.Roles.Services {
|
|||||||
|
|
||||||
_events.Invoke(x => x.Checking(context), Logger);
|
_events.Invoke(x => x.Checking(context), Logger);
|
||||||
|
|
||||||
|
|
||||||
for (var adjustmentLimiter = 0; adjustmentLimiter != 3; ++adjustmentLimiter) {
|
for (var adjustmentLimiter = 0; adjustmentLimiter != 3; ++adjustmentLimiter) {
|
||||||
if (!context.Granted && context.User != null) {
|
if (!context.Granted && context.User != null) {
|
||||||
if (String.Equals(context.User.UserName, "Administrator", StringComparison.OrdinalIgnoreCase) ||
|
if (String.Equals(context.User.UserName, "Administrator", StringComparison.OrdinalIgnoreCase) ||
|
||||||
@@ -46,11 +48,24 @@ namespace Orchard.Roles.Services {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!context.Granted && context.User != null && context.User.Has<IUserRoles>()) {
|
if (!context.Granted) {
|
||||||
var grantingNames = PermissionNames(context.Permission, Enumerable.Empty<string>()).ToArray();
|
|
||||||
IEnumerable<string> rolesForUser = context.User.As<IUserRoles>().Roles;
|
|
||||||
|
|
||||||
foreach (var role in rolesForUser) {
|
// determine which set of permissions would satisfy the access check
|
||||||
|
var grantingNames = PermissionNames(context.Permission, Enumerable.Empty<string>()).ToArray();
|
||||||
|
|
||||||
|
// determine what set of roles should be examined by the access check
|
||||||
|
IEnumerable<string> rolesToExamine;
|
||||||
|
if (context.User == null) {
|
||||||
|
rolesToExamine = AnonymousRole;
|
||||||
|
}
|
||||||
|
else if (context.User.Has<IUserRoles>()) {
|
||||||
|
rolesToExamine = context.User.As<IUserRoles>().Roles.Concat(AuthenticatedRole);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
rolesToExamine = AuthenticatedRole;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var role in rolesToExamine) {
|
||||||
RoleRecord roleRecord = _roleService.GetRoleByName(role);
|
RoleRecord roleRecord = _roleService.GetRoleByName(role);
|
||||||
foreach (var permissionName in _roleService.GetPermissionsForRole(roleRecord.Id)) {
|
foreach (var permissionName in _roleService.GetPermissionsForRole(roleRecord.Id)) {
|
||||||
string possessedName = permissionName;
|
string possessedName = permissionName;
|
||||||
@@ -61,7 +76,7 @@ namespace Orchard.Roles.Services {
|
|||||||
if (context.Granted)
|
if (context.Granted)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (context.Granted)
|
if (context.Granted)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -95,6 +110,5 @@ namespace Orchard.Roles.Services {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user