Files
mixtape/zero.Core/Api/UserApi.cs
T

284 lines
7.9 KiB
C#
Raw Normal View History

2020-11-04 23:38:06 +01:00
using Microsoft.AspNetCore.Identity;
2020-04-15 14:09:52 +02:00
using Raven.Client.Documents;
using Raven.Client.Documents.Linq;
2020-04-17 15:32:33 +02:00
using Raven.Client.Documents.Session;
using System.Collections.Generic;
using System.Linq;
2020-04-15 14:09:52 +02:00
using System.Threading.Tasks;
2021-10-13 12:47:18 +02:00
using zero.Core.Collections;
2020-04-15 14:09:52 +02:00
using zero.Core.Entities;
2020-04-17 15:32:33 +02:00
using zero.Core.Extensions;
2020-04-15 14:09:52 +02:00
namespace zero.Core.Api
{
2020-11-14 12:34:26 +01:00
public class UserApi : BackofficeApi, IUserApi
2020-04-15 14:09:52 +02:00
{
protected UserManager<BackofficeUser> UserManager { get; private set; }
2020-04-15 14:09:52 +02:00
2021-10-13 12:47:18 +02:00
public UserApi(ICollectionContext store, UserManager<BackofficeUser> userManager) : base(store, isCoreDatabase: true)
2020-04-15 14:09:52 +02:00
{
UserManager = userManager;
}
2020-04-15 14:09:52 +02:00
/// <inheritdoc />
2021-05-04 17:23:52 +02:00
public async Task<BackofficeUser> GetUserById(string id)
2020-04-15 14:09:52 +02:00
{
2021-05-04 17:23:52 +02:00
BackofficeUser user = await UserManager.FindByIdAsync(id);
2020-04-15 14:09:52 +02:00
return user;
}
/// <inheritdoc />
2021-05-04 17:23:52 +02:00
public async Task<BackofficeUser> GetUserByEmail(string email)
2020-04-15 14:09:52 +02:00
{
2021-05-04 17:23:52 +02:00
BackofficeUser user = await UserManager.FindByEmailAsync(email);
2020-04-15 14:09:52 +02:00
return user;
}
2020-04-17 15:32:33 +02:00
/// <inheritdoc />
2021-05-04 17:23:52 +02:00
public async Task<Dictionary<string, BackofficeUser>> GetByIds(params string[] ids)
2020-09-16 10:51:26 +02:00
{
2021-05-04 17:23:52 +02:00
return await GetByIds<BackofficeUser>(ids);
2020-09-16 10:51:26 +02:00
}
/// <inheritdoc />
2021-05-04 17:23:52 +02:00
public async Task<IList<BackofficeUser>> GetAll()
2020-04-17 15:32:33 +02:00
{
2021-10-13 12:47:18 +02:00
return await Session.Query<BackofficeUser>()
.OrderByDescending(x => x.CreatedDate)
.ToListAsync();
2020-04-17 15:32:33 +02:00
}
2020-04-17 15:32:33 +02:00
/// <inheritdoc />
2021-05-04 17:23:52 +02:00
public async Task<ListResult<BackofficeUser>> GetByQuery(ListQuery<BackofficeUser> query)
2020-04-17 15:32:33 +02:00
{
2021-10-13 12:47:18 +02:00
string currentUserId = UserManager.GetUserId(Context.Context.BackofficeUser);
2020-04-17 15:32:33 +02:00
query.SearchSelector = user => user.Name;
2021-10-13 12:47:18 +02:00
return await Session.Query<BackofficeUser>()
.ToQueriedListAsync(query);
2020-04-17 15:32:33 +02:00
}
/// <inheritdoc />
2021-05-04 17:23:52 +02:00
public async Task<EntityResult<BackofficeUser>> Save(BackofficeUser model)
2020-05-11 11:30:56 +02:00
{
2021-09-23 10:58:14 +02:00
bool updateSecurityStamp = false;
if (!model.Id.IsNullOrEmpty())
{
BackofficeUser origin = await GetUserById(model.Id);
updateSecurityStamp = origin != null && model.PasswordHash != origin.PasswordHash;
}
EntityResult<BackofficeUser> result = await SaveModel(model); //, new UserValidator<User>());
if (updateSecurityStamp)
{
await UserManager.UpdateSecurityStampAsync(model);
}
return result;
2020-05-11 11:30:56 +02:00
}
/// <inheritdoc />
2021-05-04 17:23:52 +02:00
public async Task<EntityResult<BackofficeUser>> Delete(string id)
2020-05-11 11:30:56 +02:00
{
2021-05-04 17:23:52 +02:00
return await DeleteById<BackofficeUser>(id);
2020-05-11 11:30:56 +02:00
}
2021-09-23 10:58:14 +02:00
/// <inheritdoc />
public async Task<EntityResult<string>> HashPassword(BackofficeUser user, string currentPassword, string newPassword, string confirmNewPassword)
{
if (newPassword != confirmNewPassword)
{
return EntityResult<string>.Fail(nameof(newPassword), "@errors.changepassword.newpasswordsnotmatching");
}
if (currentPassword.IsNullOrWhiteSpace() || newPassword.IsNullOrWhiteSpace())
{
return EntityResult<string>.Fail(nameof(currentPassword), "@errors.changepassword.emptyfields");
}
if (user == null)
{
return EntityResult<string>.Fail("@errors.changepassword.nouser");
}
if (UserManager.PasswordHasher.VerifyHashedPassword(user, user.PasswordHash, currentPassword) != PasswordVerificationResult.Success)
{
return EntityResult<string>.Fail("@errors.changepassword.passwordincorrect");
}
// validate new password
List<IdentityError> errors = new();
bool isValid = true;
foreach (var v in UserManager.PasswordValidators)
{
var result = await v.ValidateAsync(UserManager, user, newPassword);
if (!result.Succeeded)
{
if (result.Errors.Any())
{
errors.AddRange(result.Errors);
}
isValid = false;
}
}
if (!isValid)
{
EntityResult<string> result = EntityResult<string>.Fail();
foreach (IdentityError error in errors)
{
result.AddError(error.Description);
}
return result;
}
return EntityResult<string>.Success(UserManager.PasswordHasher.HashPassword(user, newPassword));
}
2020-05-11 11:30:56 +02:00
/// <inheritdoc />
2021-05-04 17:23:52 +02:00
public async Task<EntityResult<BackofficeUser>> UpdatePassword(BackofficeUser user, string currentPassword, string newPassword)
{
if (currentPassword.IsNullOrWhiteSpace() || newPassword.IsNullOrWhiteSpace())
{
2021-05-04 17:23:52 +02:00
return EntityResult<BackofficeUser>.Fail(nameof(currentPassword), "@errors.changepassword.emptyfields");
}
if (user == null)
{
2021-05-04 17:23:52 +02:00
return EntityResult<BackofficeUser>.Fail("@errors.changepassword.nouser");
}
IdentityResult identityResult = await UserManager.ChangePasswordAsync(user as BackofficeUser, currentPassword, newPassword);
if (!identityResult.Succeeded)
{
2021-05-04 17:23:52 +02:00
EntityResult<BackofficeUser> result = EntityResult<BackofficeUser>.Fail();
foreach (IdentityError error in identityResult.Errors)
{
result.AddError(error.Description);
}
return result;
}
2021-05-04 17:23:52 +02:00
return EntityResult<BackofficeUser>.Success(user);
}
/// <inheritdoc />
2021-05-04 17:23:52 +02:00
public async Task<EntityResult<BackofficeUser>> Enable(BackofficeUser user)
{
return await UpdateActiveState(user, true);
}
/// <inheritdoc />
2021-05-04 17:23:52 +02:00
public async Task<EntityResult<BackofficeUser>> Disable(BackofficeUser user)
{
return await UpdateActiveState(user, false);
}
/// <summary>
/// Updates the active state of user.
/// If IsActive=false, the user cannot login anymore
/// </summary>
2021-05-04 17:23:52 +02:00
async Task<EntityResult<BackofficeUser>> UpdateActiveState(BackofficeUser user, bool isActive)
{
user.IsActive = isActive;
IdentityResult identityResult = await UserManager.UpdateAsync(user as BackofficeUser);
if (!identityResult.Succeeded)
{
2021-05-04 17:23:52 +02:00
EntityResult<BackofficeUser> result = EntityResult<BackofficeUser>.Fail();
foreach (IdentityError error in identityResult.Errors)
{
result.AddError(error.Description);
}
return result;
}
await UserManager.UpdateSecurityStampAsync(user as BackofficeUser);
2020-05-07 15:20:41 +02:00
2021-05-04 17:23:52 +02:00
return EntityResult<BackofficeUser>.Success(user);
}
2020-04-15 14:09:52 +02:00
}
2020-11-14 12:34:26 +01:00
public interface IUserApi : IBackofficeApi
2020-04-15 14:09:52 +02:00
{
/// <summary>
/// Find user by id
/// </summary>
2021-05-04 17:23:52 +02:00
Task<BackofficeUser> GetUserById(string id);
2020-04-15 14:09:52 +02:00
/// <summary>
/// Find user by email
/// </summary>
2021-05-04 17:23:52 +02:00
Task<BackofficeUser> GetUserByEmail(string email);
2020-09-16 10:51:26 +02:00
/// <summary>
/// Get users by ids
/// </summary>
2021-05-04 17:23:52 +02:00
Task<Dictionary<string, BackofficeUser>> GetByIds(params string[] ids);
2020-05-06 11:07:44 +02:00
2020-04-17 15:32:33 +02:00
/// <summary>
/// Get all users for the selected application
/// </summary>
2021-05-04 17:23:52 +02:00
Task<IList<BackofficeUser>> GetAll();
2020-04-17 15:32:33 +02:00
/// <summary>
/// Get all available users (with query)
/// </summary>
2021-05-04 17:23:52 +02:00
Task<ListResult<BackofficeUser>> GetByQuery(ListQuery<BackofficeUser> query);
2020-05-11 11:30:56 +02:00
/// <summary>
/// Creates or updates a user
/// </summary>
2021-05-04 17:23:52 +02:00
Task<EntityResult<BackofficeUser>> Save(BackofficeUser model);
2020-05-11 11:30:56 +02:00
/// <summary>
/// Deletes a user
/// </summary>
2021-05-04 17:23:52 +02:00
Task<EntityResult<BackofficeUser>> Delete(string id);
2020-05-11 11:30:56 +02:00
/// <summary>
/// Changes the password of the current user.
/// User is logged out if this operation succeeds.
/// </summary>
2021-05-04 17:23:52 +02:00
Task<EntityResult<BackofficeUser>> UpdatePassword(BackofficeUser user, string currentPassword, string newPassword);
2021-09-23 10:58:14 +02:00
/// <summary>
/// Tries to hash a new password
/// </summary>
Task<EntityResult<string>> HashPassword(BackofficeUser user, string currentPassword, string newPassword, string confirmNewPassword);
/// <summary>
/// Enables a user
/// </summary>
2021-05-04 17:23:52 +02:00
Task<EntityResult<BackofficeUser>> Enable(BackofficeUser user);
/// <summary>
/// Disables a user
/// </summary>
2021-05-04 17:23:52 +02:00
Task<EntityResult<BackofficeUser>> Disable(BackofficeUser user);
2020-04-15 14:09:52 +02:00
}
}