Code snippets for symfony 1.x

Navigation

Refine Tags

Snippets tagged "sfdoctrineguardplugin"

modified hasCredential method that loads permissions from the database

Tired of having to signout / signin your users to reload permissions? This method fetches the credentials directly from the database so the credential changes are effective as soon as the groups or permissions are changed.

Paste this in your lib/myUser.class.php file

  /**
   * Overridden method that actually reads the permission from DB
   * instead of relying on data present when the user logs in.
   *
   * @param  string  permission name
   *
   * @return boolean true if the user has credential
   */
  public function hasCredential($permission_name)
  {
    if (!$this->isAuthenticated()) {
      return false;
    }
    $gu = $this->getGuardUser();
    $groups = $gu->getGroups();
    $permissions = $gu->getPermissions();
 
    $permission_names = array();
    foreach($permissions as $permission) {
      $permission_names[] = $permission->getName();
    }
    foreach($groups as $group) {
      $group_permissions = $group->getPermissions();
      foreach($group_permissions as $group_permission) {
        $permission_names = array_merge($permission_names, array($group_permission->getName()));
      }
    }
    $permission_names = array_unique($permission_names);
    return (in_array($permission_name, $permission_names)) ? true : false;
  }
 
by Mathieu Comandon on 2011-02-07, tagged credentials  sfdoctrineguardplugin  sfguarduser