2013-04-09 07:01:43 +06:00
|
|
|
|
using System;
|
2015-12-22 19:25:10 +01:00
|
|
|
|
using System.Security;
|
2013-04-09 07:01:43 +06:00
|
|
|
|
using Umbraco.Core;
|
2016-09-01 19:06:08 +02:00
|
|
|
|
using Umbraco.Core.Cache;
|
2013-04-09 22:11:12 +06:00
|
|
|
|
using Umbraco.Web.Security;
|
2016-03-16 17:52:08 +01:00
|
|
|
|
using Umbraco.Core.Models.Membership;
|
2016-09-01 19:06:08 +02:00
|
|
|
|
using Umbraco.Core.Services;
|
2013-04-09 07:01:43 +06:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.WebServices
|
|
|
|
|
|
{
|
|
|
|
|
|
public abstract class UmbracoAuthorizedHttpHandler : UmbracoHttpHandler
|
|
|
|
|
|
{
|
|
|
|
|
|
protected UmbracoAuthorizedHttpHandler()
|
2016-09-01 19:06:08 +02:00
|
|
|
|
{ }
|
2013-04-09 07:01:43 +06:00
|
|
|
|
|
2016-09-01 19:06:08 +02:00
|
|
|
|
protected UmbracoAuthorizedHttpHandler(UmbracoContext umbracoContext, ServiceContext services, CacheHelper appCache)
|
|
|
|
|
|
: base(umbracoContext, services, appCache)
|
2013-04-09 07:01:43 +06:00
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-04-09 22:11:12 +06:00
|
|
|
|
private bool _hasValidated = false;
|
2013-04-09 07:01:43 +06:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Checks if the umbraco context id is valid
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="currentUmbracoUserContextId"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
protected bool ValidateUserContextId(string currentUmbracoUserContextId)
|
|
|
|
|
|
{
|
2013-07-31 17:08:56 +10:00
|
|
|
|
return UmbracoContext.Security.ValidateCurrentUser();
|
2013-04-09 07:01:43 +06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Checks if the username/password credentials are valid
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="username"></param>
|
|
|
|
|
|
/// <param name="password"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
protected bool ValidateCredentials(string username, string password)
|
|
|
|
|
|
{
|
2013-04-09 22:11:12 +06:00
|
|
|
|
return UmbracoContext.Security.ValidateBackOfficeCredentials(username, password);
|
2013-04-09 07:01:43 +06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Validates the user for access to a certain application
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="app">The application alias.</param>
|
|
|
|
|
|
/// <param name="throwExceptions">true if an exception should be thrown if authorization fails</param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
protected bool AuthorizeRequest(string app, bool throwExceptions = false)
|
|
|
|
|
|
{
|
|
|
|
|
|
//ensure we have a valid user first!
|
|
|
|
|
|
if (!AuthorizeRequest(throwExceptions)) return false;
|
|
|
|
|
|
|
|
|
|
|
|
//if it is empty, don't validate
|
|
|
|
|
|
if (app.IsNullOrWhiteSpace())
|
|
|
|
|
|
{
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2016-03-16 17:52:08 +01:00
|
|
|
|
var hasAccess = UserHasAppAccess(app, Security.CurrentUser);
|
2013-04-09 07:01:43 +06:00
|
|
|
|
if (!hasAccess && throwExceptions)
|
2015-12-22 19:25:10 +01:00
|
|
|
|
throw new SecurityException("The user does not have access to the required application");
|
2013-04-09 07:01:43 +06:00
|
|
|
|
return hasAccess;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Checks if the specified user as access to the app
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="app"></param>
|
|
|
|
|
|
/// <param name="user"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2016-03-16 17:52:08 +01:00
|
|
|
|
protected bool UserHasAppAccess(string app, IUser user)
|
2013-04-09 07:01:43 +06:00
|
|
|
|
{
|
2017-09-19 15:51:47 +02:00
|
|
|
|
return Security.UserHasSectionAccess(app, user);
|
2013-04-09 07:01:43 +06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Checks if the specified user by username as access to the app
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="app"></param>
|
|
|
|
|
|
/// <param name="username"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
protected bool UserHasAppAccess(string app, string username)
|
|
|
|
|
|
{
|
2017-09-19 15:51:47 +02:00
|
|
|
|
return Security.UserHasSectionAccess(app, username);
|
2013-04-09 07:01:43 +06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Returns true if there is a valid logged in user and that ssl is enabled if required
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="throwExceptions">true if an exception should be thrown if authorization fails</param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
protected bool AuthorizeRequest(bool throwExceptions = false)
|
|
|
|
|
|
{
|
2013-06-17 16:03:27 +10:00
|
|
|
|
var result = Security.AuthorizeRequest(throwExceptions);
|
2013-04-09 22:11:12 +06:00
|
|
|
|
return result == ValidateRequestAttempt.Success;
|
2013-04-09 07:01:43 +06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
|
}
|