Help on role based default route plugin
We modified the Mobile Default Route 0.1b by @x00 | Guest Default Route 1.0.0 by @matt to create a Role based default route plugin, that with one exception works flawlessly. For whatever reason, it is interfering with the Admin default route.
Does anyone know what we might need to change so that this only changes the default route for the specific role and not the Admin?
Here is the code:
<?php if (!defined('APPLICATION')) exit();
// Define the plugin:
$PluginInfo['DefaultRouteGuest'] = array(
'Name' => 'Default Route Guest',
'Description' => 'In Roles and Permissions give view permission to the selcted Role. In settings set a route for your homepage (effectively the default controller/view) this plugin allows a different route for guests.',
'Version' => '1.0.0',
'Author' => '2KidsinUT',
'AuthorEmail' => '2kidsinut@2kidsinut.com',
'License' => 'GPL v2',
'SettingsUrl' => '/settings/defaultrouteguest',
'SettingsPermission' => 'Garden.Settings.Manage',
'RequiredApplications' => array('Vanilla' => '>=2'),
'RegisterPermissions' => array('Plugins.DefaultRouteGuest.View'),
);
/*
* # Default Route Guest#
*
* ### Thanks to ###
* http://vanillaforums.org/addon/guestdefaultroute-plugin
* http://vanillaforums.org/addon/mobiledefaultroute-plugin
*/
class DefaultRouteGuest extends Gdn_Plugin {
public function SettingsController_DefaultRouteGuest_Create($Sender, $Args = array()) {
$Sender->Permission('Garden.Settings.Manage');
$Sender->SetData('Title', T('Default Route for Guest'));
$Cfg = new ConfigurationModule($Sender);
$Cfg->Initialize(array(
'Plugins.DefaultRouteGuest.Destination' => array('Description' => 'The default route for guests (leave blank to disable)', 'Control' => 'TextBox')
));
$Sender->AddSideMenu('dashboard/settings/plugins');
$Cfg->RenderAll();
}
public function Base_BeforeDispatch_Handler($Sender,&$Args){
if((Gdn::Session()->CheckPermission('Plugins.DefaultRouteGuest.View')) && C('Plugins.DefaultRouteGuest.Destination'))
$Routes=&Gdn::Router()->Routes;
if(GetValue('DefaultController',$Routes))
$Routes['DefaultController']['Destination']=C('Plugins.DefaultRouteGuest.Destination');
}
}
?>
We added 'RegisterPermissions' => array('Plugins.DefaultRouteGuest.View'),
to the $PluginInfo section, and changed the last public function to:
public function Base_BeforeDispatch_Handler($Sender,&$Args){ if((Gdn::Session()->CheckPermission('Plugins.DefaultRouteGuest.View')) && C('Plugins.DefaultRouteGuest.Destination')) $Routes=&Gdn::Router()->Routes; if(GetValue('DefaultController',$Routes)) $Routes['DefaultController']['Destination']=C('Plugins.DefaultRouteGuest.Destination'); }
Our Guest role is actually a member role on our forum. Guest is the only Role with view permissions for Plugins.DefaultRouteGuest.View. View permissions has been disabled/unchecked for admin role.
Comments
Does anyone know what code I can use to check for a user's role rather than permissions?
I've tried
if((Gdn::Session()->User->Role == 4) && C('Plugins.DefaultRouteGuest.Destination'))
and a few others (UserRole, RoleID, etc.) but so far nothing has worked. I took the permission based route out of desperation.I've lurked in these forums for several months, and have run across several posts where others have asked for help to accomplish something similar - create separate default routes for individual roles. With exception to the plugins by @x00 and @matt, it is unfortunate that no assistance whatsoever had been offered to those pleas for help, and no solution (if one was ever discovered) had ever been posted.
Sooo... in my own desperation to find a fix, I found a solution. It might not be the best solution, and it still addresses the problem based on permissions rather than roles... but it at least works, and I'm posting it here so that anyone with a similar need will be able to find it:
<?php if (!defined('APPLICATION')) exit(); // Define the plugin: $PluginInfo['DefaultRouteRole'] = array( 'Name' => 'Default Route Role', 'Description' => 'In Roles and Permissions remove permission to view Profiles for the selected Role. In settings set a route for your homepage (effectively the default controller/view) this plugin allows a different route for selected role.', 'Version' => '1.0.0', 'Author' => '2KidsinUT', 'AuthorEmail' => '2kidsinut@2kidsinut.com', 'License' => 'GPL v2', 'SettingsUrl' => '/settings/defaultrouterole', 'SettingsPermission' => 'Garden.Settings.Manage', 'RequiredApplications' => array('Vanilla' => '>=2'), ); /* * # Default Route Guest# * * ### Thanks to ### * http://vanillaforums.org/addon/guestdefaultroute-plugin * http://vanillaforums.org/addon/mobiledefaultroute-plugin */ class DefaultRouteRole extends Gdn_Plugin { public function SettingsController_DefaultRouteRole_Create($Sender, $Args = array()) { $Sender->Permission('Garden.Settings.Manage'); $Sender->SetData('Title', T('Default Route for Role')); $Cfg = new ConfigurationModule($Sender); $Cfg->Initialize(array( 'Plugins.DefaultRouteRole.Destination' => array('Description' => 'The default route for Role (leave blank to disable)', 'Control' => 'TextBox') )); $Sender->AddSideMenu('dashboard/settings/plugins'); $Cfg->RenderAll(); } public function Base_BeforeDispatch_Handler($Sender,&$Args){ if(!(Gdn::Session()->CheckPermission('Garden.Profiles.View')) && C('Plugins.DefaultRouteRole.Destination')) $Routes=&Gdn::Router()->Routes; if(GetValue('DefaultController',$Routes)) $Routes['DefaultController']['Destination']=C('Plugins.DefaultRouteRole.Destination'); } } ?>
Just paste the code in a text file (using a program like Notepad++) and save as class.defaultrouterole.plugin.php and shove it in a folder named DefaultRouteRole. Then upload it to the plugins folder in Vanilla Forum, browse to Roles and Permissions in the Dashboard and uncheck permission to view Profiles for the selected Role, then browse to Plugins in the Dashboard and enable the plugin and set the desired default route.
Rather than relying on a custom permission as I did in the beginning of this thread, I changed the script to check for a non-permission for the particular role - in this case, view profiles:
public function Base_BeforeDispatch_Handler($Sender,&$Args){ if(!(Gdn::Session()->CheckPermission('Garden.Profiles.View')) && C('Plugins.DefaultRouteRole.Destination'))
Since the admin role has all permissions, this didn't interfere with the default route for admin.
I will work on this script some more, when I get a chance, and see if I can't create one that offers multiple default routes for many generic roles. I will then post it in this forum.
So you can have a custom validation path for specific users?