2021-01-12 16:15:19 +01:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2020-05-12 10:21:40 +10:00
|
|
|
|
using Umbraco.Core;
|
2020-08-21 14:52:47 +01:00
|
|
|
|
using Umbraco.Core.Models;
|
2020-05-12 10:21:40 +10:00
|
|
|
|
using Umbraco.Core.Models.Membership;
|
2020-10-23 14:18:53 +11:00
|
|
|
|
using Umbraco.Core.Security;
|
2020-05-19 09:52:58 +02:00
|
|
|
|
using Umbraco.Core.Services;
|
2020-05-25 23:15:32 +10:00
|
|
|
|
using Umbraco.Extensions;
|
2020-05-12 10:21:40 +10:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.Common.Security
|
|
|
|
|
|
{
|
2020-11-27 13:33:01 +01:00
|
|
|
|
// TODO: This is only for the back office, does it need to be in common?
|
2020-09-22 10:01:00 +02: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
|
|
|
|
|
2021-01-12 16:15:19 +01:00
|
|
|
|
private object _currentUserLock = new object();
|
|
|
|
|
|
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();
|
2020-06-02 14:46:58 +10:00
|
|
|
|
return identity == null ? Attempt.Fail<int>() : Attempt.Succeed(identity.Id);
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|