mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-05-24 02:02:36 -04:00
Handle require verification on account creation
This commit is contained in:
parent
78a3ae8aeb
commit
44e7323720
@ -261,4 +261,11 @@ namespace Kyoo.Abstractions.Controllers
|
||||
/// </summary>
|
||||
Type RepositoryType { get; }
|
||||
}
|
||||
|
||||
public interface IUserRepository : IRepository<User>
|
||||
{
|
||||
Task<User?> GetByExternalId(string provider, string id);
|
||||
Task<User> AddExternalToken(Guid userId, string provider, ExternalToken token);
|
||||
Task<User> DeleteExternalToken(Guid userId, string provider);
|
||||
}
|
||||
}
|
||||
|
@ -23,15 +23,15 @@ using System.Net.Http;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Kyoo.Abstractions.Controllers;
|
||||
using Kyoo.Abstractions.Models;
|
||||
using Kyoo.Authentication.Models;
|
||||
using Kyoo.Authentication.Models.DTO;
|
||||
using Kyoo.Core.Controllers;
|
||||
|
||||
namespace Kyoo.Authentication;
|
||||
|
||||
public class OidcController(
|
||||
UserRepository users,
|
||||
IUserRepository users,
|
||||
IHttpClientFactory clientFactory,
|
||||
PermissionOption options
|
||||
)
|
||||
@ -89,7 +89,6 @@ public class OidcController(
|
||||
newUser.Username = username;
|
||||
newUser.Slug = Utils.Utility.ToSlug(newUser.Username);
|
||||
newUser.ExternalId.Add(provider, extToken);
|
||||
newUser.Permissions = options.NewUser;
|
||||
return (newUser, extToken);
|
||||
}
|
||||
|
||||
|
@ -10,7 +10,6 @@
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
|
||||
<ProjectReference Include="../Kyoo.Abstractions/Kyoo.Abstractions.csproj" />
|
||||
<ProjectReference Include="../Kyoo.Core/Kyoo.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@ -28,7 +28,6 @@ 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;
|
||||
@ -45,7 +44,7 @@ namespace Kyoo.Authentication.Views
|
||||
[Route("auth")]
|
||||
[ApiDefinition("Authentication", Group = UsersGroup)]
|
||||
public class AuthApi(
|
||||
UserRepository users,
|
||||
IUserRepository users,
|
||||
OidcController oidc,
|
||||
ITokenController tokenController,
|
||||
IThumbnailsManager thumbs,
|
||||
@ -243,23 +242,20 @@ namespace Kyoo.Authentication.Views
|
||||
[ProducesResponseType(StatusCodes.Status409Conflict, Type = typeof(RequestError))]
|
||||
public async Task<ActionResult<JwtToken>> Register([FromBody] RegisterRequest request)
|
||||
{
|
||||
User user = request.ToUser();
|
||||
user.Permissions = options.NewUser;
|
||||
try
|
||||
{
|
||||
await users.Create(user);
|
||||
}
|
||||
catch (DuplicatedItemException)
|
||||
{
|
||||
return Conflict(new RequestError("A user already exists with this username."));
|
||||
}
|
||||
|
||||
User user = await users.Create(request.ToUser());
|
||||
return new JwtToken(
|
||||
tokenController.CreateAccessToken(user, out TimeSpan expireIn),
|
||||
await tokenController.CreateRefreshToken(user),
|
||||
expireIn
|
||||
);
|
||||
}
|
||||
catch (DuplicatedItemException)
|
||||
{
|
||||
return Conflict(new RequestError("A user already exists with this username."));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Refresh a token.
|
||||
|
@ -23,8 +23,8 @@ using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Kyoo.Abstractions.Controllers;
|
||||
using Kyoo.Abstractions.Models;
|
||||
using Kyoo.Abstractions.Models.Permissions;
|
||||
using Kyoo.Abstractions.Models.Utils;
|
||||
using Kyoo.Authentication.Models;
|
||||
using Kyoo.Postgresql;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
@ -40,8 +40,9 @@ public class UserRepository(
|
||||
DatabaseContext database,
|
||||
DbConnection db,
|
||||
SqlVariableContext context,
|
||||
IThumbnailsManager thumbs
|
||||
) : LocalRepository<User>(database, thumbs)
|
||||
IThumbnailsManager thumbs,
|
||||
PermissionOption options
|
||||
) : LocalRepository<User>(database, thumbs), IUserRepository
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public override async Task<ICollection<User>> Search(
|
||||
@ -60,14 +61,13 @@ public class UserRepository(
|
||||
{
|
||||
// If no users exists, the new one will be an admin. Give it every permissions.
|
||||
if (!await database.Users.AnyAsync())
|
||||
{
|
||||
obj.Permissions = Enum.GetNames<Group>()
|
||||
.Where(x => x != nameof(Group.None))
|
||||
.SelectMany(group =>
|
||||
Enum.GetNames<Kind>().Select(kind => $"{group}.{kind}".ToLowerInvariant())
|
||||
)
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
obj.Permissions = PermissionOption.Admin;
|
||||
else if (!options.RequireVerification)
|
||||
obj.Permissions = options.NewUser;
|
||||
else
|
||||
obj.Permissions = Array.Empty<string>();
|
||||
|
||||
await base.Create(obj);
|
||||
database.Entry(obj).State = EntityState.Added;
|
||||
await database.SaveChangesAsync(() => Get(obj.Slug));
|
||||
|
@ -21,5 +21,6 @@
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="../Kyoo.Abstractions/Kyoo.Abstractions.csproj" />
|
||||
<ProjectReference Include="../Kyoo.Postgresql/Kyoo.Postgresql.csproj" />
|
||||
<ProjectReference Include="../Kyoo.Authentication/Kyoo.Authentication.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
Loading…
x
Reference in New Issue
Block a user