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.3 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.3
      • 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

  • AclBehavior
  • ContainableBehavior
  • TranslateBehavior
  • TreeBehavior
  1: <?php
  2: /**
  3:  * ACL behavior class.
  4:  *
  5:  * Enables objects to easily tie into an ACL system
  6:  *
  7:  * PHP 5
  8:  *
  9:  * CakePHP :  Rapid Development Framework (http://cakephp.org)
 10:  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 11:  *
 12:  * Licensed under The MIT License
 13:  * For full copyright and license information, please see the LICENSE.txt
 14:  * Redistributions of files must retain the above copyright notice.
 15:  *
 16:  * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 17:  * @link          http://cakephp.org CakePHP Project
 18:  * @package       Cake.Model.Behavior
 19:  * @since         CakePHP v 1.2.0.4487
 20:  * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 21:  */
 22: 
 23: App::uses('ModelBehavior', 'Model');
 24: App::uses('AclNode', 'Model');
 25: App::uses('Hash', 'Utility');
 26: 
 27: /**
 28:  * ACL behavior
 29:  *
 30:  * Enables objects to easily tie into an ACL system
 31:  *
 32:  * @package       Cake.Model.Behavior
 33:  * @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/acl.html
 34:  */
 35: class AclBehavior extends ModelBehavior {
 36: 
 37: /**
 38:  * Maps ACL type options to ACL models
 39:  *
 40:  * @var array
 41:  */
 42:     protected $_typeMaps = array('requester' => 'Aro', 'controlled' => 'Aco', 'both' => array('Aro', 'Aco'));
 43: 
 44: /**
 45:  * Sets up the configuration for the model, and loads ACL models if they haven't been already
 46:  *
 47:  * @param Model $model
 48:  * @param array $config
 49:  * @return void
 50:  */
 51:     public function setup(Model $model, $config = array()) {
 52:         if (isset($config[0])) {
 53:             $config['type'] = $config[0];
 54:             unset($config[0]);
 55:         }
 56:         $this->settings[$model->name] = array_merge(array('type' => 'controlled'), $config);
 57:         $this->settings[$model->name]['type'] = strtolower($this->settings[$model->name]['type']);
 58: 
 59:         $types = $this->_typeMaps[$this->settings[$model->name]['type']];
 60: 
 61:         if (!is_array($types)) {
 62:             $types = array($types);
 63:         }
 64:         foreach ($types as $type) {
 65:             $model->{$type} = ClassRegistry::init($type);
 66:         }
 67:         if (!method_exists($model, 'parentNode')) {
 68:             trigger_error(__d('cake_dev', 'Callback parentNode() not defined in %s', $model->alias), E_USER_WARNING);
 69:         }
 70:     }
 71: 
 72: /**
 73:  * Retrieves the Aro/Aco node for this model
 74:  *
 75:  * @param Model $model
 76:  * @param string|array|Model $ref Array with 'model' and 'foreign_key', model object, or string value
 77:  * @param string $type Only needed when Acl is set up as 'both', specify 'Aro' or 'Aco' to get the correct node
 78:  * @return array
 79:  * @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/acl.html#node
 80:  */
 81:     public function node(Model $model, $ref = null, $type = null) {
 82:         if (empty($type)) {
 83:             $type = $this->_typeMaps[$this->settings[$model->name]['type']];
 84:             if (is_array($type)) {
 85:                 trigger_error(__d('cake_dev', 'AclBehavior is setup with more then one type, please specify type parameter for node()'), E_USER_WARNING);
 86:                 return null;
 87:             }
 88:         }
 89:         if (empty($ref)) {
 90:             $ref = array('model' => $model->name, 'foreign_key' => $model->id);
 91:         }
 92:         return $model->{$type}->node($ref);
 93:     }
 94: 
 95: /**
 96:  * Creates a new ARO/ACO node bound to this record
 97:  *
 98:  * @param Model $model
 99:  * @param boolean $created True if this is a new record
100:  * @return void
101:  */
102:     public function afterSave(Model $model, $created) {
103:         $types = $this->_typeMaps[$this->settings[$model->name]['type']];
104:         if (!is_array($types)) {
105:             $types = array($types);
106:         }
107:         foreach ($types as $type) {
108:             $parent = $model->parentNode();
109:             if (!empty($parent)) {
110:                 $parent = $this->node($model, $parent, $type);
111:             }
112:             $data = array(
113:                 'parent_id' => isset($parent[0][$type]['id']) ? $parent[0][$type]['id'] : null,
114:                 'model' => $model->name,
115:                 'foreign_key' => $model->id
116:             );
117:             if (!$created) {
118:                 $node = $this->node($model, null, $type);
119:                 $data['id'] = isset($node[0][$type]['id']) ? $node[0][$type]['id'] : null;
120:             }
121:             $model->{$type}->create();
122:             $model->{$type}->save($data);
123:         }
124:     }
125: 
126: /**
127:  * Destroys the ARO/ACO node bound to the deleted record
128:  *
129:  * @param Model $model
130:  * @return void
131:  */
132:     public function afterDelete(Model $model) {
133:         $types = $this->_typeMaps[$this->settings[$model->name]['type']];
134:         if (!is_array($types)) {
135:             $types = array($types);
136:         }
137:         foreach ($types as $type) {
138:             $node = Hash::extract($this->node($model, null, $type), "0.{$type}.id");
139:             if (!empty($node)) {
140:                 $model->{$type}->delete($node);
141:             }
142:         }
143:     }
144: 
145: }
146: 
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