Skip to content

Commit

Permalink
Merge branch 'RSN-60' of https://github.com/wzarek/Reasn into RSN-60
Browse files Browse the repository at this point in the history
  • Loading branch information
mkoper02 committed Jun 21, 2024
2 parents 4bc8189 + b49d768 commit 32dd9b8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 37 deletions.
4 changes: 2 additions & 2 deletions Server/ReasnAPI/ReasnAPI/Controllers/UsersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class UsersController(UserService userService, InterestService interestSe
private readonly InterestService _interestService = interestService;

[HttpGet]
[Authorize(Roles = "User")]
[Authorize(Roles = "Admin")]
[ProducesResponseType<IEnumerable<UserDto>>(StatusCodes.Status200OK)]
public IActionResult GetUsers()
{
Expand Down Expand Up @@ -84,7 +84,7 @@ public IActionResult GetUsersInterests()
[Route("interests/{interestId:int}")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public IActionResult DeleteUserInterest([FromRoute] int interestId)
{
{
_interestService.DeleteInterest(interestId);
return NoContent();
}
Expand Down
70 changes: 35 additions & 35 deletions Server/ReasnAPI/ReasnAPI/Services/UserService.cs
Original file line number Diff line number Diff line change
@@ -1,55 +1,55 @@
using Microsoft.EntityFrameworkCore;
using ReasnAPI.Exceptions;
using ReasnAPI.Mappers;
using ReasnAPI.Models.Database;
using ReasnAPI.Models.Database;
using ReasnAPI.Models.DTOs;
using Serilog;
using System.Linq.Expressions;
using System.Security.Claims;
using System.Transactions;

namespace ReasnAPI.Services;

public class UserService
{
private readonly ReasnContext _context;
private readonly IHttpContextAccessor _httpContextAccessor;


namespace ReasnAPI.Services;

public class UserService
{
private readonly ReasnContext _context;
private readonly IHttpContextAccessor _httpContextAccessor;

public UserService(ReasnContext context)

Check warning on line 18 in Server/ReasnAPI/ReasnAPI/Services/UserService.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (ubuntu-latest)

Non-nullable field '_httpContextAccessor' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 18 in Server/ReasnAPI/ReasnAPI/Services/UserService.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (macos-latest)

Non-nullable field '_httpContextAccessor' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
{
_context = context;
}

public UserService(ReasnContext context, IHttpContextAccessor httpContextAccessor)
{
_context = context;
_httpContextAccessor = httpContextAccessor;
}

public User GetCurrentUser()
{
public UserService(ReasnContext context, IHttpContextAccessor httpContextAccessor)
{
_context = context;
_httpContextAccessor = httpContextAccessor;
}

public User GetCurrentUser()
{
var httpContext = _httpContextAccessor.HttpContext;

if (httpContext is null)
{
throw new InvalidOperationException("No HTTP context available");
}

{
throw new InvalidOperationException("No HTTP context available");
}

var email = httpContext.User.FindFirstValue(ClaimTypes.Email);
if (string.IsNullOrEmpty(email))
{
throw new UnauthorizedAccessException("No email claim found in token");
}

var user = _context.Users.FirstOrDefault(u => u.Email == email);

if (user is null)
{
throw new NotFoundException("User associated with email not found");
}

return user;
}
if (string.IsNullOrEmpty(email))
{
throw new UnauthorizedAccessException("No email claim found in token");
}

var user = _context.Users.FirstOrDefault(u => u.Email == email);

if (user is null)
{
throw new NotFoundException("User associated with email not found");
}

return user;
}

public UserDto UpdateUser(string username, UserDto userDto)
{
Expand Down

0 comments on commit 32dd9b8

Please sign in to comment.