68789b70124847e52ce9c32071edd3bf06a6c67c
[grauphel.git] / controller / accesscontroller.php
1 <?php
2 /**
3  * Part of grauphel
4  *
5  * PHP version 5
6  *
7  * @category  Tools
8  * @package   Grauphel
9  * @author    Christian Weiske <cweiske@cweiske.de>
10  * @copyright 2014 Christian Weiske
11  * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
12  * @link      http://cweiske.de/grauphel.htm
13  */
14 namespace OCA\Grauphel\Controller;
15 use \OCP\AppFramework\Controller;
16
17 /**
18  * Login and authorization handling
19  *
20  * @category  Tools
21  * @package   Grauphel
22  * @author    Christian Weiske <cweiske@cweiske.de>
23  * @copyright 2014 Christian Weiske
24  * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
25  * @version   Release: @package_version@
26  * @link      http://cweiske.de/grauphel.htm
27  */
28 class AccessController extends Controller
29 {
30     public function login($returnUrl = null)
31     {
32         $returnUrl = $this->loadReturnUrl($returnUrl);
33
34         if (isset($_POST['user']) && trim($_POST['user']) != '') {
35             $this->deps->frontend->setUser(trim($_POST['user']));
36             header('Location: ' . $returnUrl);
37             exit(0);
38         }
39
40         $hFormUrl = htmlspecialchars(
41             $this->deps->urlGen->addParams(
42                 $this->deps->urlGen->accessLogin(),
43                 array('returnurl' => $returnUrl)
44             )
45         );
46         //FIXME: do some real login
47         header('HTTP/1.0 200 OK');
48
49         echo <<<HTM
50 <html>
51  <head>
52   <title>grauphel login</title>
53  </head>
54  <body>
55   <form method="post" action="$hFormUrl">
56    <p>
57     Log into <em>grauphel</em>:
58    </p>
59    <label>
60     User name:
61     <input id="user" type="text" name="user" size="20" value=""/>
62    </label>
63    <input type="submit" value="Login" />
64   </form>
65   <script type="text/javascript">
66 //FIXME
67 /*
68 document.getElementById('user').value = 'cweiske';
69 document.forms[0].submit();
70 /**/
71   </script>
72  </body>
73 </html>
74 HTM;
75         exit(0);
76     }
77
78     public function authorize($returnUrl = null)
79     {
80         var_dump('asd');die();
81         $returnUrl = $this->loadReturnUrl($returnUrl);
82
83         if (isset($_POST['auth'])) {
84             if ($_POST['auth'] == 'ok') {
85                 $this->deps->frontend->setAuth(true);
86             } else if ($_POST['auth'] == 'cancel') {
87                 $this->deps->frontend->setAuth(false);
88             }
89             header('Location: ' . $returnUrl);
90             exit(0);
91         }
92
93         header('HTTP/1.0 200 OK');
94         $hFormUrl = htmlspecialchars(
95             $this->deps->urlGen->addParams(
96                 $this->deps->urlGen->accessAuthorize(),
97                 array('returnurl' => $returnUrl)
98             )
99         );
100
101         echo <<<HTM
102 <html>
103  <head>
104   <title>grauphel authorization</title>
105  </head>
106  <body>
107   <form method="post" action="$hFormUrl">
108    <p>
109     Shall application FIXME get full access to the notes?
110    </p>
111    <button type="submit" name="auth" value="ok">Yes, authorize</button>
112    <button type="submit" name="auth" value="cancel">No, decline</button>
113  </body>
114 </html>
115 HTM;
116         exit(0);
117     }
118
119     protected function loadReturnUrl($returnUrl = null)
120     {
121         if ($returnUrl === null) {
122             if (isset($_GET['returnurl'])) {
123                 $returnUrl = $_GET['returnurl'];
124             } else {
125                 $returnUrl = $this->deps->urlGen->index();
126             }
127         }
128         return $returnUrl;
129     }
130
131     /**
132      * @NoAdminRequired
133      * @NoCSRFRequired
134      * @PublicPage
135      */
136     public function test()
137     {
138         var_dump('asd');die();
139         $this->registerResponder('xml', function($value) {
140                 return new XMLResponse($value);
141             });
142         return array('foo' => 'bar');
143     }
144 }
145 ?>