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

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