File indexing completed on 2024-06-16 05:29:51

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Amf
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /** @see Zend_Amf_Auth_Abstract */
0023 // require_once 'Zend/Amf/Auth/Abstract.php';
0024 
0025 /** @see Zend_Acl */
0026 // require_once 'Zend/Acl.php';
0027 
0028 /** @see Zend_Auth_Result */
0029 // require_once 'Zend/Auth/Result.php';
0030 
0031 /** @see Zend_Xml_Security */
0032 // require_once 'Zend/Xml/Security.php';
0033 
0034 /**
0035  * This class implements authentication against XML file with roles for Flex Builder.
0036  *
0037  * @package    Zend_Amf
0038  * @subpackage Adobe
0039  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0040  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0041  */
0042 class Zend_Amf_Adobe_Auth extends Zend_Amf_Auth_Abstract
0043 {
0044 
0045     /**
0046      * ACL for authorization
0047      *
0048      * @var Zend_Acl
0049      */
0050     protected $_acl;
0051 
0052     /**
0053      * Username/password array
0054      *
0055      * @var array
0056      */
0057     protected $_users = array();
0058 
0059     /**
0060      * Create auth adapter
0061      *
0062      * @param string $rolefile File containing XML with users and roles
0063      */
0064     public function __construct($rolefile)
0065     {
0066         $this->_acl = new Zend_Acl();
0067         $xml = Zend_Xml_Security::scanFile($rolefile);
0068 /*
0069 Roles file format:
0070  <roles>
0071    <role id=”admin”>
0072         <user name=”user1” password=”pwd”/>
0073     </role>
0074    <role id=”hr”>
0075         <user name=”user2” password=”pwd2”/>
0076     </role>
0077 </roles>
0078 */
0079         foreach($xml->role as $role) {
0080             $this->_acl->addRole(new Zend_Acl_Role((string)$role["id"]));
0081             foreach($role->user as $user) {
0082                 $this->_users[(string)$user["name"]] = array("password" => (string)$user["password"],
0083                                                              "role" => (string)$role["id"]);
0084             }
0085         }
0086     }
0087 
0088     /**
0089      * Get ACL with roles from XML file
0090      *
0091      * @return Zend_Acl
0092      */
0093     public function getAcl()
0094     {
0095         return $this->_acl;
0096     }
0097 
0098     /**
0099      * Perform authentication
0100      *
0101      * @throws Zend_Auth_Adapter_Exception
0102      * @return Zend_Auth_Result
0103      * @see Zend_Auth_Adapter_Interface#authenticate()
0104      */
0105     public function authenticate()
0106     {
0107         if (empty($this->_username) ||
0108             empty($this->_password)) {
0109             /**
0110              * @see Zend_Auth_Adapter_Exception
0111              */
0112             // require_once 'Zend/Auth/Adapter/Exception.php';
0113             throw new Zend_Auth_Adapter_Exception('Username/password should be set');
0114         }
0115 
0116         if(!isset($this->_users[$this->_username])) {
0117             return new Zend_Auth_Result(Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND,
0118                 null,
0119                 array('Username not found')
0120                 );
0121         }
0122 
0123         $user = $this->_users[$this->_username];
0124         if($user["password"] != $this->_password) {
0125             return new Zend_Auth_Result(Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID,
0126                 null,
0127                 array('Authentication failed')
0128                 );
0129         }
0130 
0131         $id = new stdClass();
0132         $id->role = $user["role"];
0133         $id->name = $this->_username;
0134         return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $id);
0135     }
0136 }