mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-05-24 02:02:36 -04:00
Fix user lookup by provider id
This commit is contained in:
parent
98e9ba0fa7
commit
3bb36f5e78
@ -10,6 +10,7 @@
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
|
||||
<ProjectReference Include="../Kyoo.Abstractions/Kyoo.Abstractions.csproj" />
|
||||
<ProjectReference Include="../Kyoo.Core/Kyoo.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@ -19,7 +19,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text;
|
||||
@ -32,13 +31,11 @@ using Kyoo.Abstractions.Models.Permissions;
|
||||
using Kyoo.Abstractions.Models.Utils;
|
||||
using Kyoo.Authentication.Models;
|
||||
using Kyoo.Authentication.Models.DTO;
|
||||
using Kyoo.Core.Controllers;
|
||||
using Kyoo.Models;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using static Kyoo.Abstractions.Models.Utils.Constants;
|
||||
using BCryptNet = BCrypt.Net.BCrypt;
|
||||
|
||||
@ -51,7 +48,7 @@ namespace Kyoo.Authentication.Views
|
||||
[Route("auth")]
|
||||
[ApiDefinition("Authentication", Group = UsersGroup)]
|
||||
public class AuthApi(
|
||||
IRepository<User> users,
|
||||
UserRepository users,
|
||||
ITokenController tokenController,
|
||||
IThumbnailsManager thumbs,
|
||||
IHttpClientFactory clientFactory,
|
||||
@ -184,11 +181,8 @@ namespace Kyoo.Authentication.Views
|
||||
User newUser = new();
|
||||
if (profile.Email is not null)
|
||||
newUser.Email = profile.Email;
|
||||
if (profile.Username is not null)
|
||||
newUser.Username = profile.Username;
|
||||
else if (profile.Name is not null)
|
||||
newUser.Username = profile.Name;
|
||||
else
|
||||
string? username = profile.Username ?? profile.Name;
|
||||
if (username is null)
|
||||
{
|
||||
return BadRequest(
|
||||
new RequestError(
|
||||
@ -196,12 +190,11 @@ namespace Kyoo.Authentication.Views
|
||||
)
|
||||
);
|
||||
}
|
||||
newUser.Username = username;
|
||||
newUser.Slug = Utils.Utility.ToSlug(newUser.Username);
|
||||
newUser.ExternalId.Add(provider, extToken);
|
||||
|
||||
User? user = await users.GetOrDefault(
|
||||
new Filter<User>.Lambda(x => x.ExternalId[provider].Id == extToken.Id)
|
||||
);
|
||||
User? user = await users.GetByExternalId(provider, extToken.Id);
|
||||
if (user == null)
|
||||
user = await users.Create(newUser);
|
||||
return new JwtToken(
|
||||
|
@ -18,6 +18,7 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Common;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Kyoo.Abstractions.Controllers;
|
||||
@ -35,10 +36,12 @@ namespace Kyoo.Core.Controllers;
|
||||
/// <remarks>
|
||||
/// Create a new <see cref="UserRepository"/>
|
||||
/// </remarks>
|
||||
/// <param name="database">The database handle to use</param>
|
||||
/// <param name="thumbs">The thumbnail manager used to store images.</param>
|
||||
public class UserRepository(DatabaseContext database, IThumbnailsManager thumbs)
|
||||
: LocalRepository<User>(database, thumbs)
|
||||
public class UserRepository(
|
||||
DatabaseContext database,
|
||||
DbConnection db,
|
||||
SqlVariableContext context,
|
||||
IThumbnailsManager thumbs
|
||||
) : LocalRepository<User>(database, thumbs)
|
||||
{
|
||||
/// <summary>
|
||||
/// The database handle
|
||||
@ -84,4 +87,26 @@ public class UserRepository(DatabaseContext database, IThumbnailsManager thumbs)
|
||||
await _database.SaveChangesAsync();
|
||||
await base.Delete(obj);
|
||||
}
|
||||
|
||||
public Task<User?> GetByExternalId(string provider, string id)
|
||||
{
|
||||
// language=PostgreSQL
|
||||
return db.QuerySingle<User>(
|
||||
$"""
|
||||
select
|
||||
u.* -- User as u
|
||||
/* includes */
|
||||
from
|
||||
users as u
|
||||
where
|
||||
u.external_id->{provider}->>'id' = {id}
|
||||
""",
|
||||
new() { ["u"] = typeof(User) },
|
||||
(items) => (items[0] as User)!,
|
||||
context,
|
||||
null,
|
||||
null,
|
||||
null
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user