-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #225 from fluentcms/200-implement-resource-base-au…
…thorization-instead-of-current-authorization-method 200 implement resource base authorization instead of current authorization method
- Loading branch information
Showing
35 changed files
with
388 additions
and
294 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace FluentCMS.Entities; | ||
|
||
public interface IAuthorizeEntity : IEntity | ||
{ | ||
public Guid SiteId { get; set; } | ||
} |
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 |
---|---|---|
@@ -1,12 +1,10 @@ | ||
namespace FluentCMS.Entities; | ||
|
||
public class Page : AuditEntity | ||
public class Page : AuditEntity, IAuthorizeEntity | ||
{ | ||
public Guid SiteId { get; set; } | ||
public required string Title { get; set; } | ||
public Guid? ParentId { get; set; } | ||
public int Order { get; set; } | ||
public required string Path { get; set; } | ||
public IEnumerable<Guid> AdminRoleIds { get; set; } = new List<Guid>(); | ||
public IEnumerable<Guid> ViewRoleIds { get; set; } = new List<Guid>(); | ||
} |
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,10 @@ | ||
namespace FluentCMS.Entities; | ||
|
||
public class Permission : AuditEntity | ||
{ | ||
public Guid SiteId { get; set; } | ||
public Guid RoleId { get; set; } | ||
public Guid EntityId { get; set; } | ||
public string EntityType { get; set; } = default!; | ||
public string Policy { get; set; } = default!; | ||
} |
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 |
---|---|---|
@@ -1,8 +1,17 @@ | ||
namespace FluentCMS.Entities; | ||
|
||
public class Role : AuditEntity | ||
public class Role : AuditEntity, IAuthorizeEntity | ||
{ | ||
public Guid SiteId { get; set; } | ||
public string Name { get; set; } = string.Empty; | ||
public string? Description { get; set; } | ||
public RoleType Type { get; set; } | ||
} | ||
|
||
public enum RoleType | ||
{ | ||
Normal = 0, | ||
Authenticated = 1, | ||
Guest = 2, | ||
All = 3 | ||
} |
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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
namespace FluentCMS.Entities; | ||
|
||
public class Site : AuditEntity | ||
public class Site : AuditEntity, IAuthorizeEntity | ||
{ | ||
public required string Name { get; set; } | ||
public string? Description { get; set; } | ||
public required string Name { get; set; } | ||
public string? Description { get; set; } | ||
public List<string> Urls { get; set; } = []; | ||
public List<Guid> AdminRoleIds { get; set; } = []; | ||
public Guid SiteId { get; set; } | ||
} |
8 changes: 8 additions & 0 deletions
8
src/FluentCMS.Repositories/Abstractions/IPermissionRepository.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,8 @@ | ||
using FluentCMS.Entities; | ||
|
||
namespace FluentCMS.Repositories; | ||
|
||
public interface IPermissionRepository : IGenericRepository<Permission> | ||
{ | ||
Task<IEnumerable<Permission>> GetPermissions(Guid siteId, IEnumerable<Guid> roleIds, CancellationToken cancellationToken = default); | ||
} |
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
23 changes: 23 additions & 0 deletions
23
src/FluentCMS.Repositories/MongoDB/PermissionRepository.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 FluentCMS.Entities; | ||
using MongoDB.Driver; | ||
|
||
namespace FluentCMS.Repositories.MongoDB; | ||
|
||
public class PermissionRepository : GenericRepository<Permission>, IPermissionRepository | ||
{ | ||
public PermissionRepository(IMongoDBContext mongoDbContext, IApplicationContext applicationContext) : base(mongoDbContext, applicationContext) | ||
{ | ||
} | ||
|
||
public async Task<IEnumerable<Permission>> GetPermissions(Guid siteId, IEnumerable<Guid> roleIds, CancellationToken cancellationToken = default) | ||
{ | ||
cancellationToken.ThrowIfCancellationRequested(); | ||
|
||
var filter = Builders<Permission>.Filter.Eq(x => x.SiteId, siteId); | ||
filter &= Builders<Permission>.Filter.In(x => x.RoleId, roleIds); | ||
|
||
var findResult = await Collection.FindAsync(filter, null, cancellationToken); | ||
|
||
return findResult.ToEnumerable(cancellationToken); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
using FluentCMS.Entities; | ||
using FluentCMS.Repositories; | ||
|
||
namespace FluentCMS.Services; | ||
|
||
public interface IAuthorizationProvider | ||
{ | ||
bool Authorize(Site site, IEnumerable<string> policyNames); | ||
bool IsSuperAdmin(); | ||
Task<Permission> Create<T>(T entity, string policyName, CancellationToken cancellationToken = default) where T : IAuthorizeEntity; | ||
} | ||
|
||
public class AuthorizationProvider( | ||
IPermissionRepository permissionRepository, | ||
IApplicationContext applicationContext, | ||
IRoleRepository roleRepository) : IAuthorizationProvider | ||
{ | ||
private readonly IEnumerable<Guid> _userRoleIds = applicationContext.Current.RoleIds; | ||
private IEnumerable<Permission> _permissions = null!; | ||
|
||
private async Task Load(Guid siteId, CancellationToken cancellationToken = default) | ||
{ | ||
if (_permissions != null) | ||
return; | ||
|
||
_permissions = await permissionRepository.GetPermissions(siteId, _userRoleIds, cancellationToken); | ||
} | ||
|
||
public bool IsSuperAdmin() | ||
{ | ||
// super admins have access to everything | ||
return applicationContext.Current.IsSuperAdmin; | ||
} | ||
|
||
public bool Authorize(Site site, IEnumerable<string> policyNames) | ||
{ | ||
ArgumentNullException.ThrowIfNull(site); | ||
ArgumentNullException.ThrowIfNull(policyNames); | ||
|
||
// super admins have access to everything | ||
if (applicationContext.Current.IsSuperAdmin) | ||
return true; | ||
|
||
Load(site.Id).Wait(); | ||
|
||
var entityType = typeof(Site).Name; | ||
var userRoleIds = applicationContext.Current.RoleIds; | ||
|
||
if (_permissions.Any(x => x.Id == site.Id && x.EntityType == typeof(Site).Name && policyNames.Contains(x.Policy))) | ||
return true; | ||
|
||
return false; | ||
} | ||
|
||
public async Task<Permission> Create<T>(T entity, string policyName, CancellationToken cancellationToken = default) where T : IAuthorizeEntity | ||
{ | ||
ArgumentNullException.ThrowIfNull(entity); | ||
|
||
var siteId = entity.GetType() == typeof(Site) ? entity.Id : entity.SiteId; | ||
|
||
var role = new Role | ||
{ | ||
SiteId = siteId, | ||
Name = policyName, | ||
Description = "Default " + policyName | ||
}; | ||
|
||
_ = await roleRepository.Create(role, cancellationToken) ?? | ||
throw new AppException(ExceptionCodes.RoleUnableToCreate); | ||
|
||
var permission = new Permission | ||
{ | ||
SiteId = siteId, | ||
EntityType = typeof(T).Name, | ||
Policy = policyName, | ||
EntityId = entity.Id, | ||
RoleId = role.Id | ||
}; | ||
|
||
return await permissionRepository.Create(permission, cancellationToken) ?? | ||
throw new AppException(ExceptionCodes.PermissionUnableToCreate); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.