Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
ProjectsStatusManagement
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
hasan.bahjat
ProjectsStatusManagement
Commits
2bdcfc1a
Commit
2bdcfc1a
authored
Aug 19, 2024
by
hasan khaddour
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
implement current user proivder serivce , occupancy service
parent
4b446462
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
104 additions
and
14 deletions
+104
-14
AuthenticationService.cs
...nt.Infrastructure/Authentication/AuthenticationService.cs
+12
-8
DependencyInjection.cs
PSManagement.Infrastructure/DI/DependencyInjection.cs
+7
-1
OccupancySystemNotifier.cs
...ement.Infrastructure/Occupancy/OccupancySystemNotifier.cs
+28
-0
CurrentUserProvider.cs
PSManagement.Infrastructure/Providers/CurrentUserProvider.cs
+48
-0
EmployeesProvider.cs
PSManagement.Infrastructure/Providers/EmployeesProvider.cs
+2
-1
FileService.cs
PSManagement.Infrastructure/Storage/FileService.cs
+1
-1
JwtTokenGenerator.cs
PSManagement.Infrastructure/Tokens/JwtTokenGenerator.cs
+6
-3
No files found.
PSManagement.Infrastructure/Authentication/AuthenticationService.cs
View file @
2bdcfc1a
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,16 +19,18 @@ 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
)
{
return
Result
.
Invalid
(
UserErrors
.
InvalidLoginAttempt
);
...
...
@@ -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
});
...
...
PSManagement.Infrastructure/DI/DependencyInjection.cs
View file @
2bdcfc1a
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
;
...
...
PSManagement.Infrastructure/Occupancy/OccupancySystemNotifier.cs
0 → 100644
View file @
2bdcfc1a
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
;
}
}
}
PSManagement.Infrastructure/Providers/CurrentUserProvider.cs
0 → 100644
View file @
2bdcfc1a
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
);
}
}
PSManagement.Infrastructure/Providers/EmployeesProvider.cs
View file @
2bdcfc1a
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
;
...
...
PSManagement.Infrastructure/Storage/FileService.cs
View file @
2bdcfc1a
...
...
@@ -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
))
{
...
...
PSManagement.Infrastructure/Tokens/JwtTokenGenerator.cs
View file @
2bdcfc1a
...
...
@@ -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
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment