Passed
Push — devel-3.0 ( ca620b...2581df )
by Rubén
03:02
created

TrackController::unlockAction()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 5
nop 1
dl 0
loc 16
rs 9.9666
c 0
b 0
f 0
1
<?php
2
/**
3
 * sysPass
4
 *
5
 * @author    nuxsmin
6
 * @link      https://syspass.org
7
 * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org
8
 *
9
 * This file is part of sysPass.
10
 *
11
 * sysPass is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation, either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * sysPass is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 *  along with sysPass.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace SP\Modules\Web\Controllers;
26
27
use SP\Core\Acl\Acl;
28
use SP\Core\Acl\UnauthorizedActionException;
29
use SP\Core\Events\Event;
30
use SP\Http\JsonResponse;
31
use SP\Modules\Web\Controllers\Helpers\Grid\TrackGrid;
32
use SP\Modules\Web\Controllers\Traits\ItemTrait;
33
use SP\Modules\Web\Controllers\Traits\JsonTrait;
34
use SP\Services\Track\TrackService;
35
36
/**
37
 * Class TrackController
38
 *
39
 * @package SP\Modules\Web\Controllers
40
 */
41
final class TrackController extends ControllerBase
42
{
43
    use JsonTrait, ItemTrait;
0 ignored issues
show
introduced by
The trait SP\Modules\Web\Controllers\Traits\ItemTrait requires some properties which are not provided by SP\Modules\Web\Controllers\TrackController: $data, $key
Loading history...
44
45
    /**
46
     * @var TrackService
47
     */
48
    protected $trackService;
49
50
    /**
51
     * Search action
52
     *
53
     * @return bool
54
     * @throws \DI\DependencyException
55
     * @throws \DI\NotFoundException
56
     * @throws \SP\Core\Exceptions\ConstraintException
57
     * @throws \SP\Core\Exceptions\QueryException
58
     * @throws UnauthorizedActionException
59
     */
60
    public function searchAction()
61
    {
62
        if (!$this->acl->checkUserAccess(Acl::TRACK_SEARCH)) {
63
            throw new UnauthorizedActionException(UnauthorizedActionException::ERROR);
64
        }
65
66
        $this->view->addTemplate('datagrid-table', 'grid');
67
        $this->view->assign('index', $this->request->analyzeInt('activetab', 0));
68
        $this->view->assign('data', $this->getSearchGrid());
69
70
        return $this->returnJsonResponseData(['html' => $this->render()]);
71
    }
72
73
    /**
74
     * getSearchGrid
75
     *
76
     * @return $this
77
     * @throws \DI\DependencyException
78
     * @throws \DI\NotFoundException
79
     * @throws \SP\Core\Exceptions\ConstraintException
80
     * @throws \SP\Core\Exceptions\QueryException
81
     */
82
    protected function getSearchGrid()
83
    {
84
        $itemSearchData = $this->getSearchData($this->configData->getAccountCount(), $this->request);
85
86
        $itemsGridHelper = $this->dic->get(TrackGrid::class);
87
88
        return $itemsGridHelper->updatePager($itemsGridHelper->getGrid($this->trackService->search($itemSearchData)), $itemSearchData);
89
    }
90
91
    /**
92
     * Unlocks a track
93
     *
94
     * @param int $id
95
     *
96
     * @return bool
97
     * @throws UnauthorizedActionException
98
     */
99
    public function unlockAction($id)
100
    {
101
        if (!$this->acl->checkUserAccess(Acl::TRACK_UNLOCK)) {
102
            throw new UnauthorizedActionException(UnauthorizedActionException::ERROR);
103
        }
104
105
        try {
106
            $this->trackService->unlock($id);
107
108
            $this->eventDispatcher->notifyEvent('unlock.track', new Event($this));
109
110
            return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Track desbloqueado'));
111
        } catch (\Exception $e) {
112
            processException($e);
113
114
            return $this->returnJsonResponseException($e);
115
        }
116
    }
117
118
    /**
119
     * Clears tracks
120
     *
121
     * @return bool
122
     * @throws UnauthorizedActionException
123
     */
124
    public function clearAction()
125
    {
126
        if (!$this->acl->checkUserAccess(Acl::TRACK_CLEAR)) {
127
            throw new UnauthorizedActionException(UnauthorizedActionException::ERROR);
128
        }
129
130
        try {
131
            $this->trackService->clear();
132
133
            $this->eventDispatcher->notifyEvent('clear.track', new Event($this));
134
135
            return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Tracks limpiados'));
136
        } catch (\Exception $e) {
137
            processException($e);
138
139
            return $this->returnJsonResponseException($e);
140
        }
141
    }
142
143
    /**
144
     * @throws \DI\DependencyException
145
     * @throws \DI\NotFoundException
146
     * @throws \SP\Services\Auth\AuthException
147
     */
148
    protected function initialize()
149
    {
150
        $this->checkLoggedIn();
151
152
        $this->trackService = $this->dic->get(TrackService::class);
153
    }
154
}