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

implement current user proivder serivce , occupancy service

parent 4b446462
using Ardalis.Result;
using AutoMapper;
using PSManagement.Application.Contracts.Authentication;
using PSManagement.Application.Contracts.Tokens;
using PSManagement.Domain.Customers.DomainErrors;
......@@ -8,6 +9,7 @@ using PSManagement.Domain.Identity.Repositories;
using PSManagement.Domain.Identity.Specification;
using PSManagement.SharedKernel.Specification;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace PSManagement.Infrastructure.Services.Authentication
......@@ -17,15 +19,17 @@ namespace PSManagement.Infrastructure.Services.Authentication
private readonly IJwtTokenGenerator _jwtTokenGenerator;
private readonly IUsersRepository _userRepository;
private readonly BaseSpecification<User> _specification;
public AuthenticationService(IJwtTokenGenerator jwtTokenGenerator, IUsersRepository userRepository)
private readonly IMapper _mapper;
public AuthenticationService(IJwtTokenGenerator jwtTokenGenerator, IUsersRepository userRepository, IMapper mapper)
{
_jwtTokenGenerator = jwtTokenGenerator;
_userRepository = userRepository;
_specification = new UserSpecification();
_mapper = mapper;
}
public async Task<Result<AuthenticationResult>> Login(String email, String password) {
_specification.AddInclude(e => e.Employee);
User u = await _userRepository.GetByEmail(email,_specification);
if (u is null || u.HashedPassword != password) {
......@@ -35,10 +39,10 @@ namespace PSManagement.Infrastructure.Services.Authentication
String token = _jwtTokenGenerator.GenerateToken(u);
return new AuthenticationResult {
Id = u.Id,
EmployeeId = u.Employee.Id,
Email=u.Email,
FirstName="",
LastName ="",
FirstName=u.Employee.PersonalInfo.FirstName,
LastName =u.Employee.PersonalInfo.LastName,
Token=token};
}
public async Task<Result<AuthenticationResult>> Register(String email, String userName, String password) {
......@@ -58,11 +62,11 @@ namespace PSManagement.Infrastructure.Services.Authentication
return (
new AuthenticationResult
{
Id = user.Id,
EmployeeId = user.Employee.Id,
Email = email,
FirstName = user.Employee?.PersonalInfo.FirstName,
LastName = user.Employee?.PersonalInfo.LastName,
Roles=user.Roles,
Roles=_mapper.Map<ICollection<RoleDTO>>(user.Roles),
Token = token
});
......
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using PSManagement.Application.Contracts.Authentication;
using PSManagement.Application.Contracts.Authorization;
using PSManagement.Application.Contracts.Email;
using PSManagement.Application.Contracts.Occupancy;
using PSManagement.Application.Contracts.Providers;
using PSManagement.Application.Contracts.Storage;
using PSManagement.Application.Contracts.SyncData;
......@@ -14,6 +16,7 @@ using PSManagement.Infrastructure.BackgroundServcies;
using PSManagement.Infrastructure.Services.Authentication;
using PSManagement.Infrastructure.Services.Authorization;
using PSManagement.Infrastructure.Services.Email;
using PSManagement.Infrastructure.Services.Occupancy;
using PSManagement.Infrastructure.Services.Providers;
using PSManagement.Infrastructure.Services.Storage;
using PSManagement.Infrastructure.Settings;
......@@ -38,6 +41,8 @@ namespace PSManagement.Infrastructure.DI
{
services.AddSingleton<IDateTimeProvider, DateTimeProvider>();
services.AddScoped<IEmployeesProvider, EmployeesProvider>();
services.AddScoped<ICurrentUserProvider,CurrentUserProvider>();
services.AddScoped<IFileService,FileService>();
services.AddScoped<IEmailService,EmailService>();
return services;
......@@ -45,9 +50,10 @@ namespace PSManagement.Infrastructure.DI
private static IServiceCollection AddBackgroundServices(this IServiceCollection services,IConfiguration configuration)
{
services.Configure<EmployeesSyncJobSettings>(configuration.GetSection("EmpoyeesSyncJobSettings"));
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddScoped<ISyncEmployeesService, SyncEmployeesService>();
services.AddScoped<IOccupancySystemNotifier,OccupancySystemNotifier>();
services.AddHostedService<BackgroundJobSyncEmployees>();
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.Identity.Entities;
using System.Collections.Generic;
......
......@@ -26,7 +26,7 @@ namespace PSManagement.Infrastructure.Services.Storage
{
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);
using (var stream = new FileStream(filePath, FileMode.Create))
{
......
......@@ -31,10 +31,13 @@ namespace PSManagement.Infrastructure.Authentication
new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtSetting.Secret)),
SecurityAlgorithms.HmacSha256
);
List<Claim> claims = new List<Claim>{
new Claim(JwtRegisteredClaimNames.Sub, user.Id.ToString()),
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
new Claim(JwtRegisteredClaimNames.Email, user.Email)
new Claim(ClaimTypes.NameIdentifier, user.Employee.Id.ToString()),
new Claim(ClaimTypes.Name,user.UserName),
new Claim(ClaimTypes.Email, user.Email),
new Claim("HiastId", user.Employee.HIASTId.ToString())
};
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