jellyfin/MediaBrowser.Api/UserService.cs

624 lines
21 KiB
C#
Raw Normal View History

using System;
using System.Linq;
using System.Threading.Tasks;
2020-05-12 19:10:35 -07:00
using Jellyfin.Data.Enums;
2018-12-27 14:43:48 -07:00
using MediaBrowser.Common.Extensions;
2014-07-12 21:55:56 -07:00
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Authentication;
2014-07-09 20:48:08 -07:00
using MediaBrowser.Controller.Configuration;
2014-12-30 09:36:49 -07:00
using MediaBrowser.Controller.Devices;
2013-02-20 18:33:05 -07:00
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Net;
2014-02-11 21:29:13 -07:00
using MediaBrowser.Controller.Session;
2014-12-18 21:20:07 -07:00
using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.Dto;
2016-10-25 12:02:04 -07:00
using MediaBrowser.Model.Services;
using MediaBrowser.Model.Users;
2019-06-28 10:13:08 -07:00
using Microsoft.Extensions.Logging;
2013-02-20 18:33:05 -07:00
namespace MediaBrowser.Api
{
/// <summary>
/// Class GetUsers
/// </summary>
2014-03-23 12:36:25 -07:00
[Route("/Users", "GET", Summary = "Gets a list of users")]
2014-07-07 18:41:03 -07:00
[Authenticated]
2017-08-19 12:43:35 -07:00
public class GetUsers : IReturn<UserDto[]>
2013-02-20 18:33:05 -07:00
{
2013-09-16 19:44:06 -07:00
[ApiMember(Name = "IsHidden", Description = "Optional filter by IsHidden=true or false", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
2013-07-08 09:13:21 -07:00
public bool? IsHidden { get; set; }
[ApiMember(Name = "IsDisabled", Description = "Optional filter by IsDisabled=true or false", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
public bool? IsDisabled { get; set; }
2014-10-19 20:04:45 -07:00
[ApiMember(Name = "IsGuest", Description = "Optional filter by IsGuest=true or false", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
public bool? IsGuest { get; set; }
2013-02-20 18:33:05 -07:00
}
2014-03-23 12:36:25 -07:00
[Route("/Users/Public", "GET", Summary = "Gets a list of publicly visible users for display on a login screen.")]
public class GetPublicUsers : IReturn<PublicUserDto[]>
2013-07-08 10:13:18 -07:00
{
}
2013-09-16 19:44:06 -07:00
2013-02-20 18:33:05 -07:00
/// <summary>
/// Class GetUser
/// </summary>
2014-03-23 12:36:25 -07:00
[Route("/Users/{Id}", "GET", Summary = "Gets a user by Id")]
2014-10-28 16:17:55 -07:00
[Authenticated(EscapeParentalControl = true)]
public class GetUser : IReturn<UserDto>
2013-02-20 18:33:05 -07:00
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
2013-03-04 22:08:27 -07:00
[ApiMember(Name = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
2018-09-12 10:26:21 -07:00
public Guid Id { get; set; }
2015-01-29 22:18:32 -07:00
}
2013-02-20 18:33:05 -07:00
/// <summary>
/// Class DeleteUser
/// </summary>
2014-03-23 12:36:25 -07:00
[Route("/Users/{Id}", "DELETE", Summary = "Deletes a user")]
2014-11-14 19:31:03 -07:00
[Authenticated(Roles = "Admin")]
2013-02-20 18:33:05 -07:00
public class DeleteUser : IReturnVoid
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
2013-03-08 12:14:09 -07:00
[ApiMember(Name = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
2018-09-12 10:26:21 -07:00
public Guid Id { get; set; }
2013-02-20 18:33:05 -07:00
}
/// <summary>
/// Class AuthenticateUser
/// </summary>
2014-03-23 12:36:25 -07:00
[Route("/Users/{Id}/Authenticate", "POST", Summary = "Authenticates a user")]
2014-02-09 14:11:11 -07:00
public class AuthenticateUser : IReturn<AuthenticationResult>
2013-02-20 18:33:05 -07:00
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
2013-03-08 12:14:09 -07:00
[ApiMember(Name = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
2018-09-12 10:26:21 -07:00
public Guid Id { get; set; }
2013-02-20 18:33:05 -07:00
2017-09-17 09:45:23 -07:00
[ApiMember(Name = "Pw", IsRequired = true, DataType = "string", ParameterType = "body", Verb = "POST")]
public string Pw { get; set; }
2013-02-20 18:33:05 -07:00
/// <summary>
/// Gets or sets the password.
/// </summary>
/// <value>The password.</value>
2013-03-08 12:14:09 -07:00
[ApiMember(Name = "Password", IsRequired = true, DataType = "string", ParameterType = "body", Verb = "POST")]
2013-02-20 18:33:05 -07:00
public string Password { get; set; }
}
2013-07-08 09:13:21 -07:00
/// <summary>
/// Class AuthenticateUser
/// </summary>
2014-03-23 12:36:25 -07:00
[Route("/Users/AuthenticateByName", "POST", Summary = "Authenticates a user")]
2013-07-08 09:13:21 -07:00
public class AuthenticateUserByName : IReturn<AuthenticationResult>
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[ApiMember(Name = "Username", IsRequired = true, DataType = "string", ParameterType = "body", Verb = "POST")]
public string Username { get; set; }
2013-07-08 09:13:21 -07:00
/// <summary>
/// Gets or sets the password.
/// </summary>
/// <value>The password.</value>
[ApiMember(Name = "Password", IsRequired = true, DataType = "string", ParameterType = "body", Verb = "POST")]
public string Password { get; set; }
2014-10-16 21:52:41 -07:00
2017-09-17 09:45:23 -07:00
[ApiMember(Name = "Pw", IsRequired = true, DataType = "string", ParameterType = "body", Verb = "POST")]
public string Pw { get; set; }
2013-07-08 09:13:21 -07:00
}
2013-02-20 18:33:05 -07:00
/// <summary>
/// Class UpdateUserPassword
/// </summary>
2014-03-23 12:36:25 -07:00
[Route("/Users/{Id}/Password", "POST", Summary = "Updates a user's password")]
[Authenticated]
2013-02-20 18:33:05 -07:00
public class UpdateUserPassword : IReturnVoid
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
2018-09-12 10:26:21 -07:00
public Guid Id { get; set; }
2013-02-20 18:33:05 -07:00
/// <summary>
/// Gets or sets the password.
/// </summary>
/// <value>The password.</value>
public string CurrentPassword { get; set; }
2017-09-17 09:45:23 -07:00
public string CurrentPw { get; set; }
public string NewPw { get; set; }
2013-02-20 18:33:05 -07:00
/// <summary>
/// Gets or sets a value indicating whether [reset password].
/// </summary>
/// <value><c>true</c> if [reset password]; otherwise, <c>false</c>.</value>
public bool ResetPassword { get; set; }
}
2015-01-28 23:06:24 -07:00
/// <summary>
/// Class UpdateUserEasyPassword
/// </summary>
[Route("/Users/{Id}/EasyPassword", "POST", Summary = "Updates a user's easy password")]
[Authenticated]
public class UpdateUserEasyPassword : IReturnVoid
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
2018-09-12 10:26:21 -07:00
public Guid Id { get; set; }
2015-01-28 23:06:24 -07:00
/// <summary>
/// Gets or sets the new password.
/// </summary>
/// <value>The new password.</value>
public string NewPassword { get; set; }
2017-09-17 09:45:23 -07:00
public string NewPw { get; set; }
2015-01-28 23:06:24 -07:00
/// <summary>
/// Gets or sets a value indicating whether [reset password].
/// </summary>
/// <value><c>true</c> if [reset password]; otherwise, <c>false</c>.</value>
public bool ResetPassword { get; set; }
}
2013-02-20 18:33:05 -07:00
/// <summary>
/// Class UpdateUser
/// </summary>
2014-03-23 12:36:25 -07:00
[Route("/Users/{Id}", "POST", Summary = "Updates a user")]
2014-11-28 10:41:47 -07:00
[Authenticated]
2013-03-04 19:05:59 -07:00
public class UpdateUser : UserDto, IReturnVoid
2013-02-20 18:33:05 -07:00
{
}
2014-12-18 21:20:07 -07:00
/// <summary>
/// Class UpdateUser
/// </summary>
[Route("/Users/{Id}/Policy", "POST", Summary = "Updates a user policy")]
[Authenticated(Roles = "admin")]
public class UpdateUserPolicy : UserPolicy, IReturnVoid
{
[ApiMember(Name = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
2018-09-12 10:26:21 -07:00
public Guid Id { get; set; }
2014-12-18 21:20:07 -07:00
}
/// <summary>
/// Class UpdateUser
/// </summary>
[Route("/Users/{Id}/Configuration", "POST", Summary = "Updates a user configuration")]
[Authenticated]
public class UpdateUserConfiguration : UserConfiguration, IReturnVoid
{
[ApiMember(Name = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
2018-09-12 10:26:21 -07:00
public Guid Id { get; set; }
2014-12-18 21:20:07 -07:00
}
2014-10-29 19:06:05 -07:00
/// <summary>
/// Class CreateUser
/// </summary>
[Route("/Users/New", "POST", Summary = "Creates a user")]
2014-11-14 19:31:03 -07:00
[Authenticated(Roles = "Admin")]
2014-10-29 19:06:05 -07:00
public class CreateUserByName : IReturn<UserDto>
{
[ApiMember(Name = "Name", IsRequired = true, DataType = "string", ParameterType = "body", Verb = "POST")]
public string Name { get; set; }
[ApiMember(Name = "Password", IsRequired = false, DataType = "string", ParameterType = "body", Verb = "POST")]
public string Password { get; set; }
2014-10-29 19:06:05 -07:00
}
[Route("/Users/ForgotPassword", "POST", Summary = "Initiates the forgot password process for a local user")]
public class ForgotPassword : IReturn<ForgotPasswordResult>
{
[ApiMember(Name = "EnteredUsername", IsRequired = false, DataType = "string", ParameterType = "body", Verb = "POST")]
public string EnteredUsername { get; set; }
}
[Route("/Users/ForgotPassword/Pin", "POST", Summary = "Redeems a forgot password pin")]
public class ForgotPasswordPin : IReturn<PinRedeemResult>
{
[ApiMember(Name = "Pin", IsRequired = false, DataType = "string", ParameterType = "body", Verb = "POST")]
public string Pin { get; set; }
}
2013-02-20 18:33:05 -07:00
/// <summary>
/// Class UsersService
/// </summary>
2016-11-10 07:41:24 -07:00
public class UserService : BaseApiService
2013-02-20 18:33:05 -07:00
{
/// <summary>
2020-02-01 08:07:46 -07:00
/// The user manager.
/// </summary>
private readonly IUserManager _userManager;
2014-02-11 21:29:13 -07:00
private readonly ISessionManager _sessionMananger;
2014-07-12 21:55:56 -07:00
private readonly INetworkManager _networkManager;
2014-12-30 09:36:49 -07:00
private readonly IDeviceManager _deviceManager;
2016-11-10 07:41:24 -07:00
private readonly IAuthorizationContext _authContext;
public UserService(
ILogger<UserService> logger,
IServerConfigurationManager serverConfigurationManager,
IHttpResultFactory httpResultFactory,
IUserManager userManager,
ISessionManager sessionMananger,
INetworkManager networkManager,
IDeviceManager deviceManager,
IAuthorizationContext authContext)
: base(logger, serverConfigurationManager, httpResultFactory)
2013-02-24 14:53:54 -07:00
{
_userManager = userManager;
2014-02-11 21:47:16 -07:00
_sessionMananger = sessionMananger;
2014-07-12 21:55:56 -07:00
_networkManager = networkManager;
2014-12-30 09:36:49 -07:00
_deviceManager = deviceManager;
2016-11-10 07:41:24 -07:00
_authContext = authContext;
2013-02-24 14:53:54 -07:00
}
/// <summary>
/// Gets the public available Users information
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
2013-07-08 10:13:18 -07:00
public object Get(GetPublicUsers request)
{
var result = _userManager
.Users
2020-05-19 16:05:17 -07:00
.Where(user => !user.HasPermission(PermissionKind.IsDisabled))
.AsQueryable();
if (ServerConfigurationManager.Configuration.IsStartupWizardCompleted)
2014-07-07 18:41:03 -07:00
{
var deviceId = _authContext.GetAuthorizationInfo(Request).DeviceId;
2020-05-12 19:25:45 -07:00
result = result.Where(item => !item.HasPermission(PermissionKind.IsHidden));
if (!string.IsNullOrWhiteSpace(deviceId))
2014-07-09 20:48:08 -07:00
{
result = result.Where(i => _deviceManager.CanAccessDevice(i, deviceId));
}
if (!_networkManager.IsInLocalNetwork(Request.RemoteIp))
2014-07-09 20:48:08 -07:00
{
2020-05-12 19:25:45 -07:00
result = result.Where(i => i.HasPermission(PermissionKind.EnableRemoteAccess));
}
2014-07-07 18:41:03 -07:00
}
return ToOptimizedResult(result
2020-05-12 19:25:45 -07:00
.OrderBy(u => u.Username)
.Select(i => _userManager.GetPublicUserDto(i, Request.RemoteIp))
.ToArray()
);
2014-07-12 21:55:56 -07:00
}
2013-02-20 18:33:05 -07:00
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
public object Get(GetUsers request)
2014-12-30 09:36:49 -07:00
{
2018-09-12 10:26:21 -07:00
return Get(request, false, false);
2014-12-30 09:36:49 -07:00
}
2018-09-12 10:26:21 -07:00
private object Get(GetUsers request, bool filterByDevice, bool filterByNetwork)
2013-02-20 18:33:05 -07:00
{
2013-07-08 09:13:21 -07:00
var users = _userManager.Users;
if (request.IsDisabled.HasValue)
{
2020-05-12 19:10:35 -07:00
users = users.Where(i => i.HasPermission(PermissionKind.IsDisabled) == request.IsDisabled.Value);
2013-07-08 09:13:21 -07:00
}
if (request.IsHidden.HasValue)
{
2020-05-12 19:10:35 -07:00
users = users.Where(i => i.HasPermission(PermissionKind.IsHidden) == request.IsHidden.Value);
2013-07-08 09:13:21 -07:00
}
2014-08-18 07:20:02 -07:00
2014-12-30 09:36:49 -07:00
if (filterByDevice)
{
2016-11-10 07:41:24 -07:00
var deviceId = _authContext.GetAuthorizationInfo(Request).DeviceId;
2014-12-30 09:36:49 -07:00
if (!string.IsNullOrWhiteSpace(deviceId))
{
2018-09-12 10:26:21 -07:00
users = users.Where(i => _deviceManager.CanAccessDevice(i, deviceId));
}
}
if (filterByNetwork)
{
if (!_networkManager.IsInLocalNetwork(Request.RemoteIp))
{
2020-05-12 19:10:35 -07:00
users = users.Where(i => i.HasPermission(PermissionKind.EnableRemoteAccess));
2014-12-30 09:36:49 -07:00
}
}
2013-09-16 19:44:06 -07:00
var result = users
2020-05-12 19:10:35 -07:00
.OrderBy(u => u.Username)
2014-08-15 09:35:41 -07:00
.Select(i => _userManager.GetUserDto(i, Request.RemoteIp))
2017-08-19 12:43:35 -07:00
.ToArray();
2013-02-20 18:33:05 -07:00
2015-01-29 22:18:32 -07:00
return ToOptimizedResult(result);
2013-02-20 18:33:05 -07:00
}
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
public object Get(GetUser request)
{
var user = _userManager.GetUserById(request.Id);
2013-02-20 18:33:05 -07:00
if (user == null)
{
throw new ResourceNotFoundException("User not found");
}
2014-08-15 09:35:41 -07:00
var result = _userManager.GetUserDto(user, Request.RemoteIp);
2013-02-20 18:33:05 -07:00
2015-01-29 22:18:32 -07:00
return ToOptimizedResult(result);
}
2013-02-20 18:33:05 -07:00
/// <summary>
/// Deletes the specified request.
/// </summary>
/// <param name="request">The request.</param>
2018-09-12 10:26:21 -07:00
public Task Delete(DeleteUser request)
2014-08-18 07:20:02 -07:00
{
2018-09-12 10:26:21 -07:00
return DeleteAsync(request);
2014-08-18 07:20:02 -07:00
}
2018-09-12 10:26:21 -07:00
public Task DeleteAsync(DeleteUser request)
2013-02-20 18:33:05 -07:00
{
var user = _userManager.GetUserById(request.Id);
2013-02-20 18:33:05 -07:00
if (user == null)
{
throw new ResourceNotFoundException("User not found");
}
2018-09-12 10:26:21 -07:00
_sessionMananger.RevokeUserTokens(user.Id, null);
2019-06-09 13:08:01 -07:00
_userManager.DeleteUser(user);
return Task.CompletedTask;
2013-02-20 18:33:05 -07:00
}
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
public object Post(AuthenticateUser request)
2013-02-20 18:33:05 -07:00
{
var user = _userManager.GetUserById(request.Id);
2013-02-20 18:33:05 -07:00
if (user == null)
{
throw new ResourceNotFoundException("User not found");
}
2019-04-18 07:24:08 -07:00
if (!string.IsNullOrEmpty(request.Password) && string.IsNullOrEmpty(request.Pw))
{
throw new MethodNotAllowedException("Hashed-only passwords are not valid for this API.");
}
// Password should always be null
2014-07-02 11:34:08 -07:00
return Post(new AuthenticateUserByName
2014-02-25 21:38:21 -07:00
{
2020-05-12 19:10:35 -07:00
Username = user.Username,
Password = null,
2017-09-17 09:45:23 -07:00
Pw = request.Pw
2014-07-02 11:34:08 -07:00
});
}
2014-02-25 21:38:21 -07:00
2014-08-16 22:38:13 -07:00
public async Task<object> Post(AuthenticateUserByName request)
2014-07-02 11:34:08 -07:00
{
2016-11-10 07:41:24 -07:00
var auth = _authContext.GetAuthorizationInfo(Request);
2013-07-08 09:13:21 -07:00
2019-06-28 10:13:08 -07:00
try
2014-08-10 15:13:17 -07:00
{
2019-06-28 10:13:08 -07:00
var result = await _sessionMananger.AuthenticateNewSession(new AuthenticationRequest
{
App = auth.Client,
AppVersion = auth.Version,
DeviceId = auth.DeviceId,
DeviceName = auth.Device,
Password = request.Pw,
PasswordSha1 = request.Password,
RemoteEndPoint = Request.RemoteIp,
Username = request.Username
}).ConfigureAwait(false);
return ToOptimizedResult(result);
}
2019-08-28 05:45:46 -07:00
catch (SecurityException e)
2019-06-28 10:13:08 -07:00
{
// rethrow adding IP address to message
throw new SecurityException($"[{Request.RemoteIp}] {e.Message}", e);
2019-06-28 10:13:08 -07:00
}
2013-02-20 18:33:05 -07:00
}
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
2018-09-12 10:26:21 -07:00
public Task Post(UpdateUserPassword request)
2014-08-18 07:20:02 -07:00
{
2018-09-12 10:26:21 -07:00
return PostAsync(request);
2014-08-18 07:20:02 -07:00
}
public async Task PostAsync(UpdateUserPassword request)
2013-02-20 18:33:05 -07:00
{
2017-04-21 13:03:07 -07:00
AssertCanUpdateUser(_authContext, _userManager, request.Id, true);
2015-01-28 23:06:24 -07:00
var user = _userManager.GetUserById(request.Id);
2013-02-20 18:33:05 -07:00
if (user == null)
{
throw new ResourceNotFoundException("User not found");
}
if (request.ResetPassword)
{
2018-09-12 10:26:21 -07:00
await _userManager.ResetPassword(user).ConfigureAwait(false);
2013-02-20 18:33:05 -07:00
}
else
{
2020-05-12 19:10:35 -07:00
var success = await _userManager.AuthenticateUser(
user.Username,
request.CurrentPw,
request.CurrentPassword,
Request.RemoteIp,
false).ConfigureAwait(false);
2013-02-20 18:33:05 -07:00
2017-04-08 11:31:18 -07:00
if (success == null)
2013-02-20 18:33:05 -07:00
{
2014-07-21 18:29:06 -07:00
throw new ArgumentException("Invalid user or password entered.");
2013-02-20 18:33:05 -07:00
}
2018-09-12 10:26:21 -07:00
await _userManager.ChangePassword(user, request.NewPw).ConfigureAwait(false);
2016-11-10 07:41:24 -07:00
var currentToken = _authContext.GetAuthorizationInfo(Request).Token;
2018-09-12 10:26:21 -07:00
_sessionMananger.RevokeUserTokens(user.Id, currentToken);
2013-02-20 18:33:05 -07:00
}
}
2015-01-28 23:06:24 -07:00
public void Post(UpdateUserEasyPassword request)
{
2017-04-21 13:03:07 -07:00
AssertCanUpdateUser(_authContext, _userManager, request.Id, true);
2015-01-28 23:06:24 -07:00
var user = _userManager.GetUserById(request.Id);
if (user == null)
{
throw new ResourceNotFoundException("User not found");
}
if (request.ResetPassword)
{
_userManager.ResetEasyPassword(user);
2015-01-28 23:06:24 -07:00
}
else
{
2017-09-17 09:45:23 -07:00
_userManager.ChangeEasyPassword(user, request.NewPw, request.NewPassword);
2015-01-28 23:06:24 -07:00
}
}
2013-02-20 18:33:05 -07:00
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
2018-09-12 10:26:21 -07:00
public async Task Post(UpdateUser request)
2013-02-20 18:33:05 -07:00
{
var id = Guid.Parse(GetPathValue(1));
2015-01-28 23:06:24 -07:00
AssertCanUpdateUser(_authContext, _userManager, id, false);
2013-02-24 14:53:54 -07:00
2013-03-04 19:05:59 -07:00
var dtoUser = request;
2013-02-20 18:33:05 -07:00
var user = _userManager.GetUserById(id);
2013-02-20 18:33:05 -07:00
2020-05-12 19:10:35 -07:00
if (string.Equals(user.Username, dtoUser.Name, StringComparison.Ordinal))
{
2020-05-12 19:10:35 -07:00
await _userManager.UpdateUserAsync(user);
_userManager.UpdateConfiguration(user.Id, dtoUser.Configuration);
}
else
{
2018-09-12 10:26:21 -07:00
await _userManager.RenameUser(user, dtoUser.Name).ConfigureAwait(false);
2013-02-20 18:33:05 -07:00
2018-09-12 10:26:21 -07:00
_userManager.UpdateConfiguration(dtoUser.Id, dtoUser.Configuration);
}
2013-02-20 18:33:05 -07:00
}
2019-06-09 13:08:01 -07:00
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
2018-09-12 10:26:21 -07:00
public async Task<object> Post(CreateUserByName request)
2014-10-29 19:06:05 -07:00
{
2019-06-09 13:08:01 -07:00
var newUser = _userManager.CreateUser(request.Name);
2014-10-29 19:06:05 -07:00
// no need to authenticate password for new user
if (request.Password != null)
{
await _userManager.ChangePassword(newUser, request.Password).ConfigureAwait(false);
}
2014-10-29 19:06:05 -07:00
var result = _userManager.GetUserDto(newUser, Request.RemoteIp);
return ToOptimizedResult(result);
}
2018-09-12 10:26:21 -07:00
public async Task<object> Post(ForgotPassword request)
{
var isLocal = Request.IsLocal || _networkManager.IsInLocalNetwork(Request.RemoteIp);
2018-09-12 10:26:21 -07:00
var result = await _userManager.StartForgotPasswordProcess(request.EnteredUsername, isLocal).ConfigureAwait(false);
return result;
}
2018-09-12 10:26:21 -07:00
public async Task<object> Post(ForgotPasswordPin request)
{
2018-09-12 10:26:21 -07:00
var result = await _userManager.RedeemPasswordResetPin(request.Pin).ConfigureAwait(false);
return result;
}
2014-12-18 21:20:07 -07:00
public void Post(UpdateUserConfiguration request)
{
2017-04-21 13:03:07 -07:00
AssertCanUpdateUser(_authContext, _userManager, request.Id, false);
2015-01-28 23:06:24 -07:00
_userManager.UpdateConfiguration(request.Id, request);
2014-12-19 23:06:27 -07:00
2014-12-18 21:20:07 -07:00
}
public void Post(UpdateUserPolicy request)
2014-12-19 23:06:27 -07:00
{
var user = _userManager.GetUserById(request.Id);
2014-12-19 23:06:27 -07:00
// If removing admin access
2020-05-12 19:10:35 -07:00
if (!request.IsAdministrator && user.HasPermission(PermissionKind.IsAdministrator))
2014-12-19 23:06:27 -07:00
{
2020-05-12 19:10:35 -07:00
if (_userManager.Users.Count(i => i.HasPermission(PermissionKind.IsAdministrator)) == 1)
2014-12-19 23:06:27 -07:00
{
throw new ArgumentException("There must be at least one user in the system with administrative access.");
}
}
// If disabling
2020-05-12 19:10:35 -07:00
if (request.IsDisabled && user.HasPermission(PermissionKind.IsAdministrator))
2014-12-19 23:06:27 -07:00
{
throw new ArgumentException("Administrators cannot be disabled.");
}
// If disabling
2020-05-12 19:10:35 -07:00
if (request.IsDisabled && !user.HasPermission(PermissionKind.IsDisabled))
2014-12-19 23:06:27 -07:00
{
2020-05-12 19:10:35 -07:00
if (_userManager.Users.Count(i => !i.HasPermission(PermissionKind.IsDisabled)) == 1)
2014-12-19 23:06:27 -07:00
{
throw new ArgumentException("There must be at least one enabled user in the system.");
}
2016-11-10 07:41:24 -07:00
var currentToken = _authContext.GetAuthorizationInfo(Request).Token;
2018-09-12 10:26:21 -07:00
_sessionMananger.RevokeUserTokens(user.Id, currentToken);
2014-12-19 23:06:27 -07:00
}
2020-05-12 19:10:35 -07:00
_userManager.UpdatePolicy(request.Id, request);
2014-12-19 23:06:27 -07:00
}
2013-02-20 18:33:05 -07:00
}
}