Move admin account creation logic to the repository

This commit is contained in:
Zoe Roux 2024-02-29 20:55:14 +01:00
parent 07f0862219
commit 85fbd37434
2 changed files with 13 additions and 1 deletions

View File

@ -16,11 +16,13 @@
// You should have received a copy of the GNU General Public License
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
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.Postgresql;
using Microsoft.EntityFrameworkCore;
@ -58,6 +60,16 @@ public class UserRepository(DatabaseContext database, IThumbnailsManager thumbs)
/// <inheritdoc />
public override async Task<User> Create(User obj)
{
// 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();
}
await base.Create(obj);
_database.Entry(obj).State = EntityState.Added;
await _database.SaveChangesAsync(() => Get(obj.Slug));

View File

@ -10,7 +10,7 @@
combinePackages [
sdk_7_0
aspnetcore_7_0
runtime_6_0
aspnetcore_6_0
];
in
pkgs.mkShell {