Skip to content

Commit

Permalink
Add EnsureApplicationUser API
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Jul 15, 2024
1 parent 197646b commit bf14a92
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 4 deletions.
29 changes: 27 additions & 2 deletions MyApp.ServiceInterface/UserServices.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MyApp.Data;
using Microsoft.AspNetCore.Identity;
using MyApp.Data;
using ServiceStack;
using MyApp.ServiceModel;
using ServiceStack.IO;
Expand All @@ -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<ApplicationUser> userManager) : Service
{
private const string AppData = "/App_Data";

Expand Down Expand Up @@ -266,6 +267,30 @@ public object Any(GetUsersInfo request)
};
}

public async Task<object> 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<object> Any(ShareContent request)
{
var postId = request.RefId.LeftPart('-').ToInt();
Expand Down
19 changes: 19 additions & 0 deletions MyApp.ServiceModel/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,25 @@ public class GetUsersInfoResponse
public ResponseStatus? ResponseStatus { get; set; }
}

[ExcludeMetadata]
[ValidateIsAdmin]
public class EnsureApplicationUser : IGet, IReturn<StringResponse>
{
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<string>
Expand Down
37 changes: 37 additions & 0 deletions MyApp.Tests/ImportTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "[email protected]",
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 = "[email protected]",
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();
}
}
4 changes: 2 additions & 2 deletions MyApp/Configure.Db.Migrations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -306,7 +306,7 @@ await EnsureUserAsync(new ApplicationUser
await EnsureUserAsync(new ApplicationUser
{
UserName = "claude3-5-sonnet",
Email = "[email protected]",
Email = "servicestack.mail+claude3-5-[email protected]",
DisplayName = "Claude 3.5 Sonnet",
EmailConfirmed = true,
ProfilePath = "/profiles/cl/claude3-5-sonnet/claude3-5-sonnet.svg",
Expand Down

0 comments on commit bf14a92

Please sign in to comment.