2019-01-13 20:24:58 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2018-12-27 22:43:48 +01:00
|
|
|
|
using MediaBrowser.Common.Extensions;
|
2014-07-13 06:55:56 +02:00
|
|
|
|
using MediaBrowser.Common.Net;
|
2019-01-13 20:24:58 +01:00
|
|
|
|
using MediaBrowser.Controller.Authentication;
|
2014-07-10 05:48:08 +02:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2014-12-30 17:36:49 +01:00
|
|
|
|
using MediaBrowser.Controller.Devices;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
2014-07-02 06:57:18 +02:00
|
|
|
|
using MediaBrowser.Controller.Net;
|
2014-02-12 05:29:13 +01:00
|
|
|
|
using MediaBrowser.Controller.Session;
|
2014-12-19 05:20:07 +01:00
|
|
|
|
using MediaBrowser.Model.Configuration;
|
2014-10-16 05:26:39 +02:00
|
|
|
|
using MediaBrowser.Model.Connect;
|
2013-02-21 18:50:46 +01:00
|
|
|
|
using MediaBrowser.Model.Dto;
|
2016-10-25 21:02:04 +02:00
|
|
|
|
using MediaBrowser.Model.Services;
|
2019-01-13 20:24:58 +01:00
|
|
|
|
using MediaBrowser.Model.Users;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Api
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class GetUsers
|
|
|
|
|
/// </summary>
|
2014-03-23 20:36:25 +01:00
|
|
|
|
[Route("/Users", "GET", Summary = "Gets a list of users")]
|
2014-07-08 03:41:03 +02:00
|
|
|
|
[Authenticated]
|
2017-08-19 21:43:35 +02:00
|
|
|
|
public class GetUsers : IReturn<UserDto[]>
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2013-09-17 04:44:06 +02:00
|
|
|
|
[ApiMember(Name = "IsHidden", Description = "Optional filter by IsHidden=true or false", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
|
2013-07-08 18:13:21 +02: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 05:04:45 +02: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 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-23 20:36:25 +01:00
|
|
|
|
[Route("/Users/Public", "GET", Summary = "Gets a list of publicly visible users for display on a login screen.")]
|
2017-08-19 21:43:35 +02:00
|
|
|
|
public class GetPublicUsers : IReturn<UserDto[]>
|
2013-07-08 19:13:18 +02:00
|
|
|
|
{
|
|
|
|
|
}
|
2013-09-17 04:44:06 +02:00
|
|
|
|
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class GetUser
|
|
|
|
|
/// </summary>
|
2014-03-23 20:36:25 +01:00
|
|
|
|
[Route("/Users/{Id}", "GET", Summary = "Gets a user by Id")]
|
2014-10-29 00:17:55 +01:00
|
|
|
|
[Authenticated(EscapeParentalControl = true)]
|
2013-02-21 18:50:46 +01:00
|
|
|
|
public class GetUser : IReturn<UserDto>
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the id.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The id.</value>
|
2013-03-05 06:08:27 +01:00
|
|
|
|
[ApiMember(Name = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
|
2018-09-12 19:26:21 +02:00
|
|
|
|
public Guid Id { get; set; }
|
2015-01-30 06:18:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class DeleteUser
|
|
|
|
|
/// </summary>
|
2014-03-23 20:36:25 +01:00
|
|
|
|
[Route("/Users/{Id}", "DELETE", Summary = "Deletes a user")]
|
2014-11-15 03:31:03 +01:00
|
|
|
|
[Authenticated(Roles = "Admin")]
|
2013-02-21 02:33:05 +01:00
|
|
|
|
public class DeleteUser : IReturnVoid
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the id.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The id.</value>
|
2013-03-08 20:14:09 +01:00
|
|
|
|
[ApiMember(Name = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
|
2018-09-12 19:26:21 +02:00
|
|
|
|
public Guid Id { get; set; }
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class AuthenticateUser
|
|
|
|
|
/// </summary>
|
2014-03-23 20:36:25 +01:00
|
|
|
|
[Route("/Users/{Id}/Authenticate", "POST", Summary = "Authenticates a user")]
|
2014-02-09 22:11:11 +01:00
|
|
|
|
public class AuthenticateUser : IReturn<AuthenticationResult>
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the id.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The id.</value>
|
2013-03-08 20:14:09 +01:00
|
|
|
|
[ApiMember(Name = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
|
2018-09-12 19:26:21 +02:00
|
|
|
|
public Guid Id { get; set; }
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2017-09-17 18:45:23 +02:00
|
|
|
|
[ApiMember(Name = "Pw", IsRequired = true, DataType = "string", ParameterType = "body", Verb = "POST")]
|
|
|
|
|
public string Pw { get; set; }
|
|
|
|
|
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the password.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The password.</value>
|
2013-03-08 20:14:09 +01:00
|
|
|
|
[ApiMember(Name = "Password", IsRequired = true, DataType = "string", ParameterType = "body", Verb = "POST")]
|
2013-02-21 02:33:05 +01:00
|
|
|
|
public string Password { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-08 18:13:21 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class AuthenticateUser
|
|
|
|
|
/// </summary>
|
2014-03-23 20:36:25 +01:00
|
|
|
|
[Route("/Users/AuthenticateByName", "POST", Summary = "Authenticates a user")]
|
2013-07-08 18:13:21 +02:00
|
|
|
|
public class AuthenticateUserByName : IReturn<AuthenticationResult>
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the id.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The id.</value>
|
2013-07-26 21:19:25 +02:00
|
|
|
|
[ApiMember(Name = "Username", IsRequired = true, DataType = "string", ParameterType = "body", Verb = "POST")]
|
|
|
|
|
public string Username { get; set; }
|
2013-07-08 18:13:21 +02: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 06:52:41 +02:00
|
|
|
|
|
2017-09-17 18:45:23 +02:00
|
|
|
|
[ApiMember(Name = "Pw", IsRequired = true, DataType = "string", ParameterType = "body", Verb = "POST")]
|
|
|
|
|
public string Pw { get; set; }
|
2013-07-08 18:13:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class UpdateUserPassword
|
|
|
|
|
/// </summary>
|
2014-03-23 20:36:25 +01:00
|
|
|
|
[Route("/Users/{Id}/Password", "POST", Summary = "Updates a user's password")]
|
2014-07-04 04:22:57 +02:00
|
|
|
|
[Authenticated]
|
2013-02-21 02:33:05 +01:00
|
|
|
|
public class UpdateUserPassword : IReturnVoid
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the id.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The id.</value>
|
2018-09-12 19:26:21 +02:00
|
|
|
|
public Guid Id { get; set; }
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the password.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The password.</value>
|
|
|
|
|
public string CurrentPassword { get; set; }
|
|
|
|
|
|
2017-09-17 18:45:23 +02:00
|
|
|
|
public string CurrentPw { get; set; }
|
|
|
|
|
|
|
|
|
|
public string NewPw { get; set; }
|
|
|
|
|
|
2013-02-21 02:33:05 +01: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 07:06:24 +01: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 19:26:21 +02:00
|
|
|
|
public Guid Id { get; set; }
|
2015-01-29 07:06:24 +01:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the new password.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The new password.</value>
|
|
|
|
|
public string NewPassword { get; set; }
|
|
|
|
|
|
2017-09-17 18:45:23 +02:00
|
|
|
|
public string NewPw { get; set; }
|
|
|
|
|
|
2015-01-29 07:06:24 +01: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 02:33:05 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class UpdateUser
|
|
|
|
|
/// </summary>
|
2014-03-23 20:36:25 +01:00
|
|
|
|
[Route("/Users/{Id}", "POST", Summary = "Updates a user")]
|
2014-11-28 18:41:47 +01:00
|
|
|
|
[Authenticated]
|
2013-03-05 03:05:59 +01:00
|
|
|
|
public class UpdateUser : UserDto, IReturnVoid
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-19 05:20:07 +01: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 19:26:21 +02:00
|
|
|
|
public Guid Id { get; set; }
|
2014-12-19 05:20:07 +01: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 19:26:21 +02:00
|
|
|
|
public Guid Id { get; set; }
|
2014-12-19 05:20:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-30 03:06:05 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class CreateUser
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Route("/Users/New", "POST", Summary = "Creates a user")]
|
2014-11-15 03:31:03 +01:00
|
|
|
|
[Authenticated(Roles = "Admin")]
|
2014-10-30 03:06:05 +01:00
|
|
|
|
public class CreateUserByName : IReturn<UserDto>
|
|
|
|
|
{
|
|
|
|
|
[ApiMember(Name = "Name", IsRequired = true, DataType = "string", ParameterType = "body", Verb = "POST")]
|
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-09 04:18:14 +01: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 02:33:05 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class UsersService
|
|
|
|
|
/// </summary>
|
2016-11-10 15:41:24 +01:00
|
|
|
|
public class UserService : BaseApiService
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2013-02-27 21:25:45 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _user manager
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly IUserManager _userManager;
|
2014-02-12 05:29:13 +01:00
|
|
|
|
private readonly ISessionManager _sessionMananger;
|
2014-07-10 05:48:08 +02:00
|
|
|
|
private readonly IServerConfigurationManager _config;
|
2014-07-13 06:55:56 +02:00
|
|
|
|
private readonly INetworkManager _networkManager;
|
2014-12-30 17:36:49 +01:00
|
|
|
|
private readonly IDeviceManager _deviceManager;
|
2016-11-10 15:41:24 +01:00
|
|
|
|
private readonly IAuthorizationContext _authContext;
|
2013-03-12 23:49:45 +01:00
|
|
|
|
|
2016-11-10 15:41:24 +01:00
|
|
|
|
public UserService(IUserManager userManager, ISessionManager sessionMananger, IServerConfigurationManager config, INetworkManager networkManager, IDeviceManager deviceManager, IAuthorizationContext authContext)
|
2013-02-24 22:53:54 +01:00
|
|
|
|
{
|
2013-02-27 21:25:45 +01:00
|
|
|
|
_userManager = userManager;
|
2014-02-12 05:47:16 +01:00
|
|
|
|
_sessionMananger = sessionMananger;
|
2014-07-10 05:48:08 +02:00
|
|
|
|
_config = config;
|
2014-07-13 06:55:56 +02:00
|
|
|
|
_networkManager = networkManager;
|
2014-12-30 17:36:49 +01:00
|
|
|
|
_deviceManager = deviceManager;
|
2016-11-10 15:41:24 +01:00
|
|
|
|
_authContext = authContext;
|
2013-02-24 22:53:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-08 19:13:18 +02:00
|
|
|
|
public object Get(GetPublicUsers request)
|
|
|
|
|
{
|
2014-10-16 05:26:39 +02:00
|
|
|
|
// If the startup wizard hasn't been completed then just return all users
|
|
|
|
|
if (!_config.Configuration.IsStartupWizardCompleted)
|
2014-07-08 03:41:03 +02:00
|
|
|
|
{
|
2014-07-10 05:48:08 +02:00
|
|
|
|
return Get(new GetUsers
|
|
|
|
|
{
|
|
|
|
|
IsDisabled = false
|
|
|
|
|
});
|
2014-07-08 03:41:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-30 17:36:49 +01:00
|
|
|
|
return Get(new GetUsers
|
2014-10-16 05:26:39 +02:00
|
|
|
|
{
|
2014-12-30 17:36:49 +01:00
|
|
|
|
IsHidden = false,
|
|
|
|
|
IsDisabled = false
|
2014-10-16 05:26:39 +02:00
|
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
|
}, true, true);
|
2014-07-13 06:55:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 02:33:05 +01: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 17:36:49 +01:00
|
|
|
|
{
|
2018-09-12 19:26:21 +02:00
|
|
|
|
return Get(request, false, false);
|
2014-12-30 17:36:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
|
private object Get(GetUsers request, bool filterByDevice, bool filterByNetwork)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2013-07-08 18:13:21 +02:00
|
|
|
|
var users = _userManager.Users;
|
|
|
|
|
|
|
|
|
|
if (request.IsDisabled.HasValue)
|
|
|
|
|
{
|
2014-12-20 07:06:27 +01:00
|
|
|
|
users = users.Where(i => i.Policy.IsDisabled == request.IsDisabled.Value);
|
2013-07-08 18:13:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (request.IsHidden.HasValue)
|
|
|
|
|
{
|
2014-12-20 07:06:27 +01:00
|
|
|
|
users = users.Where(i => i.Policy.IsHidden == request.IsHidden.Value);
|
2013-07-08 18:13:21 +02:00
|
|
|
|
}
|
2014-08-18 16:20:02 +02:00
|
|
|
|
|
2014-10-20 05:04:45 +02:00
|
|
|
|
if (request.IsGuest.HasValue)
|
|
|
|
|
{
|
|
|
|
|
users = users.Where(i => (i.ConnectLinkType.HasValue && i.ConnectLinkType.Value == UserLinkType.Guest) == request.IsGuest.Value);
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-30 17:36:49 +01:00
|
|
|
|
if (filterByDevice)
|
|
|
|
|
{
|
2016-11-10 15:41:24 +01:00
|
|
|
|
var deviceId = _authContext.GetAuthorizationInfo(Request).DeviceId;
|
2014-12-30 17:36:49 +01:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(deviceId))
|
|
|
|
|
{
|
2018-09-12 19:26:21 +02: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 17:36:49 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-17 04:44:06 +02:00
|
|
|
|
var result = users
|
|
|
|
|
.OrderBy(u => u.Name)
|
2014-08-15 18:35:41 +02:00
|
|
|
|
.Select(i => _userManager.GetUserDto(i, Request.RemoteIp))
|
2017-08-19 21:43:35 +02:00
|
|
|
|
.ToArray();
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2015-01-30 06:18:32 +01:00
|
|
|
|
return ToOptimizedResult(result);
|
2013-02-21 02:33:05 +01: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 21:25:45 +01:00
|
|
|
|
var user = _userManager.GetUserById(request.Id);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
|
|
|
|
if (user == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ResourceNotFoundException("User not found");
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-15 18:35:41 +02:00
|
|
|
|
var result = _userManager.GetUserDto(user, Request.RemoteIp);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2015-01-30 06:18:32 +01:00
|
|
|
|
return ToOptimizedResult(result);
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Deletes the specified request.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="request">The request.</param>
|
2018-09-12 19:26:21 +02:00
|
|
|
|
public Task Delete(DeleteUser request)
|
2014-08-18 16:20:02 +02:00
|
|
|
|
{
|
2018-09-12 19:26:21 +02:00
|
|
|
|
return DeleteAsync(request);
|
2014-08-18 16:20:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
|
public Task DeleteAsync(DeleteUser request)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2013-02-27 21:25:45 +01:00
|
|
|
|
var user = _userManager.GetUserById(request.Id);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
|
|
|
|
if (user == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ResourceNotFoundException("User not found");
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
|
_sessionMananger.RevokeUserTokens(user.Id, null);
|
2014-10-14 06:22:17 +02:00
|
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
|
return _userManager.DeleteUser(user);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Posts the specified request.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="request">The request.</param>
|
2013-12-22 18:17:01 +01:00
|
|
|
|
public object Post(AuthenticateUser request)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2013-02-27 21:25:45 +01:00
|
|
|
|
var user = _userManager.GetUserById(request.Id);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
|
|
|
|
if (user == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ResourceNotFoundException("User not found");
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-02 20:34:08 +02:00
|
|
|
|
return Post(new AuthenticateUserByName
|
2014-02-26 05:38:21 +01:00
|
|
|
|
{
|
2014-07-02 20:34:08 +02:00
|
|
|
|
Username = user.Name,
|
2017-09-17 18:45:23 +02:00
|
|
|
|
Password = request.Password,
|
|
|
|
|
Pw = request.Pw
|
2014-07-02 20:34:08 +02:00
|
|
|
|
});
|
|
|
|
|
}
|
2014-02-26 05:38:21 +01:00
|
|
|
|
|
2014-08-17 07:38:13 +02:00
|
|
|
|
public async Task<object> Post(AuthenticateUserByName request)
|
2014-07-02 20:34:08 +02:00
|
|
|
|
{
|
2016-11-10 15:41:24 +01:00
|
|
|
|
var auth = _authContext.GetAuthorizationInfo(Request);
|
2013-07-08 18:13:21 +02:00
|
|
|
|
|
2014-08-17 07:38:13 +02:00
|
|
|
|
var result = await _sessionMananger.AuthenticateNewSession(new AuthenticationRequest
|
2014-08-11 00:13:17 +02:00
|
|
|
|
{
|
|
|
|
|
App = auth.Client,
|
|
|
|
|
AppVersion = auth.Version,
|
|
|
|
|
DeviceId = auth.DeviceId,
|
|
|
|
|
DeviceName = auth.Device,
|
2017-09-17 18:45:23 +02:00
|
|
|
|
Password = request.Pw,
|
2014-10-17 06:52:41 +02:00
|
|
|
|
PasswordSha1 = request.Password,
|
2014-08-11 00:13:17 +02:00
|
|
|
|
RemoteEndPoint = Request.RemoteIp,
|
|
|
|
|
Username = request.Username
|
|
|
|
|
|
2014-11-09 04:18:14 +01:00
|
|
|
|
}).ConfigureAwait(false);
|
2013-07-08 18:13:21 +02:00
|
|
|
|
|
2014-07-02 20:34:08 +02:00
|
|
|
|
return ToOptimizedResult(result);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Posts the specified request.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="request">The request.</param>
|
2018-09-12 19:26:21 +02:00
|
|
|
|
public Task Post(UpdateUserPassword request)
|
2014-08-18 16:20:02 +02:00
|
|
|
|
{
|
2018-09-12 19:26:21 +02:00
|
|
|
|
return PostAsync(request);
|
2014-08-18 16:20:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task PostAsync(UpdateUserPassword request)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2017-04-21 22:03:07 +02:00
|
|
|
|
AssertCanUpdateUser(_authContext, _userManager, request.Id, true);
|
2015-01-29 07:06:24 +01:00
|
|
|
|
|
2013-02-27 21:25:45 +01:00
|
|
|
|
var user = _userManager.GetUserById(request.Id);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
|
|
|
|
if (user == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ResourceNotFoundException("User not found");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (request.ResetPassword)
|
|
|
|
|
{
|
2018-09-12 19:26:21 +02:00
|
|
|
|
await _userManager.ResetPassword(user).ConfigureAwait(false);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-09-12 19:26:21 +02:00
|
|
|
|
var success = await _userManager.AuthenticateUser(user.Name, request.CurrentPw, request.CurrentPassword, Request.RemoteIp, false).ConfigureAwait(false);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2017-04-08 20:31:18 +02:00
|
|
|
|
if (success == null)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2014-07-22 03:29:06 +02:00
|
|
|
|
throw new ArgumentException("Invalid user or password entered.");
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
|
await _userManager.ChangePassword(user, request.NewPw).ConfigureAwait(false);
|
2016-06-05 22:39:37 +02:00
|
|
|
|
|
2016-11-10 15:41:24 +01:00
|
|
|
|
var currentToken = _authContext.GetAuthorizationInfo(Request).Token;
|
2016-06-05 22:39:37 +02:00
|
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
|
_sessionMananger.RevokeUserTokens(user.Id, currentToken);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-11-09 04:18:14 +01:00
|
|
|
|
|
2015-01-29 07:06:24 +01:00
|
|
|
|
public void Post(UpdateUserEasyPassword request)
|
|
|
|
|
{
|
2017-04-21 22:03:07 +02:00
|
|
|
|
AssertCanUpdateUser(_authContext, _userManager, request.Id, true);
|
2017-08-27 02:32:33 +02:00
|
|
|
|
|
2015-01-29 07:06:24 +01:00
|
|
|
|
var user = _userManager.GetUserById(request.Id);
|
|
|
|
|
|
|
|
|
|
if (user == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ResourceNotFoundException("User not found");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (request.ResetPassword)
|
|
|
|
|
{
|
2017-08-27 02:32:33 +02:00
|
|
|
|
_userManager.ResetEasyPassword(user);
|
2015-01-29 07:06:24 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-09-17 18:45:23 +02:00
|
|
|
|
_userManager.ChangeEasyPassword(user, request.NewPw, request.NewPassword);
|
2015-01-29 07:06:24 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Posts the specified request.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="request">The request.</param>
|
2018-09-12 19:26:21 +02:00
|
|
|
|
public async Task Post(UpdateUser request)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2015-01-29 07:06:24 +01:00
|
|
|
|
var id = GetPathValue(1);
|
|
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
|
AssertCanUpdateUser(_authContext, _userManager, new Guid(id), false);
|
2013-02-24 22:53:54 +01:00
|
|
|
|
|
2013-03-05 03:05:59 +01:00
|
|
|
|
var dtoUser = request;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2013-02-27 21:25:45 +01:00
|
|
|
|
var user = _userManager.GetUserById(id);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2017-08-27 02:32:33 +02:00
|
|
|
|
if (string.Equals(user.Name, dtoUser.Name, StringComparison.Ordinal))
|
|
|
|
|
{
|
|
|
|
|
_userManager.UpdateUser(user);
|
2018-09-12 19:26:21 +02:00
|
|
|
|
_userManager.UpdateConfiguration(user, dtoUser.Configuration);
|
2017-08-27 02:32:33 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-09-12 19:26:21 +02:00
|
|
|
|
await _userManager.RenameUser(user, dtoUser.Name).ConfigureAwait(false);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
|
_userManager.UpdateConfiguration(dtoUser.Id, dtoUser.Configuration);
|
2017-08-27 02:32:33 +02:00
|
|
|
|
}
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Posts the specified request.
|
|
|
|
|
/// </summary>
|
2014-10-30 03:06:05 +01:00
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
|
/// <returns>System.Object.</returns>
|
2018-09-12 19:26:21 +02:00
|
|
|
|
public async Task<object> Post(CreateUserByName request)
|
2014-10-30 03:06:05 +01:00
|
|
|
|
{
|
|
|
|
|
var dtoUser = request;
|
|
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
|
var newUser = await _userManager.CreateUser(dtoUser.Name).ConfigureAwait(false);
|
2014-10-30 03:06:05 +01:00
|
|
|
|
|
|
|
|
|
var result = _userManager.GetUserDto(newUser, Request.RemoteIp);
|
|
|
|
|
|
|
|
|
|
return ToOptimizedResult(result);
|
|
|
|
|
}
|
2014-11-09 04:18:14 +01:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Posts the specified request.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
|
/// <returns>System.Object.</returns>
|
2018-09-12 19:26:21 +02:00
|
|
|
|
public async Task<object> Post(ForgotPassword request)
|
2014-11-09 04:18:14 +01:00
|
|
|
|
{
|
|
|
|
|
var isLocal = Request.IsLocal || _networkManager.IsInLocalNetwork(Request.RemoteIp);
|
|
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
|
var result = await _userManager.StartForgotPasswordProcess(request.EnteredUsername, isLocal).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
return result;
|
2014-11-09 04:18:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
|
public async Task<object> Post(ForgotPasswordPin request)
|
2014-11-09 04:18:14 +01:00
|
|
|
|
{
|
2018-09-12 19:26:21 +02:00
|
|
|
|
var result = await _userManager.RedeemPasswordResetPin(request.Pin).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
return result;
|
2014-11-09 04:18:14 +01:00
|
|
|
|
}
|
2014-12-19 05:20:07 +01:00
|
|
|
|
|
|
|
|
|
public void Post(UpdateUserConfiguration request)
|
|
|
|
|
{
|
2017-04-21 22:03:07 +02:00
|
|
|
|
AssertCanUpdateUser(_authContext, _userManager, request.Id, false);
|
2015-01-29 07:06:24 +01:00
|
|
|
|
|
2017-08-27 02:32:33 +02:00
|
|
|
|
_userManager.UpdateConfiguration(request.Id, request);
|
2014-12-20 07:06:27 +01:00
|
|
|
|
|
2014-12-19 05:20:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Post(UpdateUserPolicy request)
|
2014-12-20 07:06:27 +01:00
|
|
|
|
{
|
|
|
|
|
var user = _userManager.GetUserById(request.Id);
|
2017-08-27 02:32:33 +02:00
|
|
|
|
|
2014-12-20 07:06:27 +01: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 15:41:24 +01:00
|
|
|
|
var currentToken = _authContext.GetAuthorizationInfo(Request).Token;
|
2018-09-12 19:26:21 +02:00
|
|
|
|
_sessionMananger.RevokeUserTokens(user.Id, currentToken);
|
2014-12-20 07:06:27 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-27 02:32:33 +02:00
|
|
|
|
_userManager.UpdateUserPolicy(request.Id, request);
|
2014-12-20 07:06:27 +01:00
|
|
|
|
}
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
}
|