Skip to content

Commit 05cbf72

Browse files
feat: permissions
1 parent 5ab9df8 commit 05cbf72

11 files changed

Lines changed: 106 additions & 35 deletions

File tree

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright (c) CodeBlock.Dev. All rights reserved.
2+
// For more information visit https://codeblock.dev
3+
4+
using System.Reflection;
5+
using CodeBlock.DevKit.Contracts.Models;
6+
7+
namespace CanBeYours.Application.Helpers;
8+
9+
/// <summary>
10+
/// This class contains all permissions used in the application.
11+
/// They will be automatically registered in the database.
12+
/// </summary>
13+
public static class Permissions
14+
{
15+
/// <summary>
16+
/// This class name is used as a group name for permissions related to demo things.
17+
/// </summary>
18+
public static class Demo
19+
{
20+
/// <summary>
21+
/// This property represents the permission to manage demo things.
22+
/// </summary>
23+
public const string DEMO_THINGS = "DemoThings";
24+
}
25+
26+
/// <summary>
27+
/// Retrieves all permissions dynamically using reflection.
28+
/// </summary>
29+
public static IEnumerable<PermissionsSettings> GetPermissions()
30+
{
31+
var permissionsList = new List<PermissionsSettings>();
32+
33+
// Get all nested classes (categories)
34+
var categories = typeof(Permissions).GetNestedTypes(BindingFlags.Public | BindingFlags.Static);
35+
36+
foreach (var category in categories)
37+
{
38+
// Get all public constants (permissions) inside the category
39+
var permissions = category
40+
.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy)
41+
.Where(f => f.IsLiteral && !f.IsInitOnly) // Ensure they are constants
42+
.Select(f => new
43+
{
44+
SystemName = f.GetRawConstantValue()?.ToString(), // Property value
45+
DisplayName = f.GetRawConstantValue()?.ToString(),
46+
})
47+
.Where(p => p.DisplayName != null)
48+
.ToList();
49+
50+
// Add extracted permissions to the list
51+
permissionsList.AddRange(
52+
permissions.Select(permission => new PermissionsSettings
53+
{
54+
SystemName = permission.SystemName,
55+
DisplayName = permission.DisplayName,
56+
GroupName = category.Name,
57+
})
58+
);
59+
}
60+
61+
return permissionsList;
62+
}
63+
}

src/1-Libraries/Application/UseCases/DemoThings/GetDemoThing/GetDemoThingUseCase.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using AutoMapper;
22
using CanBeYours.Application.Dtos;
33
using CanBeYours.Application.Exceptions;
4+
using CanBeYours.Application.Helpers;
45
using CanBeYours.Core.Domain.DemoThings;
56
using CodeBlock.DevKit.Application.Queries;
7+
using CodeBlock.DevKit.Application.Srvices;
68
using MediatR;
79
using Microsoft.Extensions.Logging;
810

@@ -11,11 +13,18 @@ namespace CanBeYours.Application.UseCases.DemoThings.GetDemoThing;
1113
internal class GetDemoThingUseCase : BaseQueryHandler, IRequestHandler<GetDemoThingRequest, GetDemoThingDto>
1214
{
1315
private readonly IDemoThingRepository _demoThingRepository;
16+
private readonly ICurrentUser _currentUser;
1417

15-
public GetDemoThingUseCase(IDemoThingRepository demoThingRepository, IMapper mapper, ILogger<GetDemoThingUseCase> logger)
18+
public GetDemoThingUseCase(
19+
IDemoThingRepository demoThingRepository,
20+
IMapper mapper,
21+
ILogger<GetDemoThingUseCase> logger,
22+
ICurrentUser currentUser
23+
)
1624
: base(mapper, logger)
1725
{
1826
_demoThingRepository = demoThingRepository;
27+
_currentUser = currentUser;
1928
}
2029

2130
public async Task<GetDemoThingDto> Handle(GetDemoThingRequest request, CancellationToken cancellationToken)
@@ -24,6 +33,8 @@ public async Task<GetDemoThingDto> Handle(GetDemoThingRequest request, Cancellat
2433
if (demoThing == null)
2534
throw DemoThingApplicationExceptions.DemoThingNotFound(request.Id);
2635

36+
EnsureUserHasAccess(demoThing.UserId, _currentUser, Permissions.Demo.DEMO_THINGS);
37+
2738
return _mapper.Map<GetDemoThingDto>(demoThing);
2839
}
2940
}

src/1-Libraries/Infrastructure/DbContext/SettingsDbMigrator.cs renamed to src/1-Libraries/Infrastructure/DbContext/DbMigrator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace CanBeYours.Infrastructure.DbContext;
66

7-
internal static class DemoThingsDbMigrator
7+
internal static class DbMigrator
88
{
99
public static void MigrateDatabes(this IServiceProvider serviceProvider)
1010
{

src/1-Libraries/Infrastructure/DbContext/SettingsDbSeeder.cs renamed to src/1-Libraries/Infrastructure/DbContext/DbSeeder.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,35 @@
1+
using CanBeYours.Application.Helpers;
12
using CanBeYours.Core.Domain.DemoThings;
3+
using CodeBlock.DevKit.Administration.Domain.Permissions;
24
using CodeBlock.DevKit.Identity.Domain.Users;
35
using Microsoft.Extensions.DependencyInjection;
46
using MongoDB.Driver;
57

68
namespace CanBeYours.Infrastructure.DbContext;
79

8-
internal static class DemoThingsDbSeeder
10+
internal static class DbSeeder
911
{
10-
public static void SeedSampleDemoThings(this IServiceProvider serviceProvider)
12+
public static void SeedPermissions(this IServiceProvider serviceProvider)
1113
{
1214
using var serviceScope = serviceProvider.CreateScope();
15+
var permissionRepository = serviceScope.ServiceProvider.GetService<IPermissionRepository>();
16+
17+
var permissions = permissionRepository.GetListAsync().GetAwaiter().GetResult();
18+
19+
foreach (var permission in Permissions.GetPermissions())
20+
{
21+
// Check if the permission already exists
22+
if (permissions.Any(p => p.SystemName == permission.SystemName))
23+
continue;
1324

14-
serviceScope.SeedDemoThings();
25+
var newPermission = Permission.Create(permissionRepository, permission.DisplayName, permission.SystemName, permission.GroupName);
26+
permissionRepository.Add(newPermission);
27+
}
1528
}
1629

17-
private static void SeedDemoThings(this IServiceScope serviceScope)
30+
public static void SeedSampleDemoThings(this IServiceProvider serviceProvider)
1831
{
32+
using var serviceScope = serviceProvider.CreateScope();
1933
var dbContext = serviceScope.ServiceProvider.GetService<MainDbContext>();
2034

2135
if (dbContext.DemoThings.Find(_ => true).Any())

src/1-Libraries/Infrastructure/Startup.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@ public static void AddInfrastructureModule(this IServiceCollection services)
2222
services.AddValidatorsFromAssembly(typeof(Startup).Assembly);
2323
}
2424

25-
public static void UseInfrastructureModule(this IServiceProvider serviceProvider)
25+
public static void UseInfrastructureModule(this IServiceProvider serviceProvider, bool isDevelopmentEnvironment)
2626
{
27-
serviceProvider.SeedSampleDemoThings();
27+
serviceProvider.MigrateDatabes();
28+
serviceProvider.SeedPermissions();
29+
30+
if (isDevelopmentEnvironment)
31+
serviceProvider.SeedSampleDemoThings();
2832
}
2933

3034
private static void AddMongoDbContext(this IServiceCollection services)

src/2-Clients/AdminPanel/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static WebApplication ConfigureServices(this WebApplicationBuilder builde
1616
public static WebApplication ConfigurePipeline(this WebApplication app)
1717
{
1818
app.UseAdminPanelClientModule();
19-
app.Services.UseInfrastructureModule();
19+
app.Services.UseInfrastructureModule(app.Environment.IsDevelopment());
2020

2121
return app;
2222
}

src/2-Clients/AdminPanel/appsettings.json

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,7 @@
1111
"DatabaseName": "CanBeYours"
1212
},
1313
"Administration": {
14-
"AdminRole": "admin",
15-
"Permissions": [
16-
{
17-
"DisplayName": "Demo Things",
18-
"SystemName": "DemoThings",
19-
"GroupName": "Demo"
20-
}
21-
]
14+
"AdminRole": "admin"
2215
},
2316
"Identity": {
2417
"AdminUser": {

src/2-Clients/Api/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static WebApplication ConfigureServices(this WebApplicationBuilder builde
1616
public static WebApplication ConfigurePipeline(this WebApplication app)
1717
{
1818
app.UseApiClientModule();
19-
app.Services.UseInfrastructureModule();
19+
app.Services.UseInfrastructureModule(app.Environment.IsDevelopment());
2020

2121
return app;
2222
}

src/2-Clients/Api/appsettings.json

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,7 @@
1212
"DatabaseName": "CanBeYours"
1313
},
1414
"Administration": {
15-
"AdminRole": "admin",
16-
"Permissions": [
17-
{
18-
"DisplayName": "Demo Things",
19-
"SystemName": "DemoThings",
20-
"GroupName": "Demo"
21-
}
22-
]
15+
"AdminRole": "admin"
2316
},
2417
"Identity": {
2518
"AdminUser": {

src/2-Clients/WebApp/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static WebApplication ConfigureServices(this WebApplicationBuilder builde
1616
public static WebApplication ConfigurePipeline(this WebApplication app)
1717
{
1818
app.UseWebAppClientModule();
19-
app.Services.UseInfrastructureModule();
19+
app.Services.UseInfrastructureModule(app.Environment.IsDevelopment());
2020
return app;
2121
}
2222
}

0 commit comments

Comments
 (0)