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

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