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

Add authentication api #75

Merged
merged 4 commits into from
Sep 1, 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
48 changes: 48 additions & 0 deletions AnalysisData/AnalysisData/Authorization.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Text;
using AnalysisData.Repository.RoleRepository.Abstraction;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;

namespace AnalysisData;

public class Authorization
{
private readonly IRoleRepository _roleRepository;

public Authorization(IRoleRepository roleRepository)
{
_roleRepository = roleRepository;
}

public async Task ConfigureServices(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()));
});
}
}
32 changes: 31 additions & 1 deletion AnalysisData/AnalysisData/Data/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
using AnalysisData.EAV.Model;
using AnalysisData.Services.SecurityPasswordService.Abstraction;
using AnalysisData.UserManage.Model;
using Microsoft.EntityFrameworkCore;

namespace AnalysisData.Data;

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

}

public DbSet<User> Users { get; set; }
Expand All @@ -22,4 +26,30 @@ public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
public DbSet<FileEntity> FileUploadedDb { get; set; }
public DbSet<UserFile> UserFiles { get; set; }
public DbSet<Category> Categories { get; set; }

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"),
PhoneNumber = "09131111111",
FirstName = "admin",
LastName = "admin",
Email = "[email protected]",
RoleId = 1
}
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public async Task<IActionResult> GetUsersAsync([FromQuery] string username)
return Ok(users);
}

[HttpPost("files/{fileId}/access")]
[HttpPost("files/access")]
public async Task<IActionResult> AccessFileToUser([FromBody] AccessFileToUserDto request)
{
await _filePermissionService.AccessFileToUserAsync(request.UserGuidIds.ToList(), request.FileId);
Expand All @@ -43,7 +43,7 @@ public async Task<IActionResult> AccessFileToUser([FromBody] AccessFileToUserDto
});
}

[HttpGet("files/{fileId}/users")]
[HttpGet("files/users")]
public async Task<IActionResult> WhoAccessToThisFile([FromQuery] int fileId)
{
var file = await _userFileRepository.GetByFileIdAsync(fileId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ public async Task<IActionResult> GetNodesAsync([FromQuery] int pageIndex = 0, [F
return Ok(paginatedNodes);
}

[HttpGet("nodes/{headerUniqueId}/attributes")]
public async Task<IActionResult> GetNodeAttributes(int id)
[HttpGet("nodes/{nodeId}/attributes")]
public async Task<IActionResult> GetNodeAttributes(int nodeId)
{
var user = User;
var output = await _nodeAndEdgeInfo.GetNodeInformationAsync(user, id);
var output = await _nodeAndEdgeInfo.GetNodeInformationAsync(user, nodeId);
return Ok(output);
}

Expand Down
Loading
Loading