diff --git a/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs b/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs index a26c7605f61b743c49ccff01a57abfe17befab5c..4689d23684b14bdd7574ec5a687d81bf1bf1d188 100644 --- a/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs +++ b/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs @@ -83,8 +83,8 @@ namespace Tsi1.Api.Controllers }); } + [AllowAnonymous] [HttpPost("RefreshToken")] - [Authorize] public async Task<ActionResult> RefreshToken([FromBody] RefreshTokenRequest request) { try @@ -118,10 +118,38 @@ namespace Tsi1.Api.Controllers public async Task<IActionResult> Register(UserRegisterDto dto) { var tenantId = int.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "TenantId").Value); - dto.TenantId = tenantId; var userTypeResult = await _userTypeService.GetById(dto.UserTypeId); + if (userTypeResult.HasError) + { + return BadRequest(userTypeResult.Message); + } + + var userType = userTypeResult.Data; + + if (userType.Name == UserTypes.UdelarAdmin || + userType.Name == UserTypes.FacultyAdmin) + { + return BadRequest(string.Format(ErrorMessages.InvalidUserType, userType.Name)); + } + + var userServiceResult = await _userService.Create(dto, userType.Name, tenantId); + + if (userServiceResult.HasError) + { + return BadRequest(userServiceResult.Message); + } + + return Ok(); + } + + [Authorize(Roles = UserTypes.UdelarAdmin)] + [HttpPost("RegisterAdmin/{tenantId}")] + public async Task<IActionResult> RegisterAdmin(UserRegisterDto dto, int tenantId) + { + var userTypeResult = await _userTypeService.GetById(dto.UserTypeId); + if (userTypeResult.HasError) { BadRequest(userTypeResult.Message); @@ -129,7 +157,13 @@ namespace Tsi1.Api.Controllers var userType = userTypeResult.Data; - var userServiceResult = await _userService.Create(dto, userType.Name); + if (userType.Name == UserTypes.Student || + userType.Name == UserTypes.Professor) + { + return BadRequest(string.Format(ErrorMessages.InvalidUserType, userType.Name)); + } + + var userServiceResult = await _userService.Create(dto, userType.Name, tenantId); if (userServiceResult.HasError) { @@ -170,5 +204,21 @@ namespace Tsi1.Api.Controllers return Ok(result.Data); } + [Authorize(Roles = UserTypes.UdelarAdmin + ", " + UserTypes.FacultyAdmin)] + [HttpGet("GetUserTypes")] + public async Task<IActionResult> GetUserTypes() + { + var userType = HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Role).Value; + + var result = await _userTypeService.GetAll(userType); + + if (result.HasError) + { + return BadRequest(result.Message); + } + + return Ok(result.Data); + } + } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserRegisterDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserRegisterDto.cs index 2cc5d2740edf2171ffa30d17e60389bc4290a17a..d01877d97c514b123db7163125918cf0d920e131 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserRegisterDto.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserRegisterDto.cs @@ -29,9 +29,6 @@ namespace Tsi1.BusinessLayer.Dtos [Required] public string IdentityCard { get; set; } - [JsonIgnore] - public int TenantId { get; set; } - public int Age { get; set; } } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserTypeDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserTypeDto.cs new file mode 100644 index 0000000000000000000000000000000000000000..5ba1793dd70f29f35fee0498b858c45a7e3b4a8a --- /dev/null +++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserTypeDto.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tsi1.BusinessLayer.Dtos +{ + public class UserTypeDto + { + public int Id { get; set; } + + public string Name { get; set; } + } +} diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/ErrorMessages.cs b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/ErrorMessages.cs index 29bacb3defb6998bd6b4fc0be48b0a31c8dc3fa9..99c521a4d670fd66eb3eee6753c7b97c4afc902b 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/ErrorMessages.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/ErrorMessages.cs @@ -34,5 +34,7 @@ namespace Tsi1.BusinessLayer.Helpers public const string TenantDoesNotExist = "La Facultad '{0}' no existe"; public const string DuplicateTenantName = "Ya existe una Facultad con nombre '{0}'"; + public const string InvalidUserType = "Tipo de usuario invalido '{0}'"; + } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs index c00cf1abd7fef1ab43d08a0a6f98a8d3e7ba756c..5d666d16c6f9578c22a8c22b867329345ee444ae 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs @@ -28,6 +28,7 @@ namespace Tsi1.BusinessLayer.Helpers CreateMap<Course, CoursePreviewDto>(); CreateMap<Tenant, TenantPreviewDto>(); CreateMap<Tenant, TenantCreateDto>(); + CreateMap<UserType, UserTypeDto>(); CreateMap<ForumCreateDto, Forum>(); CreateMap<ForumPreviewDto, Forum>(); @@ -45,6 +46,7 @@ namespace Tsi1.BusinessLayer.Helpers CreateMap<CoursePreviewDto, Course>(); CreateMap<TenantPreviewDto, Tenant>(); CreateMap<TenantCreateDto, Tenant>(); + CreateMap<UserTypeDto, UserType>(); } } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs index ed780959cdca1def549471d306c0ad64ef0f7ac6..12444e66d7fe3e492bbcbdb8b0d7353730c89beb 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs @@ -12,7 +12,7 @@ namespace Tsi1.BusinessLayer.Interfaces { Task<ServiceResult<User>> Authenticate(string username, string password, int tenantId); - Task<ServiceResult<User>> Create(UserRegisterDto dto, string type); + Task<ServiceResult<User>> Create(UserRegisterDto dto, string type, int tenantId); Task<ServiceResult<List<UserPreviewDto>>> GetAll(int tenantId); diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserTypeService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserTypeService.cs index 733e3d045d6811b1d29fcdae88e6427ab51876b0..e84218a454134b3cc6e5e2e9497059a8c9bbc2fb 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserTypeService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserTypeService.cs @@ -2,13 +2,16 @@ using System.Collections.Generic; using System.Text; using System.Threading.Tasks; +using Tsi1.BusinessLayer.Dtos; using Tsi1.BusinessLayer.Helpers; -using Tsi1.DataLayer.Entities; + namespace Tsi1.BusinessLayer.Interfaces { public interface IUserTypeService { - public Task<ServiceResult<UserType>> GetById(int id); + public Task<ServiceResult<UserTypeDto>> GetById(int id); + + public Task<ServiceResult<List<UserTypeDto>>> GetAll(string userType); } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/UserService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/UserService.cs index b38e807990555358a68b41e281e3131b7865be78..3aa39cc07fc53d7fae54d12517381a2e411e7f53 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/UserService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/UserService.cs @@ -52,19 +52,19 @@ namespace Tsi1.BusinessLayer.Services return result; } - public async Task<ServiceResult<User>> Create(UserRegisterDto dto, string type) + public async Task<ServiceResult<User>> Create(UserRegisterDto dto, string type, int tenantId) { var result = new ServiceResult<User>(); var user = _mapper.Map<User>(dto); - + user.TenantId = tenantId; + if (type == UserTypes.Student) { user.Student = new Student() { IdentityCard = dto.IdentityCard, Age = dto.Age, - TenantId = dto.TenantId }; } @@ -73,7 +73,6 @@ namespace Tsi1.BusinessLayer.Services user.Professor = new Professor() { IdentityCard = dto.IdentityCard, - TenantId = dto.TenantId }; } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/UserTypeService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/UserTypeService.cs index 18180fd57ad7c186d9ff40588009a08457956f56..e11f550b6dd13589a7379b8579efa715632b989b 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/UserTypeService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/UserTypeService.cs @@ -1,8 +1,11 @@ -using Microsoft.EntityFrameworkCore; +using AutoMapper; +using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; +using System.Linq; using System.Text; using System.Threading.Tasks; +using Tsi1.BusinessLayer.Dtos; using Tsi1.BusinessLayer.Helpers; using Tsi1.BusinessLayer.Interfaces; using Tsi1.DataLayer; @@ -14,14 +17,43 @@ namespace Tsi1.BusinessLayer.Services { private readonly Tsi1Context _context; - public UserTypeService(Tsi1Context context) + private readonly IMapper _mapper; + + public UserTypeService(Tsi1Context context, IMapper mapper) { _context = context; + _mapper = mapper; } - public async Task<ServiceResult<UserType>> GetById(int id) + public async Task<ServiceResult<List<UserTypeDto>>> GetAll(string userType) { - var result = new ServiceResult<UserType>(); + var result = new ServiceResult<List<UserTypeDto>>(); + + List<UserType> userTypes = null; + + if (userType == UserTypes.UdelarAdmin) + { + userTypes = await _context.UserTypes + .Where(x => x.Name == UserTypes.UdelarAdmin || + x.Name == UserTypes.FacultyAdmin) + .ToListAsync(); + } + else if (userType == UserTypes.FacultyAdmin) + { + userTypes = await _context.UserTypes + .Where(x => x.Name == UserTypes.Student || + x.Name == UserTypes.Professor) + .ToListAsync(); + } + + result.Data = _mapper.Map<List<UserTypeDto>>(userTypes); + + return result; + } + + public async Task<ServiceResult<UserTypeDto>> GetById(int id) + { + var result = new ServiceResult<UserTypeDto>(); var userType = await _context.UserTypes.FirstOrDefaultAsync(x => x.Id == id); @@ -30,10 +62,11 @@ namespace Tsi1.BusinessLayer.Services result.HasError = true; result.Message = string.Format(ErrorMessages.UserTypeDoesNotExist, id); } - - result.Data = userType; + + result.Data = _mapper.Map<UserTypeDto>(userType); return result; } + } }