serviceAccount.php 8.16 KB
Newer Older
Prosper De Laure's avatar
Prosper De Laure committed
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
60
<?php

/*
 * Copyright (C) 2015 Maarch
 *
 * This file is part of bundle auth.
 *
 * Bundle auth is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Bundle auth is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with bundle auth.  If not, see <http://www.gnu.org/licenses/>.
 */

namespace Presentation\maarchRM\Presenter\auth;

/**
 * Serializer for service account in html
 *
 * @package Auth
 * @author Alexis Ragot <alexis.ragot@maarch.org>
 */
class serviceAccount
{
    use \presentation\maarchRM\Presenter\exceptions\exceptionTrait;

    public $view;
    public $json;
    public $translator;

    /**
     * Constructor
     * @param \dependency\html\Document   $view The default view document
     * @param \dependency\json\JsonObject $json The default JSON object
     *
     * @return void
     */
    public function __construct(\dependency\html\Document $view, \dependency\json\JsonObject $json)
    {
        $this->view = $view;

        $this->json = $json;
        $this->json->status = true;

        $this->translator = $view->translator;
        $this->translator->setCatalog('auth/messages');
    }

    /**
     * List all service account
     *
     * @return string
     */
61
    public function indexHtml()
Prosper De Laure's avatar
Prosper De Laure committed
62
    {
63
64
65
66
        $maxResults = null;
        if (isset(\laabs::configuration('presentation.maarchRM')['maxResults'])) {
            $maxResults = \laabs::configuration('presentation.maarchRM')['maxResults'];
        }
Prosper De Laure's avatar
Prosper De Laure committed
67
        $this->view->addContentFile("auth/serviceAccount/index.html");
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
        
        $this->view->setSource("maxResults", $maxResults);
        

        $this->view->translate();
        $this->view->merge();

        return $this->view->saveHtml();
    }

    /**
     * @param array     $serviceAccounts    Array of service accounts
     * @param integer   $count              Count of service accounts without limits
     * 
     */
    public function accountList(array $serviceAccounts, $count)
    {
Prosper De Laure's avatar
Prosper De Laure committed
85
        $this->view->setSource("serviceAccounts", $serviceAccounts);
86
87
        $this->view->setSource("totalResultsWithoutLimit", $count);
        $this->view->addContentFile("auth/serviceAccount/serviceAccountsList.html");
88
        $hasSecurityLevel = isset(\laabs::configuration('auth')['useSecurityLevel']) ? (bool) \laabs::configuration('auth')['useSecurityLevel'] : false;
89
90
91
92
93
94
        $accountId = \laabs::getToken("AUTH")->accountId;
        $account = \laabs::callService("auth/userAccount/read_userAccountId_", $accountId);

        $securityLevel = $account->securityLevel;

        $manageUserRights = true;
95
        if ($hasSecurityLevel && $securityLevel == \bundle\auth\Model\account::SECLEVEL_USER) {
96
97
98
            $manageUserRights = false;
        }

99
100
101
102
103
104
105
        $hasReachMaxResults = false;
        if (isset(\laabs::configuration('presentation.maarchRM')['maxResults'])
            && count($serviceAccounts) >= \laabs::configuration('presentation.maarchRM')['maxResults']) {
            $hasReachMaxResults = true;
        }

        $this->view->setSource('hasReachMaxResults', $hasReachMaxResults);
106
107
        $this->view->setSource('manageUserRights', $manageUserRights);

Prosper De Laure's avatar
Prosper De Laure committed
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
        $table = $this->view->getElementById("list-serviceAccount");
        $dataTable = $table->plugin['dataTable'];
        $dataTable->setPaginationType("full_numbers");
        $dataTable->setUnsortableColumns(3);
        $dataTable->setUnsearchableColumns(1, 3);

        $this->view->translate();
        $this->view->merge();

        return $this->view->saveHtml();
    }

    /**
     * View form to edit a token
     * @param auth/serviceAccount $serviceAccount Service account object
     *
     * @return string
     */
    public function edit($serviceAccount)
    {
Arnaud Pauget's avatar
Arnaud Pauget committed
128
129
130
        if (isset($serviceAccount->orgId)) {
            $serviceAccount->orgName = \laabs::callService('organization/organization/read_orgId_', (string) $serviceAccount->orgId)->displayName;
            $serviceAccount->ownerOrgName = \laabs::callService('organization/organization/read_orgId_', (string) $serviceAccount->ownerOrgId)->displayName;
Dylan's avatar
Dylan committed
131
132
        }

133
        if ($serviceAccount->servicePrivilege) {
134
            $noDescription = true;
135
            foreach ($serviceAccount->servicePrivilege as $servicePrivilege) {
136
                foreach ($serviceAccount->servicePrivilegeOptions as $option) {
137
                    if ($servicePrivilege->serviceURI == $option->serviceURI) {
138
139
140
141
                        $servicePrivilege->description = $option->description;
                        $noDescription = false;
                    }
                }
142
                if ($noDescription) {
143
144
145
146
                    $servicePrivilege->description =  $servicePrivilege->serviceURI;
                }
                $noDescription = true;
            }
147
        }
148

149
150
151
        $accountId = \laabs::getToken("AUTH")->accountId;
        $account = \laabs::callService("auth/userAccount/read_userAccountId_", $accountId);

152
153
        if (!is_null($account->securityLevel) &&  $account->securityLevel != "") {
            $whatAmI = $account->securityLevel;
154
155
        } else {
            $whatAmI = 'userWithoutSecurityLevel';
156
157
        }

Prosper De Laure's avatar
Prosper De Laure committed
158
159
        $this->view->addContentFile("auth/serviceAccount/edit.html");
        $this->view->setSource("serviceAccount", $serviceAccount);
160
        $this->view->setSource('whatAmI', $whatAmI);
161

Prosper De Laure's avatar
Prosper De Laure committed
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259

        $this->view->translate();
        $this->view->merge();

        return $this->view->saveHtml();
    }

    /**
     * Json serializer for creation method
     * @param auth/serviceAccount $serviceAccount Service account object
     *
     * @return string
     */
    public function create($serviceAccount)
    {
        $this->json->message = "Service account created";
        $this->json->message = $this->translator->getText($this->json->message);

        return $this->json->save();
    }

    /**
     * Json serializer for update method
     * @param auth/serviceAccount $serviceAccount Service account object
     *
     * @return string
     */
    public function update($serviceAccount)
    {
        $this->json->message = "Service account updated";
        $this->json->message = $this->translator->getText($this->json->message);

        return $this->json->save();
    }

    /**
     * Json serializer for enable method
     *
     * @return string
     */
    public function enable()
    {
        $this->json->message = "Service account is enabled";
        $this->json->message = $this->translator->getText($this->json->message);

        return $this->json->save();
    }

    /**
     * Json serializer for disable method
     *
     * @return string
     */
    public function disable()
    {
        $this->json->message = "Service account is disabled";
        $this->json->message = $this->translator->getText($this->json->message);

        return $this->json->save();
    }

    /**
     * Exception
     * @param auth/Exception/serviceAlreadyExistException $serviceException
     *
     * @return string
     */
    public function serviceAlreadyExistException($serviceException)
    {
        $this->json->load($serviceException);
        $this->json->message = $this->translator->getText($this->json->message);
        $this->json->status = false;

        return $this->json->save();
    }

    public function serviceToken($cookieToken) {
        $this->json->cookie = $cookieToken;
        $this->json->message = "Token changed";
        $this->json->message = $this->translator->getText($this->json->message);
        $this->json->status = true;

        return $this->json->save();
    }
    /**
     * Exception
     * @param auth/Exception/badValueException $exception
     *
     * @return string
     */
    public function badValueException($exception)
    {
        $this->json->message = $this->translator->getText($exception->getMessage());
        $this->json->status = false;

        return $this->json->save();
    }
}