using System; using System.Linq; using System.Web; using System.Web.Security; using Umbraco.Core; using Umbraco.Web.Security; using umbraco; using umbraco.BasePages; using umbraco.BusinessLogic; using umbraco.businesslogic.Exceptions; namespace Umbraco.Web.WebServices { public abstract class UmbracoAuthorizedHttpHandler : UmbracoHttpHandler { protected UmbracoAuthorizedHttpHandler() : base() { } protected UmbracoAuthorizedHttpHandler(UmbracoContext umbracoContext) : base(umbracoContext) { } 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, UmbracoUser); if (!hasAccess && throwExceptions) throw new UserAuthorizationException("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, User user) { return Security.UserHasAppAccess(app, user); } /// /// Checks if the specified user by username as access to the app /// /// /// /// protected bool UserHasAppAccess(string app, string username) { return Security.UserHasAppAccess(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; } /// /// Returns the current user /// [Obsolete("This should no longer be used since it returns the legacy user object, use The Security.CurrentUser instead to return the proper user object")] protected User UmbracoUser { get { if (!_hasValidated) { Security.ValidateCurrentUser(); _hasValidated = true; } return new User(Security.CurrentUser); } } } }