ExternalSignatoryBookController.php 10.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php

/**
* Copyright Maarch since 2008 under license.
* See LICENSE.txt file at the root folder for more details.
* This file is part of Maarch software.
*
*/

/**
* @brief ExternalSignatoryBook Controller
* @author dev@maarch.org
*/

namespace ExternalSignatoryBook\controllers;

17
use Document\models\DocumentModel;
18
19
20
21
22
23
use ExternalSignatoryBook\models\ExternalSignatoryBookModel;
use Group\controllers\PrivilegeController;
use History\controllers\HistoryController;
use Respect\Validation\Validator;
use Slim\Http\Request;
use Slim\Http\Response;
24
use Workflow\controllers\WorkflowController;
25
use Workflow\models\WorkflowModel;
26
27
28
29
30

class ExternalSignatoryBookController
{
    public function get(Request $request, Response $response)
    {
31
        $connectors = ExternalSignatoryBookModel::get(['select' => ['id', 'label', 'type', 'otp_code'], 'orderBy' => ['label']]);
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
        foreach ($connectors as $key => $data) {
            $connectors[$key]['securityModes'] = json_decode($data['otp_code'], true);
            unset($connectors[$key]['otp_code']);
        }

        return $response->withJson(['otp' => $connectors]);
    }

    public function getById(Request $request, Response $response, array $args)
    {
        if (!PrivilegeController::hasPrivilege(['userId' => $GLOBALS['id'], 'privilege' => 'manage_otp_connectors'])) {
            return $response->withStatus(403)->withJson(['errors' => 'Privilege forbidden']);
        }

        if (!Validator::intVal()->notEmpty()->validate($args['id'])) {
            return $response->withStatus(400)->withJson(['errors' => 'Route id must be an integer']);
        }

        $connector = ExternalSignatoryBookModel::getById(['id' => $args['id']]);
        if (empty($connector)) {
            return $response->withStatus(400)->withJson(['errors' => 'Connector not found']);
        }

        $connector['securityModes'] = json_decode($connector['otp_code'], true);
        unset($connector['otp_code']);

58
59
60
        $connector['message'] = json_decode($connector['message_content'], true);
        unset($connector['message_content']);

61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
        $connectionData = json_decode($connector['connection_data'], true);
        $connector['apiUri'] = $connectionData['apiUri'];
        $connector['apiKey'] = $connectionData['apiKey'];
        unset($connector['connection_data']);

        return $response->withJson(['otp' => $connector]);
    }

    public function create(Request $request, Response $response)
    {
        if (!PrivilegeController::hasPrivilege(['userId' => $GLOBALS['id'], 'privilege' => 'manage_otp_connectors'])) {
            return $response->withStatus(403)->withJson(['errors' => 'Privilege forbidden']);
        }

        $body = $request->getParsedBody();

77
78
79
        $control = ExternalSignatoryBookController::control(['body' => $body]);
        if (!empty($control['errors'])) {
            return $response->withStatus(400)->withJson(['errors' => $control['errors']]);
80
81
82
83
84
85
86
87
88
        }

        $connectionData = ['apiUri' => $body['apiUri'], 'apiKey' => $body['apiKey']];

        $id = ExternalSignatoryBookModel::create([
            'label'           => $body['label'],
            'type'            => $body['type'],
            'connection_data' => json_encode($connectionData),
            'otp_code'        => json_encode($body['securityModes']),
89
            'message_content' => json_encode($body['message']),
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
        ]);

        HistoryController::add([
            'code'          => 'OK',
            'objectType'    => 'connectors',
            'objectId'      => $id,
            'type'          => 'CREATION',
            'message'       => "{connectorAdded} : {$body['label']}"
        ]);

        return $response->withJson(['id' => $id]);
    }

    public function update(Request $request, Response $response, array $aArgs)
    {
        if (!PrivilegeController::hasPrivilege(['userId' => $GLOBALS['id'], 'privilege' => 'manage_otp_connectors'])) {
            return $response->withStatus(403)->withJson(['errors' => 'Privilege forbidden']);
        }

        $body = $request->getParsedBody();

111
        if (!Validator::intVal()->notEmpty()->validate($aArgs['id'])) {
112
113
114
            return $response->withStatus(400)->withJson(['errors' => 'Id must be an integer']);
        }

115
116
117
        $control = ExternalSignatoryBookController::control(['body' => $body]);
        if (!empty($control['errors'])) {
            return $response->withStatus(400)->withJson(['errors' => $control['errors']]);
118
119
120
121
122
123
124
125
126
127
128
129
130
131
        }

        $connectionData = ['apiUri' => $body['apiUri'], 'apiKey' => $body['apiKey']];

        $connector = ExternalSignatoryBookModel::getById(['id' => $aArgs['id']]);
        if (empty($connector)) {
            return $response->withStatus(400)->withJson(['errors' => 'Connector not found']);
        }

        ExternalSignatoryBookModel::update([
            'set' => [
                'label'           => $body['label'],
                'type'            => $body['type'],
                'connection_data' => json_encode($connectionData),
132
133
                'otp_code'        => json_encode($body['securityModes']),
                'message_content' => json_encode($body['message']),
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
            ],
            'where' => ['id = ?'],
            'data'  => [$aArgs['id']]
        ]);

        HistoryController::add([
            'code'       => 'OK',
            'objectType' => 'connectors',
            'objectId'   => $aArgs['id'],
            'type'       => 'MODIFICATION',
            'message'    => "{connectorUpdated} : {$body['label']}"
        ]);

        return $response->withStatus(204);
    }

150
151
152
153
154
155
156
157
158
159
160
161
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
    public static function control(array $args)
    {
        if (empty($args['body'])) {
            return ['errors' => 'Body is not set or empty'];
        } elseif (!Validator::stringType()->notEmpty()->length(1, 255)->validate($args['body']['label'])) {
            return ['errors' => 'Body label is empty or not a string or longer than 255 caracteres'];
        } elseif (!Validator::stringType()->notEmpty()->length(1, 128)->validate($args['body']['type'])) {
            return ['errors' => 'Body type is empty or not a string or longer than 128 caracteres'];
        } elseif (!Validator::arrayType()->notEmpty()->validate($args['body']['securityModes'])) {
            return ['errors' => 'Body securityModes is empty or not an array'];
        }

        if ($args['body']['type'] != 'yousign') {
            return ['errors' => 'Only type yousign is allowed'];
        }

        $invalidOtpCode = array_diff($args['body']['securityModes'], ['sms', 'email']);
        if (!empty($invalidOtpCode)) {
            return ['errors' => 'Only sms and/or email are allowed for securityModes'];
        }

        if ($args['body']['type'] == 'yousign') {
            if (empty($args['body']['apiUri']) || empty($args['body']['apiKey'])) {
                return ['errors' => 'Body apiUri or apiKey is empty'];
            }
            if (empty($args['body']['message']['notification']['subject']) || empty($args['body']['message']['notification']['body'])) {
                return ['errors' => 'Body message notification must have subject and body attributes'];
            }
            if (!empty($args['body']['message']['otp_sms']) && !Validator::stringType()->notEmpty()->length(1, 150)->validate($args['body']['message']['otp_sms'])) {
                return ['errors' => 'Body message otp_sms length must be less than 150 caracteres'];
            }
            if (!empty($args['body']['message']['otp_sms']) && strpos($args['body']['message']['otp_sms'], '{{code}}') === false) {
                return ['errors' => 'Body message otp_sms must contains {{code}}'];
            }
            if (in_array('sms', $args['body']['securityModes']) && empty($args['body']['message']['otp_sms'])) {
                return ['errors' => 'Body message otp_sms must be set'];
            }
        }

        return [];
    }

192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
    public function delete(Request $request, Response $response, array $aArgs)
    {
        if (!PrivilegeController::hasPrivilege(['userId' => $GLOBALS['id'], 'privilege' => 'manage_otp_connectors'])) {
            return $response->withStatus(403)->withJson(['errors' => 'Privilege forbidden']);
        }

        if (!Validator::intVal()->notEmpty()->validate($aArgs['id'])) {
            return $response->withStatus(400)->withJson(['errors' => 'Id must be an integer']);
        }

        $connector = ExternalSignatoryBookModel::getById(['id' => $aArgs['id']]);
        if (empty($connector)) {
            return $response->withStatus(400)->withJson(['errors' => 'Connector not found']);
        }

207
208
        $workflows = WorkflowModel::getWorkflowWithExternalInfo([
            'select' => ['DISTINCT(w.main_document_id)'],
209
            'where'  => ['w.status is null', 'wei.external_signatory_book_id = ?'],
210
211
212
213
            'data'   => [$aArgs['id']]
        ]);
        $currentDocumentsId = array_column($workflows, 'main_document_id');

214
215
216
217
218
219
220
        if (!empty($currentDocumentsId)) {
            $documents = DocumentModel::get(['select' => ['typist', 'title', 'id'], 'where' => ['id in (?)'], 'data' => [$currentDocumentsId]]);
            foreach ($documents as $document) {
                $process = WorkflowController::interruptProcess(['id' => $document['id'], 'documentTypist' => $document['typist'], 'documentTitle' => $document['title']]);
                if (!empty($process['errors'])) {
                    return $response->withStatus(400)->withJson(['errors' => $process['errors']]);
                }
221
222
223
            }
        }

224
225
226
        ExternalSignatoryBookModel::delete(['where' => ['id = ?'], 'data' => [$aArgs['id']]]);

        HistoryController::add([
227
228
229
230
231
            'code'       => 'OK',
            'objectType' => 'connectors',
            'objectId'   => $aArgs['id'],
            'type'       => 'SUPPRESSION',
            'message'    => "{connectordeleted} : {$connector['label']}"
232
233
234
235
        ]);

        return $response->withStatus(204);
    }
236
237
238
239
240
241
242
243
244
245
246

    public function getCurrentVisa(Request $request, Response $response, array $aArgs)
    {
        if (!PrivilegeController::hasPrivilege(['userId' => $GLOBALS['id'], 'privilege' => 'manage_otp_connectors'])) {
            return $response->withStatus(403)->withJson(['errors' => 'Privilege forbidden']);
        }

        if (!Validator::intVal()->notEmpty()->validate($aArgs['id'])) {
            return $response->withStatus(400)->withJson(['errors' => 'Id must be an integer']);
        }

247
248
        $workflows = WorkflowModel::getWorkflowWithExternalInfo([
            'select' => ['DISTINCT(w.main_document_id)'],
249
            'where'  => ['w.status is null', 'wei.external_signatory_book_id = ?'],
250
251
            'data'   => [$aArgs['id']]
        ]);
252
253
254

        return $response->withJson(['nbCurrentWorkflow' => count($workflows)]);
    }
255
}