Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix migrate2 #78

Merged
merged 2 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 6 additions & 24 deletions AnalysisData/AnalysisData/Authorization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace AnalysisData;

public class Authorization
public class Authorization
{
private readonly IRoleRepository _roleRepository;

Expand All @@ -14,35 +14,17 @@ public Authorization(IRoleRepository roleRepository)
_roleRepository = roleRepository;
}

public async Task ConfigureServices(IServiceCollection services)
public async Task ConfigureAuthorizationPolicies(IServiceCollection services)
{
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = "yourIssuer",
ValidAudience = "yourAudience",
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("yourSecretKey"))
};
});

var goldRoles = await _roleRepository.GetRolesByPolicyAsync("Gold");
var silverRoles = await _roleRepository.GetRolesByPolicyAsync("Silver");
var bronzeRoles = await _roleRepository.GetRolesByPolicyAsync("Bronze");

services.AddAuthorization(options =>
{
options.AddPolicy("gold", policy =>
policy.RequireRole(goldRoles.ToArray()));
options.AddPolicy("silver", policy =>
policy.RequireRole(silverRoles.ToArray()));

options.AddPolicy("bronze", policy =>
policy.RequireRole(bronzeRoles.ToArray()));
options.AddPolicy("gold", policy => policy.RequireRole(goldRoles.ToArray()));
options.AddPolicy("silver", policy => policy.RequireRole(silverRoles.ToArray()));
options.AddPolicy("bronze", policy => policy.RequireRole(bronzeRoles.ToArray()));
});
}
}
12 changes: 5 additions & 7 deletions AnalysisData/AnalysisData/Data/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ namespace AnalysisData.Data;

public class ApplicationDbContext : DbContext
{
private readonly IPasswordHasher _passwordHasher;
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options,IPasswordHasher passwordHasher)
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
_passwordHasher = passwordHasher;

}


public DbSet<User> Users { get; set; }
public DbSet<AttributeEdge> AttributeEdges { get; set; }
Expand All @@ -30,19 +28,19 @@ public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options,IPass
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.Entity<Role>().HasData(
new Role { Id = 1, RoleName = "admin", RolePolicy = "gold" },
new Role { Id = 2, RoleName = "Data-Analyst", RolePolicy = "bronze" },
new Role { Id = 3, RoleName = "Data-Manager", RolePolicy = "silver" }
);

modelBuilder.Entity<User>().HasData(
new User
{
Id = Guid.NewGuid(),
Username = "admin",
Password = _passwordHasher.HashPassword("admin"),
Password = ("admin"),
PhoneNumber = "09131111111",
FirstName = "admin",
LastName = "admin",
Expand Down
Loading
Loading