Revision 6754,
1.1 KB
checked in by niltonneto, 12 years ago
(diff) |
Ticket #0000 - Copiadas as alterações do Trunk. Versão final da 2.4.1.
|
-
Property svn:executable set to
*
|
Rev | Line | |
---|
[6351] | 1 | <?php |
---|
| 2 | |
---|
| 3 | /** |
---|
| 4 | * Authentication example |
---|
| 5 | * |
---|
| 6 | * An example authentication resource, the isSecured() method can be used to ensure |
---|
| 7 | * that only authorised users can access the resource. |
---|
| 8 | * |
---|
| 9 | * username: user |
---|
| 10 | * password: pass |
---|
| 11 | * |
---|
| 12 | * @namespace Tonic\Examples\Auth |
---|
| 13 | * @uri /auth |
---|
| 14 | */ |
---|
| 15 | class AuthResource extends Resource { |
---|
| 16 | |
---|
| 17 | const USERNAME = 'user'; |
---|
| 18 | const PASSWORD = 'pass'; |
---|
| 19 | |
---|
| 20 | function isSecured() { |
---|
| 21 | |
---|
| 22 | if ( |
---|
| 23 | isset($_SERVER['PHP_AUTH_USER']) && $_SERVER['PHP_AUTH_USER'] == AuthResource::USERNAME && |
---|
| 24 | isset($_SERVER['PHP_AUTH_PW']) && $_SERVER['PHP_AUTH_PW'] == AuthResource::PASSWORD |
---|
| 25 | ) { |
---|
| 26 | return; |
---|
| 27 | } |
---|
| 28 | |
---|
| 29 | throw new ResponseException('Incorrect username and password', Response::UNAUTHORIZED); |
---|
| 30 | |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | /** |
---|
| 34 | * Handle a GET request for this resource |
---|
| 35 | * @param Request request |
---|
| 36 | * @return Response |
---|
| 37 | */ |
---|
| 38 | function get($request) { |
---|
| 39 | |
---|
| 40 | $this->isSecured(); |
---|
| 41 | |
---|
| 42 | $response = new Response($request); |
---|
| 43 | |
---|
| 44 | $response->body = 'You have access to the secret'; |
---|
| 45 | |
---|
| 46 | return $response; |
---|
| 47 | |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | } |
---|
| 51 | |
---|
Note: See
TracBrowser
for help on using the repository browser.