Last change
on this file since 256 was
256,
checked in by hendrikvanantwerpen, 13 years ago
|
Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).
|
File size:
1.1 KB
|
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 | */ |
---|
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.