Auth.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /** Zend_Amf_Auth_Abstract */
  21. require_once 'Zend/Amf/Auth/Abstract.php';
  22. /** Zend_Acl */
  23. require_once 'Zend/Acl.php';
  24. /** Zend_Auth_Result */
  25. require_once 'Zend/Auth/Result.php';
  26. /**
  27. * This class implements authentication against XML file with roles for Flex Builder.
  28. *
  29. * @package Zend_Amf
  30. * @subpackage Adobe
  31. * @copyright Copyright (c) 2009 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. class Zend_Amf_Adobe_Auth extends Zend_Amf_Auth_Abstract
  35. {
  36. /**
  37. * ACL for authorization
  38. *
  39. * @var Zend_Acl
  40. */
  41. protected $_acl;
  42. /**
  43. * Username/password array
  44. *
  45. * @var array
  46. */
  47. protected $_users = array();
  48. /**
  49. * Create auth adapter
  50. *
  51. * @param string $rolefile File containing XML with users and roles
  52. */
  53. public function __construct($rolefile)
  54. {
  55. $this->_acl = new Zend_Acl();
  56. $xml = simplexml_load_file($rolefile);
  57. /*
  58. Roles file format:
  59. <roles>
  60. <role id=”admin”>
  61. <user name=”user1” password=”pwd”/>
  62. </role>
  63. <role id=”hr”>
  64. <user name=”user2” password=”pwd2”/>
  65. </role>
  66. </roles>
  67. */
  68. foreach($xml->role as $role) {
  69. $this->_acl->addRole(new Zend_Acl_Role((string)$role["id"]));
  70. foreach($role->user as $user) {
  71. $this->_users[(string)$user["name"]] = array("password" => (string)$user["password"],
  72. "role" => (string)$role["id"]);
  73. }
  74. }
  75. }
  76. /**
  77. * Get ACL with roles from XML file
  78. *
  79. * @return Zend_Acl
  80. */
  81. public function getAcl()
  82. {
  83. return $this->_acl;
  84. }
  85. /**
  86. * Perform authentication
  87. *
  88. * @throws Zend_Auth_Adapter_Exception
  89. * @return Zend_Auth_Result
  90. * @see Zend_Auth_Adapter_Interface#authenticate()
  91. */
  92. public function authenticate()
  93. {
  94. if (empty($this->_username) ||
  95. empty($this->_password)) {
  96. /**
  97. * @see Zend_Auth_Adapter_Exception
  98. */
  99. require_once 'Zend/Auth/Adapter/Exception.php';
  100. throw new Zend_Auth_Adapter_Exception('Username/password should be set');
  101. }
  102. if(!isset($this->_users[$this->_username])) {
  103. return new Zend_Auth_Result(Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND,
  104. null,
  105. array('Username not found')
  106. );
  107. }
  108. $user = $this->_users[$this->_username];
  109. if($user["password"] != $this->_password) {
  110. return new Zend_Auth_Result(Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID,
  111. null,
  112. array('Authentication failed')
  113. );
  114. }
  115. $id = new stdClass();
  116. $id->role = $user["role"];
  117. $id->name = $this->_username;
  118. return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $id);
  119. }
  120. }