Version: current

# Class BasicAuth

Provides an interface to HTTP basic authentication.

This utility class can be used to secure any request with basic authentication. To do so, `BasicAuth::requireLogin()` from your Controller's init() method or action handler method.

It also has a function to protect your entire site. See `BasicAuth::protect_entire_site()` for more information.

Package: sapphire\security
Located at sapphire/security/BasicAuth.php

## Methods summary

`public static `Member`` | # `requireLogin( string $realm, string|array $permissionCode )` Require basic authentication. Will request a username and password if none is given. Require basic authentication. Will request a username and password if none is given. Used by `Controller::init()`. #### Parameters $realm string $realm $permissionCode string|array $permissionCode #### Returns ``Member`` $member #### Used by ``BookingPage_Controller::init()`` ``Cart_Controller::init()`` ``Catalog_Controller::init()`` ``CliController::init()`` ``CodeViewer::init()`` ``ContentController::init()`` ``Controller::init()`` ``DatabaseAdmin::init()`` ``DevelopmentAdmin::init()`` ``Email_BounceHandler::init()`` ``ErrorPage_Controller::init()`` ``Import1C_Controller::init()`` ``ImportCatalog1C_Controller::init()`` ``JSTestRunner::init()`` ``MapObjectGroup_Controller::init()`` ``ModelAsController::init()`` ``ModelViewer::init()`` ``NewsHolder_Controller::init()`` ``OrderExporter::init()`` ``Orders1CExchange_Controller::init()`` ``Page_Controller::init()`` ``ProfilePage_Controller::init()`` ``RealtyXMLImportTask::init()`` ``RebuildStaticCacheTask::init()`` ``RedirectorPage_Controller::init()`` ``RegistrationPage_Controller::init()`` ``RemoveOrphanedPagesTask::init()`` ``SapphireInfo::init()`` ``Search_Controller::init()`` ``SimpleOrderPage_Controller::init()`` ``StaticExporter::init()`` ``SubsitesSelectorPage_Controller::init()`` ``TaskRunner::init()`` ``TestRunner::init()`` ``TestViewer::init()`` ``VideoBankPage_Controller::init()``
`public static` | # `protect_entire_site( mixed $protect = true )` Enable protection of the entire site with basic authentication. Enable protection of the entire site with basic authentication. This log-in uses the Member database for authentication, but doesn't interfere with the regular log-in form. This can be useful for test sites, where you want to hide the site away from prying eyes, but still be able to test the regular log-in features of the site. If you are including conf/ConfigureFromEnv.php in your _config.php file, you can also enable this feature by adding this line to your _ss_environment.php: define('SS_USE_BASIC_AUTH', true); #### Parameters $protect $protect Set this to false to disable protection.
`public static` | # `enable( )` #### Deprecated Use BasicAuth::protect_entire_site() instead.
`public static` | # `disable( )` #### Deprecated Use BasicAuth::protect_entire_site(false) instead.
`public static` | # `protect_site_if_necessary( )` Call `BasicAuth::requireLogin()` if `BasicAuth::protect_entire_site()` has been called. This is a helper function used by Controller. Call `BasicAuth::requireLogin()` if `BasicAuth::protect_entire_site()` has been called. This is a helper function used by Controller.

### Magic methods summary

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -