Created
January 21, 2011 15:13
-
-
Save jaredhoyt/789805 to your computer and use it in GitHub Desktop.
BuildAclShell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
App::import('Core', 'Controller'); | |
class BuildAclShell extends Shell { | |
function main() { | |
$log = array(); | |
App::import('Component', 'Acl'); | |
$acl =& new AclComponent(null); | |
$aco =& $acl->Aco; | |
$root = $aco->node('controllers'); | |
if (!$root) { | |
$aco->create(array('parent_id' => null, 'model' => null, 'alias' => 'controllers')); | |
$root = $aco->save(); | |
$root['Aco']['id'] = $aco->id; | |
$log[] = 'Created Aco node for controllers'; | |
} else { | |
$root = $root[0]; | |
} | |
App::import('Core', 'File'); | |
$Controllers = App::objects('controller'); | |
$appIndex = array_search('App', $Controllers); | |
if ($appIndex !== false ) { | |
unset($Controllers[$appIndex]); | |
} | |
$baseMethods = get_class_methods('Controller'); | |
$baseMethods[] = 'buildAcl'; | |
// look at each controller in app/controllers | |
foreach ($Controllers as $ctrlName) { | |
App::import('Controller', $ctrlName); | |
$ctrlclass = $ctrlName . 'Controller'; | |
$methods = get_class_methods($ctrlclass); | |
// find / make controller node | |
$controllerNode = $aco->node('controllers/'.$ctrlName); | |
if (!$controllerNode) { | |
$aco->create(array('parent_id' => $root['Aco']['id'], 'model' => null, 'alias' => $ctrlName)); | |
$controllerNode = $aco->save(); | |
$controllerNode['Aco']['id'] = $aco->id; | |
$log[] = 'Created Aco node for '.$ctrlName; | |
} else { | |
$controllerNode = $controllerNode[0]; | |
} | |
//clean the methods. to remove those in Controller and private actions. | |
foreach ($methods as $k => $method) { | |
if (strpos($method, '_', 0) === 0) { | |
unset($methods[$k]); | |
continue; | |
} | |
if (in_array($method, $baseMethods)) { | |
unset($methods[$k]); | |
continue; | |
} | |
$methodNode = $aco->node('controllers/'.$ctrlName.'/'.$method); | |
if (!$methodNode) { | |
$aco->create(array('parent_id' => $controllerNode['Aco']['id'], 'model' => null, 'alias' => $method)); | |
$methodNode = $aco->save(); | |
$log[] = 'Created Aco node for '. $method; | |
} | |
} | |
} | |
debug($log); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment