80 lines
2.0 KiB
C#
80 lines
2.0 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Options;
|
|
using System.Threading.Tasks;
|
|
using zero.Core;
|
|
using zero.Core.Api;
|
|
using zero.Core.Entities;
|
|
using zero.Core.Identity;
|
|
using zero.Web.Mapper;
|
|
using zero.Web.Models;
|
|
|
|
namespace zero.Web.Controllers
|
|
{
|
|
[ZeroAuthorize(Permissions.Settings.Users, PermissionsValue.Read)]
|
|
public class UsersController : BackofficeController
|
|
{
|
|
IUserApi Api = null;
|
|
|
|
IUserRolesApi RolesApi = null;
|
|
|
|
ZeroOptions Options = null;
|
|
|
|
|
|
public UsersController(IZeroConfiguration config, IUserApi api, IUserRolesApi rolesApi, IMapper mapper, IToken token, IOptionsMonitor<ZeroOptions> options) : base(config, mapper, token)
|
|
{
|
|
Api = api;
|
|
RolesApi = rolesApi;
|
|
Options = options.CurrentValue;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Get user by id
|
|
/// </summary>
|
|
public async Task<IActionResult> GetById([FromQuery] string id)
|
|
{
|
|
return await As<User, UserEditModel>(await Api.GetUserById(id));
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Get all users
|
|
/// </summary>
|
|
public async Task<IActionResult> GetAll([FromQuery] ListQuery<User> query)
|
|
{
|
|
return await As<User, UserListModel>(await Api.GetByQuery(query, "zero.applications.1-A"));
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Get all permissions for selection
|
|
/// </summary>
|
|
public IActionResult GetAllPermissions()
|
|
{
|
|
return Json(Options.Authorization.Permissions);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Updates a user password
|
|
/// </summary>
|
|
[ZeroAuthorize]
|
|
public async Task<IActionResult> UpdatePassword([FromBody] UserPasswordEditModel model)
|
|
{
|
|
EntityResult<User> result = null;
|
|
|
|
if (model.NewPassword != model.ConfirmNewPassword)
|
|
{
|
|
result = EntityResult<User>.Fail(nameof(model.NewPassword), "@errors.changepassword.newpasswordsnotmatching");
|
|
}
|
|
else
|
|
{
|
|
result = await Api.UpdatePassword(model.CurrentPassword, model.NewPassword);
|
|
}
|
|
|
|
return Json(result);
|
|
//return await As<Translation, TranslationEditModel>(await Api.);
|
|
}
|
|
}
|
|
}
|