Issues (493)

app/modules/web/Controllers/ItemsController.php (1 issue)

Severity
1
<?php
2
/**
3
 * sysPass
4
 *
5
 * @author    nuxsmin
6
 * @link      https://syspass.org
7
 * @copyright 2012-2019, 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 DI\DependencyException;
28
use DI\NotFoundException;
29
use SP\Core\Exceptions\ConstraintException;
30
use SP\Core\Exceptions\QueryException;
31
use SP\Core\Exceptions\SessionTimeout;
32
use SP\Core\Exceptions\SPException;
33
use SP\DataModel\DataModelInterface;
34
use SP\DataModel\NotificationData;
35
use SP\Html\Html;
36
use SP\Http\Json;
37
use SP\Http\JsonResponse;
38
use SP\Mvc\View\Components\SelectItemAdapter;
39
use SP\Services\Account\AccountService;
40
use SP\Services\Category\CategoryService;
41
use SP\Services\Client\ClientService;
42
use SP\Services\Notification\NotificationService;
43
use SP\Services\Tag\TagService;
44
use stdClass;
45
46
/**
47
 * Class ItemsController
48
 *
49
 * @package SP\Modules\Web\Controllers
50
 */
51
final class ItemsController extends SimpleControllerBase
52
{
53
    /**
54
     * Devolver las cuentas visibles por el usuario
55
     *
56
     * @param int $accountId
57
     *
58
     * @throws DependencyException
59
     * @throws NotFoundException
60
     * @throws ConstraintException
61
     * @throws QueryException
62
     */
63
    public function accountsUserAction($accountId = null)
64
    {
65
        $outItems = [];
66
67
        foreach ($this->dic->get(AccountService::class)->getForUser($accountId) as $account) {
68
            $obj = new stdClass();
69
            $obj->id = $account->id;
70
            $obj->name = $account->clientName . ' - ' . $account->name;
71
72
            $outItems[] = $obj;
73
        }
74
75
        $jsonResponse = new JsonResponse();
76
        $jsonResponse->setStatus(0);
77
        $jsonResponse->setData($outItems);
78
        $jsonResponse->setCsrf($this->session->getSecurityKey());
79
80
        Json::fromDic()->returnJson($jsonResponse);
81
    }
82
83
    /**
84
     * @throws DependencyException
85
     * @throws NotFoundException
86
     * @throws ConstraintException
87
     * @throws QueryException
88
     * @throws SPException
89
     */
90
    public function clientsAction()
91
    {
92
        Json::factory($this->router->response())
93
            ->returnRawJson(
94
                SelectItemAdapter::factory(
95
                    $this->dic->get(ClientService::class)
96
                        ->getAllForUser())->getJsonItemsFromModel());
97
    }
98
99
    /**
100
     * @throws DependencyException
101
     * @throws NotFoundException
102
     * @throws ConstraintException
103
     * @throws QueryException
104
     * @throws SPException
105
     */
106
    public function categoriesAction()
107
    {
108
        Json::factory($this->router->response())
109
            ->returnRawJson(
110
                SelectItemAdapter::factory(
111
                    $this->dic->get(CategoryService::class)
112
                        ->getAllBasic())->getJsonItemsFromModel());
113
    }
114
115
    /**
116
     * @throws DependencyException
117
     * @throws NotFoundException
118
     * @throws ConstraintException
119
     * @throws QueryException
120
     * @throws SPException
121
     */
122
    public function notificationsAction()
123
    {
124
        $notifications = array_map(
125
            function ($notification) {
126
                /** @@var $notification NotificationData */
127
                return sprintf(
128
                    '(%s) - %s',
129
                    $notification->getComponent(),
130
                    Html::truncate(Html::stripTags($notification->getDescription()), 30)
131
                );
132
            }, $this->dic
133
            ->get(NotificationService::class)
134
            ->getAllActiveForUserId($this->session->getUserData()->getId()));
135
136
        $count = count($notifications);
137
138
        $jsonResponse = new JsonResponse();
139
        $jsonResponse->setStatus(0);
140
        $jsonResponse->setData([
141
            'message' => __('There aren\'t any pending notifications'),
142
            'message_has' => sprintf(__('There are pending notifications: %d'), $count),
143
            'count' => $count,
144
            'notifications' => $notifications,
145
            'hash' => sha1(implode('', $notifications))
146
        ]);
147
148
        Json::factory($this->router->response())
149
            ->returnJson($jsonResponse);
150
    }
151
152
    /**
153
     * @throws DependencyException
154
     * @throws NotFoundException
155
     * @throws ConstraintException
156
     * @throws QueryException
157
     * @throws SPException
158
     */
159
    public function tagsAction()
160
    {
161
        Json::factory($this->router->response())
162
            ->returnRawJson(
163
                SelectItemAdapter::factory(
164
                    $this->dic->get(TagService::class)
165
                        ->getAllBasic())->getJsonItemsFromModel());
166
    }
167
168
    /**
169
     * ItemsController constructor.
170
     *
171
     * @throws SessionTimeout
172
     */
173
    protected function initialize()
174
    {
175
        $this->checks();
176
    }
177
178
    /**
179
     * Preparar los elementos para devolverlos
180
     *
181
     * @param array $items
182
     *
183
     * @return array
184
     */
185
    private function prepareItems(array $items)
0 ignored issues
show
The method prepareItems() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
186
    {
187
        $outItems = [];
188
189
        /** @var DataModelInterface $item */
190
        foreach ($items as $item) {
191
            $obj = new stdClass();
192
            $obj->id = $item->getId();
193
            $obj->name = $item->getName();
194
195
            $outItems[] = $obj;
196
        }
197
198
        return $outItems;
199
    }
200
}