<commit_before><?php

/*
 * This file is part of the symfony package.
 * (c) <NAME> <<EMAIL>>
 * 
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * sfUoWidgetAdminMenu
 * Admin menu widget rend a menu from a yaml definition.
 *
 * @package    symfony
 * @subpackage sfUnobstrusiveWidgetPlugin
 * @author     <NAME>  <<EMAIL>>
 */
class sfUoWidgetAdminMenu extends sfUoWidgetMenu
{
  /**
   * @see sfUoWidget
   */
  public function __construct($options = array(), $attributes = array())
  {
    $options['choices'] = new sfCallable(array($this, 'getChoices'));

    parent::__construct($options, $attributes);
  }

  /**
   * Configures the current widget.
   *
   * Available options:
   *
   *  * is_super_admin_method:  The isSuperAdmin method name ("isSuperAdmin" by default)
   *  * is_authenticated_method:  The isAuthenticated method name ("isAuthenticated" by default)
   *  * has_credential_method:  The hasCredential method name ("hasCredential" by default) 
   *  * has_permission_method:  The HasPermission method name ("hasPermission" by default)
   *
   * @see sfUoWidget->configure()
   */
  protected function configure($options = array(), $attributes = array())
  {
    parent::configure($options, $attributes);
    
    $this->addOption('is_super_admin_method', 'isSuperAdmin');
    $this->addOption('is_authenticated_method', 'isAuthenticated');
    $this->addOption('has_credential_method', 'hasCredential');
    $this->addOption('has_permission_method', 'hasPermission');
  }

  /**
   * @return string An HTML tag string
   *
   * @see render()
   */
  protected function doRender()
  {
    if ('' == $this->getRenderName())
    {
      throw new InvalidArgumentException('Empty name for sfAdminMenuWidget is not available.');
    }
    
    return parent::doRender();
  }

  /**
   * Return menu choices.
   *
   * @return array
   */
  public function getChoices()
  {
    $manager = sfUoWidgetHelper::getAdminMenuConfigManager();
    try
    {
      return $manager->getConfiguration($this->getRenderName());
    }
    catch (Exception $e)
    {
      throw $e;
    }
  }

  /**
   * Return list item content.
   *
   * @param  string
   * @param  mixed
   *
   * @return string
   */
  protected function getItemContent($key, $value)
  {
    if (
      $this->checkUserRights($value, 'credentials', $this->getOption('has_credential_method'))
      && $this->checkUserRights($value, 'permissions', $this->getOption('has_permission_method')))
    {
      return parent::getItemContent($key, $value);
    }
    else
    {
      return '';
    }
  }

  /**
   * hasCredential.
   *
   * @param  array $values       The values
   * @param  array $fieldName       The fieldName to test
   * @param  array $values       The user method to use
   *
   * @return boolean
   */
  protected function checkUserRights($values, $fieldName, $method)
  {
    if ($this->isSuperAdmin())
    {
      return true;
    }
  
    if (is_array($values) && array_key_exists($fieldName, $values) && !empty($values[$fieldName]))
    {
      if (!$this->isAuthenticated())
      {
        return false;
      }

      $result = false;
      foreach ($values[$fieldName] as $key => $value)
      {
        $result = $this->getUser()->$method($value);
        if ($result)
        {
          break;
        }
      }
      return $result;
    }

    return true;
  }
  
  /**
   * isSuperAdmin.
   *
   * @return boolean
   */
  protected function isSuperAdmin()
  {
    if (!$this->isAuthenticated())
    {
      return false;
    }
  
    $isSuperAdminMethod = $this->getOption('is_super_admin_method');
    return $this->getUser()->$isSuperAdminMethod();
  }
  
  /**
   * isAuthenticated.
   *
   * @return boolean
   */
  protected function isAuthenticated()
  {
    $isAuthenticatedMethod = $this->getOption('is_authenticated_method');
    return $this->getUser()->$isAuthenticatedMethod();
  }
}<commit_msg>[sfUnobstrusiveWidgetPlugin] check method exists before use them in sfUoWidgetAdminMenu<commit_after><?php

/*
 * This file is part of the symfony package.
 * (c) <NAME> <<EMAIL>>
 * 
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * sfUoWidgetAdminMenu
 * Admin menu widget rend a menu from a yaml definition.
 *
 * @package    symfony
 * @subpackage sfUnobstrusiveWidgetPlugin
 * @author     <NAME>  <<EMAIL>>
 */
class sfUoWidgetAdminMenu extends sfUoWidgetMenu
{
  /**
   * @see sfUoWidget
   */
  public function __construct($options = array(), $attributes = array())
  {
    $options['choices'] = new sfCallable(array($this, 'getChoices'));

    parent::__construct($options, $attributes);
  }

  /**
   * Configures the current widget.
   *
   * Available options:
   *
   *  * is_super_admin_method:  The isSuperAdmin method name ("isSuperAdmin" by default)
   *  * is_authenticated_method:  The isAuthenticated method name ("isAuthenticated" by default)
   *  * has_credential_method:  The hasCredential method name ("hasCredential" by default) 
   *  * has_permission_method:  The HasPermission method name ("hasPermission" by default)
   *
   * @see sfUoWidget->configure()
   */
  protected function configure($options = array(), $attributes = array())
  {
    parent::configure($options, $attributes);
    
    $this->addOption('is_super_admin_method', 'isSuperAdmin');
    $this->addOption('is_authenticated_method', 'isAuthenticated');
    $this->addOption('has_credential_method', 'hasCredential');
    $this->addOption('has_permission_method', 'hasPermission');
  }

  /**
   * @return string An HTML tag string
   *
   * @see render()
   */
  protected function doRender()
  {
    if ('' == $this->getRenderName())
    {
      throw new InvalidArgumentException('Empty name for sfAdminMenuWidget is not available.');
    }
    
    return parent::doRender();
  }

  /**
   * Return menu choices.
   *
   * @return array
   */
  public function getChoices()
  {
    $manager = sfUoWidgetHelper::getAdminMenuConfigManager();
    try
    {
      return $manager->getConfiguration($this->getRenderName());
    }
    catch (Exception $e)
    {
      throw $e;
    }
  }

  /**
   * Return list item content.
   *
   * @param  string
   * @param  mixed
   *
   * @return string
   */
  protected function getItemContent($key, $value)
  {
    if (
      $this->checkUserRights($value, 'credentials', $this->getOption('has_credential_method'))
      && $this->checkUserRights($value, 'permissions', $this->getOption('has_permission_method')))
    {
      return parent::getItemContent($key, $value);
    }
    else
    {
      return '';
    }
  }

  /**
   * hasCredential.
   *
   * @param  array $values       The values
   * @param  array $fieldName       The fieldName to test
   * @param  array $values       The user method to use
   *
   * @return boolean
   */
  protected function checkUserRights($values, $fieldName, $method)
  {
    if ($this->isSuperAdmin())
    {
      return true;
    }
  
    if (is_array($values) && array_key_exists($fieldName, $values) && !empty($values[$fieldName]))
    {
      if (!$this->isAuthenticated() || !method_exists($this->getUser(), $method))
      {
        return false;
      }

      $result = false;
      foreach ($values[$fieldName] as $key => $value)
      {
        $result = $this->getUser()->$method($value);
        if ($result)
        {
          break;
        }
      }
      return $result;
    }

    return true;
  }
  
  /**
   * isSuperAdmin.
   *
   * @return boolean
   */
  protected function isSuperAdmin()
  {
    if (!$this->isAuthenticated())
    {
      return false;
    }
  
    $isSuperAdminMethod = $this->getOption('is_super_admin_method');
    return method_exists($this->getUser(), $isSuperAdminMethod) ? $this->getUser()->$isSuperAdminMethod() : false;
  }
  
  /**
   * isAuthenticated.
   *
   * @return boolean
   */
  protected function isAuthenticated()
  {
    $isAuthenticatedMethod = $this->getOption('is_authenticated_method');
    return method_exists($this->getUser(), $isAuthenticatedMethod) ? $this->getUser()->$isAuthenticatedMethod() : false;
  }
}
{'fraction_non_alphanumeric': 0.1376969696969697, 'fraction_numerical': 0.0, 'mean_word_length': 2.9234427009034714, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 66, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6537661', 'n_tokens_mistral': 2589, 'n_tokens_neox': 2562, 'n_words': 560}