// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddDbContext <KodkodDbContext>(options => options.UseSqlServer(_configuration.GetConnectionString("DefaultConnection")) .UseLazyLoadingProxies()); services.AddIdentity <User, Role>() .AddEntityFrameworkStores <KodkodDbContext>() .AddDefaultTokenProviders(); services.Configure <JwtTokenConfiguration>(options => { options.Issuer = _configuration["Authentication:JwtBearer:Issuer"]; options.Audience = _configuration["Authentication:JwtBearer:Audience"]; options.SigningCredentials = new SigningCredentials(_signingKey, SecurityAlgorithms.HmacSha256); }); services.AddAuthentication(options => { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }).AddJwtBearer(jwtBearerOptions => { jwtBearerOptions.TokenValidationParameters = new TokenValidationParameters { ValidateActor = true, ValidateAudience = true, ValidateLifetime = true, ValidateIssuerSigningKey = true, ValidIssuer = _configuration["Authentication:JwtBearer:Issuer"], ValidAudience = _configuration["Authentication:JwtBearer:Audience"], IssuerSigningKey = _signingKey }; }); services.AddAuthorization(options => { foreach (var permission in PermissionConsts.AllPermissions()) { options.AddPolicy(permission.Name, policy => policy.Requirements.Add(new PermissionRequirement(permission))); } }); services.AddMvc(options => options.Filters.Add <KodkodDbContextActionFilter>()); services.AddCors(); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Title = "Kodkod API", Version = "v1" }); }); services.AddKodkodEntityFramework(); services.AddKodkodApplication(); services.AddScoped <IAuthorizationHandler, PermissionHandler>(); services.AddScoped <KodkodDbContextActionFilter>(); }
public async Task TestInitializePermissions() { var testPermission = new Permission { Id = Guid.NewGuid(), Name = "TestPermission", DisplayName = "Test permission" }; var permissions = PermissionConsts.AllPermissions(); permissions.Add(testPermission); await _permissionAppService.InitializePermissions(permissions); await KodkodInMemoryContext.SaveChangesAsync(); var initializedPermission = KodkodInMemoryContext.Permissions.FirstOrDefault(p => p.Id == testPermission.Id); Assert.NotNull(initializedPermission); var isPermissionGranted = await _permissionAppService.IsPermissionGrantedForRoleAsync(AdminRole, testPermission); Assert.True(isPermissionGranted); }
public async Task TestInitializePermissions() { var testPermission = new Permission { Id = Guid.NewGuid(), Name = "TestPermission", DisplayName = "Test permission" }; var permissions = PermissionConsts.AllPermissions(); permissions.Add(testPermission); await _permissionAppService.InitializePermissions(permissions); await _kodkodDbContext.SaveChangesAsync(); var latestPermissionsCount = (await _permissionAppService.GetAllAsync()).Count; Assert.Equal(latestPermissionsCount, PermissionConsts.AllPermissions().Count + 1); }
public static int Main(string[] args) { Log.Logger = new LoggerConfiguration() .MinimumLevel.Debug() .MinimumLevel.Override("Microsoft", LogEventLevel.Information) .Enrich.FromLogContext() .WriteTo.Console() .WriteTo.File( "Logs/Logs.txt", fileSizeLimitBytes: 1_000_000, rollOnFileSizeLimit: true, shared: true, flushToDiskInterval: TimeSpan.FromSeconds(1)) .CreateLogger(); try { Log.Information("Starting web host"); var host = BuildWebHost(args); using (var scope = host.Services.CreateScope()) { var permissionAppService = scope.ServiceProvider.GetRequiredService <IPermissionAppService>(); permissionAppService.InitializePermissions(PermissionConsts.AllPermissions()); } host.Run(); return(0); } catch (Exception ex) { Log.Fatal(ex, "Host terminated unexpectedly"); return(1); } finally { Log.CloseAndFlush(); } }
public static RolePermission[] BuildRolePermissions() { var rolePermissions = PermissionConsts.AllPermissions().Select(p => new RolePermission { PermissionId = p.Id, RoleId = AdminRole.Id }).ToList(); var apiUserPermission = PermissionConsts.AllPermissions() .FirstOrDefault(p => p.Name == PermissionConsts.ApiUserPermissionName); if (apiUserPermission != null) { rolePermissions.Add(new RolePermission { PermissionId = apiUserPermission.Id, RoleId = ApiUserRole.Id }); } return(rolePermissions.ToArray()); }
public static Permission[] BuildPermissions() { return(PermissionConsts.AllPermissions().ToArray()); }