Commit 2bdcfc1a authored by hasan khaddour's avatar hasan khaddour

implement current user proivder serivce , occupancy service

parent 4b446462
using Ardalis.Result; using Ardalis.Result;
using AutoMapper;
using PSManagement.Application.Contracts.Authentication; using PSManagement.Application.Contracts.Authentication;
using PSManagement.Application.Contracts.Tokens; using PSManagement.Application.Contracts.Tokens;
using PSManagement.Domain.Customers.DomainErrors; using PSManagement.Domain.Customers.DomainErrors;
...@@ -8,6 +9,7 @@ using PSManagement.Domain.Identity.Repositories; ...@@ -8,6 +9,7 @@ using PSManagement.Domain.Identity.Repositories;
using PSManagement.Domain.Identity.Specification; using PSManagement.Domain.Identity.Specification;
using PSManagement.SharedKernel.Specification; using PSManagement.SharedKernel.Specification;
using System; using System;
using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace PSManagement.Infrastructure.Services.Authentication namespace PSManagement.Infrastructure.Services.Authentication
...@@ -17,16 +19,18 @@ namespace PSManagement.Infrastructure.Services.Authentication ...@@ -17,16 +19,18 @@ namespace PSManagement.Infrastructure.Services.Authentication
private readonly IJwtTokenGenerator _jwtTokenGenerator; private readonly IJwtTokenGenerator _jwtTokenGenerator;
private readonly IUsersRepository _userRepository; private readonly IUsersRepository _userRepository;
private readonly BaseSpecification<User> _specification; private readonly BaseSpecification<User> _specification;
private readonly IMapper _mapper;
public AuthenticationService(IJwtTokenGenerator jwtTokenGenerator, IUsersRepository userRepository) public AuthenticationService(IJwtTokenGenerator jwtTokenGenerator, IUsersRepository userRepository, IMapper mapper)
{ {
_jwtTokenGenerator = jwtTokenGenerator; _jwtTokenGenerator = jwtTokenGenerator;
_userRepository = userRepository; _userRepository = userRepository;
_specification = new UserSpecification(); _specification = new UserSpecification();
_mapper = mapper;
} }
public async Task<Result<AuthenticationResult>> Login(String email, String password) { public async Task<Result<AuthenticationResult>> Login(String email, String password) {
_specification.AddInclude(e => e.Employee);
User u = await _userRepository.GetByEmail(email,_specification); User u = await _userRepository.GetByEmail(email,_specification);
if (u is null || u.HashedPassword != password) { if (u is null || u.HashedPassword != password) {
return Result.Invalid(UserErrors.InvalidLoginAttempt); return Result.Invalid(UserErrors.InvalidLoginAttempt);
...@@ -35,10 +39,10 @@ namespace PSManagement.Infrastructure.Services.Authentication ...@@ -35,10 +39,10 @@ namespace PSManagement.Infrastructure.Services.Authentication
String token = _jwtTokenGenerator.GenerateToken(u); String token = _jwtTokenGenerator.GenerateToken(u);
return new AuthenticationResult { return new AuthenticationResult {
Id = u.Id, EmployeeId = u.Employee.Id,
Email=u.Email, Email=u.Email,
FirstName="", FirstName=u.Employee.PersonalInfo.FirstName,
LastName ="", LastName =u.Employee.PersonalInfo.LastName,
Token=token}; Token=token};
} }
public async Task<Result<AuthenticationResult>> Register(String email, String userName, String password) { public async Task<Result<AuthenticationResult>> Register(String email, String userName, String password) {
...@@ -58,11 +62,11 @@ namespace PSManagement.Infrastructure.Services.Authentication ...@@ -58,11 +62,11 @@ namespace PSManagement.Infrastructure.Services.Authentication
return ( return (
new AuthenticationResult new AuthenticationResult
{ {
Id = user.Id, EmployeeId = user.Employee.Id,
Email = email, Email = email,
FirstName = user.Employee?.PersonalInfo.FirstName, FirstName = user.Employee?.PersonalInfo.FirstName,
LastName = user.Employee?.PersonalInfo.LastName, LastName = user.Employee?.PersonalInfo.LastName,
Roles=user.Roles, Roles=_mapper.Map<ICollection<RoleDTO>>(user.Roles),
Token = token Token = token
}); });
......
using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using PSManagement.Application.Contracts.Authentication; using PSManagement.Application.Contracts.Authentication;
using PSManagement.Application.Contracts.Authorization; using PSManagement.Application.Contracts.Authorization;
using PSManagement.Application.Contracts.Email; using PSManagement.Application.Contracts.Email;
using PSManagement.Application.Contracts.Occupancy;
using PSManagement.Application.Contracts.Providers; using PSManagement.Application.Contracts.Providers;
using PSManagement.Application.Contracts.Storage; using PSManagement.Application.Contracts.Storage;
using PSManagement.Application.Contracts.SyncData; using PSManagement.Application.Contracts.SyncData;
...@@ -14,6 +16,7 @@ using PSManagement.Infrastructure.BackgroundServcies; ...@@ -14,6 +16,7 @@ using PSManagement.Infrastructure.BackgroundServcies;
using PSManagement.Infrastructure.Services.Authentication; using PSManagement.Infrastructure.Services.Authentication;
using PSManagement.Infrastructure.Services.Authorization; using PSManagement.Infrastructure.Services.Authorization;
using PSManagement.Infrastructure.Services.Email; using PSManagement.Infrastructure.Services.Email;
using PSManagement.Infrastructure.Services.Occupancy;
using PSManagement.Infrastructure.Services.Providers; using PSManagement.Infrastructure.Services.Providers;
using PSManagement.Infrastructure.Services.Storage; using PSManagement.Infrastructure.Services.Storage;
using PSManagement.Infrastructure.Settings; using PSManagement.Infrastructure.Settings;
...@@ -38,6 +41,8 @@ namespace PSManagement.Infrastructure.DI ...@@ -38,6 +41,8 @@ namespace PSManagement.Infrastructure.DI
{ {
services.AddSingleton<IDateTimeProvider, DateTimeProvider>(); services.AddSingleton<IDateTimeProvider, DateTimeProvider>();
services.AddScoped<IEmployeesProvider, EmployeesProvider>(); services.AddScoped<IEmployeesProvider, EmployeesProvider>();
services.AddScoped<ICurrentUserProvider,CurrentUserProvider>();
services.AddScoped<IFileService,FileService>(); services.AddScoped<IFileService,FileService>();
services.AddScoped<IEmailService,EmailService>(); services.AddScoped<IEmailService,EmailService>();
return services; return services;
...@@ -45,9 +50,10 @@ namespace PSManagement.Infrastructure.DI ...@@ -45,9 +50,10 @@ namespace PSManagement.Infrastructure.DI
private static IServiceCollection AddBackgroundServices(this IServiceCollection services,IConfiguration configuration) private static IServiceCollection AddBackgroundServices(this IServiceCollection services,IConfiguration configuration)
{ {
services.Configure<EmployeesSyncJobSettings>(configuration.GetSection("EmpoyeesSyncJobSettings")); services.Configure<EmployeesSyncJobSettings>(configuration.GetSection("EmpoyeesSyncJobSettings"));
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddScoped<ISyncEmployeesService, SyncEmployeesService>(); services.AddScoped<ISyncEmployeesService, SyncEmployeesService>();
services.AddScoped<IOccupancySystemNotifier,OccupancySystemNotifier>();
services.AddHostedService<BackgroundJobSyncEmployees>(); services.AddHostedService<BackgroundJobSyncEmployees>();
return services; return services;
......
using Microsoft.Extensions.Logging;
using PSManagement.Application.Contracts.Occupancy;
using PSManagement.Domain.Tracking;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PSManagement.Infrastructure.Services.Occupancy
{
public class OccupancySystemNotifier : IOccupancySystemNotifier
{
private readonly ILogger<EmployeeTrack> _logger;
public OccupancySystemNotifier(ILogger<EmployeeTrack> logger)
{
_logger = logger;
}
public Task SendEmployeeOccupancyNotification(EmployeeTrack employeeTrack)
{
_logger.LogInformation("employee track sended");
return Task.CompletedTask;
}
}
}
using Microsoft.AspNetCore.Http;
using PSManagement.Application.Contracts.Providers;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
namespace PSManagement.Infrastructure.Services.Providers
{
public class CurrentUserProvider : ICurrentUserProvider
{
private readonly IHttpContextAccessor _httpContextAccessor;
public CurrentUserProvider(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public int? EmployeeId
{
get
{
var userIdClaim = _httpContextAccessor.HttpContext?.User?.FindFirstValue(ClaimTypes.NameIdentifier);
if (int.TryParse(userIdClaim, out var userId))
{
return userId;
}
return null;
}
}
public int? HiastId {
get
{
var userIdClaim = _httpContextAccessor.HttpContext?.User?.FindFirstValue("HiastId");
if (int.TryParse(userIdClaim, out var hiastId))
{
return hiastId;
}
return null;
}
}
public IEnumerable<string> Roles => _httpContextAccessor.HttpContext?.User?.FindAll(ClaimTypes.Role)?.Select(r => r.Value) ?? Enumerable.Empty<string>();
public string Email => _httpContextAccessor.HttpContext?.User?.FindFirstValue(ClaimTypes.Email);
}
}
using PSManagement.Application.Contracts.Providers; using PSManagement.Application.Contracts.Authentication;
using PSManagement.Application.Contracts.Providers;
using PSManagement.Domain.Employees.Entities; using PSManagement.Domain.Employees.Entities;
using PSManagement.Domain.Identity.Entities; using PSManagement.Domain.Identity.Entities;
using System.Collections.Generic; using System.Collections.Generic;
......
...@@ -26,7 +26,7 @@ namespace PSManagement.Infrastructure.Services.Storage ...@@ -26,7 +26,7 @@ namespace PSManagement.Infrastructure.Services.Storage
{ {
return Result.Invalid(new ValidationError("File type not allowed.")); return Result.Invalid(new ValidationError("File type not allowed."));
} }
fileName = fileName + "." + Path.GetExtension(file.FileName); fileName = fileName + Path.GetExtension(file.FileName);
var filePath = Path.Combine("wwwroot\\uploads", fileName); var filePath = Path.Combine("wwwroot\\uploads", fileName);
using (var stream = new FileStream(filePath, FileMode.Create)) using (var stream = new FileStream(filePath, FileMode.Create))
{ {
......
...@@ -31,10 +31,13 @@ namespace PSManagement.Infrastructure.Authentication ...@@ -31,10 +31,13 @@ namespace PSManagement.Infrastructure.Authentication
new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtSetting.Secret)), new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtSetting.Secret)),
SecurityAlgorithms.HmacSha256 SecurityAlgorithms.HmacSha256
); );
List<Claim> claims = new List<Claim>{ List<Claim> claims = new List<Claim>{
new Claim(JwtRegisteredClaimNames.Sub, user.Id.ToString()), new Claim(ClaimTypes.NameIdentifier, user.Employee.Id.ToString()),
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()), new Claim(ClaimTypes.Name,user.UserName),
new Claim(JwtRegisteredClaimNames.Email, user.Email) new Claim(ClaimTypes.Email, user.Email),
new Claim("HiastId", user.Employee.HIASTId.ToString())
}; };
foreach (Role role in user.Roles) { foreach (Role role in user.Roles) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment