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