sfGuardValidatorUser
首先,您应该将类复制plugins/sfDoctrineGuardPlugin/lib/validator
到lib/validator
并清除缓存。
比修改它的doClean
方法来检查用户是否具有所需的权限。
class sfGuardValidatorUser extends sfValidatorBase
{
// ...
protected function doClean($values)
{
$username = isset($values[$this->getOption('username_field')]) ? $values[$this->getOption('username_field')] : '';
$password = isset($values[$this->getOption('password_field')]) ? $values[$this->getOption('password_field')] : '';
$callable = sfConfig::get('app_sf_guard_plugin_retrieve_by_username_callable');
if ($username && $callable)
{
$user = call_user_func_array($callable, array($username));
}
elseif ($username)
{
$user = $this->getTable()->retrieveByUsername($username);
}
else
{
$user = false;
}
/** @var $user sfGuardUser */
if ($user && $user->isAllowedToLogIn(sfConfig::get('sf_app'), $password))
{
return array_merge($values, array('user' => $user));
}
if ($this->getOption('throw_global_error'))
{
throw new sfValidatorError($this, 'invalid');
}
throw new sfValidatorErrorSchema($this, array($this->getOption('username_field') => new sfValidatorError($this, 'invalid')));
}
// ...
}
比向类添加isAllowedToLogIn
方法sfGuardUser
:
public function isAllowedToLogIn($app, $password)
{
if ('admin' == $app)
{
$hasPermission = $this->getIsSuperAdmin() || $this->hasPermission('admin');
}
elseif ('site' == $app)
{
$hasPermission = $this->hasPermission('public_access');
}
else
{
$hasPermission = true;
}
return $this->getIsActive() && $this->checkPassword($password) && $hasPermission;
}
如果需要,修改权限检查部分。
我建议添加一个管理员组和权限,如果你不能使用该is_super_admin
标志。