Skip to content

Instantly share code, notes, and snippets.

@nojimage
Created June 11, 2010 06:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nojimage/434135 to your computer and use it in GitHub Desktop.
Save nojimage/434135 to your computer and use it in GitHub Desktop.
Extend Acl Behavior
<?php
App::import('Behavior', 'Acl');
/**
* Extend Acl Behavior
*
* PHP versions 4, 5
*
* Copyright 2011, ELASTIC Consultants Inc. (http://elasticconsultants.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @version 1.1
* @author nojimage <nojima at elasticconsultants.com>
* @copyright 2011, ELASTIC Consultants Inc.
* @link http://elasticconsultants.com
* @package elastic_kit
* @subpackage elastic_kit.models.behaviors
* @since ElasticKit 1.1
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*
* This Behavior extent AclBehavior.
* * add parentNode()
* * set alias field
*
* */
class AclPlusBehavior extends AclBehavior {
var $defaults = array(
'type' => 'requester',
'aliasField' => false,
'parentNodeType' => null,
'parentNodeModel' => null,
'parentNodeForeignKey' => null,
);
/**
* Maps ACL type options to ACL models
*
* @var array
* @access protected
*/
var $__typeMaps = array(
'requester' => 'Aro', 'controlled' => 'Aco',
'aro' => 'Aro', 'aco' => 'Aco',
);
/**
* Sets up the configuation for the model, and loads ACL models if they haven't been already
*
* @param AppModel $model
* @param mixed $config
* type: requester or controlled, or aro or aco.
* aliasField: If this field name is setting, set field value to Aro/Aco alias.
* parentNodeType: root or model. (default: root) 'root': always parentNode() return null, 'model': parentNode() return parent Model.
* parentNodeModel: parent model name. (default: Group)
* parentNodeForeignKey: parent model forein key. (default: group_id)
* @return void
* @access public
*/
function setup(&$model, $config = array()) {
if (is_string($config)) {
$config = array('type' => $config);
}
// -- get from belognsTo
if (empty($config['parentNodeModel']) && !empty($model->belongsTo)) {
foreach ($model->belongsTo as $key => $belongsTo) {
$config['parentNodeModel'] = $belongsTo['className'];
$config['parentNodeForeignKey'] = $belongsTo['foreignKey'];
break;
}
}
$this->settings[$model->name] = array_merge($this->defaults, (array) $config);
$this->settings[$model->name]['type'] = strtolower($this->settings[$model->name]['type']);
$type = $this->__typeMaps[$this->settings[$model->name]['type']];
if (!class_exists('AclNode')) {
require LIBS . 'model' . DS . 'db_acl.php';
}
if (PHP5) {
$model->{$type} = ClassRegistry::init($type);
} else {
$model->{$type} = & ClassRegistry::init($type);
}
}
/**
* Creates a new ARO/ACO node bound to this record
*
* @param AppModel $model
* @param boolean $created True if this is a new record
* @return void
* @access public
*/
function afterSave(&$model, $created) {
$type = $this->__typeMaps[$this->settings[$model->name]['type']];
$parent = $model->parentNode();
if (!empty($parent)) {
$parent = $this->node($model, $parent);
}
$data = array(
'parent_id' => isset($parent[0][$type]['id']) ? $parent[0][$type]['id'] : null,
'model' => $model->name,
'foreign_key' => $model->id
);
if (!$created) {
$node = $this->node($model);
$data['id'] = isset($node[0][$type]['id']) ? $node[0][$type]['id'] : null;
}
// -- if set aliasField, set alias value
if ($this->settings[$model->name]['aliasField'] !== false
&& !empty($model->data[$model->alias][$this->settings[$model->name]['aliasField']])) {
$data['alias'] = $model->data[$model->alias][$this->settings[$model->name]['aliasField']];
}
$model->{$type}->create();
$model->{$type}->save($data);
}
/**
* get parent node
*
* @param AppModel $model
* @return array|null
*/
function parentNode(&$model) {
extract($this->settings[$model->name]);
if (strtolower($parentNodeType) == 'root' || empty($parentNodeModel)) {
// -- If this model root node
return null;
}
if (!$model->id && empty($model->data)) {
return null;
}
$parentId = isset($model->data[$model->alias][$parentNodeForeignKey]) ? $model->data[$model->alias][$parentNodeForeignKey] : null;
if (empty($parentId)) {
$parentId = $model->field($parentNodeForeignKey,
array($model->alias . '.' . $model->primaryKey => $model->id));
}
if (empty($parentId)) {
return null;
} else {
$parentModel = ClassRegistry::init($parentNodeModel);
return array($parentNodeModel => array($parentModel->primaryKey => $parentId));
}
}
}
@nojimage
Copy link
Author

CakePHP 1.3.8の修正に伴う変更を行いました。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment