I’m looking for some way to list the controllers and actions of my application dynamically.
there are some way to make this?
eg: I have a controller called "application" and a into it I have two actions "index" and "about", how can I list it into a hash like array("application" => "index, about")?
You need to come up with your own solution. What i would do: Read in protected/controller, and check all /.*Controller.php/ files. Then load them and use PHP’s reflection to inspect the classes. I don’t know, what you aim for, but that should give you a start.
Hi GodFather, I’ll get you started! this is the simplest, you can definitely use PHP reflection, thats exactly how MetaData extension does it and gives much more information than just running get_class_methods() http://php.net/manual/en/book.reflection.php
try this
$declaredClasses = get_declared_classes();
foreach (glob(Yii::getPathOfAlias('application.controllers') . "/*Controller.php") as $controller){
$class = basename($controller, ".php");
if (!in_array($class, $declaredClasses))
Yii::import("application.controllers." . $class, true);
//if you want to use reflection
$reflection = new ReflectionClass($class);
$methods = $reflection->getMethods();
//uncomment this if you want to get the class methods with more details
//print_r($methods);
//uncomment this if you want to get the class methods
//print_r(get_class_methods($class));
}
//you should see a list of all Controllers/Models
print_r(get_declared_classes());
I thought to use the tips of Sniper and boydzethuong.
The idea is make a rule based access system different of default yii, I intend to do this dynamic, where I’ll can create groups and set rules to each group separately.
My idea is:
create a user interface, where the admin user can create groups, this interface has a list of all controllers and actions
the user select this actions and controllers and save this on a database
on user manager area the admin can set the group to each user
I did the same thing… inspired on Alternate RBAC… and the other RBAC extensions but the code and ui is still pretty rough…
A filter in the controller will run CheckAccess on the user current route.
The only problem that I have stumbled on is when using bizrules for example edit own… You will have 2 routes. And it will try to execute both rules. In practice I haven’t found this to be a problem though but if you require many rules with different bizrules for the same route you could run into issues
The code above will add all routes to the database.
some times passed, have you accomplished you goal, recently i faced the same things ;so need some help, could you describe what 's your final approach to get it?
Please find the below code to get the list of controllers in the YII application :
$appControllerPath = Yii::getPathOfAlias('application.controllers');
//checking existence of controllers directory
if(is_dir($appControllerPath))
$fileLists = CFileHelper::findFiles($appControllerPath);
foreach($fileLists as $controllerPath)
{
//getting controller name like e.g. 'siteController.php'
$controllerName = substr($controllerPath, strrpos($controllerPath, DIRECTORY_SEPARATOR)+1,-4);
}
I have recently completed my custom user authentication module for the requirements like you have mentioned,
Please find my appraoch as follows:
Implemented user authentication module by that enables admin to create user roles, tasks and operation.
These auth items could be further assigned as a child to other auth item.
One can assign auth items created above directly to user also.
Automated list of controllers and their action, that gives you to save them as auth item into the application.
Here I will suggest you to save their name as prefix of theri alias (replace "." with "_").
For example : "op_controllers_site_index"
Either check for access in each action or use beforeAction method of Controller component at application.components.controller in that you can check for authorization in generalized way as follows:
Note : For above first three steps, you should refer user authentication system of FULL CMS, they have also implemented in the almost same way.
function beforeAction($action){
$accessRule = 'op_controllers_'.Yii::app()->controller->id.'_'.$action->id;
if(Yii::app()->user->checkAccess($accessRule)){
return true;
}else{
//do something here to show error that you have no access to this page
}
}
Hope this might help you, well it is not a example with 100% code, you can ask me for that also, if you need.