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