web/enmi/Zend/Amf/Adobe/Auth.php
changeset 19 1c2f13fd785c
parent 0 4eba9c11703f
equal deleted inserted replaced
18:bd595ad770fc 19:1c2f13fd785c
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Amf
       
    17  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    18  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    19  * @version    $Id: Auth.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    20  */
       
    21 
       
    22 /** @see Zend_Amf_Auth_Abstract */
       
    23 require_once 'Zend/Amf/Auth/Abstract.php';
       
    24 
       
    25 /** @see Zend_Acl */
       
    26 require_once 'Zend/Acl.php';
       
    27 
       
    28 /** @see Zend_Auth_Result */
       
    29 require_once 'Zend/Auth/Result.php';
       
    30 
       
    31 /**
       
    32  * This class implements authentication against XML file with roles for Flex Builder.
       
    33  *
       
    34  * @package    Zend_Amf
       
    35  * @subpackage Adobe
       
    36  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    37  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    38  */
       
    39 class Zend_Amf_Adobe_Auth extends Zend_Amf_Auth_Abstract
       
    40 {
       
    41 
       
    42     /**
       
    43      * ACL for authorization
       
    44      *
       
    45      * @var Zend_Acl
       
    46      */
       
    47     protected $_acl;
       
    48 
       
    49     /**
       
    50      * Username/password array
       
    51      *
       
    52      * @var array
       
    53      */
       
    54     protected $_users = array();
       
    55 
       
    56     /**
       
    57      * Create auth adapter
       
    58      *
       
    59      * @param string $rolefile File containing XML with users and roles
       
    60      */
       
    61     public function __construct($rolefile)
       
    62     {
       
    63         $this->_acl = new Zend_Acl();
       
    64         $xml = simplexml_load_file($rolefile);
       
    65 /*
       
    66 Roles file format:
       
    67  <roles>
       
    68    <role id=”admin”>
       
    69         <user name=”user1” password=”pwd”/>
       
    70     </role>
       
    71    <role id=”hr”>
       
    72         <user name=”user2” password=”pwd2”/>
       
    73     </role>
       
    74 </roles>
       
    75 */
       
    76         foreach($xml->role as $role) {
       
    77             $this->_acl->addRole(new Zend_Acl_Role((string)$role["id"]));
       
    78             foreach($role->user as $user) {
       
    79                 $this->_users[(string)$user["name"]] = array("password" => (string)$user["password"],
       
    80                                                              "role" => (string)$role["id"]);
       
    81             }
       
    82         }
       
    83     }
       
    84 
       
    85     /**
       
    86      * Get ACL with roles from XML file
       
    87      *
       
    88      * @return Zend_Acl
       
    89      */
       
    90     public function getAcl()
       
    91     {
       
    92         return $this->_acl;
       
    93     }
       
    94 
       
    95     /**
       
    96      * Perform authentication
       
    97      *
       
    98      * @throws Zend_Auth_Adapter_Exception
       
    99      * @return Zend_Auth_Result
       
   100      * @see Zend_Auth_Adapter_Interface#authenticate()
       
   101      */
       
   102     public function authenticate()
       
   103     {
       
   104         if (empty($this->_username) ||
       
   105             empty($this->_password)) {
       
   106             /**
       
   107              * @see Zend_Auth_Adapter_Exception
       
   108              */
       
   109             require_once 'Zend/Auth/Adapter/Exception.php';
       
   110             throw new Zend_Auth_Adapter_Exception('Username/password should be set');
       
   111         }
       
   112 
       
   113         if(!isset($this->_users[$this->_username])) {
       
   114             return new Zend_Auth_Result(Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND,
       
   115                 null,
       
   116                 array('Username not found')
       
   117                 );
       
   118         }
       
   119 
       
   120         $user = $this->_users[$this->_username];
       
   121         if($user["password"] != $this->_password) {
       
   122             return new Zend_Auth_Result(Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID,
       
   123                 null,
       
   124                 array('Authentication failed')
       
   125                 );
       
   126         }
       
   127 
       
   128         $id = new stdClass();
       
   129         $id->role = $user["role"];
       
   130         $id->name = $this->_username;
       
   131         return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $id);
       
   132     }
       
   133 }