<commit_before><?php
/**
 * @package    Gems
 * @subpackage User
 * @copyright  Copyright (c) 2011 <NAME>
 * @license    New BSD License
 * @version    $Id$
 */

/**
 * Delegates authentication to the Radius server
 *
 * Configuration should be done at organization level, and users should still be
 * added and created as usual. There is now way of changing the remote password.
 * It might be possible to show a custom message maybe with a link to the place
 * where people can change their password in the near future but that is not
 * planned for now.
 *
 * @package    Gems
 * @subpackage User
 * @copyright  Copyright (c) 2011 <NAME>
 * @license    New BSD License
 * @since      Class available since version 1.5
 */
class Gems_User_RadiusUserDefinition extends \Gems_User_StaffUserDefinition implements \Gems_User_UserDefinitionConfigurableInterface
{
    /**
     * @var \Gems_Model_JoinModel
     */
    protected $_configModel;

    /**
     * @var \Gems_Loader
     */
    protected $loader;

    /**
     *
     * @var \Gems_Project_ProjectSettings
     */
    protected $project;

    /**
     * @var \Zend_Translate_Adapter
     */
    protected $translate;

    /**
     * Appends the needed fields for this config to the $bridge
     *
     * @param \MUtil_Model_ModelAbstract $orgModel
     */
    public function addConfigFields(\MUtil_Model_ModelAbstract $orgModel)
    {
        $configModel = $this->getConfigModel(true);
        $order       = $orgModel->getOrder('gor_user_class') + 1;

        foreach ($configModel->getItemNames() as $name) {
            $orgModel->set($name, 'order', $order++);
            $orgModel->set($name, $configModel->get($name));
        }
    }

    /**
     * Return true if a password reset key can be created.
     *
     * Returns the setting for the definition whan no user is passed, otherwise
     * returns the answer for this specific user.
     *
     * @param \Gems_User_User $user Optional, the user whose password might change
     * @return boolean
     */
    public function canResetPassword(\Gems_User_User $user = null)
    {
        return false;
    }

    /**
     * Return true if the password can be set.
     *
     * Returns the setting for the definition whan no user is passed, otherwise
     * returns the answer for this specific user.
     *
     * @param \Gems_User_User $user Optional, the user whose password might change
     * @return boolean
     */
    public function canSetPassword(\Gems_User_User $user = null)
    {
        return false;
    }

    /**
     * Returns an initialized Zend\Authentication\Adapter\AdapterInterface
     *
     * @param \Gems_User_User $user
     * @param string $password
     * @return Zend\Authentication\Adapter\AdapterInterface
     */
    public function getAuthAdapter(\Gems_User_User $user, $password)
    {
        //Ok hardcoded for now this needs to be read from the userdefinition
        $configData = $this->loadConfig(array('gor_id_organization' => $user->getBaseOrganizationId()));

        $config  = array('ip'                 => $configData['grcfg_ip'],
                         'authenticationport' => $configData['grcfg_port'],
                         'sharedsecret'       => $configData['grcfg_secret']);

        //Unset empty
        foreach($config as $key=>$value) {
            if (empty($value)) {
                unset($config[$key]);
            }
        }
        $adapter = new \Gems_User_Adapter_Radius($config);

        $adapter->setIdentity($user->getLoginName())
                ->setCredential($password);

        return $adapter;
    }

    /**
     * Return the number of changed records for the save performed
     */
    public function getConfigChanged()
    {
        return $this->getConfigModel(true)->getChanged();
    }

    /**
     * Get a model to store the config
     *
     * @param boolean $valueMask MAsk the password or if false decrypt it
     * @return \Gems_Model_JoinModel
     */
    protected function getConfigModel($valueMask = true)
    {
        if (!$this->_configModel) {
            $model = new \MUtil_Model_TableModel('gems__radius_config', 'config');
            // $model = new \Gems_Model_JoinModel('config', 'gems__radius_config', 'grcfg');

            $model->setIfExists('grcfg_ip', 'label', $this->translate->_('IP address'), 'required', true);
            $model->setIfExists('grcfg_port', 'label', $this->translate->_('Port'), 'required', true);
            $model->setIfExists('grcfg_secret',
                    'label', $this->translate->_('Shared secret'),
                    'description', $this->translate->_('Enter only when changing'),
                    'elementClass', 'password',
                    'required', false,<commit_msg>Fixed #177: Radius never needs a rehash<commit_after><?php
/**
 * @package    Gems
 * @subpackage User
 * @copyright  Copyright (c) 2011 <NAME>
 * @license    New BSD License
 * @version    $Id$
 */

/**
 * Delegates authentication to the Radius server
 *
 * Configuration should be done at organization level, and users should still be
 * added and created as usual. There is now way of changing the remote password.
 * It might be possible to show a custom message maybe with a link to the place
 * where people can change their password in the near future but that is not
 * planned for now.
 *
 * @package    Gems
 * @subpackage User
 * @copyright  Copyright (c) 2011 <NAME>
 * @license    New BSD License
 * @since      Class available since version 1.5
 */
class Gems_User_RadiusUserDefinition extends \Gems_User_StaffUserDefinition implements \Gems_User_UserDefinitionConfigurableInterface
{
    /**
     * @var \Gems_Model_JoinModel
     */
    protected $_configModel;

    /**
     * @var \Gems_Loader
     */
    protected $loader;

    /**
     *
     * @var \Gems_Project_ProjectSettings
     */
    protected $project;

    /**
     * @var \Zend_Translate_Adapter
     */
    protected $translate;

    /**
     * Appends the needed fields for this config to the $bridge
     *
     * @param \MUtil_Model_ModelAbstract $orgModel
     */
    public function addConfigFields(\MUtil_Model_ModelAbstract $orgModel)
    {
        $configModel = $this->getConfigModel(true);
        $order       = $orgModel->getOrder('gor_user_class') + 1;

        foreach ($configModel->getItemNames() as $name) {
            $orgModel->set($name, 'order', $order++);
            $orgModel->set($name, $configModel->get($name));
        }
    }

    /**
     * Return true if a password reset key can be created.
     *
     * Returns the setting for the definition whan no user is passed, otherwise
     * returns the answer for this specific user.
     *
     * @param \Gems_User_User $user Optional, the user whose password might change
     * @return boolean
     */
    public function canResetPassword(\Gems_User_User $user = null)
    {
        return false;
    }

    /**
     * Return true if the password can be set.
     *
     * Returns the setting for the definition whan no user is passed, otherwise
     * returns the answer for this specific user.
     *
     * @param \Gems_User_User $user Optional, the user whose password might change
     * @return boolean
     */
    public function canSetPassword(\Gems_User_User $user = null)
    {
        return false;
    }

    /**
     * Returns an initialized Zend\Authentication\Adapter\AdapterInterface
     *
     * @param \Gems_User_User $user
     * @param string $password
     * @return Zend\Authentication\Adapter\AdapterInterface
     */
    public function getAuthAdapter(\Gems_User_User $user, $password)
    {
        //Ok hardcoded for now this needs to be read from the userdefinition
        $configData = $this->loadConfig(array('gor_id_organization' => $user->getBaseOrganizationId()));

        $config  = array('ip'                 => $configData['grcfg_ip'],
                         'authenticationport' => $configData['grcfg_port'],
                         'sharedsecret'       => $configData['grcfg_secret']);

        //Unset empty
        foreach($config as $key=>$value) {
            if (empty($value)) {
                unset($config[$key]);
            }
        }
        $adapter = new \Gems_User_Adapter_Radius($config);

        $adapter->setIdentity($user->getLoginName())
                ->setCredential($password);

        return $adapter;
    }
    
    /**
     * We never need a rehash
     * 
     * @param \Gems_User_User $user
     * @param type $password
     * @return boolean
     */
    public function checkRehash(\Gems_User_User $user, $password) {
        return false;
    }

    /**
     * Return the number of changed records for the save performed
     */
    public function getConfigChanged()
    {
        return $this->getConfigModel(true)->getChanged();
    }

    /**
     * Get a model to store the config
     *
     * @param boolean $valueMask MAsk the password or if false decrypt it
     * @return \Gems_Model_JoinModel
     */
    protected function getConfigModel($valueMask = true)
    {
        if (!$this->_configModel) {
            $model = new \MUtil_Model_TableModel('gems__radius_config', 'config');
            // $model = new \Gems_Model_JoinModel('config', 'gems__radius_config', 'grcfg');

            $model->setIfExists('grcfg_ip', 'label', $this->translate->_('IP address'), 'required', true);
            $model->setIfExists('grcfg_port', 'label', $this->translate->_('Port'), 'required', true);
            $model->setIfExists('grcfg_secret',
                    'label', $this->translate->_('Shared secret'),
                    'description', $this->translate->_('Enter only when changing'),
                    'elementClass', 'password',
                    'required', false,
{'fraction_non_alphanumeric': 0.11179298353278101, 'fraction_numerical': 0.0025570215812621457, 'mean_word_length': 2.3833910034602077, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 67, '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': '1447720', 'n_tokens_mistral': 2731, 'n_tokens_neox': 2657, 'n_words': 775}