Symfony Plugin sfCssTabs - Recognition of credentials addon
The sfCssTabsPlugin is one of the userfull plugins of Symfony.
Monday I installed this plugin in a sandbox installation and I began to play with it.
In this article i will explain how to install this plugin and how to add the recongnition of credentials funcionality.
Enjoy.
Mauro Casula.
Symofony-Framework.com
PLUGIN INSTALLATION
The installation is very simple. I followed the plugin wiki site instructions:
1. Installation
symfony plugin-install http://plugins.symfony-project.com/sfCssTabsPlugin
That command will install the plugin in :
<project> /web/sfCssTabsPlugin/css/sfCssTabs.css
2. Copy of css file
We have to copy the css file to:
<project> /web/css/sfCssTabs.css
3. Change your view.yml
So we can modify the view.yml in config directory of our application ( backend or frontend for example):
We have to add the line:
stylesheets: [main, sfCssTabs]
4. Modify sfCssTabs configuration file
It is in:
<project>/web/sfCssTabsPlugin/config/ctSite.yml
An example configuration can be:
———————————————–
cssTabs:
# general config
configTabs:
div: {id: navcontainer}
mt_ul: {id: adminmenu}
mt_li: {id: active}
mt_a: {class: current}
st_ul: {id: submenu}
st_li: {id: activeII}
st_a: {class: current}
# Configuration of the main Tabs
mainTabs:
- label: TITULO1
module: default
action: ”
linkOptions: {}
- label: Photo Gallery
module: anotherModuleAction
action: ‘customAction’
linkOptions: {}
# Configuration of the sub Tabs
subTabs:
- label: SUB MENU TITULO1
module: default
action: ‘default’
linkOptions: {}
parentTab: TITULO1
……
—————————–
5. Put the menu in Layout file
Now.. in your application layout ( <project>/apps/<appName>/templeates/layout.php ) add the following:
<?php
sfCssTabs::singleton()->render(’site’);
?>
( ’site’ is the config file name ( ctFile.yml ) without ‘ct’ and without extention )
And…. a fantastic wordpress style menu will appear in your layout…
HOW TO ADD THE RECOGNITION OF CREDENTIALS ADDON
In this part I’ll explain how to add the recognition of credentials..
If you are not interested in learn “how to” but only want to download my Addon and use it, you only need to download attachment zip file and follow the point 1 and 6 of the following instructions…
1. We need to add a functionality in the myUser or in sfGuardSecurityUser if you use sfGuard Plugin
Modify this file if you dont have sfGuard:
<project>/web/sfCssTabsPlugin/lib/myUser.class.php
If you dont have myUser.class.php in <project>/web/sfCssTabsPlugin/lib/ folder than create it.
or this one if you have sfGuardPlugin installed:
<project>/plugins/sfGuardPlugin/lib/user/sfGuardSecurityUser.class.php
add the following code:
——————————
private $security;
public function canAccess($module, $action)
{ require(sfConfigCache::getInstance()->checkConfig(sfConfig::get(’sf_app_module_dir_name’)
.’/’.$module.’/’.sfConfig::get(’sf_app_module_config_dir_name’).’/security.yml’, true));
if (isset($this->security[$action][’credentials’]))
{
$credentials = $this->security[$action][’credentials’];
}
else if (isset($this->security[’all’][’credentials’]))
{
$credentials = $this->security[’all’][’credentials’];
}
else
{
$credentials = null;
}
if ($credentials === null)
return true;
return $this->hasCredential($credentials);
}
2. Modify the sfCssTabs.class.php adding the recognition credentials funcionality
The file is in:
<project>/plugins/sfCssTabsPlugin/lib/sfCssTabs.class.php
The function that draw the buttons is buildTabs
private function buildTabs( $tabs, $prefix, $levelTabs, $parentTab = ” )
{ …… }
In that function, there is a foreach function that loop over the buttons and draw them in the right position.
After the foreach:
foreach( $tabs as $tab )
{
We have to add the control:
if ($this->sf_user->canAccess($tab[’module’],$tab[’action’] ))
{ …………
closing it before the foreach…
} //
}
$ulLi = content_tag(’ul’, $li, $this->configTabs[$prefix.’ul’]);
………..
3. Add a $sf_user variable in the class:
private $sf_user;
4. Change the loadAndAssignConfig function to accept the $sf_user parameter
private function loadAndAssignConfig($sf_user, $name )
{
// Guardo el objeto sfUser..
$this->sf_user = $sf_user;
5. Change the render function
public function render($sf_user, $name=” )
{
// Guardo la configuracion
$this->loadAndAssignConfig($sf_user, $name );
6. Change the call to render function from layout
<?php sfCssTabs::singleton()->render($sf_user, ’site’); ?>
And now enjoy.. if you secure a module or an action, the user logged in will not see the buttons if doent have access to them…
OTHERS FUNCTIONALITY IN MY sfCssTabs Class
1. Include a partial inside the menu
I have added this functionality so that it will be possible to add an “intelligent part” in your menu.
Specifically I use this functionality to insert the partial _loginlogoutpartial.php
—————————
<?php use_helper(’Url’, ‘Tag’) ?>
<?php if ($sf_user->isAuthenticated()) { ?>
link_to(’Logout’, ’sfGuardAuth/signout’);
<?php } else {
$a = link_to(’Login’, ’sfGuardAuth/signin’);
$li = content_tag(’li’, $a, ”);
echo $li;
}
?>
—————————
In the sfCssTabs configuration file we have only to specify the partial with the underscore character.
————————-
mainTabs:
- label: TITLE
module: moduleName
action: ‘_partialName’
linkOptions: {}
icon: ‘/images/inmueble.jpg’
——————————————-
In the menu bar will appear a button that is the result of the specified partial. If the user is authenticated it will display Logout, otherwise will display Login.
2. Specify an icon for the buttons
If you want to display a little icon in your buttons menu, you only have to specify the url of the image file in the config file:
mainTabs:
- label: Inmuebles
module: aInmueble
action: ‘list’
linkOptions: {}
icon: ‘/images/inmueble.jpg’
Good Bye.
I hope many of you can leave comments and suggestions…
I’m finding professional people to write in this Blog…
If you are interested, write me at: maurocasula [at] gmail.com
Mauro Casula.
symfony-framework.com
9 Responses to “Symfony Plugin sfCssTabs - Recognition of credentials addon”
You can leave a response, or trackback from your own site.
Adell
Said this at 9:31pm:Hi, i can´t download the file!
admin
Said this at 10:26am:I’m sorry.. I have updated the post..
Now its possible to download the file.
I will appreciate any help, suggestion, critique or opinion…
Regards.
Symfony-framework.com admin
Adell
Said this at 1:24pm:Hi Mauro,
“Modify this file if you dont have sfGuard:
/web/sfCssTabsPlugin/lib/myUser.class.php”
They file don´t exist, have to create?
admin
Said this at 1:40pm:Hi Adell,
if you dont use sfGuardPlugin you have to create myUser.class.php and extend the functionality of sf_user class.
I will add this in my tutorial.
Brandy Norwood
Said this at 5:23pm:Hey!…I Googled for wiki install, but found your page about Plugin sfCssTabs - Recognition of credentials addon | Symfony-framework.com…and have to say thanks. nice read.
ajax ct
Said this at 4:34pm:ajax ct
Websites with web 2.0 standards are not always the better ones.
Brandy Norwood
Said this at 9:06pm:Hi there…Man i love reading your blog, interesting posts ! it was a great Sunday
Freeman
Said this at 3:34am:You make my day!
merlin
Said this at 2:04pm:hey,
I’ve tried to add the canAccess method in /project/apps/frontend/lib
class myTestUser extends sfUser
{
public function canAccess($module, $action)
{
…..
}
}
Autoloading the class should work for this directories by default.. but I get an exception “Call to undefined method sfUser::canAccess” I guess, the sfUser is not extended correctly??
thanks in advance,
merlin