using System;
using System.Security;
using Umbraco.Core;
using Umbraco.Core.Cache;
using Umbraco.Web.Security;
using Umbraco.Core.Models.Membership;
using Umbraco.Core.Services;
namespace Umbraco.Web
{
public abstract class UmbracoAuthorizedHttpHandler : UmbracoHttpHandler
{
protected UmbracoAuthorizedHttpHandler()
{ }
protected UmbracoAuthorizedHttpHandler(UmbracoContext umbracoContext, ServiceContext services, CacheHelper appCache)
: base(umbracoContext, services, appCache)
{
}
private bool _hasValidated = false;
///
/// Checks if the umbraco context id is valid
///
///
///
protected bool ValidateUserContextId(string currentUmbracoUserContextId)
{
return UmbracoContext.Security.ValidateCurrentUser();
}
///
/// Checks if the username/password credentials are valid
///
///
///
///
protected bool ValidateCredentials(string username, string password)
{
return UmbracoContext.Security.ValidateBackOfficeCredentials(username, password);
}
///
/// Validates the user for access to a certain application
///
/// The application alias.
/// true if an exception should be thrown if authorization fails
///
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;
}
var hasAccess = UserHasAppAccess(app, Security.CurrentUser);
if (!hasAccess && throwExceptions)
throw new SecurityException("The user does not have access to the required application");
return hasAccess;
}
///
/// Checks if the specified user as access to the app
///
///
///
///
protected bool UserHasAppAccess(string app, IUser user)
{
return Security.UserHasSectionAccess(app, user);
}
///
/// Checks if the specified user by username as access to the app
///
///
///
///
protected bool UserHasAppAccess(string app, string username)
{
return Security.UserHasSectionAccess(app, username);
}
///
/// Returns true if there is a valid logged in user and that ssl is enabled if required
///
/// true if an exception should be thrown if authorization fails
///
protected bool AuthorizeRequest(bool throwExceptions = false)
{
var result = Security.AuthorizeRequest(throwExceptions);
return result == ValidateRequestAttempt.Success;
}
}
}