Skip to content

Commit

Permalink
Refactor, Support for role and permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
Ali-YousefiTelori committed Oct 11, 2023
1 parent 0bf37e2 commit 2d8bcef
Show file tree
Hide file tree
Showing 37 changed files with 302 additions and 459 deletions.
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
using EasyMicroservices.AuthenticationsMicroservice.Database.Entities;
using Microsoft.EntityFrameworkCore;
using EasyMicroservices.Cores.Relational.EntityFrameworkCore;
using EasyMicroservices.Cores.Relational.EntityFrameworkCore.Intrerfaces;
using Microsoft.EntityFrameworkCore;

namespace EasyMicroservices.AuthenticationsMicroservice.Database.Contexts
{
public class AuthenticationsContext : RelationalCoreContext
{
IDatabaseBuilder _builder;
public AuthenticationsContext(IDatabaseBuilder builder)
public AuthenticationsContext(IEntityFrameworkCoreDatabaseBuilder builder) : base(builder)
{
_builder = builder;
}

public DbSet<UserEntity> Users { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (_builder != null)
_builder.OnConfiguring(optionsBuilder);
base.OnConfiguring(optionsBuilder);
}
public DbSet<RoleEntity> Roles { get; set; }
public DbSet<UserRoleEntity> UserRoles { get; set; }
public DbSet<ServicePermissionEntity> ServicePermissions { get; set; }
public DbSet<RoleServicePermissionEntity> RoleServicePermissions { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
base.AutoModelCreating(modelBuilder);

modelBuilder.Entity<UserEntity>(model =>
{
model.HasKey(x => x.Id);
model.HasIndex(u => u.UserName)
.IsUnique();
});

modelBuilder.Entity<ServicePermissionEntity>(model =>
{
model.HasIndex(u => u.MicroserviceName);
model.HasIndex(u => u.ServiceName);
model.HasIndex(u => u.MethodName);
model.HasIndex(u => new { u.MicroserviceName, u.ServiceName, u.MethodName }).IsUnique();
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using EasyMicroservices.AuthenticationsMicroservice.Database.Schemas;
using EasyMicroservices.Cores.Interfaces;
using System.Collections.Generic;

namespace EasyMicroservices.AuthenticationsMicroservice.Database.Entities
{
public class RoleEntity : RoleSchema, IIdSchema<long>
{
public long Id { get; set; }
public long? ParentId { get; set; }
public RoleEntity Parent { get; set; }
public ICollection<RoleEntity> Children { get; set; }
public ICollection<UserRoleEntity> UserRoles { get; set; }
public ICollection<RoleServicePermissionEntity> RoleServicePermissions { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using EasyMicroservices.Cores.Database.Schemas;

namespace EasyMicroservices.AuthenticationsMicroservice.Database.Entities
{
public class RoleServicePermissionEntity : FullAbilityIdSchema<long>
{
public long RoleId { get; set; }
public long ServicePermissionId { get; set; }

public RoleEntity Role { get; set; }
public ServicePermissionEntity ServicePermission { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using EasyMicroservices.AuthenticationsMicroservice.Database.Schemas;
using EasyMicroservices.Cores.Interfaces;
using System.Collections.Generic;

namespace EasyMicroservices.AuthenticationsMicroservice.Database.Entities
{
public class ServicePermissionEntity : ServicePermissionSchema, IIdSchema<long>
{
public long Id { get; set; }
public ICollection<RoleServicePermissionEntity> RoleServicePermissions { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
using EasyMicroservices.AuthenticationsMicroservice.Database.Schemas;
using EasyMicroservices.Cores.Interfaces;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyMicroservices.AuthenticationsMicroservice.Database.Entities
{
public class UserEntity : UserSchema, IIdSchema<long>
{
public long Id { get; set; }
public ICollection<UserRoleEntity> UserRoles { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using EasyMicroservices.Cores.Database.Schemas;

namespace EasyMicroservices.AuthenticationsMicroservice.Database.Entities
{
public class UserRoleEntity : FullAbilityIdSchema<long>
{
public long RoleId { get; set; }
public long UserId { get; set; }

public RoleEntity Role { get; set; }
public UserEntity User { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using EasyMicroservices.Cores.Database.Schemas;

namespace EasyMicroservices.AuthenticationsMicroservice.Database.Schemas
{
public class RoleSchema : FullAbilitySchema
{
public string Name { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using EasyMicroservices.Cores.Database.Schemas;

namespace EasyMicroservices.AuthenticationsMicroservice.Database.Schemas
{
public class ServicePermissionSchema : FullAbilitySchema
{
public string MicroserviceName { get; set; }
public string ServiceName { get; set; }
public string MethodName { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

<ItemGroup>
<PackageReference Include="EasyMicroservices.Configuration" Version="0.0.0.2" />
<PackageReference Include="EasyMicroservices.Cores.Relational.EntityFrameworkCore" Version="0.0.0.26" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.12" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using EasyMicroservices.Cores.Database.Schemas;

namespace EasyMicroservices.AuthenticationsMicroservice.Contracts.Common
{
public class RoleContract : FullAbilityIdSchema<long>
{
public string Name { get; set; }
public long? ParentId { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyMicroservices.AuthenticationsMicroservice.Contracts.Common
{
public class RoleServicePermissionContract
{
public long RoleId { get; set; }
public long ServicePermissionId { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace EasyMicroservices.AuthenticationsMicroservice.Contracts.Common
{
public class ServicePermissionContract
{
public string MicroserviceName { get; set; }
public string ServiceName { get; set; }
public string MethodName { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace EasyMicroservices.AuthenticationsMicroservice.Contracts.Common
{
public class UserRoleContract
{
public long RoleId { get; set; }
public long UserId { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace EasyMicroservices.AuthenticationsMicroservice.Contracts.Requests
{
public class AddRoleRequestContract
{
public string Name { get; set; }
public long? ParentId { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyMicroservices.AuthenticationsMicroservice.Contracts.Requests
{
public class AddRoleServicePermissionRequestContract
{
public long RoleId { get; set; }
public long ServicePermissionId { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace EasyMicroservices.AuthenticationsMicroservice.Contracts.Requests
{
public class AddServicePermissionRequestContract
{
public string MicroserviceName { get; set; }
public string ServiceName { get; set; }
public string MethodName { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace EasyMicroservices.AuthenticationsMicroservice.Contracts.Requests
{
public class AddUserRoleRequestContract
{
public long RoleId { get; set; }
public long UserId { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
Expand All @@ -11,8 +11,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="EasyMicroservices.Cores.AspCoreApi" Version="0.0.0.37" />
<PackageReference Include="EasyMicroservices.Cores.Database" Version="0.0.0.38" />
<PackageReference Include="EasyMicroservices.Cores.AspEntityFrameworkCoreApi" Version="0.0.0.21" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
</ItemGroup>

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
using Microsoft.AspNetCore.Cryptography.KeyDerivation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace EasyMicroservices.AuthenticationsMicroservice.Helpers
{
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using EasyMicroservices.AuthenticationsMicroservice.Database.Entities;
using EasyMicroservices.AuthenticationsMicroservice.Helpers;
using EasyMicroservices.AuthenticationsMicroservice.Interfaces;
using EasyMicroservices.Cores.AspEntityFrameworkCoreApi.Interfaces;
using EasyMicroservices.Cores.Database.Interfaces;
using EasyMicroservices.ServiceContracts;
using Microsoft.Extensions.Configuration;
Expand All @@ -20,17 +21,18 @@ namespace EasyMicroservices.AuthenticationsMicroservice
public class JWTManager : IJWTManager
{
private readonly IConfiguration _config;
private readonly IContractLogic<UserEntity, AddUserRequestContract, UserContract, UserContract, long> _userLogic;
private readonly IUnitOfWork _unitOfWork;

public JWTManager(IContractLogic<UserEntity, AddUserRequestContract, UserContract, UserContract, long> userLogic, IConfiguration config)
public JWTManager(IUnitOfWork unitOfWork, IConfiguration config)
{
_config = config;
_userLogic = userLogic;
_unitOfWork = unitOfWork;
}

public virtual async Task<MessageContract<long>> Login(UserSummaryContract cred)
{
var userRecords = await _userLogic.GetAll();
var logic = _unitOfWork.GetLongContractLogic<UserEntity, AddUserRequestContract, UserContract, UserContract>();
var userRecords = await logic.GetAll();
var user = userRecords.Result.Where(x => x.UserName == cred.UserName && x.Password == cred.Password);
if (!user.Any())
return (FailedReasonType.AccessDenied, "Username or password is invalid."); //"Username or password is invalid."
Expand All @@ -41,12 +43,12 @@ public virtual async Task<MessageContract<long>> Login(UserSummaryContract cred)

public virtual async Task<MessageContract<UserResponseContract>> GenerateToken(UserClaimContract cred)
{

var response = await Login(cred);
if (!response)
return response.ToContract<UserResponseContract>();

var user = await _userLogic.GetBy(x => x.UserName == cred.UserName && x.Password == cred.Password);
var logic = _unitOfWork.GetLongContractLogic<UserEntity, AddUserRequestContract, UserContract, UserContract>();
var user = await logic.GetBy(x => x.UserName == cred.UserName && x.Password == cred.Password);

var tokenHandler = new JwtSecurityTokenHandler();
var key = Encoding.UTF8.GetBytes(_config.GetValue<string>("JWT:Key"));
Expand Down Expand Up @@ -74,12 +76,13 @@ public virtual async Task<MessageContract<long>> Register(AddUserRequestContract
string Password = input.Password;
input.Password = await AuthenticationHelper.HashPassword(input.Password);

var usersRecords = await _userLogic.GetBy(x => x.UserName == input.UserName.ToLower());
var logic = _unitOfWork.GetLongContractLogic<UserEntity, AddUserRequestContract, UserContract, UserContract>();
var usersRecords = await logic.GetBy(x => x.UserName == input.UserName.ToLower());

if (usersRecords.IsSuccess)
return (FailedReasonType.Duplicate, "User already exists!");

var user = await _userLogic.Add(input);
var user = await logic.Add(input);

return user;
}
Expand Down
Loading

0 comments on commit 2d8bcef

Please sign in to comment.