Symfony provides some cool configuration features that would allow you to minimize your coding part . In this tutorial i would explain how to configure security.yml to set credentials for a different users.
Create a class file named account.class.php under apps/frontend/lib folder with content .The class below checks weather the user is admin and sets his credential as ‘admin’ . Update the class file as your requirement .
[php]
class account
{
public static function getLoginAdmin($username, $password)
{
// code to check for username and password in a particular table : ORM is doctrine
$usr = Doctrine::getTable(‘TableName’)
->createQuery(‘a’)
->where(‘a.adminuser = ?’,$username)
->andWhere(‘a.password = ?’,$password)
->execute();
$login_user = count($usr);
if($login_user > 0)
{
sfContext::getInstance()->getUser()->setAuthenticated(true);
sfContext::getInstance()->getUser()->addCredential(‘admin’);
return true;
}
else
{
return false;
}
}
public static function LogoutSession()
{
sfContext::getInstance()->getUser()->setAuthenticated(false);
sfContext::getInstance()->getUser()->clearCredentials();
sfContext::getInstance()->getUser()->getAttributeHolder()->removeNamespace(‘admin’);
return true;
}
}
[/php]
And in login action file after receiving username and password call the library function as shown below
[php]
$login_flag = account::getLoginAdmin($this->username,$this->password);
[/php]
and if $login_flag returns true you have successfully logged in else enter username or password is invalid .
You have done with the class file and action page . Now you need to configure security.yml .
Create a folder named config inside a module ( say for eg : under admin module ) with security.yml in it and paste the following code .
[html]
index:
is_secure: true
credentials: admin
[/html]
Now admin module can be accessed only by one with ‘admin’ credential .
You can flush the credential as
[php]
$logout = account::LogoutSession();
[/php]
You are done !