Auth.php 3.7 KB

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