set version 0.2.1.10

complete blog and authorize
release
Amir Hossein Khademi 2024-01-31 15:03:18 +03:30
parent 5ed8073c35
commit b1b831f136
15 changed files with 62 additions and 64 deletions

View File

@ -1 +1 @@
0.0.0.9 0.2.1.10

View File

@ -32,8 +32,7 @@ public class BlogCategoryController : ICarterModule
} }
// GET:Get All Entity // GET:Get All Entity
public async Task<IResult> GetAllAsync([FromQuery] int? page, IRepositoryWrapper repositoryWrapper, public async Task<IResult> GetAllAsync([FromQuery] int? page, IRepositoryWrapper repositoryWrapper, CancellationToken cancellationToken)
CancellationToken cancellationToken)
{ {
if (page != null) if (page != null)
{ {

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
@ -6,8 +6,8 @@
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<InvariantGlobalization>true</InvariantGlobalization> <InvariantGlobalization>true</InvariantGlobalization>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS> <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<AssemblyVersion>0.0.0.9</AssemblyVersion> <AssemblyVersion>0.2.1.10</AssemblyVersion>
<FileVersion>0.0.0.9</FileVersion> <FileVersion>0.2.1.10</FileVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -45,6 +45,7 @@
public TUser User { get; set; } public TUser User { get; set; }
public string BearerToken => $"Bearer {access_token}"; public string BearerToken => $"Bearer {access_token}";
public List<string> Permissions { get; set; } public List<string> Permissions { get; set; }
public string RoleName { get; set; }
} }

View File

@ -2,11 +2,8 @@
public interface IJwtService : IScopedDependency public interface IJwtService : IScopedDependency
{ {
Task<AccessToken<TUser>> Generate<TUser>(TUser user, Guid complexId, Guid roleId) where TUser : ApplicationUser;
Task<AccessToken<TUser>> Generate<TUser>(TUser user, Guid complexId) where TUser : ApplicationUser;
Task<AccessToken<TUser>> Generate<TUser>(TUser user) where TUser : ApplicationUser; Task<AccessToken<TUser>> Generate<TUser>(TUser user) where TUser : ApplicationUser;
Task<AccessToken<TUserDto>> Generate<TUserDto, TUser>(TUser user, Guid complexId, Guid roleId) where TUser : ApplicationUser; Task<AccessToken<TUserDto>> Generate<TUserDto, TUser>(TUser user, List<string> roleNames) where TUser : ApplicationUser;
Task<AccessToken<TUserDto>> Generate<TUserDto, TUser>(TUser user, Guid complexId) where TUser : ApplicationUser;
Task<AccessToken<TUserDto>> Generate<TUserDto, TUser>(TUser user) where TUser : ApplicationUser; Task<AccessToken<TUserDto>> Generate<TUserDto, TUser>(TUser user) where TUser : ApplicationUser;
} }

View File

@ -16,24 +16,6 @@ public class JwtService : IJwtService
_roleManager = roleManager; _roleManager = roleManager;
_siteSettings = siteSettings.Value; _siteSettings = siteSettings.Value;
} }
public async Task<AccessToken<TUser>> Generate<TUser>(TUser user, Guid complexId, Guid roleId) where TUser : ApplicationUser
{
var tokenId = StringExtensions.GetId(8);
var claims = await GetClaims(user, tokenId, roleId.ToString());
claims.Add(new Claim("ComplexId", complexId.ToString()));
var token = BaseGenerate<TUser>(user, claims);
token.Permissions = claims.Where(c => c.Type == "Permission").Select(c => c.Value).ToList();
return token;
}
public async Task<AccessToken<TUser>> Generate<TUser>(TUser user, Guid complexId) where TUser : ApplicationUser
{
var tokenId = StringExtensions.GetId(8);
var claims = await GetClaims(user, tokenId);
claims.Add(new Claim("ComplexId", complexId.ToString()));
return BaseGenerate(user, claims);
}
public async Task<AccessToken<TUser>> Generate<TUser>(TUser user) where TUser : ApplicationUser public async Task<AccessToken<TUser>> Generate<TUser>(TUser user) where TUser : ApplicationUser
{ {
var tokenId = StringExtensions.GetId(8); var tokenId = StringExtensions.GetId(8);
@ -42,26 +24,17 @@ public class JwtService : IJwtService
} }
public async Task<AccessToken<TUserDto>> Generate<TUserDto, TUser>(TUser user, Guid complexId, Guid roleId) where TUser : ApplicationUser public async Task<AccessToken<TUserDto>> Generate<TUserDto, TUser>(TUser user, List<string> roleNames) where TUser : ApplicationUser
{ {
var tokenId = StringExtensions.GetId(8); var tokenId = StringExtensions.GetId(8);
var claims = await GetClaims(user, tokenId, roleId.ToString()); var claims = await GetClaims(user, tokenId, roleNames.ToArray());
claims.Add(new Claim("ComplexId", complexId.ToString()));
var token = BaseGenerate<TUserDto, TUser>(user, claims); var token = BaseGenerate<TUserDto, TUser>(user, claims);
token.Permissions = claims.Where(c => c.Type == "Permission").Select(c => c.Value).ToList(); token.Permissions = claims.Where(c => c.Type == "Permission").Select(c => c.Value).ToList();
token.RoleName = claims.FirstOrDefault(c => c.Type == ClaimTypes.Role)?.Value;
return token; return token;
} }
public async Task<AccessToken<TUserDto>> Generate<TUserDto, TUser>(TUser user, Guid complexId) where TUser : ApplicationUser
{
var tokenId = StringExtensions.GetId(8);
var claims = await GetClaims(user, tokenId);
claims.Add(new Claim("ComplexId", complexId.ToString()));
return BaseGenerate<TUserDto, TUser>(user, claims);
}
public async Task<AccessToken<TUserDto>> Generate<TUserDto, TUser>(TUser user) where TUser : ApplicationUser public async Task<AccessToken<TUserDto>> Generate<TUserDto, TUser>(TUser user) where TUser : ApplicationUser
{ {
var tokenId = StringExtensions.GetId(8); var tokenId = StringExtensions.GetId(8);
@ -129,19 +102,25 @@ public class JwtService : IJwtService
} }
private async Task<List<Claim>> GetClaims<TUser>(TUser baseUser, string jwtId, string roleId) where TUser : ApplicationUser private async Task<List<Claim>> GetClaims<TUser>(TUser baseUser, string jwtId, params string[] roleNames) where TUser : ApplicationUser
{ {
var applicationRole = await _roleManager.FindByIdAsync(roleId);
var roleClaims = await _roleManager.GetClaimsAsync(applicationRole);
var claims = new List<Claim>(); var claims = new List<Claim>();
foreach (var roleName in roleNames)
{
var applicationRole = await _roleManager.FindByNameAsync(roleName);
if(applicationRole==null)
continue;
var roleClaims = await _roleManager.GetClaimsAsync(applicationRole);
claims.AddRange(roleClaims);
claims.Add(new Claim(ClaimTypes.Role, applicationRole.EnglishName));
claims.Add(new Claim("RoleId", applicationRole.Id.ToString()));
}
claims.Add(new Claim("SignUpStatus", ((int)baseUser.SignUpStatus).ToString())); claims.Add(new Claim("SignUpStatus", ((int)baseUser.SignUpStatus).ToString()));
claims.Add(new Claim(ClaimTypes.Name, baseUser.UserName)); claims.Add(new Claim(ClaimTypes.Name, baseUser.UserName));
claims.Add(new Claim(ClaimTypes.NameIdentifier, baseUser.Id.ToString())); claims.Add(new Claim(ClaimTypes.NameIdentifier, baseUser.Id.ToString()));
claims.Add(new Claim(ClaimTypes.Role, applicationRole.EnglishName));
claims.Add(new Claim("RoleId", applicationRole.Id.ToString()));
if (baseUser.Email != null) if (baseUser.Email != null)
claims.Add(new Claim(ClaimTypes.Email, baseUser.Email)); claims.Add(new Claim(ClaimTypes.Email, baseUser.Email));
claims.AddRange(roleClaims);
claims.Add(new Claim("JwtID", jwtId)); claims.Add(new Claim("JwtID", jwtId));
claims.Add(new Claim(ClaimTypes.Gender, baseUser.Gender == 0 ? "Female" : "Mail")); claims.Add(new Claim(ClaimTypes.Gender, baseUser.Gender == 0 ? "Female" : "Mail"));
return claims; return claims;

View File

@ -145,9 +145,9 @@ public class AccountService : IAccountService
private async Task<AccessToken<ApplicationUserSDto>> CompleteLogin(ApplicationUser user, CancellationToken cancellationToken) private async Task<AccessToken<ApplicationUserSDto>> CompleteLogin(ApplicationUser user, CancellationToken cancellationToken)
{ {
AccessToken<ApplicationUserSDto> jwt; AccessToken<ApplicationUserSDto> jwt;
jwt = await _jwtService.Generate<ApplicationUserSDto, ApplicationUser>(user); var role = await _userManager.GetRolesAsync(user);
jwt = await _jwtService.Generate<ApplicationUserSDto, ApplicationUser>(user, role.ToList());
jwt.User.RoleName = jwt.RoleName;
return jwt; return jwt;
} }

View File

@ -10,5 +10,5 @@ public class BlogLDto : BaseDto<BlogLDto , Blog>
public bool IsSuggested { get; set; } public bool IsSuggested { get; set; }
public Guid CategoryId { get; set; } public Guid CategoryId { get; set; }
public string CategoryName { get; set; } = string.Empty; public string CategoryName { get; set; } = string.Empty;
public List<StorageFileSDto> Files { get; internal set; } = new(); public List<StorageFileSDto> Files { get; set; } = new();
} }

View File

@ -10,6 +10,9 @@ public class ApplicationUserSDto : BaseDto<ApplicationUserSDto, ApplicationUser>
public Gender Gender { get; set; } public Gender Gender { get; set; }
public SignUpStatus SignUpStatus { get; set; } public SignUpStatus SignUpStatus { get; set; }
public string NationalId { get; set; } = string.Empty; public string NationalId { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public string FullName => FirstName + " " + LastName;
public string RoleName { get; set; } = string.Empty;
public List<Guid> RoleIds { get; set; } = new(); public List<Guid> RoleIds { get; set; } = new();
public long BirthDateTimeStamp => DateTimeExtensions.DateTimeToUnixTimeStamp(BirthDate); public long BirthDateTimeStamp => DateTimeExtensions.DateTimeToUnixTimeStamp(BirthDate);

View File

@ -18,6 +18,7 @@ namespace NetinaShop.Domain.Mappers
Gender = p1.Gender, Gender = p1.Gender,
SignUpStatus = p1.SignUpStatus, SignUpStatus = p1.SignUpStatus,
Id = p1.Id, Id = p1.Id,
Email = p1.Email,
PhoneNumber = p1.PhoneNumber PhoneNumber = p1.PhoneNumber
}; };
} }
@ -36,6 +37,7 @@ namespace NetinaShop.Domain.Mappers
result.Gender = p2.Gender; result.Gender = p2.Gender;
result.SignUpStatus = p2.SignUpStatus; result.SignUpStatus = p2.SignUpStatus;
result.Id = p2.Id; result.Id = p2.Id;
result.Email = p2.Email;
result.PhoneNumber = p2.PhoneNumber; result.PhoneNumber = p2.PhoneNumber;
return result; return result;
@ -51,6 +53,7 @@ namespace NetinaShop.Domain.Mappers
Gender = p4.Gender, Gender = p4.Gender,
SignUpStatus = p4.SignUpStatus, SignUpStatus = p4.SignUpStatus,
NationalId = p4.NationalId, NationalId = p4.NationalId,
Email = p4.Email,
Id = p4.Id Id = p4.Id
}; };
} }
@ -69,6 +72,7 @@ namespace NetinaShop.Domain.Mappers
result.Gender = p5.Gender; result.Gender = p5.Gender;
result.SignUpStatus = p5.SignUpStatus; result.SignUpStatus = p5.SignUpStatus;
result.NationalId = p5.NationalId; result.NationalId = p5.NationalId;
result.Email = p5.Email;
result.Id = p5.Id; result.Id = p5.Id;
return result; return result;
@ -82,6 +86,7 @@ namespace NetinaShop.Domain.Mappers
Gender = p7.Gender, Gender = p7.Gender,
SignUpStatus = p7.SignUpStatus, SignUpStatus = p7.SignUpStatus,
NationalId = p7.NationalId, NationalId = p7.NationalId,
Email = p7.Email,
Id = p7.Id Id = p7.Id
}; };
} }

View File

@ -63,7 +63,8 @@ namespace NetinaShop.Domain.Mappers
IsHeader = p8.IsHeader, IsHeader = p8.IsHeader,
IsPrimary = p8.IsPrimary, IsPrimary = p8.IsPrimary,
FileType = p8.FileType, FileType = p8.FileType,
Id = p8.Id Id = p8.Id,
CreatedAt = p8.CreatedAt
}).ToList<BlogStorageFile>(), }).ToList<BlogStorageFile>(),
Id = p7.Id Id = p7.Id
}; };
@ -238,7 +239,8 @@ namespace NetinaShop.Domain.Mappers
IsHeader = item.IsHeader, IsHeader = item.IsHeader,
IsPrimary = item.IsPrimary, IsPrimary = item.IsPrimary,
FileType = item.FileType, FileType = item.FileType,
Id = item.Id Id = item.Id,
CreatedAt = item.CreatedAt
}); });
i++; i++;
} }
@ -268,7 +270,8 @@ namespace NetinaShop.Domain.Mappers
IsHeader = item.IsHeader, IsHeader = item.IsHeader,
IsPrimary = item.IsPrimary, IsPrimary = item.IsPrimary,
FileType = item.FileType, FileType = item.FileType,
Id = item.Id Id = item.Id,
CreatedAt = item.CreatedAt
}); });
i++; i++;
} }

View File

@ -53,7 +53,8 @@ namespace NetinaShop.Domain.Mappers
IsHeader = p8.IsHeader, IsHeader = p8.IsHeader,
IsPrimary = p8.IsPrimary, IsPrimary = p8.IsPrimary,
FileType = p8.FileType, FileType = p8.FileType,
Id = p8.Id Id = p8.Id,
CreatedAt = p8.CreatedAt
}).ToList<BrandStorageFile>(), }).ToList<BrandStorageFile>(),
Id = p7.Id Id = p7.Id
}; };
@ -192,7 +193,8 @@ namespace NetinaShop.Domain.Mappers
IsHeader = item.IsHeader, IsHeader = item.IsHeader,
IsPrimary = item.IsPrimary, IsPrimary = item.IsPrimary,
FileType = item.FileType, FileType = item.FileType,
Id = item.Id Id = item.Id,
CreatedAt = item.CreatedAt
}); });
i++; i++;
} }
@ -222,7 +224,8 @@ namespace NetinaShop.Domain.Mappers
IsHeader = item.IsHeader, IsHeader = item.IsHeader,
IsPrimary = item.IsPrimary, IsPrimary = item.IsPrimary,
FileType = item.FileType, FileType = item.FileType,
Id = item.Id Id = item.Id,
CreatedAt = item.CreatedAt
}); });
i++; i++;
} }

View File

@ -50,7 +50,8 @@ namespace NetinaShop.Domain.Mappers
IsHeader = p8.IsHeader, IsHeader = p8.IsHeader,
IsPrimary = p8.IsPrimary, IsPrimary = p8.IsPrimary,
FileType = p8.FileType, FileType = p8.FileType,
Id = p8.Id Id = p8.Id,
CreatedAt = p8.CreatedAt
}).ToList<ProductCategoryStorageFile>(), }).ToList<ProductCategoryStorageFile>(),
Id = p7.Id Id = p7.Id
}; };
@ -186,7 +187,8 @@ namespace NetinaShop.Domain.Mappers
IsHeader = item.IsHeader, IsHeader = item.IsHeader,
IsPrimary = item.IsPrimary, IsPrimary = item.IsPrimary,
FileType = item.FileType, FileType = item.FileType,
Id = item.Id Id = item.Id,
CreatedAt = item.CreatedAt
}); });
i++; i++;
} }
@ -216,7 +218,8 @@ namespace NetinaShop.Domain.Mappers
IsHeader = item.IsHeader, IsHeader = item.IsHeader,
IsPrimary = item.IsPrimary, IsPrimary = item.IsPrimary,
FileType = item.FileType, FileType = item.FileType,
Id = item.Id Id = item.Id,
CreatedAt = item.CreatedAt
}); });
i++; i++;
} }

View File

@ -104,7 +104,8 @@ namespace NetinaShop.Domain.Mappers
IsHeader = p16.IsHeader, IsHeader = p16.IsHeader,
IsPrimary = p16.IsPrimary, IsPrimary = p16.IsPrimary,
FileType = p16.FileType, FileType = p16.FileType,
Id = p16.Id Id = p16.Id,
CreatedAt = p16.CreatedAt
}).ToList<ProductStorageFile>(), }).ToList<ProductStorageFile>(),
Id = p13.Id Id = p13.Id
}; };
@ -415,7 +416,8 @@ namespace NetinaShop.Domain.Mappers
IsHeader = item.IsHeader, IsHeader = item.IsHeader,
IsPrimary = item.IsPrimary, IsPrimary = item.IsPrimary,
FileType = item.FileType, FileType = item.FileType,
Id = item.Id Id = item.Id,
CreatedAt = item.CreatedAt
}); });
i++; i++;
} }
@ -505,7 +507,8 @@ namespace NetinaShop.Domain.Mappers
IsHeader = item.IsHeader, IsHeader = item.IsHeader,
IsPrimary = item.IsPrimary, IsPrimary = item.IsPrimary,
FileType = item.FileType, FileType = item.FileType,
Id = item.Id Id = item.Id,
CreatedAt = item.CreatedAt
}); });
i++; i++;
} }

View File

@ -17,7 +17,8 @@ namespace NetinaShop.Domain.Mappers
IsHeader = p1.IsHeader, IsHeader = p1.IsHeader,
IsPrimary = p1.IsPrimary, IsPrimary = p1.IsPrimary,
FileType = p1.FileType, FileType = p1.FileType,
Id = p1.Id Id = p1.Id,
CreatedAt = p1.CreatedAt
}; };
} }
public static StorageFile AdaptTo(this StorageFileSDto p2, StorageFile p3) public static StorageFile AdaptTo(this StorageFileSDto p2, StorageFile p3)
@ -35,6 +36,7 @@ namespace NetinaShop.Domain.Mappers
result.IsPrimary = p2.IsPrimary; result.IsPrimary = p2.IsPrimary;
result.FileType = p2.FileType; result.FileType = p2.FileType;
result.Id = p2.Id; result.Id = p2.Id;
result.CreatedAt = p2.CreatedAt;
return result; return result;
} }