mirror of
https://github.com/azaion/admin.git
synced 2026-04-22 05:16:32 +00:00
add enable disable remove user and isenabled in user column
This commit is contained in:
@@ -167,6 +167,21 @@ app.MapPost("/resources/{dataFolder?}",
|
||||
//.WithOpenApi(op => new(op){ Summary = "Upload resource"}); //For some reason doesn't work when this is specified.
|
||||
.DisableAntiforgery();
|
||||
|
||||
app.MapPut("/users/{email}/enable", async (string email, IUserService userService, CancellationToken ct)
|
||||
=> await userService.SetEnableStatus(email, true, ct))
|
||||
.RequireAuthorization(apiAdminPolicy)
|
||||
.WithOpenApi(op => new OpenApiOperation(op) { Summary = "Disable user" });
|
||||
|
||||
app.MapPut("/users/{email}/disable", async (string email, IUserService userService, CancellationToken ct)
|
||||
=> await userService.SetEnableStatus(email, false, ct))
|
||||
.RequireAuthorization(apiAdminPolicy)
|
||||
.WithOpenApi(op => new OpenApiOperation(op) { Summary = "Disable user" });
|
||||
|
||||
app.MapDelete("/users/{email}", async (string email, IUserService userService, CancellationToken ct)
|
||||
=> await userService.RemoveUser(email, ct))
|
||||
.RequireAuthorization(apiAdminPolicy)
|
||||
.WithOpenApi(op => new OpenApiOperation(op) { Summary = "Remove user" });
|
||||
|
||||
app.MapGet("/resources/list/{dataFolder?}",
|
||||
async ([FromRoute]string? dataFolder, string? search, IResourcesService resourcesService, CancellationToken ct)
|
||||
=> await resourcesService.ListResources(dataFolder, search, ct))
|
||||
|
||||
@@ -11,6 +11,7 @@ public class User
|
||||
public DateTime CreatedAt { get; set; }
|
||||
public DateTime? LastLogin { get; set; }
|
||||
public UserConfig? UserConfig { get; set; } = null!;
|
||||
public bool IsEnabled { get; set; }
|
||||
|
||||
public static string GetCacheKey(string email) =>
|
||||
string.IsNullOrEmpty(email) ? "" : $"{nameof(User)}.{email}";
|
||||
|
||||
@@ -4,7 +4,6 @@ using Azaion.Common.Entities;
|
||||
using Azaion.Common.Extensions;
|
||||
using Azaion.Common.Requests;
|
||||
using LinqToDB;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Azaion.Services;
|
||||
|
||||
@@ -17,6 +16,8 @@ public interface IUserService
|
||||
Task UpdateQueueOffsets(string email, UserQueueOffsets queueOffsets, CancellationToken ct = default);
|
||||
Task<IEnumerable<User>> GetUsers(string? searchEmail, RoleEnum? searchRole, CancellationToken ct = default);
|
||||
Task<string> CheckHardwareHash(User user, string hardware, CancellationToken ct = default);
|
||||
Task SetEnableStatus(string email, bool isEnabled, CancellationToken ct = default);
|
||||
Task RemoveUser(string email, CancellationToken ct = default);
|
||||
}
|
||||
|
||||
public class UserService(IDbFactory dbFactory, ICache cache) : IUserService
|
||||
@@ -128,4 +129,20 @@ public class UserService(IDbFactory dbFactory, ICache cache) : IUserService
|
||||
LastLogin = DateTime.UtcNow
|
||||
}, ct));
|
||||
}
|
||||
|
||||
public async Task SetEnableStatus(string email, bool isEnabled, CancellationToken ct = default)
|
||||
{
|
||||
await dbFactory.RunAdmin(async db =>
|
||||
await db.Users.UpdateAsync(x => x.Email == email, u => new User
|
||||
{
|
||||
IsEnabled = isEnabled
|
||||
}, ct));
|
||||
}
|
||||
|
||||
|
||||
public async Task RemoveUser(string email, CancellationToken ct = default)
|
||||
{
|
||||
await dbFactory.RunAdmin(async db =>
|
||||
await db.Users.DeleteAsync(x => x.Email == email, ct));
|
||||
}
|
||||
}
|
||||
|
||||
Vendored
+2
-1
@@ -1,3 +1,4 @@
|
||||
ALTER TABLE public.users
|
||||
ADD COLUMN IF NOT EXISTS created_at timestamp not null default now(),
|
||||
ADD COLUMN IF NOT EXISTS last_login timestamp null;
|
||||
ADD COLUMN IF NOT EXISTS last_login timestamp null,
|
||||
ADD COLUMN IF NOT EXISTS is_enabled bool not null default true;
|
||||
Reference in New Issue
Block a user