mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-05-31 12:14:46 -04:00
Move admin account creation logic to the repository
This commit is contained in:
parent
07f0862219
commit
85fbd37434
@ -16,11 +16,13 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
|
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Kyoo.Abstractions.Controllers;
|
using Kyoo.Abstractions.Controllers;
|
||||||
using Kyoo.Abstractions.Models;
|
using Kyoo.Abstractions.Models;
|
||||||
|
using Kyoo.Abstractions.Models.Permissions;
|
||||||
using Kyoo.Abstractions.Models.Utils;
|
using Kyoo.Abstractions.Models.Utils;
|
||||||
using Kyoo.Postgresql;
|
using Kyoo.Postgresql;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
@ -58,6 +60,16 @@ public class UserRepository(DatabaseContext database, IThumbnailsManager thumbs)
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override async Task<User> Create(User obj)
|
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);
|
await base.Create(obj);
|
||||||
_database.Entry(obj).State = EntityState.Added;
|
_database.Entry(obj).State = EntityState.Added;
|
||||||
await _database.SaveChangesAsync(() => Get(obj.Slug));
|
await _database.SaveChangesAsync(() => Get(obj.Slug));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user