cakephp2-php8/lib/Cake/Controller/Component/Auth/BaseAuthorize.php

169 lines
5 KiB
PHP
Raw Normal View History

<?php
/**
2017-06-10 21:33:55 +00:00
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
2017-06-10 21:33:55 +00:00
* @link https://cakephp.org CakePHP(tm) Project
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
2012-03-11 01:57:18 +00:00
App::uses('Hash', 'Utility');
/**
* Abstract base authorization adapter for AuthComponent.
*
* @package Cake.Controller.Component.Auth
* @since 2.0
* @see AuthComponent::$authenticate
*/
abstract class BaseAuthorize {
/**
* Controller for the request.
*
* @var Controller
*/
protected $_Controller = null;
/**
* Component collection instance for getting more components.
*
* @var ComponentCollection
*/
protected $_Collection;
2011-08-16 03:55:08 +00:00
/**
* Settings for authorize objects.
*
2012-12-22 22:48:15 +00:00
* - `actionPath` - The path to ACO nodes that contains the nodes for controllers. Used as a prefix
* when calling $this->action();
* - `actionMap` - Action -> crud mappings. Used by authorization objects that want to map actions to CRUD roles.
2012-12-22 22:48:15 +00:00
* - `userModel` - Model name that ARO records can be found under. Defaults to 'User'.
*
* @var array
*/
public $settings = array(
'actionPath' => null,
'actionMap' => array(
'index' => 'read',
'add' => 'create',
'edit' => 'update',
'view' => 'read',
'delete' => 'delete',
'remove' => 'delete'
),
'userModel' => 'User'
);
/**
* Constructor
*
2011-07-29 04:06:43 +00:00
* @param ComponentCollection $collection The controller for this request.
2012-12-22 22:48:15 +00:00
* @param string $settings An array of settings. This class does not use any settings.
*/
public function __construct(ComponentCollection $collection, $settings = array()) {
$this->_Collection = $collection;
$controller = $collection->getController();
$this->controller($controller);
2012-03-11 01:57:18 +00:00
$this->settings = Hash::merge($this->settings, $settings);
}
/**
* Checks user authorization.
*
* @param array $user Active user data
2014-05-31 21:36:05 +00:00
* @param CakeRequest $request Request instance.
* @return bool
*/
abstract public function authorize($user, CakeRequest $request);
/**
* Accessor to the controller object.
*
* @param Controller $controller null to get, a controller to set.
* @return mixed
* @throws CakeException
*/
public function controller(?Controller $controller = null) {
if ($controller) {
if (!$controller instanceof Controller) {
throw new CakeException(__d('cake_dev', '$controller needs to be an instance of Controller'));
}
$this->_Controller = $controller;
return true;
}
return $this->_Controller;
}
/**
2012-12-22 22:48:15 +00:00
* Get the action path for a given request. Primarily used by authorize objects
* that need to get information about the plugin, controller, and action being invoked.
*
* @param CakeRequest $request The request a path is needed for.
2014-05-31 21:36:05 +00:00
* @param string $path Path format.
* @return string the action path for the given request.
*/
2013-02-09 12:39:11 +00:00
public function action(CakeRequest $request, $path = '/:plugin/:controller/:action') {
$plugin = empty($request['plugin']) ? null : Inflector::camelize($request['plugin']) . '/';
$path = str_replace(
array(':controller', ':action', ':plugin/'),
array(Inflector::camelize($request['controller']), $request['action'], $plugin),
$this->settings['actionPath'] . $path
);
$path = str_replace('//', '/', $path);
return trim($path, '/');
}
/**
2012-12-22 22:48:15 +00:00
* Maps crud actions to actual action names. Used to modify or get the current mapped actions.
*
* Create additional mappings for a standard CRUD operation:
*
* ```
* $this->Auth->mapActions(array('create' => array('add', 'register'));
* ```
*
* Or equivalently:
*
* ```
* $this->Auth->mapActions(array('register' => 'create', 'add' => 'create'));
* ```
*
* Create mappings for custom CRUD operations:
*
* ```
* $this->Auth->mapActions(array('range' => 'search'));
* ```
*
* You can use the custom CRUD operations to create additional generic permissions
2012-12-22 22:48:15 +00:00
* that behave like CRUD operations. Doing this will require additional columns on the
2014-05-31 21:36:05 +00:00
* permissions lookup. For example if one wanted an additional search CRUD operation
* one would create and additional column '_search' in the aros_acos table. One could
* create a custom admin CRUD operation for administration functions similarly if needed.
*
* @param array $map Either an array of mappings, or undefined to get current values.
* @return mixed Either the current mappings or null when setting.
* @see AuthComponent::mapActions()
*/
public function mapActions($map = array()) {
if (empty($map)) {
return $this->settings['actionMap'];
}
foreach ($map as $action => $type) {
if (is_array($type)) {
foreach ($type as $typedAction) {
$this->settings['actionMap'][$typedAction] = $action;
}
} else {
$this->settings['actionMap'][$action] = $type;
}
}
}
}