Files
mixtape/zero.Api/Modules_legacy/Users/UsersController.cs
T

118 lines
3.6 KiB
C#
Raw Normal View History

2020-04-17 15:32:33 +02:00
using Microsoft.AspNetCore.Mvc;
2020-09-16 10:51:26 +02:00
using System.Collections.Generic;
using System.Linq;
2020-04-17 15:32:33 +02:00
using System.Threading.Tasks;
using zero.Core.Api;
using zero.Core.Entities;
using zero.Core.Identity;
using zero.Web.Models;
namespace zero.Web.Controllers
{
[ZeroAuthorize(Permissions.Settings.Users, PermissionsValue.Read)]
public class UsersController : BackofficeController
2020-04-17 15:32:33 +02:00
{
2021-11-23 22:56:22 +01:00
IUserService Api;
IAuthenticationService AuthenticationApi;
IPermissionsService PermissionsApi;
2020-04-29 12:11:35 +02:00
2021-11-23 22:56:22 +01:00
public UsersController(IUserService api, IAuthenticationService authenticationApi, IPermissionsService permissionsApi)
2020-04-17 15:32:33 +02:00
{
Api = api;
2020-05-11 11:30:56 +02:00
AuthenticationApi = authenticationApi;
PermissionsApi = permissionsApi;
2021-09-23 11:29:10 +02:00
IsCoreDatabase = true;
2020-04-17 15:32:33 +02:00
}
2021-11-23 22:56:22 +01:00
public async Task<EditModel<ZeroUser>> GetById([FromQuery] string id) => Edit(await Api.GetUserById(id));
2020-04-17 15:32:33 +02:00
2021-11-26 15:47:11 +01:00
public async Task<Paged<ZeroUser>> GetAll([FromQuery] ListQuery<ZeroUser> query) => await Api.GetByQuery(query);
2020-07-06 14:35:58 +02:00
public IList<PermissionCollection> GetAllPermissions() => PermissionsApi.GetAll();
2021-11-27 16:09:02 +01:00
public async Task<IEnumerable<PickerModel>> GetForPicker() => (await Api.GetAll()).Select(x => new PickerModel()
2020-09-16 10:51:26 +02:00
{
Id = x.Id,
Name = x.Name,
IsActive = x.IsActive
});
2020-09-16 10:51:26 +02:00
2021-11-27 16:09:02 +01:00
public async Task<IList<PickerPreviewModel>> GetPreviews([FromQuery] List<string> ids)
2020-09-16 10:51:26 +02:00
{
2021-11-27 16:09:02 +01:00
return Previews(await Api.GetByIds(ids.ToArray()), item => new PickerPreviewModel()
2020-09-16 10:51:26 +02:00
{
Id = item.Id,
2020-11-24 12:02:03 +01:00
Icon = item.AvatarId,
2020-09-16 10:51:26 +02:00
Name = item.Name
});
}
[ZeroAuthorize]
2021-11-26 15:47:11 +01:00
public async Task<Result<ZeroUser>> UpdatePassword([FromBody] UserPasswordEditModel model)
{
2021-11-26 15:47:11 +01:00
Result<ZeroUser> result;
if (model.NewPassword != model.ConfirmNewPassword)
{
2021-11-26 15:47:11 +01:00
result = Result<ZeroUser>.Fail(nameof(model.NewPassword), "@errors.changepassword.newpasswordsnotmatching");
}
else
{
2021-11-23 22:56:22 +01:00
ZeroUser user = await AuthenticationApi.GetUser();
result = await Api.UpdatePassword(user as ZeroUser, model.CurrentPassword, model.NewPassword);
2020-05-11 11:30:56 +02:00
if (result.IsSuccess)
{
await AuthenticationApi.Logout();
}
}
return result;
}
2021-09-23 10:58:14 +02:00
[ZeroAuthorize]
2021-11-26 15:47:11 +01:00
public async Task<Result<string>> HashPassword([FromBody] UserPasswordEditModel model)
2021-09-23 10:58:14 +02:00
{
2021-11-23 22:56:22 +01:00
ZeroUser user = await Api.GetUserById(model.UserId);
2021-09-23 10:58:14 +02:00
return await Api.HashPassword(user, model.CurrentPassword, model.NewPassword, model.ConfirmNewPassword);
}
2020-08-11 16:01:10 +02:00
[ZeroAuthorize(Permissions.Settings.Users, PermissionsValue.Update)]
2021-11-26 15:47:11 +01:00
public async Task<Result<ZeroUser>> Disable([FromBody] ZeroUser model)
{
2021-11-23 22:56:22 +01:00
ZeroUser entity = await Api.GetUserById(model.Id);
return await Api.Disable(entity);
}
2020-08-11 16:01:10 +02:00
[ZeroAuthorize(Permissions.Settings.Users, PermissionsValue.Update)]
2021-11-26 15:47:11 +01:00
public async Task<Result<ZeroUser>> Enable([FromBody] ZeroUser model)
{
2021-11-23 22:56:22 +01:00
ZeroUser entity = await Api.GetUserById(model.Id);
return await Api.Enable(entity);
}
2020-05-11 11:30:56 +02:00
2020-08-11 16:01:10 +02:00
[ZeroAuthorize(Permissions.Settings.Users, PermissionsValue.Update)]
2021-11-26 15:47:11 +01:00
public async Task<Result<ZeroUser>> Save([FromBody] ZeroUser model) => await Api.Save(model);
2020-05-11 11:30:56 +02:00
2020-08-11 16:01:10 +02:00
[ZeroAuthorize(Permissions.Settings.Users, PermissionsValue.Update)]
2020-05-11 11:30:56 +02:00
// TODO do not need settings.users authorization for editing current user profiles
2021-11-26 15:47:11 +01:00
public async Task<Result<ZeroUser>> SaveCurrent([FromBody] ZeroUser model) => await Api.Save(model);
2020-05-11 11:30:56 +02:00
2020-08-11 16:01:10 +02:00
[ZeroAuthorize(Permissions.Settings.Users, PermissionsValue.Update)]
2021-11-26 15:47:11 +01:00
public async Task<Result<ZeroUser>> Delete([FromQuery] string id) => await Api.Delete(id);
2020-04-17 15:32:33 +02:00
}
}