From 7b7998f40868ed60f887ba122c9b013a166ffc3f Mon Sep 17 00:00:00 2001 From: mborzyszkowski Date: Sat, 12 Sep 2020 16:49:44 +0200 Subject: [PATCH] fixes --- src/InternshipSystem.Api/Controllers/AccessController.cs | 2 +- .../Controllers/DocumentsController.cs | 7 +++---- .../Controllers/EditionController.cs | 8 +++----- .../Controllers/InternshipRegistrationController.cs | 8 +++----- .../Controllers/InternshipTypesController.cs | 5 +++-- .../Controllers/RegistrationController.cs | 8 +++----- src/InternshipSystem.Api/ModelBinders/UserBinder.cs | 9 ++++++++- 7 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/InternshipSystem.Api/Controllers/AccessController.cs b/src/InternshipSystem.Api/Controllers/AccessController.cs index 44d1b95..ea15e8f 100644 --- a/src/InternshipSystem.Api/Controllers/AccessController.cs +++ b/src/InternshipSystem.Api/Controllers/AccessController.cs @@ -44,7 +44,7 @@ namespace InternshipSystem.Api.Controllers [HttpGet("loginEdition")] [Authorize] - public async Task LoginIntoEdition(Guid editionId, User user, CancellationToken token) + public async Task LoginIntoEdition(Guid editionId, [FromServices] User user, CancellationToken token) { var edition = await _context.Editions.FindAsync(editionId); diff --git a/src/InternshipSystem.Api/Controllers/DocumentsController.cs b/src/InternshipSystem.Api/Controllers/DocumentsController.cs index 3406f9b..033b2aa 100644 --- a/src/InternshipSystem.Api/Controllers/DocumentsController.cs +++ b/src/InternshipSystem.Api/Controllers/DocumentsController.cs @@ -35,7 +35,8 @@ namespace InternshipSystem.Api.Controllers [ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [Authorize(Policy = Policies.RegisteredOnly)] - public async Task AddDocumentToInternship([FromBody] DocumentPublishRequest documentRequest, CancellationToken cancellationToken) + public async Task AddDocumentToInternship([FromBody] DocumentPublishRequest documentRequest, + [FromServices] User user, CancellationToken cancellationToken) { var validator = new DocumentPublishRequest.Validator(); var validationResult = await validator.ValidateAsync(documentRequest, cancellationToken); @@ -45,9 +46,7 @@ namespace InternshipSystem.Api.Controllers return BadRequest(validationResult.ToString()); } - var personNumber = long.Parse(User.FindFirst(InternshipClaims.PersonNumber).Value); - - return await _internshipService.AddDocumentToInternship(documentRequest, personNumber, cancellationToken); + return await _internshipService.AddDocumentToInternship(documentRequest, user.PersonNumber, cancellationToken); } } } \ No newline at end of file diff --git a/src/InternshipSystem.Api/Controllers/EditionController.cs b/src/InternshipSystem.Api/Controllers/EditionController.cs index f9e6774..777524c 100644 --- a/src/InternshipSystem.Api/Controllers/EditionController.cs +++ b/src/InternshipSystem.Api/Controllers/EditionController.cs @@ -38,15 +38,13 @@ namespace InternshipSystem.Api.Controllers [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] [Authorize] - public async Task>> GetAvailableEditions(CancellationToken token) + public async Task>> GetAvailableEditions([FromServices] User user, CancellationToken token) { - var personNumber = long.Parse(User.FindFirst(InternshipClaims.PersonNumber).Value); - var editions = await Context.Editions .Where(edition => edition.Internships - .Any(internship => internship.Student.Id == personNumber)) + .Any(internship => internship.Student.Id == user.PersonNumber)) .ProjectTo(Mapper.ConfigurationProvider) .ToListAsync(token); @@ -74,7 +72,7 @@ namespace InternshipSystem.Api.Controllers var edition = await Context.Editions .Include(e => e.AvailableSubjects) - .Where(e => e.Id == id) + .Where(e => e.Id.Equals(id)) .ProjectTo(Mapper.ConfigurationProvider) .FirstOrDefaultAsync(token); diff --git a/src/InternshipSystem.Api/Controllers/InternshipRegistrationController.cs b/src/InternshipSystem.Api/Controllers/InternshipRegistrationController.cs index 44c8eb6..6bc7ab8 100644 --- a/src/InternshipSystem.Api/Controllers/InternshipRegistrationController.cs +++ b/src/InternshipSystem.Api/Controllers/InternshipRegistrationController.cs @@ -31,8 +31,8 @@ namespace InternshipSystem.Api.Controllers [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [Authorize] - public async Task SubmitRegistrationForm([FromBody] RegistrationFormQuery registrationQuery, - CancellationToken cancellationToken) + public async Task SubmitRegistrationForm([FromBody] RegistrationFormQuery registrationQuery, + [FromServices] User user, CancellationToken cancellationToken) { var validator = new RegistrationFormQuery.Validator(); var validationResult = await validator.ValidateAsync(registrationQuery, cancellationToken); @@ -41,10 +41,8 @@ namespace InternshipSystem.Api.Controllers { return BadRequest(validationResult.ToString()); } - - var personNumber = long.Parse(User.FindFirst(InternshipClaims.PersonNumber).Value); - return await _internshipService.SubmitRegistration(registrationQuery, personNumber, cancellationToken); + return await _internshipService.SubmitRegistration(registrationQuery, user.PersonNumber, cancellationToken); } } } \ No newline at end of file diff --git a/src/InternshipSystem.Api/Controllers/InternshipTypesController.cs b/src/InternshipSystem.Api/Controllers/InternshipTypesController.cs index d4afa36..f7a77dd 100644 --- a/src/InternshipSystem.Api/Controllers/InternshipTypesController.cs +++ b/src/InternshipSystem.Api/Controllers/InternshipTypesController.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -41,7 +42,7 @@ namespace InternshipSystem.Api.Controllers [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status404NotFound)] - [Authorize] + [Authorize(Policy = Policies.RegisteredOnly)] public async Task>> GetInternshipTypesForEdition([FromServices] User user, CancellationToken cancellationToken) { var edition = diff --git a/src/InternshipSystem.Api/Controllers/RegistrationController.cs b/src/InternshipSystem.Api/Controllers/RegistrationController.cs index 89d2cb2..f5de0b1 100644 --- a/src/InternshipSystem.Api/Controllers/RegistrationController.cs +++ b/src/InternshipSystem.Api/Controllers/RegistrationController.cs @@ -33,7 +33,7 @@ namespace InternshipSystem.Api.Controllers [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status404NotFound)] [Authorize] - public async Task RegisterStudentForEdition([FromBody] Guid registrationCode, CancellationToken token) + public async Task RegisterStudentForEdition([FromBody] Guid registrationCode, [FromServices] User user, CancellationToken token) { var edition = await _context.Editions.FindAsync(registrationCode, token); @@ -41,10 +41,8 @@ namespace InternshipSystem.Api.Controllers { return NotFound(); } - - var personNumber = long.Parse(User.FindFirst(InternshipClaims.PersonNumber).Value); - - var student = await _context.Students.FindAsync(personNumber, token); + + var student = await _context.Students.FindAsync(user.PersonNumber, token); edition.RegisterInternship(student); await _context.SaveChangesAsync(token); diff --git a/src/InternshipSystem.Api/ModelBinders/UserBinder.cs b/src/InternshipSystem.Api/ModelBinders/UserBinder.cs index 8a5fe2a..62096e2 100644 --- a/src/InternshipSystem.Api/ModelBinders/UserBinder.cs +++ b/src/InternshipSystem.Api/ModelBinders/UserBinder.cs @@ -22,12 +22,19 @@ namespace InternshipSystem.Api.ModelBinders { return Task.CompletedTask; } + + Guid? editionGuid = null; + if (principal.FindFirst(InternshipClaims.Edition) != null + && Guid.TryParse(principal.FindFirst(InternshipClaims.Edition).Value, out var edition)) + { + editionGuid = edition; + } var user = new User { Name = principal.FindFirst(ClaimTypes.Name).Value, PersonNumber = long.Parse(principal.FindFirst(InternshipClaims.PersonNumber).Value), - EditionId = Guid.TryParse(principal.FindFirst(InternshipClaims.Edition).Value, out var edition) ? edition : (Guid?) null + EditionId = editionGuid }; bindingContext.Result = ModelBindingResult.Success(user);