|
|
using EPAServeur.Context;
|
|
|
using EPAServeur.IServices;
|
|
|
using EPAServeur.Security;
|
|
|
using EPAServeur.Services;
|
|
|
using IO.Swagger.ApiCollaborateur;
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
using System.IO;
|
|
|
|
|
|
namespace EPAServeur
|
|
|
{
|
|
|
public class Startup
|
|
|
{
|
|
|
readonly string AllowCrossClientEPA = "_AllowsCrossOriginClientEPA";
|
|
|
|
|
|
public Startup(IConfiguration configuration, IWebHostEnvironment env)
|
|
|
{
|
|
|
Configuration = configuration;
|
|
|
Environment = env;
|
|
|
}
|
|
|
|
|
|
public IConfiguration Configuration { get; }
|
|
|
public IWebHostEnvironment Environment { get; }
|
|
|
|
|
|
// This method gets called by the runtime. Use this method to add services to the container.
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
|
{
|
|
|
services.AddCors(options =>
|
|
|
{
|
|
|
options.AddPolicy(name: AllowCrossClientEPA,
|
|
|
builder =>
|
|
|
{
|
|
|
builder.WithOrigins("http://localhost:4200").AllowAnyHeader().AllowAnyMethod();
|
|
|
});
|
|
|
});
|
|
|
|
|
|
services.AddControllers();
|
|
|
|
|
|
services.AddAuthentication(options =>
|
|
|
{
|
|
|
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
|
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
|
}).AddJwtBearer(o =>
|
|
|
{
|
|
|
o.Authority = Configuration["Jwt:Authority"];
|
|
|
o.Audience = Configuration["Jwt:Audience"];
|
|
|
|
|
|
if (Environment.IsDevelopment())
|
|
|
{
|
|
|
o.RequireHttpsMetadata = false;
|
|
|
}
|
|
|
|
|
|
o.Events = new JwtBearerEvents()
|
|
|
{
|
|
|
OnAuthenticationFailed = c =>
|
|
|
{
|
|
|
c.NoResult();
|
|
|
|
|
|
c.Response.StatusCode = 500;
|
|
|
c.Response.ContentType = "text/plain";
|
|
|
|
|
|
if (Environment.IsDevelopment())
|
|
|
{
|
|
|
return c.Response.WriteAsync(c.Exception.ToString());
|
|
|
}
|
|
|
|
|
|
return c.Response.WriteAsync("Une erreur s'est produite lors du processus d'authentification.");
|
|
|
},
|
|
|
OnForbidden = c =>
|
|
|
{
|
|
|
c.NoResult();
|
|
|
|
|
|
c.Response.StatusCode = 403;
|
|
|
c.Response.ContentType = "text/plain";
|
|
|
|
|
|
return c.Response.WriteAsync("L'utilisateur n'est pas autorisé à accéder à cette ressource.");
|
|
|
}
|
|
|
};
|
|
|
});
|
|
|
|
|
|
services.AddAuthorization(options =>
|
|
|
{
|
|
|
options.AddPolicy("SameMailRequirement",
|
|
|
policy => policy.Requirements.Add(new SameMailCollaborateurRequirement()));
|
|
|
});
|
|
|
|
|
|
services.AddDbContext<EpContext>(b => b.UseMySQL(Configuration["Data:DefaultConnection:ConnectionString"]));
|
|
|
|
|
|
var optionBuider = new DbContextOptionsBuilder<EpContext>()
|
|
|
.UseMySQL(Configuration["Data:DefaultConnection:ConnectionString"])
|
|
|
.Options;
|
|
|
|
|
|
using (var context = new EpContext(optionBuider))
|
|
|
{
|
|
|
context.Database.EnsureDeleted(); //PENSEZ A ENLEVER CETTE LIGNE ET A NE JAMAIS LA REMETTRE QUAND LE SERVEUR SERA MIS EN PRODUCTION ^^
|
|
|
context.Database.EnsureCreated();
|
|
|
context.SaveChanges();
|
|
|
|
|
|
DataSeeder.AddInformationsDeBase(context);
|
|
|
DataSeeder.AddChamps(context);
|
|
|
DataSeeder.AddNotes(context);
|
|
|
DataSeeder.AddEp(context);
|
|
|
DataSeeder.AddEngagements(context);
|
|
|
}
|
|
|
//faire using, check si kekchoz exkist puis appeler les m<EFBFBD>thodes de cr<EFBFBD>ation si il n'y a rien
|
|
|
|
|
|
//API Collaborateurs
|
|
|
services.AddScoped<ICollaborateurApi, CollaborateurApi>();
|
|
|
services.AddScoped<IAgenceApi, AgenceApi>();
|
|
|
|
|
|
|
|
|
|
|
|
//Services
|
|
|
services.AddScoped<ICollaborateurService, CollaborateurService>();
|
|
|
services.AddScoped<IFormationService, FormationService>();
|
|
|
services.AddScoped<INoteService, NoteService>();
|
|
|
services.AddScoped<IReferentEPService, ReferentEPService>();
|
|
|
services.AddScoped<IEngagementService, EngagementService>();
|
|
|
|
|
|
//Handlers
|
|
|
services.AddSingleton<IAuthorizationHandler, CollaborateurAuthorizationHandler>();
|
|
|
|
|
|
}
|
|
|
|
|
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
|
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory logger)
|
|
|
{
|
|
|
string path = Directory.GetCurrentDirectory();
|
|
|
logger.AddFile(path + "Log/loggerfile-{Date}.txt");
|
|
|
if (env.IsDevelopment())
|
|
|
{
|
|
|
app.UseDeveloperExceptionPage();
|
|
|
}
|
|
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
|
|
app.UseRouting();
|
|
|
|
|
|
app.UseCors(AllowCrossClientEPA);
|
|
|
|
|
|
app.UseAuthentication();
|
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
|
|
|
app.UseEndpoints(endpoints =>
|
|
|
{
|
|
|
endpoints.MapControllers();
|
|
|
});
|
|
|
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|