Skip to content

Commit

Permalink
identity added and ui finished
Browse files Browse the repository at this point in the history
  • Loading branch information
rahiyansafin committed May 6, 2023
1 parent e11597e commit e114e28
Show file tree
Hide file tree
Showing 28 changed files with 1,844 additions and 9 deletions.
1 change: 1 addition & 0 deletions HR.LeaveManagement.API/HR.LeaveManagement.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

<ItemGroup>
<ProjectReference Include="..\HR.LeaveManagement.Application\HR.LeaveManagement.Application.csproj" />
<ProjectReference Include="..\HR.LeaveManagement.Identity\HR.LeaveManagement.Identity.csproj" />
<ProjectReference Include="..\HR.LeaveManagement.Infrastructure\HR.LeaveManagement.Infrastructure.csproj" />
<ProjectReference Include="..\HR.LeaveManagement.Persistence\HR.LeaveManagement.Persistence.csproj" />
</ItemGroup>
Expand Down
3 changes: 3 additions & 0 deletions HR.LeaveManagement.API/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using HR.LeaveManagement.API.Middlewares;
using HR.LeaveManagement.Application;
using HR.LeaveManagement.Identity;
using HR.LeaveManagement.Infrastructure;
using HR.LeaveManagement.Persistence;

Expand All @@ -15,6 +16,7 @@
builder.Services.AddApplicationServices();
builder.Services.AddInfrastructureServices(builder.Configuration);
builder.Services.AddPersistenceServices(builder.Configuration);
builder.Services.AddIdentityServices(builder.Configuration);

builder.Services.AddControllers();

Expand Down Expand Up @@ -49,6 +51,7 @@
app.UseCors("all");

app.UseAuthentication();

app.UseAuthorization();

app.MapControllers();
Expand Down
2 changes: 1 addition & 1 deletion HR.LeaveManagement.API/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
}
},
"ConnectionStrings": {
"HrDatabaseConnectionString": "Server=LAPTOP-B438U61N\\SQLEXPRESS;Database=db_hr_leavemanagement;Trusted_Connection=True;MultipleActiveResultSets=true"
"HrDatabaseConnectionString": "Server=LAPTOP-B438U61N\\SQLEXPRESS;Database=db_hr_leavemanagement;Trusted_Connection=True;MultipleActiveResultSets=true;Encrypt=False"
},
"EmailSettings": {
"ApiKey": "SendGrid-Key",
Expand Down
22 changes: 19 additions & 3 deletions HR.LeaveManagement.BlazorUI/App.razor
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
<Router AppAssembly="@typeof(App).Assembly">
<CascadingAuthenticationState>
<Router AppAssembly="@typeof(App).Assembly">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
<NotAuthorized>
@if (context.User?.Identity?.IsAuthenticated == false)
{
<RedirectToLogin />
}
else
{
<PageTitle>Not Authorized</PageTitle>
<LayoutView Layout="@typeof(MainLayout)">
<p role="alert">Sorry, you cannot view this page</p>
</LayoutView>
}
</NotAuthorized>
</AuthorizeRouteView>
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
</Found>
<NotFound>
Expand All @@ -9,4 +24,5 @@
<p role="alert">Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
</Router>
</CascadingAuthenticationState>
2 changes: 1 addition & 1 deletion HR.LeaveManagement.BlazorUI/Pages/Index.razor
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@page "/"

<PageTitle>Index</PageTitle>
<PageTitle>Home</PageTitle>

<h1 class="text-center">Welcome to HR Leave Management System</h1>
<br />
Expand Down
5 changes: 4 additions & 1 deletion HR.LeaveManagement.BlazorUI/Pages/LeaveRequests/Index.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
@page "/leaverequests/"
<h3>Index</h3>

<PageTitle>Leave Requests</PageTitle>

<h3>Leave Requests</h3>

<div class="m-2 d-flex justify-content-end">
<a href="/leaverequests/create" class="btn btn-success">Add Leave Request</a>
Expand Down
2 changes: 2 additions & 0 deletions HR.LeaveManagement.BlazorUI/Pages/LeaveTypes/Index.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
@page "/leavetypes/"

<PageTitle>Leave Types</PageTitle>

<h3>Leave Types</h3>

<div class="m-2 d-flex justify-content-end">
Expand Down
2 changes: 2 additions & 0 deletions HR.LeaveManagement.BlazorUI/Pages/Login.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
@page "/login"

<PageTitle>Login</PageTitle>

<h3>Login</h3>
@if (string.IsNullOrEmpty(Message) == false)
{
Expand Down
2 changes: 2 additions & 0 deletions HR.LeaveManagement.BlazorUI/Pages/Register.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
@page "/register"

<PageTitle>Register</PageTitle>

<h3>Register</h3>

@if (string.IsNullOrEmpty(Message) == false)
Expand Down
2 changes: 1 addition & 1 deletion HR.LeaveManagement.BlazorUI/Shared/MainLayout.razor.css
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ main {
}

.sidebar {
width: 250px;
width: 290px;
height: 100vh;
position: sticky;
top: 0;
Expand Down
4 changes: 2 additions & 2 deletions HR.LeaveManagement.BlazorUI/Shared/NavMenu.razor
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="top-row ps-3 navbar navbar-dark">
<div class="container-fluid">
<a class="navbar-brand" href="">HR.LeaveManagement.BlazorUI</a>
<a class="navbar-brand" href="">HR Leave Management System</a>
<button title="Navigation menu" class="navbar-toggler" @onclick="ToggleNavMenu">
<span class="navbar-toggler-icon"></span>
</button>
Expand Down Expand Up @@ -76,4 +76,4 @@
{
collapseNavMenu = !collapseNavMenu;
}
}
}
6 changes: 6 additions & 0 deletions HR.LeaveManagement.BlazorUI/_Imports.razor
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@
@using Microsoft.AspNetCore.Components.WebAssembly.Http
@using Microsoft.JSInterop
@using HR.LeaveManagement.BlazorUI
@using HR.LeaveManagement.BlazorUI.Pages.LeaveTypes
@using HR.LeaveManagement.BlazorUI.Shared
@using HR.LeaveManagement.BlazorUI.Models
@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Authorization;
@using Blazored.Toast
@using Blazored.Toast.Services
25 changes: 25 additions & 0 deletions HR.LeaveManagement.Identity/Configurations/RoleConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace HR.LeaveManagement.Identity.Configurations;
public class RoleConfiguration : IEntityTypeConfiguration<IdentityRole>
{
public void Configure(EntityTypeBuilder<IdentityRole> builder)
{
builder.HasData(
new IdentityRole
{
Id = "cac43a6e-f7bb-4448-baaf-1add431ccbbf",
Name = "Employee",
NormalizedName = "EMPLOYEE"
},
new IdentityRole
{
Id = "cbc43a8e-f7bb-4445-baaf-1add431ffbbf",
Name = "Administrator",
NormalizedName = "ADMINISTRATOR"
}
);
}
}
40 changes: 40 additions & 0 deletions HR.LeaveManagement.Identity/Configurations/UserConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using HR.LeaveManagement.Identity.Models;

using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace HR.LeaveManagement.Identity.Configurations;
public class UserConfiguration : IEntityTypeConfiguration<ApplicationUser>
{
public void Configure(EntityTypeBuilder<ApplicationUser> builder)
{
var hasher = new PasswordHasher<ApplicationUser>();
builder.HasData(
new ApplicationUser
{
Id = "8e445865-a24d-4543-a6c6-9443d048cdb9",
Email = "[email protected]",
NormalizedEmail = "[email protected]",
FirstName = "System",
LastName = "Admin",
UserName = "[email protected]",
NormalizedUserName = "[email protected]",
PasswordHash = hasher.HashPassword(null, "P@ssword1"),
EmailConfirmed = true
},
new ApplicationUser
{
Id = "9e224968-33e4-4652-b7b7-8574d048cdb9",
Email = "[email protected]",
NormalizedEmail = "[email protected]",
FirstName = "System",
LastName = "User",
UserName = "[email protected]",
NormalizedUserName = "[email protected]",
PasswordHash = hasher.HashPassword(null, "P@ssword1"),
EmailConfirmed = true
}
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace HR.LeaveManagement.Identity.Configurations;
public class UserRoleConfiguration : IEntityTypeConfiguration<IdentityUserRole<string>>
{
public void Configure(EntityTypeBuilder<IdentityUserRole<string>> builder)
{
builder.HasData(
new IdentityUserRole<string>
{
RoleId = "cbc43a8e-f7bb-4445-baaf-1add431ffbbf",
UserId = "8e445865-a24d-4543-a6c6-9443d048cdb9"
},
new IdentityUserRole<string>
{
RoleId = "cac43a6e-f7bb-4448-baaf-1add431ccbbf",
UserId = "9e224968-33e4-4652-b7b7-8574d048cdb9"
}
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using HR.LeaveManagement.Identity.Models;

using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace HR.LeaveManagement.Identity.DbContext;
public class HrLeaveManagementIdentityDbContext : IdentityDbContext<ApplicationUser>
{
public HrLeaveManagementIdentityDbContext(DbContextOptions<HrLeaveManagementIdentityDbContext> options)
: base(options)
{
}

protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.ApplyConfigurationsFromAssembly(typeof(HrLeaveManagementIdentityDbContext).Assembly);
}
}
25 changes: 25 additions & 0 deletions HR.LeaveManagement.Identity/HR.LeaveManagement.Identity.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.5" />
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="7.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="7.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\HR.LeaveManagement.Application\HR.LeaveManagement.Application.csproj" />
</ItemGroup>

</Project>
54 changes: 54 additions & 0 deletions HR.LeaveManagement.Identity/IdentityServicesRegistration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System.Text;

using HR.LeaveManagement.Application.Contracts.Identity;
using HR.LeaveManagement.Application.Models.Identity;
using HR.LeaveManagement.Identity.DbContext;
using HR.LeaveManagement.Identity.Models;
using HR.LeaveManagement.Identity.Services;

using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;

namespace HR.LeaveManagement.Identity;
public static class IdentityServicesRegistration
{
public static IServiceCollection AddIdentityServices(this IServiceCollection services, IConfiguration configuration)
{
services.Configure<JwtSettings>(configuration.GetSection("JwtSettings"));

services.AddDbContext<HrLeaveManagementIdentityDbContext>(options =>
options.UseSqlServer(configuration.GetConnectionString("HrDatabaseConnectionString")));

services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<HrLeaveManagementIdentityDbContext>().AddDefaultTokenProviders();

services.AddTransient<IAuthService, AuthService>();
services.AddTransient<IUserService, UserService>();

services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(o =>
{
o.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ClockSkew = TimeSpan.Zero,
ValidIssuer = configuration["JwtSettings:Issuer"],
ValidAudience = configuration["JwtSettings:Audience"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration["JwtSettings:Key"]))

};
});

return services;
}
}
Loading

0 comments on commit e114e28

Please sign in to comment.