diff --git a/Application/Features/Appointments/Services/AppointmentService.cs b/Application/Features/Appointments/Services/AppointmentService.cs index 9501aef..c81356c 100644 --- a/Application/Features/Appointments/Services/AppointmentService.cs +++ b/Application/Features/Appointments/Services/AppointmentService.cs @@ -29,10 +29,10 @@ public AppointmentService(IAppointmentRepository repository, IUserRepository use _userContext = userContext; } - private void EnsureAdmin() + private void EnsureSuperAdmin() { - if (_userContext.Role!=UserRole.Admin) - throw new UnauthorizedAccessException("Only Admin Access"); + if (_userContext.Role!=UserRole.SuperAdmin) + throw new UnauthorizedAccessException("Only SuperAdmin Access"); } public async Task CreateAppointmentAsync(CreateAppointment dto) { @@ -93,7 +93,7 @@ await _appointmenServiceLinkRepository public async Task> ViewAppointments() { - EnsureAdmin(); + EnsureSuperAdmin(); return await _appointmentRepository.ViewAppointments(); } diff --git a/Application/Features/Auth/Interfaces/IUserService.cs b/Application/Features/Auth/Interfaces/IUserService.cs index 50fbd7b..8e99345 100644 --- a/Application/Features/Auth/Interfaces/IUserService.cs +++ b/Application/Features/Auth/Interfaces/IUserService.cs @@ -1,4 +1,5 @@ using Application.Features.Auth.DTOs; +using Domain.Enums; namespace Application.Features.Auth.Interfaces; @@ -6,6 +7,7 @@ public interface IUserService { Task RegisterUserAsync(RegisterUserRequestDto registerUserRequestDto); Task LoginUserAsync(LoginUserRequestDto loginUserRequestDto); + Task ChangeRoleTo(UserRole role); Task LogoutUserAsync(); Task RefreshTokenAsync(string refreshToken); Task ViewProfileAsync(); diff --git a/Application/Features/Auth/Services/UserService.cs b/Application/Features/Auth/Services/UserService.cs index ccdcca1..da1725b 100644 --- a/Application/Features/Auth/Services/UserService.cs +++ b/Application/Features/Auth/Services/UserService.cs @@ -4,6 +4,7 @@ using Application.Features.Auth.DTOs; using Application.Features.Auth.Interfaces; using Domain.Entities; +using Domain.Enums; namespace Application.Features.Auth.Services; @@ -68,6 +69,13 @@ public async Task LoginUserAsync(LoginUserRequestDto login return new LoginUserResponseDto(token, refreshTokenValue); } + public async Task ChangeRoleTo(UserRole role) + { + var userId =_userContext.UserId; + var user =await _userRepository.GetUserByIdAsync(userId); + user?.ChangeRoleTo(role); + } + public async Task LogoutUserAsync() { var userId = _userContext.UserId; diff --git a/Application/Features/Service/Services/ServiceAppService.cs b/Application/Features/Service/Services/ServiceAppService.cs index d9fbc74..5f9094e 100644 --- a/Application/Features/Service/Services/ServiceAppService.cs +++ b/Application/Features/Service/Services/ServiceAppService.cs @@ -16,14 +16,14 @@ public ServiceAppService(IServiceRepository repository, IUSerContext context) _userContext = context; } - private void EnsureAdmin() + private void EnsureSuperAdmin() { - if (_userContext.Role != UserRole.Admin) + if (_userContext.Role != UserRole.SuperAdmin) throw new ForbiddenAccessException("Only Admin Access"); } public async Task CreateServiceAsync(CreateService createService) { - EnsureAdmin(); + EnsureSuperAdmin(); var service=new Domain.Entities.Service(createService.Title,createService.DurationMinutes); await _repository.CreatServiceAsync(service); return $"{service.Title} created"; @@ -31,7 +31,7 @@ public async Task CreateServiceAsync(CreateService createService) public async Task EditServiceAsync(Guid serviceId,EditService editService) { - EnsureAdmin(); + EnsureSuperAdmin(); var appointment = await _repository.GetServiceByIdAsync(serviceId) ?? throw new NotFoundException("Service not found"); appointment.Edit(editService.DurationMinutes,editService.Title); @@ -41,7 +41,7 @@ public async Task EditServiceAsync(Guid serviceId,EditService editServic public async Task DeleteServiceAsync(Guid serviceId) { - EnsureAdmin(); + EnsureSuperAdmin(); await _repository.DeleteServiceAsync(serviceId); return $"{serviceId} deleted"; } diff --git a/Application/Features/Tenant/Services/TenantService.cs b/Application/Features/Tenant/Services/TenantService.cs index 05bffc3..b7aa15c 100644 --- a/Application/Features/Tenant/Services/TenantService.cs +++ b/Application/Features/Tenant/Services/TenantService.cs @@ -1,24 +1,46 @@ +using Application.Common.Interfaces; +using Application.Features.Auth.Interfaces; +using Application.Features.Auth.Services; using Application.Features.Tenant.DTO_s; using Application.Features.Tenant.Interfaces; using Domain.Entities; +using Domain.Enums; +using Domain.Exceptions; +using Microsoft.Extensions.Configuration; + namespace Application.Features.Tenant.Services; public class TenantService:TenantServiceContract { private readonly TenantRepositoryContract _tenantRepository; + private readonly IUSerContext _userContext; + private readonly IUserRepository _userRepository; - public TenantService(TenantRepositoryContract tenantRepository) + public TenantService(TenantRepositoryContract tenantRepository, IUSerContext userContext, IUserRepository userRepository) { _tenantRepository = tenantRepository; + _userContext = userContext; + _userRepository = userRepository; } public async Task Register(RegisterTenantDTO dto) { //Todo: slug maker // Todo : Date manager + // Todo : if for duplicate slug var tenant = new Domain.Entities.Tenant(dto.Name,dto.Slug,dto.SubscriptionExpireDate); + + var userId=_userContext.UserId; + var user = await _userRepository.GetUserByIdAsync(userId); + + if(user==null) + throw new NotFoundException("User not found"); + await _tenantRepository.RegisterTenant(tenant); + user.ChangeRoleTo(UserRole.Admin); + user.AssignTenantToUser(tenant.Id); await _tenantRepository.SaveAsync(); - return $"{tenant.Name} Registered"; + + return $"{tenant.Name} Registered belong to user {user.FullName} created"; } } \ No newline at end of file diff --git a/Domain/Entities/User.cs b/Domain/Entities/User.cs index cedf899..5827fac 100644 --- a/Domain/Entities/User.cs +++ b/Domain/Entities/User.cs @@ -42,6 +42,14 @@ public User(string fullName, string email, string phoneNumber, UserRole role, st Password = password; } + public void AssignTenantToUser(Guid tenantId) + { + TenantId = tenantId; + } + public void ChangeRoleTo(UserRole role) + { + Role = role; + } public void UpdateProfile(string fullName, string phoneNumber) { FullName = fullName; diff --git a/Domain/Enums/UserRole.cs b/Domain/Enums/UserRole.cs index e326556..5e6c232 100644 --- a/Domain/Enums/UserRole.cs +++ b/Domain/Enums/UserRole.cs @@ -2,6 +2,8 @@ namespace Domain.Enums; public enum UserRole { + SuperAdmin, + User, Admin, - User + Customer } \ No newline at end of file diff --git a/Infrastructure/Persistence/DbInitializer.cs b/Infrastructure/Persistence/DbInitializer.cs index 6347ce2..d937772 100644 --- a/Infrastructure/Persistence/DbInitializer.cs +++ b/Infrastructure/Persistence/DbInitializer.cs @@ -10,13 +10,13 @@ public class DbInitializer public static void Seed(AppDbContext context, IPasswordHasher passwordHasher,IConfiguration configuration) { - if (!context.Users.Any(u => u.Role == UserRole.Admin)) + if (!context.Users.Any(u => u.Role == UserRole.SuperAdmin)) { - var FullName=configuration["SeedAdmin:FullName"]; - var Email=configuration["SeedAdmin:Email"]; - var PhoneNumber=configuration["SeedAdmin:PhoneNumber"]; - var Password=passwordHasher.Hash(configuration["SeedAdmin:Password"]); - var role=UserRole.Admin; + var FullName=configuration["SeedSuperAdmin:FullName"]; + var Email=configuration["SeedSuperAdmin:Email"]; + var PhoneNumber=configuration["SeedSuperAdmin:PhoneNumber"]; + var Password=passwordHasher.Hash(configuration["SeedSuperAdmin:Password"]); + var role=UserRole.SuperAdmin; var User=new User(FullName,Email,PhoneNumber,role,Password); diff --git a/api/Controllers/AppointmentController.cs b/api/Controllers/AppointmentController.cs index bf7f55f..d0587c3 100644 --- a/api/Controllers/AppointmentController.cs +++ b/api/Controllers/AppointmentController.cs @@ -33,7 +33,7 @@ public async Task Cancel([FromRoute]Guid appointmentId) [HttpGet] [Route("ViewAppointments")] - [Authorize(Roles = "Admin")] + [Authorize(Roles = "SuperAdmin")] public async Task ViewAppointments() { var appointments=await _service.ViewAppointments(); diff --git a/api/Controllers/ServiceController.cs b/api/Controllers/ServiceController.cs index 5278df3..d77250d 100644 --- a/api/Controllers/ServiceController.cs +++ b/api/Controllers/ServiceController.cs @@ -16,7 +16,7 @@ public ServiceController(IServiceAppService serviceAppService) } [HttpPost] - [Authorize(Roles = "Admin")] + [Authorize(Roles = "SuperAdmin")] public async Task Create([FromBody] CreateService service) { var res=await _serviceAppService.CreateServiceAsync(service); @@ -24,7 +24,7 @@ public async Task Create([FromBody] CreateService service) } [HttpPatch("{serviceId}")] - [Authorize(Roles = "Admin")] + [Authorize(Roles = "SuperAdmin")] public async Task Edit([FromRoute] Guid serviceId,[FromBody] EditService service) { var res= await _serviceAppService.EditServiceAsync(serviceId,service); @@ -39,7 +39,7 @@ public async Task> ViewServices() } [HttpDelete("{serviceId}")] - [Authorize(Roles = "Admin")] + [Authorize(Roles = "SuperAdmin")] public async Task Delete([FromRoute] Guid serviceId) { var res = await _serviceAppService.DeleteServiceAsync(serviceId); diff --git a/api/Controllers/TenantController.cs b/api/Controllers/TenantController.cs index 94200cb..f6a67f0 100644 --- a/api/Controllers/TenantController.cs +++ b/api/Controllers/TenantController.cs @@ -1,5 +1,6 @@ using Application.Features.Tenant.DTO_s; using Application.Features.Tenant.Services; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace api.Controllers; @@ -15,6 +16,7 @@ public TenantController(TenantServiceContract tenantService) } [HttpPost] + [Authorize(Roles = "User,Admin")] public async Task RegisterTenant([FromBody] RegisterTenantDTO dto) { // Todo: expire date base on plan diff --git a/api/Controllers/UserController.cs b/api/Controllers/UserController.cs index 02711cb..8c672ca 100644 --- a/api/Controllers/UserController.cs +++ b/api/Controllers/UserController.cs @@ -33,7 +33,7 @@ public async Task Refresh([FromBody]RefreshTokenRequest dto) return Ok(await _service.RefreshTokenAsync(dto.RefreshToken)); } [HttpGet("ViewUsers")] - [Authorize(Roles = "Admin")] + [Authorize(Roles = "SuperAdmin")] public async Task ViewUsers() { var res= await _service.GetAllUsersAsync();