Files
Wollmilchsau/Wollmilchsau/Store/OfficeStore/OfficeEffects.cs
T
2026-08-31 08:39:15 +02:00

51 lines
2.1 KiB
C#

using Fluxor;
using Wollmilchsau.Helpers;
using Wollmilchsau.Services;
using Wollmilchsau.Services.Ldap;
using Wollmilchsau.Services.Office;
namespace Wollmilchsau.Store.OfficeStore;
public sealed class OfficeEffects(IOfficeService officeService, ILdapService ldapService)
{
[EffectMethod]
public async Task HandleGetCurrentUserInfoAction(GetCurrentUserInfoAction action, IDispatcher dispatcher)
{
if (action.UserId is null) return;
var graphUser = await officeService.GetUserByAliasesAsync(action.UserId);
var ldaUser = await ldapService.GetUser(action.UserId);
dispatcher.Dispatch(new UpdateCurrentUserInfoWithResultAction(action.UserId, graphUser, ldaUser));
}
[EffectMethod]
public async Task HandleCreateNewAccountAction(CreateNewAccountAction action, IDispatcher dispatcher)
{
var initialPassword = PasswordGenerator.Create(length: 10);
var createdUser = await officeService.CreateUserAsync(
displayName: $"{action.NewUser.GivenName} {action.NewUser.Surname}",
userPrincipalName: $"{action.NewUser.Username}@bs18.de",
mailNickname: action.NewUser.Username,
initialPassword: initialPassword,
givenName: action.NewUser.GivenName ?? string.Empty,
surname: action.NewUser.Surname ?? string.Empty,
department: action.NewUser.Role == Models.LdapUserRole.Teacher ? "Lehrer" : "Schüler",
jobTitle: action.NewUser.Classroom ?? "n/a"
);
dispatcher.Dispatch(new SetNewlyCreatedGraphUserAction(
action.NewUser.Username,
createdUser,
initialPassword));
}
[EffectMethod]
public async Task HandleSetNewPasswordInGraphAction(SetNewPasswordInGraphAction action, IDispatcher dispatcher)
{
var initialPassword = PasswordGenerator.Create(length: 10);
var success = await officeService.ChangeUserPasswordAsync(
userId: action.UserPrincipalName, newPassword: initialPassword);
if (success)
{
dispatcher.Dispatch(new SetNewPasswordInStoreAction(action.UserId, initialPassword));
}
}
}