Zend_Auth.xml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. <sect1 id="zend.auth.introduction">
  2. <title>简介</title>
  3. <para>
  4. Zend_Auth 为认证(authentication)和一些通用用例情景的具体认证适配器提供了一个API。
  5. </para>
  6. <para>
  7. Zend_Auth 只涉及 <emphasis role="strong">认证</emphasis>而不是<emphasis role="strong">授权</emphasis>。认证被宽松地定义为基于一些证书(credential)来确定一个实体(例如,身份)是否确实是它所声称的。授权是一个过程,它决定是否允许一个实体对其他实体进行访问、执行操作,它超出了Zend_Auth的范围。更多关于Zend Framework 授权和访问控制的信息,参见<link linkend="zend.acl">Zend_Acl</link>.
  8. </para>
  9. <note>
  10. <para>
  11. <code>Zend_Auth</code> 类通过它的<code>getInstance()</code>方法实现 Singleton 模式 - 只有一个实例可用。这意味着使用 <code>new</code>操作符和 <code>clone</code> 关键字将不能在<code>Zend_Auth</code> 类中工作,而要使用 <code>Zend_Auth::getInstance()</code>来代替。
  12. </para>
  13. </note>
  14. <sect2 id="zend.auth.introduction.adapters">
  15. <title>适配器</title>
  16. <para>
  17. Zend_Auth适配器被用来依靠特定的认证服务(例如LDAP、RDBMS或基于文件的存储)来认证。不同的适配器可能有不同的选项和行为,但有些基本的事情在认证适配器中是通用的。例如,接受认证证书(包括声称身份)、依靠认证服务执行查询、返回结果在Zend_Auth适配器中是通用的。
  18. </para>
  19. <para>
  20. 每个Zend_Auth适配器类都实现<code>Zend_Auth_Adapter_Interface</code>。这个接口定义了一个方法<code>authenticate()</code>,适配器必须为执行认证查询而实现它。在调用<code>authenticate()</code>之前,每个适配器必需准备就绪。这样适配器准备包括设置证书(例如,用户名和密码)并为适配器专用的配置选项定义一些值,例如为数据库表适配器做的连接设置。
  21. </para>
  22. <para>
  23. 下面是一个认证适配器的例子,它要求为认证设置用户名和密码。为简明扼要,其它的细节(如查询认证服务)被省略了。
  24. <programlisting role="php"><![CDATA[
  25. class MyAuthAdapter implements Zend_Auth_Adapter_Interface
  26. {
  27. /**
  28. * Sets username and password for authentication
  29. *
  30. * @return void
  31. */
  32. public function __construct($username, $password)
  33. {
  34. // ...
  35. }
  36. /**
  37. * Performs an authentication attempt
  38. *
  39. * @throws Zend_Auth_Adapter_Exception If authentication cannot
  40. * be performed
  41. * @return Zend_Auth_Result
  42. */
  43. public function authenticate()
  44. {
  45. // ...
  46. }
  47. }
  48. ]]>
  49. </programlisting>
  50. 如上面所示,<code>authenticate()</code>必需返回一个<code>Zend_Auth_Result</code>的实例(或从<code>Zend_Auth_Result</code>派生的一个类的实例)。如果因为某些原因认证查询不能执行,<code>authenticate()</code>应该抛出一个由<code>Zend_Auth_Adapter_Exception</code>产生的异常。
  51. </para>
  52. </sect2>
  53. <sect2 id="zend.auth.introduction.results">
  54. <title>结果</title>
  55. <para>
  56. 为了表示一个认证尝试的结果,Zend_Auth适配器返回一个带有<code>authenticate()</code>的<code>Zend_Auth_Result</code>的实例。适配器基于结构组成<code>Zend_Auth_Result</code>对象,下面四个方法提供了一组基本的用户面临的通用Zend_Auth适配器结果的操作:
  57. <itemizedlist>
  58. <listitem>
  59. <para>
  60. <code>isValid()</code> - 返回 true 当且仅当结果表示一个成功的认证尝试
  61. </para>
  62. </listitem>
  63. <listitem>
  64. <para>
  65. <code>getCode()</code> - 返回一个 <code>Zend_Auth_Result</code> 常量标识符用来决定认证失败的类型或者是否认证成功。这个可以用于开发者希望区别若干认证结果类型的情形,例如,这允许开发者维护详细的认证结果统计。尽管开发这被鼓励去考虑提供这样详细的原因给用户的风险,替而代之使用一般的认证失败信息,这个功能的其它用法是由于可用性的原因提供专用的,定制的信息给用户。更多的信息,参见下面的注释。
  66. </para>
  67. </listitem>
  68. <listitem>
  69. <para>
  70. <code>getIdentity()</code> - 返回认证尝试的身份
  71. </para>
  72. </listitem>
  73. <listitem>
  74. <para>
  75. <code>getMessages()</code> - 返回关于认证尝试失败的数组
  76. </para>
  77. </listitem>
  78. </itemizedlist>
  79. </para>
  80. <para>
  81. 为了执行更多的操作,开发者可能希望基于认证结果的类型来分支化。一些开发者可能发信有用的操作是:在太多的不成功的密码尝试之后锁住帐号,在太多不存在的身份尝试后标记IP地址,并提供专用的,定制的认证结果信息给用户。下面的结果代码是可用的:
  82. <programlisting role="php"><![CDATA[
  83. Zend_Auth_Result::SUCCESS
  84. Zend_Auth_Result::FAILURE
  85. Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND
  86. Zend_Auth_Result::FAILURE_IDENTITY_AMBIGUOUS
  87. Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID
  88. Zend_Auth_Result::FAILURE_UNCATEGORIZED
  89. ]]>
  90. </programlisting>
  91. </para>
  92. <para>
  93. 下面的例子举例说明开发者如何分支化结果代码:
  94. <programlisting role="php"><![CDATA[
  95. // inside of AuthController / loginAction
  96. $result = $this->_auth->authenticate($adapter);
  97. switch ($result->getCode()) {
  98. case Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND:
  99. /** do stuff for nonexistent identity **/
  100. break;
  101. case Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID:
  102. /** do stuff for invalid credential **/
  103. break;
  104. case Zend_Auth_Result::SUCCESS:
  105. /** do stuff for successful authentication **/
  106. break;
  107. default:
  108. /** do stuff for other failure **/
  109. break;
  110. }
  111. ]]>
  112. </programlisting>
  113. </para>
  114. </sect2>
  115. <sect2 id="zend.auth.introduction.persistence">
  116. <title>身份的持久(Persistence)</title>
  117. <para>
  118. 实质上,认证一个包含认证证书的请求很有用,但是维护已认证的身份并在每次请求时不需要出示认证证书也同样很重要。
  119. </para>
  120. <para>
  121. HTTP是一个无连接的协议,然而,象cookie和session这样的技术已经被开发出来使在服务器端的web应用维护多请求状态变得容易。
  122. </para>
  123. <sect3 id="zend.auth.introduction.persistence.default">
  124. <title>在PHP Session 中的缺省持久(Persistence)</title>
  125. <para>
  126. 缺省地,<code>Zend_Auth</code>从使用PHP session成功的认证尝试中提供身份的持久存储。基于一个成功的认证尝试,<code>Zend_Auth::authenticate()</code>通过把认证结果放入持久存储中来保存身份。除非另有配置,<code>Zend_Auth</code> 使用名称为<code>Zend_Auth_Storage_Session</code> 的存储类,这个类使用<link linkend="zend.session">Zend_Session</link>。通过实现<code>Zend_Auth_Storage_Interface</code>给<code>Zend_Auth::setStorage()</code>提供一个对象,一个定制的类可以被替代使用。
  127. </para>
  128. <note>
  129. <para>
  130. 对于特定的用例,如果身份的持久存储不合适,开发者可以放弃使用<code>Zend_Auth</code>类,替代地,而直接使用适配器类。
  131. </para>
  132. </note>
  133. <example id="zend.auth.introduction.persistence.default.example">
  134. <title>修改 Session 名字空间</title>
  135. <para>
  136. <code>Zend_Auth_Storage_Session</code>使用<code>'Zend_Auth'</code>的seesion名字空间。通过给<code>Zend_Auth_Storage_Session</code>的构造器传递不同的值,这个名字空间可以被替换,并且这个值被从内部传递给<code>Zend_Session_Namespace</code>的构造器。这应该发生在认证尝试之前,因为<code>Zend_Auth::authenticate()</code>执行身份的自动存储。
  137. <programlisting role="php"><![CDATA[
  138. // Save a reference to the Singleton instance of Zend_Auth
  139. $auth = Zend_Auth::getInstance();
  140. // Use 'someNamespace' instead of 'Zend_Auth'
  141. $auth->setStorage(new Zend_Auth_Storage_Session('someNamespace'));
  142. /**
  143. * @todo Set up the auth adapter, $authAdapter
  144. */
  145. // Authenticate, saving the result, and persisting the identity on
  146. // success
  147. $result = $auth->authenticate($authAdapter);
  148. ]]>
  149. </programlisting>
  150. </para>
  151. </example>
  152. </sect3>
  153. <sect3 id="zend.auth.introduction.persistence.custom">
  154. <title>实现订制存储</title>
  155. <para>
  156. 有时候开发者需要使用不同的身份持久行为,而不是<code>Zend_Auth_Storage_Session</code>提供的。对于这样的案例开发者可以简单地实现<code>Zend_Auth_Storage_Interface</code>并给<code>Zend_Auth::setStorage()</code>提供一个类的实例。
  157. </para>
  158. <example id="zend.auth.introduction.persistence.custom.example">
  159. <title>使用定制存储类</title>
  160. <para>
  161. 为了使用不同于<code>Zend_Auth_Storage_Session</code>的身份之久存储类,开发者可实现<code>Zend_Auth_Storage_Interface</code>:
  162. <programlisting role="php"><![CDATA[
  163. class MyStorage implements Zend_Auth_Storage_Interface
  164. {
  165. /**
  166. * Returns true if and only if storage is empty
  167. *
  168. * @throws Zend_Auth_Storage_Exception If it is impossible to
  169. * determine whether storage
  170. * is empty
  171. * @return boolean
  172. */
  173. public function isEmpty()
  174. {
  175. /**
  176. * @todo implementation
  177. */
  178. }
  179. /**
  180. * Returns the contents of storage
  181. *
  182. * Behavior is undefined when storage is empty.
  183. *
  184. * @throws Zend_Auth_Storage_Exception If reading contents from
  185. * storage is impossible
  186. * @return mixed
  187. */
  188. public function read()
  189. {
  190. /**
  191. * @todo implementation
  192. */
  193. }
  194. /**
  195. * Writes $contents to storage
  196. *
  197. * @param mixed $contents
  198. * @throws Zend_Auth_Storage_Exception If writing $contents to
  199. * storage is impossible
  200. * @return void
  201. */
  202. public function write($contents)
  203. {
  204. /**
  205. * @todo implementation
  206. */
  207. }
  208. /**
  209. * Clears contents from storage
  210. *
  211. * @throws Zend_Auth_Storage_Exception If clearing contents from
  212. * storage is impossible
  213. * @return void
  214. */
  215. public function clear()
  216. {
  217. /**
  218. * @todo implementation
  219. */
  220. }
  221. }
  222. ]]>
  223. </programlisting>
  224. </para>
  225. <para>
  226. 为了使用这个定制的存储类,在认证查询被尝试前,<code>Zend_Auth::setStorage()</code>被调用:
  227. <programlisting role="php"><![CDATA[
  228. // Instruct Zend_Auth to use the custom storage class
  229. Zend_Auth::getInstance()->setStorage(new MyStorage());
  230. /**
  231. * @todo Set up the auth adapter, $authAdapter
  232. */
  233. // Authenticate, saving the result, and persisting the identity on
  234. // success
  235. $result = Zend_Auth::getInstance()->authenticate($authAdapter);
  236. ]]>
  237. </programlisting>
  238. </para>
  239. </example>
  240. </sect3>
  241. </sect2>
  242. <sect2 id="zend.auth.introduction.using">
  243. <title>使用Zend_Auth</title>
  244. <para>
  245. 这里提供了两种方法使用Zend_Auth适配器:
  246. <orderedlist>
  247. <listitem>
  248. <para>
  249. 非直接地,通过<code>Zend_Auth::authenticate()</code>
  250. </para>
  251. </listitem>
  252. <listitem>
  253. <para>
  254. 直接地,通过适配器的 <code>authenticate()</code> 方法
  255. </para>
  256. </listitem>
  257. </orderedlist>
  258. </para>
  259. <para>
  260. 下面的例子通过<code>Zend_Auth</code>类来示例如何非直接地使用Zend_Auth适配器:
  261. <programlisting role="php"><![CDATA[
  262. // Get a reference to the singleton instance of Zend_Auth
  263. require_once 'Zend/Auth.php';
  264. $auth = Zend_Auth::getInstance();
  265. // Set up the authentication adapter
  266. $authAdapter = new MyAuthAdapter($username, $password);
  267. // Attempt authentication, saving the result
  268. $result = $auth->authenticate($authAdapter);
  269. if (!$result->isValid()) {
  270. // Authentication failed; print the reasons why
  271. foreach ($result->getMessages() as $message) {
  272. echo "$message\n";
  273. }
  274. } else {
  275. // Authentication succeeded; the identity ($username) is stored
  276. // in the session
  277. // $result->getIdentity() === $auth->getIdentity()
  278. // $result->getIdentity() === $username
  279. }
  280. ]]>
  281. </programlisting>
  282. </para>
  283. <para>
  284. 一旦在一个请求里的认证被尝试,如上面的例子,检查一个成功的被认证的身份是否存在就是一个简单的匹配:
  285. <programlisting role="php"><![CDATA[
  286. $auth = Zend_Auth::getInstance();
  287. if ($auth->hasIdentity()) {
  288. // Identity exists; get it
  289. $identity = $auth->getIdentity();
  290. }
  291. ]]>
  292. </programlisting>
  293. </para>
  294. <para>
  295. 从持久存储空间出去一个身份,可简单地使用<code>clearIdentity()</code>方法。这将被典型地用作“logout”操作。
  296. <programlisting role="php"><![CDATA[
  297. Zend_Auth::getInstance()->clearIdentity();
  298. ]]>
  299. </programlisting>
  300. </para>
  301. <para>
  302. 当自动使用持久存储空间对特定的用例不合适,开发者可简单地忽略<code>Zend_Auth</code>类,直接使用适配器类。直接使用适配器类需要配置和准备适配器对象和调用它的<code>authenticate()</code>方法。适配器规范细节将在每个适配器的文档中讨论。下面的例子直接使用 <code>MyAuthAdapter</code>:
  303. <programlisting role="php"><![CDATA[
  304. // Set up the authentication adapter
  305. $authAdapter = new MyAuthAdapter($username, $password);
  306. // Attempt authentication, saving the result
  307. $result = $authAdapter->authenticate();
  308. if (!$result->isValid()) {
  309. // Authentication failed; print the reasons why
  310. foreach ($result->getMessages() as $message) {
  311. echo "$message\n";
  312. }
  313. } else {
  314. // Authentication succeeded
  315. // $result->getIdentity() === $username
  316. }
  317. ]]>
  318. </programlisting>
  319. </para>
  320. </sect2>
  321. </sect1>
  322. <!--
  323. vim:se ts=4 sw=4 et:
  324. -->