source: trunk/prototype/library/tonic/examples/auth/auth.php @ 6528

Revision 6528, 1.1 KB checked in by gustavo, 12 years ago (diff)

Ticket #2766 - Merge do branch das novas funcionalidaes para o trunk

  • Property svn:executable set to *
Line 
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 */
15class 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.