summaryrefslogtreecommitdiff
path: root/app/Controller/UserApiAccessController.php
blob: f9628ae6e10cc3fa3ccc4021507ed07f32e18cde (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php

namespace Kanboard\Controller;

use Kanboard\Core\Security\Token;

/**
 * Class UserApiAccessController
 *
 * @package Kanboard\Controller
 * @author  Frederic Guillot
 */
class UserApiAccessController extends BaseController
{
    public function show()
    {
        $user = $this->getUser();

        return $this->response->html($this->helper->layout->user('user_api_access/show', array(
            'user'  => $user,
            'title' => t('API User Access'),
        )));
    }

    public function generate()
    {
        $user = $this->getUser();
        $this->checkCSRFParam();

        $this->userModel->update(array(
            'id' => $user['id'],
            'api_access_token' => Token::getToken(),
        ));

        $this->renderResponse($user);
    }

    public function remove()
    {
        $user = $this->getUser();
        $this->checkCSRFParam();

        $this->userModel->update(array(
            'id' => $user['id'],
            'api_access_token' => null,
        ));

        $this->renderResponse($user);
    }

    protected function renderResponse(array $user)
    {
        if ($this->request->isAjax()) {
            $this->show();
        } else {
            $this->response->redirect($this->helper->url->to('UserApiAccessController', 'show', array('user_id' => $user['id'])));
        }
    }
}