Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8122929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:59:34+00:00 2026-06-06T05:59:34+00:00

I found this great code snippet that works wonderfully during circumstances that change a

  • 0

I found this great code snippet that works wonderfully during circumstances that change a user’s credentials during that user’s session. The hasCredential() method defaults to looking at the database rather than the user’s session – which meets my needs perfectly (as a user’s credentials are often changed programmatically during a user’s session). So I really need to keep this functionality.

However, for circumstances that have multiple credentials, either an OR circumstance: [[A, B]] or an AND circumstance: [A, B] the code snippet fails as it only checks for the one instance and does not read the yaml security file for AND OR instances.

I’m looking for help with how to tweak the code snippet to take into account AND OR yaml credential permissions. Here’s the code snippet:

public function hasCredential($permission_name)
{
//this overrides the default action (hasCredential) and instead of checking
//the user's session, it now checks the database directly.  
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;
}

EDIT:

I think I need to merge the above code with the original hasCredential() below but I’m struggling with the logic:

public function hasCredential($credentials, $useAnd = true)
{
if (null === $this->credentials)
{
  return false;
}

if (!is_array($credentials))
{
  return in_array($credentials, $this->credentials);
}

// now we assume that $credentials is an array
$test = false;

foreach ($credentials as $credential)
{
  // recursively check the credential with a switched AND/OR mode
  $test = $this->hasCredential($credential, $useAnd ? false : true);

  if ($useAnd)
  {
    $test = $test ? false : true;
  }

  if ($test) // either passed one in OR mode or failed one in AND mode
  {
    break; // the matter is settled
  }
}

if ($useAnd) // in AND mode we succeed if $test is false
{
  $test = $test ? false : true;
}

return $test;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T05:59:36+00:00Added an answer on June 6, 2026 at 5:59 am

    I think I found the correct implementation of the hasCredential method that overrides the original code found in the sfGuardSecurityUser Class. The code asks the underlying GuardUser model to reload all the permissions and groups from the database and adds theses credentials to the SecurityUser object before checking the credentials.

    /**
     * Returns whether or not the user has the given credential.
     * 
     * EXTENDED to reload all permission so that changes take
     * immediate effect and user does not have to log out
     *
     * @param string $credential The credential name
     * @param boolean $useAnd Whether or not to use an AND condition
     * @return boolean
     */
    public function hasCredential($credential, $useAnd = true)
    {
      if (empty($credentials))
      {
        return true;
      }
    
      if (!$this->getGuardUser())
      {
        return false;
      }
    
      if ($this->getGuardUser()->getIsSuperAdmin())
      {
        return true;
      }
    
      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);
    
      if (!is_array($credentials))
      {
        return in_array($credentials, $permission_names);
      }
    
      // now we assume that $credentials is an array
      $test = false;
    
      foreach ($credentials as $credential)
      {
        // recursively check the credential with a switched AND/OR mode
        $test = $this->hasCredential($credential, $useAnd ? false : true);
    
        if ($useAnd)
        {
          $test = $test ? false : true;
        }
    
        if ($test) // either passed one in OR mode or failed one in AND mode
        {
          break; // the matter is settled
        }
      }
    
      if ($useAnd) // in AND mode we succeed if $test is false
      {
        $test = $test ? false : true;
      }
    
      return $test;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

found this little code snippet that seems to do what i want, but im
I found this great code here the Demo . I just need some change.
I found this code witch I tried and it works great but(!). I want
I found this great class, all works well except... I spcify the output directory
I have just found this great tutorial as it is something that I need.
I found this nice snippet of code online: rkApp = Registry.LocalMachine.OpenSubKey(SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run, true); Which runs
I found some great code that is a subclass of UIView which draws that
I'm reading Randall Hyde's Write great code (volume 2) and I found this: [...]
I found this great library for Silverlight that basically a port of libjpeg, so
I found this great code on MSDN for a UDP Client/Server connection, however the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.