2021-03-04 15:07:54 +11:00
|
|
|
using Microsoft.AspNetCore.Http;
|
2021-02-18 11:06:02 +01:00
|
|
|
using Umbraco.Cms.Core;
|
|
|
|
|
using Umbraco.Cms.Core.Models.Membership;
|
|
|
|
|
using Umbraco.Cms.Core.Security;
|
|
|
|
|
using Umbraco.Cms.Core.Services;
|
2020-05-25 23:15:32 +10:00
|
|
|
using Umbraco.Extensions;
|
2020-05-12 10:21:40 +10:00
|
|
|
|
2021-02-18 11:06:02 +01:00
|
|
|
namespace Umbraco.Cms.Web.Common.Security
|
2020-05-12 10:21:40 +10:00
|
|
|
{
|
2020-10-23 14:18:53 +11:00
|
|
|
public class BackOfficeSecurity : IBackOfficeSecurity
|
2020-05-12 10:21:40 +10:00
|
|
|
{
|
2020-05-19 09:52:58 +02:00
|
|
|
private readonly IUserService _userService;
|
2020-05-25 23:15:32 +10:00
|
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
2020-05-19 09:52:58 +02:00
|
|
|
|
Implement password config storage for members (#10170)
* Getting new netcore PublicAccessChecker in place
* Adds full test coverage for PublicAccessChecker
* remove PublicAccessComposer
* adjust namespaces, ensure RoleManager works, separate public access controller, reduce content controller
* Implements the required methods on IMemberManager, removes old migrated code
* Updates routing to be able to re-route, Fixes middleware ordering ensuring endpoints are last, refactors pipeline options, adds public access middleware, ensures public access follows all hops
* adds note
* adds note
* Cleans up ext methods, ensures that members identity is added on both front-end and back ends. updates how UmbracoApplicationBuilder works in that it explicitly starts endpoints at the time of calling.
* Changes name to IUmbracoEndpointBuilder
* adds note
* Fixing tests, fixing error describers so there's 2x one for back office, one for members, fixes TryConvertTo, fixes login redirect
* fixing build
* Updates user manager to correctly validate password hashing and injects the IBackOfficeUserPasswordChecker
* Merges PR
* Fixes up build and notes
* Implements security stamp and email confirmed for members, cleans up a bunch of repo/service level member groups stuff, shares user store code between members and users and fixes the user identity object so we arent' tracking both groups and roles.
* Security stamp for members is now working
* Fixes keepalive, fixes PublicAccessMiddleware to not throw, updates startup code to be more clear and removes magic that registers middleware.
* adds note
* removes unused filter, fixes build
* fixes WebPath and tests
* Looks up entities in one query
* remove usings
* Fix test, remove stylesheet
* Set status code before we write to response to avoid error
* Ensures that users and members are validated when logging in. Shares more code between users and members.
* merge changes
* oops
* Reducing and removing published member cache
* Fixes RepositoryCacheKeys to ensure the keys are normalized
* oops didn't mean to commit this
* Fix casing issues with caching, stop boxing value types for all cache operations, stop re-creating string keys in DefaultRepositoryCachePolicy
* oops didn't mean to comit this
* bah, far out this keeps getting recommitted. sorry
* cannot inject IPublishedMemberCache and cannot have IPublishedMember
* splits out files, fixes build
* fix tests
* removes membership provider classes
* removes membership provider classes
* updates the identity map definition
* reverts commented out lines
* reverts commented out lines
* Implements members Password config in db, fixes members cookie auth to not interfere with the back office cookie auth, fixes Startup sequence, fixes startup pipeline
* commits change to Startup
* Rename migration from `MemberTableColumns2` to `AddPasswordConfigToMemberTable`
* Fix test
* Fix tests, but adding default passwordConfig to members
Co-authored-by: Bjarke Berg <mail@bergmania.dk>
2021-04-22 23:59:13 +10:00
|
|
|
private readonly object _currentUserLock = new object();
|
2021-01-12 16:15:19 +01:00
|
|
|
private IUser _currentUser;
|
|
|
|
|
|
2020-10-23 14:18:53 +11:00
|
|
|
public BackOfficeSecurity(
|
2020-06-02 13:28:30 +10:00
|
|
|
IUserService userService,
|
|
|
|
|
IHttpContextAccessor httpContextAccessor)
|
2020-05-19 09:52:58 +02:00
|
|
|
{
|
|
|
|
|
_userService = userService;
|
2020-05-25 23:15:32 +10:00
|
|
|
_httpContextAccessor = httpContextAccessor;
|
2020-05-19 09:52:58 +02:00
|
|
|
}
|
|
|
|
|
|
2021-01-12 16:15:19 +01:00
|
|
|
|
2020-05-19 09:52:58 +02:00
|
|
|
|
2020-06-02 14:46:58 +10:00
|
|
|
/// <inheritdoc />
|
2020-05-19 09:52:58 +02:00
|
|
|
public IUser CurrentUser
|
2020-05-18 15:19:52 +02:00
|
|
|
{
|
2020-05-19 09:52:58 +02:00
|
|
|
get
|
|
|
|
|
{
|
2021-01-12 16:15:19 +01:00
|
|
|
|
2020-05-19 09:52:58 +02:00
|
|
|
//only load it once per instance! (but make sure groups are loaded)
|
|
|
|
|
if (_currentUser == null)
|
|
|
|
|
{
|
2021-01-12 16:15:19 +01:00
|
|
|
lock (_currentUserLock)
|
|
|
|
|
{
|
|
|
|
|
//Check again
|
|
|
|
|
if (_currentUser == null)
|
|
|
|
|
{
|
|
|
|
|
var id = GetUserId();
|
|
|
|
|
_currentUser = id ? _userService.GetUserById(id.Result) : null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-19 09:52:58 +02:00
|
|
|
}
|
2020-05-18 15:19:52 +02:00
|
|
|
|
2020-05-19 09:52:58 +02:00
|
|
|
return _currentUser;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-12 10:21:40 +10:00
|
|
|
|
2020-06-02 14:46:58 +10:00
|
|
|
/// <inheritdoc />
|
2020-05-12 10:21:40 +10:00
|
|
|
public Attempt<int> GetUserId()
|
|
|
|
|
{
|
2020-10-05 10:02:11 +02:00
|
|
|
var identity = _httpContextAccessor.HttpContext?.GetCurrentIdentity();
|
2021-02-17 10:11:04 +01:00
|
|
|
return identity == null ? Attempt.Fail<int>() : Attempt.Succeed(identity.GetId());
|
2020-05-12 10:21:40 +10:00
|
|
|
}
|
|
|
|
|
|
2020-06-02 14:46:58 +10:00
|
|
|
/// <inheritdoc />
|
2020-05-12 10:21:40 +10:00
|
|
|
public bool IsAuthenticated()
|
|
|
|
|
{
|
2020-05-25 23:15:32 +10:00
|
|
|
var httpContext = _httpContextAccessor.HttpContext;
|
|
|
|
|
return httpContext?.User != null && httpContext.User.Identity.IsAuthenticated && httpContext.GetCurrentIdentity() != null;
|
2020-05-12 10:21:40 +10:00
|
|
|
}
|
|
|
|
|
|
2020-06-02 14:46:58 +10:00
|
|
|
/// <inheritdoc />
|
2020-05-12 10:21:40 +10:00
|
|
|
public bool UserHasSectionAccess(string section, IUser user)
|
|
|
|
|
{
|
2020-06-02 14:46:58 +10:00
|
|
|
return user.HasSectionAccess(section);
|
2020-05-12 10:21:40 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|