mirror of
https://github.com/GCMatters/hermes.git
synced 2026-02-04 13:40:13 +01:00
fix: rev db model, added missing entities and helper functions
This commit is contained in:
69
WebApp/Endpoints/GeneralUseHelperFunctions.cs
Normal file
69
WebApp/Endpoints/GeneralUseHelperFunctions.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using WebApp.Data;
|
||||
using WebApp.Entities;
|
||||
|
||||
namespace WebApp.Endpoints;
|
||||
|
||||
public class GeneralUseHelpers
|
||||
{
|
||||
|
||||
private readonly ApplicationDbContext _context;
|
||||
|
||||
public GeneralUseHelpers(ApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
async public Task<Token?> FindTokenFromString(string token_str)
|
||||
{
|
||||
// foreach (Token t in _context.Tokens) if (t.Value == token) return t;
|
||||
// return null;
|
||||
return await _context.Tokens.FirstOrDefaultAsync(t => t.Value == token_str);
|
||||
}
|
||||
|
||||
async public Task<User?> GetUserFromToken(Token? t)
|
||||
{
|
||||
// Zwróci null, gdy nie znaleziono użytkownika
|
||||
if (t is null) return null;
|
||||
User? user = await _context.WebUsers.FindAsync(t.Value);
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
async public Task<Organisation?> GetOrganisationFromToken(Token? t)
|
||||
{
|
||||
User? user = await GetUserFromToken(t);
|
||||
if (user is not null && user.IsOrganisation)
|
||||
{
|
||||
Organisation? org = await _context.Organisations.FindAsync(t.UserId);
|
||||
|
||||
if (org is null)
|
||||
{
|
||||
Console.WriteLine("!!!");
|
||||
}
|
||||
|
||||
return org;
|
||||
}
|
||||
else return null;
|
||||
|
||||
}
|
||||
|
||||
public string? GetTokenStrFromHTTPContext(HttpContext httpContext)
|
||||
{
|
||||
var cookies = httpContext.Request.Cookies;
|
||||
string? token = cookies["token"];
|
||||
return token;
|
||||
}
|
||||
|
||||
async public Task<Token?> GetTokenFromHTTPContext(HttpContext httpContext)
|
||||
{
|
||||
var cookies = httpContext.Request.Cookies;
|
||||
string? token_str = cookies["token"];
|
||||
if (token_str is not null)
|
||||
{
|
||||
Token? token = await FindTokenFromString(token_str);
|
||||
if (token is not null) return token;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user