Files
mixtape/zero.Web/Controllers/UsersController.cs
T

118 lines
3.7 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
{
IUserApi Api;
IAuthenticationApi AuthenticationApi;
IPermissionsApi PermissionsApi;
2020-04-29 12:11:35 +02:00
2020-09-09 13:43:01 +02:00
public UsersController(IUserApi api, IAuthenticationApi authenticationApi, IPermissionsApi 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-05-04 17:23:52 +02:00
public async Task<EditModel<BackofficeUser>> GetById([FromQuery] string id) => Edit(await Api.GetUserById(id));
2020-04-17 15:32:33 +02:00
2021-05-04 17:23:52 +02:00
public async Task<ListResult<BackofficeUser>> GetAll([FromQuery] ListBackofficeQuery<BackofficeUser> query) => await Api.GetByQuery(query);
2020-07-06 14:35:58 +02:00
public IList<PermissionCollection> GetAllPermissions() => PermissionsApi.GetAll();
public async Task<IEnumerable<SelectModel>> GetForPicker() => (await Api.GetAll()).Select(x => new SelectModel()
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
2020-10-29 19:29:30 +01:00
public async Task<IList<PreviewModel>> GetPreviews([FromQuery] List<string> ids)
2020-09-16 10:51:26 +02:00
{
return Previews(await Api.GetByIds(ids.ToArray()), item => new PreviewModel()
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-05-04 17:23:52 +02:00
public async Task<EntityResult<BackofficeUser>> UpdatePassword([FromBody] UserPasswordEditModel model)
{
2021-05-04 17:23:52 +02:00
EntityResult<BackofficeUser> result;
if (model.NewPassword != model.ConfirmNewPassword)
{
2021-05-04 17:23:52 +02:00
result = EntityResult<BackofficeUser>.Fail(nameof(model.NewPassword), "@errors.changepassword.newpasswordsnotmatching");
}
else
{
BackofficeUser user = await AuthenticationApi.GetUser();
2021-05-04 17:23:52 +02:00
result = await Api.UpdatePassword(user as BackofficeUser, 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]
public async Task<EntityResult<string>> HashPassword([FromBody] UserPasswordEditModel model)
{
BackofficeUser user = await Api.GetUserById(model.UserId);
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-05-04 17:23:52 +02:00
public async Task<EntityResult<BackofficeUser>> Disable([FromBody] BackofficeUser model)
{
2021-05-04 17:23:52 +02:00
BackofficeUser 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-05-04 17:23:52 +02:00
public async Task<EntityResult<BackofficeUser>> Enable([FromBody] BackofficeUser model)
{
2021-05-04 17:23:52 +02:00
BackofficeUser 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-05-04 17:23:52 +02:00
public async Task<EntityResult<BackofficeUser>> Save([FromBody] BackofficeUser 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-05-04 17:23:52 +02:00
public async Task<EntityResult<BackofficeUser>> SaveCurrent([FromBody] BackofficeUser 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-05-04 17:23:52 +02:00
public async Task<EntityResult<BackofficeUser>> Delete([FromQuery] string id) => await Api.Delete(id);
2020-04-17 15:32:33 +02:00
}
}