Files
mixtape/zero.Core/Identity/BackofficeUserStore.cs
T

62 lines
1.5 KiB
C#
Raw Normal View History

2020-03-25 00:47:14 +01:00
using FluentValidation.Results;
using Raven.Client.Documents;
using System;
using System.Threading;
using System.Threading.Tasks;
using zero.Core.Entities;
using zero.Core.Validation;
using zero.Core.Extensions;
namespace zero.Core.Identity
{
public class BackofficeUserStore<TUser, TRole>
2020-04-03 18:03:36 +02:00
where TUser : IUser, new()
where TRole : IUserRole, new()
2020-03-25 00:47:14 +01:00
{
/// <summary>
/// Gets the database context for this store.
/// </summary>
protected IDocumentStore Raven { get; set; }
/// <summary>
/// Configuration
/// </summary>
protected IZeroConfiguration Config { get; private set; }
2020-03-25 00:47:14 +01:00
/// <summary>
/// Currently logged-in user
/// </summary>
2020-04-03 18:03:36 +02:00
protected IUser Current { get; private set; }
2020-03-25 00:47:14 +01:00
2020-04-03 18:03:36 +02:00
public BackofficeUserStore(IDocumentStore raven, IZeroConfiguration config, IUser currentUser)
2020-03-25 00:47:14 +01:00
{
Raven = raven;
Config = config;
}
/// <summary>
/// Adds a new backoffice user
/// </summary>
public async Task<EntityChangeResult<TUser>> Add(TUser model, CancellationToken token = default)
{
// set default language
if (model.LanguageId.IsNullOrEmpty())
{
model.LanguageId = Config.DefaultLanguage;
}
// validate model
ValidationResult validation = await new BackofficeUserValidator(isCreate: true).ValidateAsync(model);
if (!validation.IsValid)
{
return EntityChangeResult<TUser>.Fail(validation);
}
return EntityChangeResult<TUser>.Success();
}
}
}