1: <?php
2: /**
3: * PHP 5
4: *
5: * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
6: * Copyright 2005-2011, 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-2011, 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: /**
17: * Abstract base authorization adapter for AuthComponent.
18: *
19: * @package Cake.Controller.Component.Auth
20: * @since 2.0
21: * @see AuthComponent::$authenticate
22: */
23: abstract class BaseAuthorize {
24: /**
25: * Controller for the request.
26: *
27: * @var Controller
28: */
29: protected $_Controller = null;
30:
31: /**
32: * Component collection instance for getting more components.
33: *
34: * @var ComponentCollection
35: */
36: protected $_Collection;
37:
38: /**
39: * Settings for authorize objects.
40: *
41: * - `actionPath` - The path to ACO nodes that contains the nodes for controllers. Used as a prefix
42: * when calling $this->action();
43: * - `actionMap` - Action -> crud mappings. Used by authorization objects that want to map actions to CRUD roles.
44: * - `userModel` - Model name that ARO records can be found under. Defaults to 'User'.
45: *
46: * @var array
47: */
48: public $settings = array(
49: 'actionPath' => null,
50: 'actionMap' => array(
51: 'index' => 'read',
52: 'add' => 'create',
53: 'edit' => 'update',
54: 'view' => 'read',
55: 'delete' => 'delete',
56: 'remove' => 'delete'
57: ),
58: 'userModel' => 'User'
59: );
60:
61: /**
62: * Constructor
63: *
64: * @param ComponentCollection $collection The controller for this request.
65: * @param string $settings An array of settings. This class does not use any settings.
66: */
67: public function __construct(ComponentCollection $collection, $settings = array()) {
68: $this->_Collection = $collection;
69: $controller = $collection->getController();
70: $this->controller($controller);
71: $this->settings = Set::merge($this->settings, $settings);
72: }
73:
74: /**
75: * Checks user authorization.
76: *
77: * @param array $user Active user data
78: * @param CakeRequest $request
79: * @return boolean
80: */
81: abstract public function authorize($user, CakeRequest $request);
82:
83: /**
84: * Accessor to the controller object.
85: *
86: * @param mixed $controller null to get, a controller to set.
87: * @return mixed
88: * @throws CakeException
89: */
90: public function controller($controller = null) {
91: if ($controller) {
92: if (!$controller instanceof Controller) {
93: throw new CakeException(__d('cake_dev', '$controller needs to be an instance of Controller'));
94: }
95: $this->_Controller = $controller;
96: return true;
97: }
98: return $this->_Controller;
99: }
100:
101: /**
102: * Get the action path for a given request. Primarily used by authorize objects
103: * that need to get information about the plugin, controller, and action being invoked.
104: *
105: * @param CakeRequest $request The request a path is needed for.
106: * @param string $path
107: * @return string the action path for the given request.
108: */
109: public function action($request, $path = '/:plugin/:controller/:action') {
110: $plugin = empty($request['plugin']) ? null : Inflector::camelize($request['plugin']) . '/';
111: return str_replace(
112: array(':controller', ':action', ':plugin/'),
113: array(Inflector::camelize($request['controller']), $request['action'], $plugin),
114: $this->settings['actionPath'] . $path
115: );
116: }
117:
118: /**
119: * Maps crud actions to actual action names. Used to modify or get the current mapped actions.
120: *
121: * Create additional mappings for a standard CRUD operation:
122: *
123: * {{{
124: * $this->Auth->mapActions(array('create' => array('add', 'register'));
125: * }}}
126: *
127: * Create mappings for custom CRUD operations:
128: *
129: * {{{
130: * $this->Auth->mapActions(array('my_action' => 'admin'));
131: * }}}
132: *
133: * You can use the custom CRUD operations to create additional generic permissions
134: * that behave like CRUD operations. Doing this will require additional columns on the
135: * permissions lookup. When using with DbAcl, you'll have to add additional _admin type columns
136: * to the `aros_acos` table.
137: *
138: * @param mixed $map Either an array of mappings, or undefined to get current values.
139: * @return mixed Either the current mappings or null when setting.
140: * @see AuthComponent::mapActions()
141: */
142: public function mapActions($map = array()) {
143: if (empty($map)) {
144: return $this->settings['actionMap'];
145: }
146: $crud = array('create', 'read', 'update', 'delete');
147: foreach ($map as $action => $type) {
148: if (in_array($action, $crud) && is_array($type)) {
149: foreach ($type as $typedAction) {
150: $this->settings['actionMap'][$typedAction] = $action;
151: }
152: } else {
153: $this->settings['actionMap'][$action] = $type;
154: }
155: }
156: }
157: }
158: