Adding role creation during startup + minor fixes in tests
This commit is contained in:
@@ -17,10 +17,11 @@ public class AuthService : BaseService, IAuthService
|
||||
protected readonly IUserService _userService;
|
||||
|
||||
public AuthService(
|
||||
IHttpContextAccessor httpContextAccessor,
|
||||
IConfiguration configuration,
|
||||
SqlServerContext sqlServerContext,
|
||||
IUserService userService
|
||||
) : base(configuration, sqlServerContext)
|
||||
) : base(httpContextAccessor, configuration, sqlServerContext)
|
||||
{
|
||||
_cryptUtils = new CryptUtils(_appSettings);
|
||||
_userService = userService;
|
||||
@@ -29,7 +30,8 @@ public class AuthService : BaseService, IAuthService
|
||||
public async Task<AuthenticatedUser?> AuthenticateAsync(AuthenticateRequestData data)
|
||||
{
|
||||
AuthenticatedUser? authenticatedUser = null;
|
||||
var decryptedUsername = _cryptUtils.Decrypt(data.Username ?? String.Empty);
|
||||
|
||||
var decryptedUsername = _cryptUtils.Decrypt(data.Email ?? String.Empty);
|
||||
var decryptedPassword = _cryptUtils.Decrypt(data.Password ?? String.Empty);
|
||||
|
||||
if (!String.IsNullOrEmpty(decryptedUsername) && !String.IsNullOrEmpty(decryptedPassword))
|
||||
|
||||
@@ -1,23 +1,43 @@
|
||||
using BasicDotnetTemplate.MainProject.Core.Database;
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Common.User;
|
||||
using BasicDotnetTemplate.MainProject.Models.Settings;
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject.Services;
|
||||
|
||||
public class BaseService
|
||||
{
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
protected readonly IConfiguration _configuration;
|
||||
protected readonly AppSettings _appSettings;
|
||||
protected readonly SqlServerContext _sqlServerContext;
|
||||
|
||||
public BaseService(
|
||||
IHttpContextAccessor httpContextAccessor,
|
||||
IConfiguration configuration,
|
||||
SqlServerContext sqlServerContext
|
||||
)
|
||||
{
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
_configuration = configuration;
|
||||
_appSettings = new AppSettings();
|
||||
_configuration.GetSection("AppSettings").Bind(_appSettings);
|
||||
_sqlServerContext = sqlServerContext;
|
||||
}
|
||||
|
||||
protected int? GetCurrentUserId()
|
||||
{
|
||||
int? userId = null;
|
||||
var user = this.GetCurrentUser();
|
||||
if (user != null)
|
||||
{
|
||||
userId = this._sqlServerContext.Users.Where(x => !x.IsDeleted && x.Guid == user.Guid).FirstOrDefault()?.Id;
|
||||
}
|
||||
return userId;
|
||||
}
|
||||
|
||||
protected AuthenticatedUser? GetCurrentUser()
|
||||
{
|
||||
return _httpContextAccessor.HttpContext?.Items["User"] as AuthenticatedUser;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,10 +21,10 @@ public class JwtService : BaseService, IJwtService
|
||||
private readonly JwtTokenUtils _jwtTokenUtils;
|
||||
|
||||
public JwtService(
|
||||
IHttpContextAccessor httpContextAccessor,
|
||||
IConfiguration configuration,
|
||||
SqlServerContext sqlServerContext,
|
||||
IUserService userService
|
||||
) : base(configuration, sqlServerContext)
|
||||
SqlServerContext sqlServerContext
|
||||
) : base(httpContextAccessor, configuration, sqlServerContext)
|
||||
{
|
||||
_jwtTokenUtils = new JwtTokenUtils(_appSettings);
|
||||
}
|
||||
|
||||
103
MainProject/Services/RoleService.cs
Normal file
103
MainProject/Services/RoleService.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
|
||||
using System.Collections;
|
||||
using BasicDotnetTemplate.MainProject.Core.Database;
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Data.Role;
|
||||
using BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject.Services;
|
||||
|
||||
public interface IRoleService
|
||||
{
|
||||
Task<Role?> GetRoleByIdAsync(int id);
|
||||
Task<Role?> GetRoleByGuidAsync(string guid);
|
||||
Task<bool> CheckIfNameIsValid(string name, string? guid = "");
|
||||
Task<Role?> CreateRole(CreateRoleRequestData data);
|
||||
}
|
||||
|
||||
public class RoleService : BaseService, IRoleService
|
||||
{
|
||||
public RoleService(
|
||||
IHttpContextAccessor httpContextAccessor,
|
||||
IConfiguration configuration,
|
||||
SqlServerContext sqlServerContext
|
||||
) : base(httpContextAccessor, configuration, sqlServerContext)
|
||||
{ }
|
||||
|
||||
private IQueryable<Role> GetRolesQueryable()
|
||||
{
|
||||
return this._sqlServerContext.Roles.Where(x => !x.IsDeleted);
|
||||
}
|
||||
|
||||
private IQueryable<Role> GetRoleByNameQueryable(string name)
|
||||
{
|
||||
return this.GetRolesQueryable().Where(x =>
|
||||
x.Name.ToString() == name.ToString()
|
||||
);
|
||||
}
|
||||
|
||||
private Role CreateRoleData(CreateRoleRequestData data)
|
||||
{
|
||||
Role role = new()
|
||||
{
|
||||
CreationTime = DateTime.UtcNow,
|
||||
CreationUserId = this.GetCurrentUserId(),
|
||||
IsDeleted = false,
|
||||
Guid = Guid.NewGuid().ToString(),
|
||||
Name = data.Name,
|
||||
IsNotEditable = data.IsNotEditable
|
||||
};
|
||||
|
||||
return role;
|
||||
}
|
||||
|
||||
|
||||
public async Task<Role?> GetRoleByIdAsync(int id)
|
||||
{
|
||||
return await this.GetRolesQueryable().Where(x => x.Id == id).FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public async Task<Role?> GetRoleByGuidAsync(string guid)
|
||||
{
|
||||
return await this.GetRolesQueryable().Where(x => x.Guid == guid).FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public async Task<bool> CheckIfNameIsValid(string name, string? guid = "")
|
||||
{
|
||||
var valid = false;
|
||||
|
||||
Role? role = await this.GetRoleByNameQueryable(name).FirstOrDefaultAsync();
|
||||
if (role != null)
|
||||
{
|
||||
if (!String.IsNullOrEmpty(guid))
|
||||
{
|
||||
valid = role.Guid == guid && role.Name == name;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
valid = true;
|
||||
}
|
||||
|
||||
return valid;
|
||||
}
|
||||
|
||||
public async Task<Role?> CreateRole(CreateRoleRequestData data)
|
||||
{
|
||||
Role? role = null;
|
||||
|
||||
using (var transaction = _sqlServerContext.Database.BeginTransactionAsync())
|
||||
{
|
||||
var tempRole = this.CreateRoleData(data);
|
||||
await _sqlServerContext.Roles.AddAsync(tempRole);
|
||||
await _sqlServerContext.SaveChangesAsync();
|
||||
await (await transaction).CommitAsync();
|
||||
role = tempRole;
|
||||
}
|
||||
|
||||
return role;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
|
||||
using System.Collections;
|
||||
using BasicDotnetTemplate.MainProject.Core.Database;
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Data.User;
|
||||
using BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
@@ -10,65 +11,109 @@ public interface IUserService
|
||||
{
|
||||
Task<User?> GetUserByIdAsync(int id);
|
||||
Task<User?> GetUserByGuidAsync(string guid);
|
||||
Task<User?> GetUserByUsernameAndPassword(string username, string password);
|
||||
Task<User?> GetUserByUsernameAndPassword(string email, string password);
|
||||
Task<bool> CheckIfEmailIsValid(string email, string? guid = "");
|
||||
Task<User?> CreateUser(CreateUserRequestData data, Role role);
|
||||
}
|
||||
|
||||
public class UserService : BaseService, IUserService
|
||||
{
|
||||
|
||||
public UserService(
|
||||
IHttpContextAccessor httpContextAccessor,
|
||||
IConfiguration configuration,
|
||||
SqlServerContext sqlServerContext
|
||||
) : base(configuration, sqlServerContext)
|
||||
) : base(httpContextAccessor, configuration, sqlServerContext)
|
||||
{ }
|
||||
|
||||
private IQueryable<User> GetUsers()
|
||||
private IQueryable<User> GetUsersQueryable()
|
||||
{
|
||||
return this._sqlServerContext.Users.Where(x => !x.IsDeleted);
|
||||
}
|
||||
|
||||
private IQueryable<User> GetUserByUsername(string username)
|
||||
private IQueryable<User> GetUserByEmailQueryable(string email)
|
||||
{
|
||||
return this.GetUsers().Where(x =>
|
||||
x.Username.ToString() == username.ToString()
|
||||
return this.GetUsersQueryable().Where(x =>
|
||||
x.Email.ToString() == email.ToString()
|
||||
);
|
||||
}
|
||||
|
||||
public async Task<User?> GetUserByIdAsync(int id)
|
||||
private User CreateUserData(CreateUserRequestData data, Role role)
|
||||
{
|
||||
return await this.GetUsers().Where(x => x.Id == id).FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public async Task<User?> GetUserByGuidAsync(string guid)
|
||||
{
|
||||
return await this.GetUsers().Where(x => x.Guid == guid).FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public async Task<User?> GetUserByUsernameAndPassword(string username, string password)
|
||||
{
|
||||
User? user = null;
|
||||
|
||||
try
|
||||
User user = new()
|
||||
{
|
||||
user = await this.GetUserByUsername(username).FirstOrDefaultAsync();
|
||||
if (user != null)
|
||||
{
|
||||
var encryptedPassword = user.PasswordHash;
|
||||
Console.WriteLine(encryptedPassword);
|
||||
}
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Console.WriteLine(exception.Message);
|
||||
}
|
||||
CreationTime = DateTime.UtcNow,
|
||||
CreationUserId = this.GetCurrentUserId(),
|
||||
IsDeleted = false,
|
||||
Guid = Guid.NewGuid().ToString(),
|
||||
FirstName = data.FirstName,
|
||||
LastName = data.LastName,
|
||||
Email = data.Email,
|
||||
PasswordSalt = "",
|
||||
PasswordHash = "",
|
||||
Password = "",
|
||||
Role = role,
|
||||
IsTestUser = false
|
||||
};
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
// public async Task<User?> CreateUser(CreateUserRequestData data)
|
||||
// {
|
||||
|
||||
// }
|
||||
public async Task<User?> GetUserByIdAsync(int id)
|
||||
{
|
||||
return await this.GetUsersQueryable().Where(x => x.Id == id).FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public async Task<User?> GetUserByGuidAsync(string guid)
|
||||
{
|
||||
return await this.GetUsersQueryable().Where(x => x.Guid == guid).FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public async Task<User?> GetUserByUsernameAndPassword(string email, string password)
|
||||
{
|
||||
User? user = await this.GetUserByEmailQueryable(email).FirstOrDefaultAsync();
|
||||
if (user != null)
|
||||
{
|
||||
var encryptedPassword = user.PasswordHash;
|
||||
Console.WriteLine(encryptedPassword);
|
||||
}
|
||||
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
public async Task<bool> CheckIfEmailIsValid(string email, string? guid = "")
|
||||
{
|
||||
var valid = false;
|
||||
|
||||
User? user = await this.GetUserByEmailQueryable(email).FirstOrDefaultAsync();
|
||||
if (user != null)
|
||||
{
|
||||
if (!String.IsNullOrEmpty(guid))
|
||||
{
|
||||
valid = user.Guid == guid && user.Email == email;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
valid = true;
|
||||
}
|
||||
|
||||
return valid;
|
||||
}
|
||||
|
||||
public async Task<User?> CreateUser(CreateUserRequestData data, Role role)
|
||||
{
|
||||
User? user = null;
|
||||
|
||||
using (var transaction = _sqlServerContext.Database.BeginTransactionAsync())
|
||||
{
|
||||
var tempUser = this.CreateUserData(data, role);
|
||||
}
|
||||
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user