## Apache Zeta Components Manual :: File Source for database_filter.php

## Source for file database_filter.php

Documentation is available at database_filter.php

1. <?php
2. /**
3. * File containing the ezcAuthenticationDatabaseFilter class.
4. *
5. * Licensed to the Apache Software Foundation (ASF) under one
6. * or more contributor license agreements. See the NOTICE file
7. * distributed with this work for additional information
8. * regarding copyright ownership. The ASF licenses this file
9. * to you under the Apache License, Version 2.0 (the
10. * "License"); you may not use this file except in compliance
11. * with the License. You may obtain a copy of the License at
12. *
13. * http://www.apache.org/licenses/LICENSE-2.0
14. *
15. * Unless required by applicable law or agreed to in writing,
16. * software distributed under the License is distributed on an
17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18. * KIND, either express or implied. See the License for the
19. * specific language governing permissions and limitations
20. * under the License.
21. *
22. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
23. * @filesource
24. * @package AuthenticationDatabaseTiein
25. * @version //autogentag//
26. */
28. /**
29. * Filter to authenticate against a database.
30. *
31. * The database instance to use is specified using a ezcAuthenticationDatabaseInfo
32. * structure. Table name and field names are specified in the same structure.
33. *
34. * Example:
35. * <code>
36. * $credentials = new ezcAuthenticationPasswordCredentials( 'jan.modaal', 'b1b3773a05c0ed0176787a4f1574ff0075f7521e' );
37. * $database = new ezcAuthenticationDatabaseInfo( ezcDbInstance::get(), 'users', array( 'user', 'password' ) );
38. * $authentication = new ezcAuthentication( $credentials );
39. * $authentication->addFilter( new ezcAuthenticationDatabaseFilter( $database ) );
40. * if ( !$authentication->run() )
41. * {
42. * // authentication did not succeed, so inform the user
43. * $status = $authentication->getStatus();
44. * $err = array(
45. * 'ezcAuthenticationDatabaseFilter' => array(
46. * ezcAuthenticationDatabaseFilter::STATUS_USERNAME_INCORRECT => 'Incorrect username',
47. * ezcAuthenticationDatabaseFilter::STATUS_PASSWORD_INCORRECT => 'Incorrect password'
48. * )
49. * );
50. * foreach ( $status as $line )
51. * {
52. * list( $key, $value ) = each( $line );
53. * echo $err[$key][$value] . "\n";
54. * }
55. * }
56. * else
57. * {
58. * // authentication succeeded, so allow the user to see his content
59. * }
60. * </code>
61. *
62. * Extra data can be fetched from the database during the authentication process,
63. * by registering the data to be fetched before calling run(). Example:
64. * <code>
65. * // $filter is an ezcAuthenticationDatabaseFilter object
66. * $filter->registerFetchData( array( 'name', 'country' ) );
67. *
68. * // after run()
69. * $data = $filter->fetchData();
70. * </code>
71. *
72. * The $data array will be something like:
73. * <code>
74. * array( 'name' => array( 'John Doe' ),
75. * 'country' => array( 'US' )
76. * );
77. * </code>
78. *
79. * @property ezcAuthenticationDatabaseInfo $database
80. * Structure which holds a database instance, table name and fields
81. * which are used for authentication.
82. *
83. * @package AuthenticationDatabaseTiein
84. * @version //autogentag//
85. * @mainclass
86. */
87. class ezcAuthenticationDatabaseFilter extends ezcAuthenticationFilter implements ezcAuthenticationDataFetch
88. {
89. /**
90. * Username is not found in the database.
91. */
92. const STATUS_USERNAME_INCORRECT = 1;
94. /**
95. * Password is incorrect.
96. */
97. const STATUS_PASSWORD_INCORRECT = 2;
99. /**
100. * Holds the attributes which will be requested during the authentication
101. * process.
102. *
103. * Usually it has this structure:
104. * <code>
105. * array( 'fullname', 'gender', 'country', 'language' );
106. * </code>
107. *
108. * @var array(string)
109. */
110. protected $requestedData = array();
112. /**
113. * Holds the extra data fetched during the authentication process.
114. *
115. * Usually it has this structure:
116. * <code>
117. * array( 'name' => array( 'John Doe' ),
118. * 'country' => array( 'US' )
119. * );
120. * </code>
121. *
122. * @var array(string=>mixed)
123. */
124. protected $data = array();
126. /**
127. * Holds the properties of this class.
128. *
129. * @var array(string=>mixed)
130. */
131. private $properties = array();
133. /**
134. * Creates a new object of this class.
135. *
136. * @param ezcAuthenticationDatabaseInfo $database Database to use in authentication
137. * @param ezcAuthenticationDatabaseOptions $options Options for this class
138. */
139. public function __construct ( ezcAuthenticationDatabaseInfo $database, ezcAuthenticationDatabaseOptions $options = null )
140. {
141. $this-> options = ( $options === null ) ? new ezcAuthenticationDatabaseOptions () : $options;
142. $this->database = $database;
143. }
145. /**
146. * Sets the property $name to $value.
147. *
148. * @throws ezcBasePropertyNotFoundException
149. * if the property $name does not exist
150. * @throws ezcBaseValueException
151. * if $value is not correct for the property $name
152. * @param string $name The name of the property to set
153. * @param mixed $value The new value of the property
154. * @ignore
155. */
156. public function __set( $name, $value )
157. {
158. switch ( $name )
159. {
160. case 'database':
161. if ( $value instanceof ezcAuthenticationDatabaseInfo )
162. {
163. $this->properties[$name] = $value;
164. }
165. else
166. {
167. throw new ezcBaseValueException ( $name, $value, 'ezcAuthenticationDatabaseInfo' );
168. }
169. break;
171. default:
172. throw new ezcBasePropertyNotFoundException ( $name );
173. }
174. }
176. /**
177. * Returns the value of the property $name.
178. *
179. * @throws ezcBasePropertyNotFoundException
180. * if the property $name does not exist
181. * @param string $name The name of the property for which to return the value
182. * @return mixed
183. * @ignore
184. */
185. public function __get( $name )
186. {
187. switch ( $name )
188. {
189. case 'database':
190. return $this->properties[$name];
192. default:
193. throw new ezcBasePropertyNotFoundException ( $name );
194. }
195. }
197. /**
198. * Returns true if the property $name is set, otherwise false.
199. *
200. * @param string $name The name of the property to test if it is set
201. * @return bool
202. * @ignore
203. */
204. public function __isset( $name )
205. {
206. switch ( $name )
207. {
208. case 'database':
209. return isset( $this->properties[$name] );
211. default:
212. return false;
213. }
214. }
216. /**
217. * Runs the filter and returns a status code when finished.
218. *
219. * @param ezcAuthenticationPasswordCredentials $credentials Authentication credentials
220. * @return int
221. */
222. public function run ( $credentials )
223. {
224. $db = $this->database;
226. // see if username exists
227. $query = new ezcQuerySelect ( $db->instance );
228. $e = $query->expr;
229. $query->select( 'COUNT( ' . $db->instance->quoteIdentifier( $db->fields[0] ) . ' )' )
230. ->from( $db->instance->quoteIdentifier( $db->table ) )
231. ->where(
232. $e->eq( $db->instance->quoteIdentifier( $db->fields[0] ), $query->bindValue( $credentials->id ) )
233. );
234. $rows = $query->prepare();
235. $rows->execute();
236. $count = (int)$rows->fetchColumn( 0 );
237. if ( $count === 0 )
238. {
239. return self::STATUS_USERNAME_INCORRECT;
240. }
241. $rows->closeCursor();
243. // see if username has the specified password
244. $query = new ezcQuerySelect ( $db->instance );
245. $e = $query->expr;
246. $query->select( 'COUNT( ' . $db->instance->quoteIdentifier( $db->fields[0] ) . ' )' )
247. ->from( $db->instance->quoteIdentifier( $db->table ) )
248. ->where( $e->lAnd(
249. $e->eq( $db->instance->quoteIdentifier( $db->fields[0] ), $query->bindValue( $credentials->id ) ),
250. $e->eq( $db->instance->quoteIdentifier( $db->fields[1] ), $query->bindValue( $credentials->password ) )
251. ) );
252. $rows = $query->prepare();
253. $rows->execute();
254. $count = (int)$rows->fetchColumn( 0 );
255. if ( $count === 0 )
256. {
257. return self::STATUS_PASSWORD_INCORRECT;
258. }
259. $rows->closeCursor();
261. if ( count ( $this-> requestedData ) > 0 )
262. {
263. // fetch extra data from the database
264. $query = new ezcQuerySelect ( $db->instance );
265. $e = $query->expr;
266. $params = array();
267. foreach ( $this-> requestedData as $param )
268. {
269. $params[] = $db->instance->quoteIdentifier( $param );
270. }
271. $query->select( implode ( ', ', $params ) )
272. ->from( $db->instance->quoteIdentifier( $db->table ) )
273. ->where( $e->lAnd(
274. $e->eq( $db->instance->quoteIdentifier( $db->fields[0] ), $query->bindValue( $credentials->id ) ),
275. $e->eq( $db->instance->quoteIdentifier( $db->fields[1] ), $query->bindValue( $credentials->password ) )
276. ) );
277. $rows = $query->prepare();
278. $rows->execute();
279. $data = $rows->fetchAll();
280. $data = $data[0];
282. foreach ( $this-> requestedData as $attribute )
283. {
284. $this-> data [$attribute] = array( $data[$attribute] );
285. }
286. }
288. return self::STATUS_OK;
289. }
291. /**
292. * Registers the extra data which will be fetched by the filter during the
293. * authentication process.
294. *
295. * The input $data should be an array of attributes, for example:
296. * <code>
297. * array( 'name', 'country' );
298. * </code>
299. *
300. * @param array(string) $data The extra data to fetch during authentication
301. */
302. public function registerFetchData ( array $data = array() )
303. {
304. $this-> requestedData = $data;
305. }
307. /**
308. * Returns the extra data which was fetched during the authentication process.
309. *
310. * Example of returned array:
311. * <code>
312. * array( 'name' => array( 'John Doe' ),
313. * 'country' => array( 'US' )
314. * );
315. * </code>
316. *
317. * @return array(string=>mixed)
318. */
319. public function fetchData ()
320. {
321. return $this-> data ;
322. }
323. }
324. ?>

Documentation generated by phpDocumentor 1.4.3