Skip to content

Commit 2869ad9

Browse files
committed
Fixed logs list for Employee role
1 parent 1b48df5 commit 2869ad9

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

WebApi/Features/Logs/List.cs

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IdentityModel.Tokens.Jwt;
34
using System.Linq;
45
using System.Linq.Expressions;
56
using System.Threading;
67
using System.Threading.Tasks;
78
using MediatR;
9+
using Microsoft.AspNetCore.Http;
10+
using Microsoft.AspNetCore.Identity;
811
using Microsoft.EntityFrameworkCore;
912
using WebApi.Entities;
1013
using WebApi.Extensions;
@@ -32,10 +35,17 @@ public class QueryHandler : IRequestHandler<Query, Object>
3235
private readonly int DEFAULT_PAGE = 1;
3336
private readonly int DEFAULT_ROWS_PER_PAGE = 10;
3437
private readonly ApplicationDbContext _context;
38+
private readonly IHttpContextAccessor _httpContext;
39+
private readonly UserManager<User> _manager;
3540

36-
public QueryHandler(ApplicationDbContext context)
41+
public QueryHandler(
42+
ApplicationDbContext context,
43+
IHttpContextAccessor httpContext,
44+
UserManager<User> manager)
3745
{
3846
_context = context;
47+
_httpContext = httpContext;
48+
_manager = manager;
3949
}
4050

4151
public async Task<Object> Handle(Query request, CancellationToken cancellationToken)
@@ -45,16 +55,38 @@ public async Task<Object> Handle(Query request, CancellationToken cancellationTo
4555
IQueryable<LogViewModel> queryableModel;
4656
var startDate = request.Parameters.StartDate;
4757
var endDate = request.Parameters.EndDate;
48-
49-
// Apply Search filter if not null
5058
var searchQuery = request.Parameters.Search;
51-
if(!string.IsNullOrEmpty(searchQuery))
59+
60+
// Check if the current user is Employee
61+
var isEmployee = _httpContext.HttpContext.User.IsInRole("Employee");
62+
if (isEmployee)
63+
{
64+
// var username = _httpContext.HttpContext.User.FindFirst("sub");
65+
66+
// Get the username in sub type claim
67+
var username = _httpContext.HttpContext.User.Claims
68+
.First(m => m.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier")
69+
.Value;
70+
71+
// Get account details
72+
var account = _context.Users
73+
.Include(m => m.Employee)
74+
.First(m => m.UserName == username);
75+
76+
queryableModel = _context.Logs.MapToViewModel()
77+
.Where(m =>
78+
m.EmployeeId == account.Employee.Id &&
79+
m.Deleted == null);
80+
}
81+
// Apply Search filter
82+
else if(!string.IsNullOrEmpty(searchQuery))
5283
{
5384
queryableModel = _context.Logs.MapToViewModel()
5485
.Where(m =>
5586
m.FullName.Contains(searchQuery) &&
5687
m.Deleted == null);
5788
}
89+
// Get all List
5890
else
5991
{
6092
queryableModel = _context.Logs.MapToViewModel()

0 commit comments

Comments
 (0)