CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 2.2 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.2
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
      • Validator
    • Network
      • Email
      • Http
    • Routing
      • Filter
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

  • ActionsAuthorize
  • BaseAuthenticate
  • BaseAuthorize
  • BasicAuthenticate
  • ControllerAuthorize
  • CrudAuthorize
  • DigestAuthenticate
  • FormAuthenticate
  1: <?php
  2: /**
  3:  * PHP 5
  4:  *
  5:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6:  * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  7:  *
  8:  * Licensed under The MIT License
  9:  * Redistributions of files must retain the above copyright notice.
 10:  *
 11:  * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 12:  * @link          http://cakephp.org CakePHP(tm) Project
 13:  * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 14:  */
 15: 
 16: App::uses('BaseAuthenticate', 'Controller/Component/Auth');
 17: 
 18: /**
 19:  * Basic Authentication adapter for AuthComponent.
 20:  *
 21:  * Provides Basic HTTP authentication support for AuthComponent.  Basic Auth will authenticate users
 22:  * against the configured userModel and verify the username and passwords match.  Clients using Basic Authentication
 23:  * must support cookies.  Since AuthComponent identifies users based on Session contents, clients using Basic
 24:  * Auth must support cookies.
 25:  *
 26:  * ### Using Basic auth
 27:  *
 28:  * In your controller's components array, add auth + the required settings.
 29:  * {{{
 30:  *  public $components = array(
 31:  *      'Auth' => array(
 32:  *          'authenticate' => array('Basic')
 33:  *      )
 34:  *  );
 35:  * }}}
 36:  *
 37:  * In your login function just call `$this->Auth->login()` without any checks for POST data.  This
 38:  * will send the authentication headers, and trigger the login dialog in the browser/client.
 39:  *
 40:  * @package       Cake.Controller.Component.Auth
 41:  * @since 2.0
 42:  */
 43: class BasicAuthenticate extends BaseAuthenticate {
 44: 
 45: /**
 46:  * Settings for this object.
 47:  *
 48:  * - `fields` The fields to use to identify a user by.
 49:  * - `userModel` The model name of the User, defaults to User.
 50:  * - `scope` Additional conditions to use when looking up and authenticating users,
 51:  *    i.e. `array('User.is_active' => 1).`
 52:  * - `recursive` The value of the recursive key passed to find(). Defaults to 0.
 53:  * - `contain` Extra models to contain and store in session.
 54:  * - `realm` The realm authentication is for.  Defaults the server name.
 55:  *
 56:  * @var array
 57:  */
 58:     public $settings = array(
 59:         'fields' => array(
 60:             'username' => 'username',
 61:             'password' => 'password'
 62:         ),
 63:         'userModel' => 'User',
 64:         'scope' => array(),
 65:         'recursive' => 0,
 66:         'contain' => null,
 67:         'realm' => '',
 68:     );
 69: 
 70: /**
 71:  * Constructor, completes configuration for basic authentication.
 72:  *
 73:  * @param ComponentCollection $collection The Component collection used on this request.
 74:  * @param array $settings An array of settings.
 75:  */
 76:     public function __construct(ComponentCollection $collection, $settings) {
 77:         parent::__construct($collection, $settings);
 78:         if (empty($this->settings['realm'])) {
 79:             $this->settings['realm'] = env('SERVER_NAME');
 80:         }
 81:     }
 82: 
 83: /**
 84:  * Authenticate a user using basic HTTP auth.  Will use the configured User model and attempt a
 85:  * login using basic HTTP auth.
 86:  *
 87:  * @param CakeRequest $request The request to authenticate with.
 88:  * @param CakeResponse $response The response to add headers to.
 89:  * @return mixed Either false on failure, or an array of user data on success.
 90:  */
 91:     public function authenticate(CakeRequest $request, CakeResponse $response) {
 92:         $result = $this->getUser($request);
 93: 
 94:         if (empty($result)) {
 95:             $response->header($this->loginHeaders());
 96:             $response->statusCode(401);
 97:             $response->send();
 98:             return false;
 99:         }
100:         return $result;
101:     }
102: 
103: /**
104:  * Get a user based on information in the request.  Used by cookie-less auth for stateless clients.
105:  *
106:  * @param CakeRequest $request Request object.
107:  * @return mixed Either false or an array of user information
108:  */
109:     public function getUser($request) {
110:         $username = env('PHP_AUTH_USER');
111:         $pass = env('PHP_AUTH_PW');
112: 
113:         if (empty($username) || empty($pass)) {
114:             return false;
115:         }
116:         return $this->_findUser($username, $pass);
117:     }
118: 
119: /**
120:  * Generate the login headers
121:  *
122:  * @return string Headers for logging in.
123:  */
124:     public function loginHeaders() {
125:         return sprintf('WWW-Authenticate: Basic realm="%s"', $this->settings['realm']);
126:     }
127: 
128: }
129: 
OpenHub
Rackspace
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (GitHub)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • Slack
  • Paid Support

Generated using CakePHP API Docs