From bf14a92b826d200157fd156009eae385df09dab9 Mon Sep 17 00:00:00 2001 From: Demis Bellot Date: Tue, 16 Jul 2024 01:49:31 +0800 Subject: [PATCH] Add EnsureApplicationUser API --- MyApp.ServiceInterface/UserServices.cs | 29 ++++++++++++++++++-- MyApp.ServiceModel/User.cs | 19 +++++++++++++ MyApp.Tests/ImportTests.cs | 37 ++++++++++++++++++++++++++ MyApp/Configure.Db.Migrations.cs | 4 +-- 4 files changed, 85 insertions(+), 4 deletions(-) diff --git a/MyApp.ServiceInterface/UserServices.cs b/MyApp.ServiceInterface/UserServices.cs index 61af0c7..3db8512 100644 --- a/MyApp.ServiceInterface/UserServices.cs +++ b/MyApp.ServiceInterface/UserServices.cs @@ -1,4 +1,5 @@ -using MyApp.Data; +using Microsoft.AspNetCore.Identity; +using MyApp.Data; using ServiceStack; using MyApp.ServiceModel; using ServiceStack.IO; @@ -7,7 +8,7 @@ namespace MyApp.ServiceInterface; -public class UserServices(AppConfig appConfig, R2VirtualFiles r2, ImageCreator imageCreator) : Service +public class UserServices(AppConfig appConfig, R2VirtualFiles r2, ImageCreator imageCreator, UserManager userManager) : Service { private const string AppData = "/App_Data"; @@ -266,6 +267,30 @@ public object Any(GetUsersInfo request) }; } + public async Task Any(EnsureApplicationUser request) + { + var existingUser = await userManager.FindByEmailAsync(request.Email); + if (existingUser == null) + { + var user = new ApplicationUser + { + UserName = request.UserName, + Email = request.Email, + DisplayName = request.DisplayName, + EmailConfirmed = request.EmailConfirmed ?? false, + ProfilePath = request.ProfilePath, + Model = request.Model, + }; + await userManager!.CreateAsync(user, request.Password); + } + + var newUser = await userManager.FindByEmailAsync(request.Email!); + return new StringResponse + { + Result = newUser?.Id + }; + } + public async Task Any(ShareContent request) { var postId = request.RefId.LeftPart('-').ToInt(); diff --git a/MyApp.ServiceModel/User.cs b/MyApp.ServiceModel/User.cs index 0ae732c..d29bba3 100644 --- a/MyApp.ServiceModel/User.cs +++ b/MyApp.ServiceModel/User.cs @@ -108,6 +108,25 @@ public class GetUsersInfoResponse public ResponseStatus? ResponseStatus { get; set; } } +[ExcludeMetadata] +[ValidateIsAdmin] +public class EnsureApplicationUser : IGet, IReturn +{ + public string? Id { get; set; } + [ValidateNotEmpty] + public string UserName { get; set; } + [ValidateNotEmpty] + public string Email { get; set; } + [ValidateNotEmpty] + public string DisplayName { get; set; } + public string? Model { get; set; } + [ValidateNotEmpty] + public string ProfilePath { get; set; } + public bool? EmailConfirmed { get; set; } + [ValidateNotEmpty] + public string Password { get; set; } +} + [Route("/q/{RefId}")] [Route("/q/{RefId}/{UserId}")] public class ShareContent : IGet, IReturn diff --git a/MyApp.Tests/ImportTests.cs b/MyApp.Tests/ImportTests.cs index b314291..5184555 100644 --- a/MyApp.Tests/ImportTests.cs +++ b/MyApp.Tests/ImportTests.cs @@ -252,4 +252,41 @@ public async Task Can_call_curl_to_get_url() var result = StringBuilderCache.ReturnAndFree(sb); Assert.That(result.Trim(), Does.StartWith("[{")); } + + private static string Password = Environment.GetEnvironmentVariable("AUTH_SECRET") ?? "p@55wOrd"; + + [Explicit("Run Manually")] + [Test] + public async Task Can_create_new_users() + { + var client = await TestUtils.CreateAdminProdClientAsync(); + + var api = await client.ApiAsync(new EnsureApplicationUser + { + UserName = "gemma2-27b", + Email = "servicestack.mail+gemma2-27b@gmail.com", + DisplayName = "Gemma2 27B", + EmailConfirmed = true, + ProfilePath = "/profiles/ge/gemma2-27b/gemma2-27b.svg", + Model = "gemma2:27b", //27B + Password = Password, + }); + + api.ThrowIfError(); + api.Response.PrintDump(); + + api = await client.ApiAsync(new EnsureApplicationUser + { + UserName = "claude3-5-sonnet", + Email = "servicestack.mail+claude3-5-sonnet@gmail.com", + DisplayName = "Claude 3.5 Sonnet", + EmailConfirmed = true, + ProfilePath = "/profiles/cl/claude3-5-sonnet/claude3-5-sonnet.svg", + Model = "claude-3-5-sonnet", + Password = Password, + }); + + api.ThrowIfError(); + api.Response.PrintDump(); + } } \ No newline at end of file diff --git a/MyApp/Configure.Db.Migrations.cs b/MyApp/Configure.Db.Migrations.cs index 9d1b459..c1428c0 100644 --- a/MyApp/Configure.Db.Migrations.cs +++ b/MyApp/Configure.Db.Migrations.cs @@ -206,7 +206,7 @@ await EnsureUserAsync(new ApplicationUser await EnsureUserAsync(new ApplicationUser { UserName = "gemma2-27b", - Email = "servicestack.mail+gemma@gmail.com", + Email = "servicestack.mail+gemma2-27b@gmail.com", DisplayName = "Gemma2 27B", EmailConfirmed = true, ProfilePath = "/profiles/ge/gemma2-27b/gemma2-27b.svg", @@ -306,7 +306,7 @@ await EnsureUserAsync(new ApplicationUser await EnsureUserAsync(new ApplicationUser { UserName = "claude3-5-sonnet", - Email = "servicestack.mail+claude3-sonnet@gmail.com", + Email = "servicestack.mail+claude3-5-sonnet@gmail.com", DisplayName = "Claude 3.5 Sonnet", EmailConfirmed = true, ProfilePath = "/profiles/cl/claude3-5-sonnet/claude3-5-sonnet.svg",