diff --git a/src/Umbraco.Core/Services/IUserService.cs b/src/Umbraco.Core/Services/IUserService.cs index b45499a76f..ffb42061a2 100644 --- a/src/Umbraco.Core/Services/IUserService.cs +++ b/src/Umbraco.Core/Services/IUserService.cs @@ -20,6 +20,12 @@ namespace Umbraco.Core.Services IUser GetUserByUserName(string username); IUser GetUserById(int id); + /// + /// Returns all users + /// + /// + IEnumerable GetAllUsers(); + /// /// Gets an IUserType by its Alias /// diff --git a/src/Umbraco.Core/Services/UserService.cs b/src/Umbraco.Core/Services/UserService.cs index 448a1f46e9..7118e5cc1e 100644 --- a/src/Umbraco.Core/Services/UserService.cs +++ b/src/Umbraco.Core/Services/UserService.cs @@ -12,6 +12,8 @@ using Umbraco.Core.Persistence.UnitOfWork; namespace Umbraco.Core.Services { + + /// /// Represents the UserService, which is an easy access to operations involving , and eventually Backoffice Users. /// @@ -62,6 +64,18 @@ namespace Umbraco.Core.Services } } + /// + /// Returns all users + /// + /// + public IEnumerable GetAllUsers() + { + using (var repository = _repositoryFactory.CreateUserRepository(_uowProvider.GetUnitOfWork())) + { + return repository.GetAll(); + } + } + public IUser GetUserById(int id) { using (var repository = _repositoryFactory.CreateUserRepository(_uowProvider.GetUnitOfWork())) @@ -69,8 +83,7 @@ namespace Umbraco.Core.Services return repository.Get(id); } } - - + /// /// Gets an IUserType by its Alias /// diff --git a/src/Umbraco.Web/Editors/UserController.cs b/src/Umbraco.Web/Editors/UserController.cs index 929ae46831..b4b30ddba9 100644 --- a/src/Umbraco.Web/Editors/UserController.cs +++ b/src/Umbraco.Web/Editors/UserController.cs @@ -1,9 +1,11 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net; using System.Text; using System.Threading.Tasks; using System.Web.Http; +using AutoMapper; using Umbraco.Web.Models.ContentEditing; using Umbraco.Web.Models.Mapping; using Umbraco.Web.Mvc; @@ -19,26 +21,29 @@ namespace Umbraco.Web.Editors [PluginController("UmbracoApi")] public class UserController : UmbracoAuthorizedJsonController { - private readonly UserModelMapper _userModelMapper; - public UserController() - : this(new UserModelMapper()) - { - } - internal UserController(UserModelMapper userModelMapper) - { - _userModelMapper = userModelMapper; - } - + /// + /// Returns a user by id + /// + /// + /// public UserDetail GetById(int id) { var user = Services.UserService.GetUserById(id); - return _userModelMapper.ToUserDetail(user); + if (user == null) + { + throw new HttpResponseException(HttpStatusCode.NotFound); + } + + return Mapper.Map(user); } - //TODO: Change to a service / repo + /// + /// Returns all active users + /// + /// public IEnumerable GetAll() { - return legacyUser.getAll().Where(x => !x.Disabled).Select(x => new UserBasic() { Name = x.Name, UserId = x.Id }); + return Services.UserService.GetAllUsers().Where(x => x.IsLockedOut == false).Select(Mapper.Map); } } } diff --git a/src/Umbraco.Web/Models/Mapping/UserModelMapper.cs b/src/Umbraco.Web/Models/Mapping/UserModelMapper.cs index 9b160f3cbd..66c552726b 100644 --- a/src/Umbraco.Web/Models/Mapping/UserModelMapper.cs +++ b/src/Umbraco.Web/Models/Mapping/UserModelMapper.cs @@ -9,8 +9,6 @@ namespace Umbraco.Web.Models.Mapping { internal class UserModelMapper : MapperConfiguration { - - #region Mapper config public override void ConfigureMappings(IConfiguration config, ApplicationContext applicationContext) { config.CreateMap() @@ -21,8 +19,7 @@ namespace Umbraco.Web.Models.Mapping config.CreateMap() .ForMember(detail => detail.UserId, opt => opt.MapFrom(profile => GetIntId(profile.Id))); } - #endregion - + private static int GetIntId(object id) { var result = id.TryConvertTo(); @@ -33,15 +30,6 @@ namespace Umbraco.Web.Models.Mapping } return result.Result; } - - public UserDetail ToUserDetail(IUser user) - { - return Mapper.Map(user); - } - - public UserBasic ToUserBasic(IProfile profile) - { - return Mapper.Map(profile); - } + } } \ No newline at end of file