organization.php 69.9 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
<?php
/*
 * Copyright (C) 2015 Maarch
 *
 * This file is part of bundle organization.
 *
 * Bundle organization 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 organization 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 organization.  If not, see <http://www.gnu.org/licenses/>.
 */
namespace bundle\organization\Controller;
21

22
use core\Exception;
Prosper De Laure's avatar
Prosper De Laure committed
23
24
25
26
27

/**
 * Control of the organization
 *
 * @package Organization
28
 * @author  Prosper De Laure <prosper.delaure@maarch.org>
Prosper De Laure's avatar
Prosper De Laure committed
29
30
31
32
 */
class organization
{
    protected $sdoFactory;
33
    protected $csv;
34
    protected $accountController;
35
    protected $hasSecurityLevel;
Prosper De Laure's avatar
Prosper De Laure committed
36
37
38

    /**
     * Constructor
Alexis Ragot's avatar
Alexis Ragot committed
39
     * @param \dependency\sdo\Factory $sdoFactory The dependency sdo factory service
40
     * @param \dependency\csv\Csv     $csv        The dependency csv
Prosper De Laure's avatar
Prosper De Laure committed
41
42
43
     *
     * @return void
     */
44
    public function __construct(\dependency\sdo\Factory $sdoFactory, \dependency\csv\Csv $csv = null)
Prosper De Laure's avatar
Prosper De Laure committed
45
46
    {
        $this->sdoFactory = $sdoFactory;
47
        $this->csv = $csv;
48
        $this->accountController = \laabs::newController('auth/userAccount');
49
        $this->hasSecurityLevel = isset(\laabs::configuration('auth')['useSecurityLevel']) ? (bool) \laabs::configuration('auth')['useSecurityLevel'] : false;
Prosper De Laure's avatar
Prosper De Laure committed
50
51
52
53
    }

    /**
     * Index of organizations
54
55
     *
     * @param integer $limit Maximal number of results to dispay
Jerome Boucher's avatar
Jerome Boucher committed
56
     * @param string  $query The query of the index
Prosper De Laure's avatar
Prosper De Laure committed
57
     *
Dylan's avatar
Dylan committed
58
     * @return organization/organization[] An array of organization
Prosper De Laure's avatar
Prosper De Laure committed
59
     */
60
    public function index($limit = null, $query = null)
Prosper De Laure's avatar
Prosper De Laure committed
61
    {
62
        return $this->sdoFactory->index("organization/organization", array("orgId", "displayName", "isOrgUnit", "registrationNumber", "parentOrgId", "ownerOrgId"), $query, null, null, $limit);
Prosper De Laure's avatar
Prosper De Laure committed
63
64
    }

Dylan's avatar
Dylan committed
65
66
67
    /**
     * List of organizations
     *
68
69
     * @param bool $ownerOrg
     * @param bool $orgUnit
Arnaud Pauget's avatar
Arnaud Pauget committed
70
     * @param string $term The term to search in database
71
     *
Dylan's avatar
Dylan committed
72
     * @return organization/organization[] An array of organization whith service
Dylan's avatar
Dylan committed
73
     */
Arnaud Pauget's avatar
Arnaud Pauget committed
74
    public function todisplay($ownerOrg, $orgUnit, $term)
Dylan's avatar
Dylan committed
75
    {
76
77
78
        $authController = \laabs::newController("auth/userAccount");
        $user = $authController->get(\laabs::getToken('AUTH')->accountId);

Dylan's avatar
Dylan committed
79
        $currentOrg = \laabs::getToken("ORGANIZATION");
80
        $orgList = [];
Dylan's avatar
Dylan committed
81

82
        if (!$currentOrg || (!empty($currentOrg->orgRoleCodes) && in_array('owner', $currentOrg->orgRoleCodes))) {
Arnaud Pauget's avatar
Arnaud Pauget committed
83
            $orgUnitList = $this->getOwnerOriginatorsOrgs(null, $term);
Dylan's avatar
Dylan committed
84
        } else {
Arnaud Pauget's avatar
Arnaud Pauget committed
85
            $orgUnitList = $this->getOwnerOriginatorsOrgs($currentOrg, $term);
Dylan's avatar
Dylan committed
86
87
        }

88
        foreach ($orgUnitList as $org) {
Dylan's avatar
Dylan committed
89
            $organization = \laabs::newInstance('organization/organization');
90
91
92
            $organization->displayName = $org->displayName;
            $organization->orgId = $org->orgId;
            $organization->parentOrgId = $org->parentOrgId;
Dylan's avatar
Dylan committed
93

94
95
96
97
98
99
100
101
102
103
            if ($ownerOrg) {
                $orgList[] = $organization;
            }

            if ($orgUnit) {
                foreach ($org->originators as $orgUnit) {
                    if ($org->orgId == $orgUnit->ownerOrgId) {
                        $orgUnit->ownerOrgName = $org->displayName;
                        $orgList[] = $orgUnit;
                    }
Dylan's avatar
Dylan committed
104
105
106
107
                }
            }
        }

Alexandre Morin's avatar
Alexandre Morin committed
108
109
110
111
112
113
114
        if (isset($orgList)) {
            foreach ($orgList as $org) {
                foreach ($orgList as $orgParent) {
                    if (isset($org->parentOrgId)) {
                        if ($org->parentOrgId == $orgParent->orgId) {
                            $org->parentOrgName = $orgParent->displayName;
                        }
115
116
117
                    }
                }
            }
Alexandre Morin's avatar
Alexandre Morin committed
118
        }
Dylan's avatar
#6998    
Dylan committed
119
120
121
        return $orgList;
    }

122
123
    public function search($term = null, $enabled = "all")
    {
Alexandre Morin's avatar
Alexandre Morin committed
124
        $user = $this->accountController->get(\laabs::getToken('AUTH')->accountId);
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139

        $currentOrg = \laabs::getToken("ORGANIZATION");
        $owner = true;

        if (isset($currentOrg)) {
            if (!$currentOrg->orgRoleCodes) {
                $owner = false;
            } elseif (is_array($currentOrg->orgRoleCodes) && !in_array('owner', $currentOrg->orgRoleCodes)) {
                $owner = false;
            }
        }

        $queryParts = array();
        $queryParams = array();

140
141
142
143
        $securityLevel = null;
        if ($this->hasSecurityLevel) {
            $securityLevel = $user->getSecurityLevel();
        }
144
145
146
147
148
149
        if ($securityLevel == $user::SECLEVEL_GENADMIN) {
            $queryParams['isOrgUnit'] = 'false';
            $queryParts['isOrgUnit'] = "isOrgUnit=:isOrgUnit";
        }

        if ($term) {
150
151
152
            $queryParts['term'] = "(registrationNumber ='*".$term."*'
            OR orgName = '*".$term."*'
            OR displayName = '*".$term."*'
153
154
155
            OR parentOrgId = '*".$term."*')";
        }

156
        if ($enabled !== 'all') {
157
158
159
160
161
162
            $queryParams['enabled'] = $enabled;
            $queryParts['enabled'] = "enabled=:enabled";
        }

        $queryString = "";
        if (count($queryParts)) {
163
            $queryString = implode(' AND ', $queryParts);
164
165
        }

Alexandre Morin's avatar
Alexandre Morin committed
166
        $length = \laabs::configuration('presentation.maarchRM')['maxResults'];
167
168
169
170
171
172
        $organizationList = $this->sdoFactory->find("organization/organization", $queryString, $queryParams, null, 0, $length);
        $organizationList = \laabs::castMessageCollection($organizationList, "organization/organizationList");

        if ($term || $enabled !== 'all') {
            $organizations = [];
            foreach ($organizationList as $organization) {
Alexandre Morin's avatar
Alexandre Morin committed
173
174
                $parentOrgId = (string)$organization->parentOrgId;

175
176
177
178
179
180
                if ($user->ownerOrgId && $organization->ownerOrgId != $user->ownerOrgId) {
                    if (empty($parentOrgId) && $organization->orgId != $user->ownerOrgId) {
                        continue;
                    }
                }

Alexandre Morin's avatar
Alexandre Morin committed
181
                if ($parentOrgId) {
182
183
184
                    $organization->parentOrgName = $this->sdoFactory->index(
                        'organization/organization',
                        ['displayName'],
Alexandre Morin's avatar
Alexandre Morin committed
185
186
                        "orgId='". $parentOrgId . "'"
                    )[$parentOrgId];
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
                }
                $organizations[] = $organization;
            }

            return $organizations;
        }

        $tree = array();
        $organizationByParent = array();
        foreach ($organizationList as $organization) {
            $parentOrgId = (string)$organization->parentOrgId;

            if ($user->ownerOrgId && $organization->ownerOrgId != $user->ownerOrgId) {
                if (empty($parentOrgId) && $organization->orgId != $user->ownerOrgId) {
                    continue;
                }
            }

            if (empty($parentOrgId) && $owner) {
                $tree[] = $organization;
                continue;
Alexandre Morin's avatar
Alexandre Morin committed
208
209
210
            } elseif (!$owner && (
                (string)$organization->orgId == (string)$currentOrg->ownerOrgId)
                || (string)$organization->orgId == (string) $user->ownerOrgId) {
211
212
213
214
215
216
217
218
219
220
221
                $tree[] = $organization;
                continue;
            }

            if (!isset($organizationByParent[$parentOrgId])) {
                $organizationByParent[$parentOrgId] = array();
            }

            $organization->parentOrgName = $this->sdoFactory->index(
                'organization/organization',
                ['displayName'],
Alexandre Morin's avatar
Alexandre Morin committed
222
223
                "orgId='". $parentOrgId . "'"
            )[$parentOrgId];
224
225
226
227
228
229
            $organizationByParent[$parentOrgId][] = $organization;
        }

        return $this->buildTree($tree, $organizationByParent);
    }

Prosper De Laure's avatar
Prosper De Laure committed
230
231
232
233
234
235
236
237
238
    /**
     * Get organizations tree
     *
     * @return organization/organizationTree[] The tree of organization
     */
    public function getTree()
    {
        $tree = array();

239
240
241
        $authController = \laabs::newController("auth/userAccount");
        $user = $authController->get(\laabs::getToken('AUTH')->accountId);

Prosper De Laure's avatar
Prosper De Laure committed
242
243
244
245
246
247
        $currentOrg = \laabs::getToken("ORGANIZATION");
        $owner = true;

        if (isset($currentOrg)) {
            if (!$currentOrg->orgRoleCodes) {
                $owner = false;
248
            } elseif (is_array($currentOrg->orgRoleCodes) && !in_array('owner', $currentOrg->orgRoleCodes)) {
Prosper De Laure's avatar
Prosper De Laure committed
249
250
251
252
                $owner = false;
            }
        }

253
254
255
256
        $securityLevel = null;
        if ($this->hasSecurityLevel) {
            $securityLevel = $user->getSecurityLevel();
        }
257

258
        if ($securityLevel == $user::SECLEVEL_GENADMIN) {
259
260
261
262
263
264
265
266
267
268
269
270
271
272
            $organizationList = $this->sdoFactory->find(
                "organization/organization",
                'isOrgUnit = false',
                [],
                'orgName'
            );
        } else {
            $organizationList = $this->sdoFactory->find(
                "organization/organization",
                null,
                [],
                'orgName'
            );
        }
Prosper De Laure's avatar
Prosper De Laure committed
273
274
275
276
277
        $organizationList = \laabs::castMessageCollection($organizationList, "organization/organizationTree");

        // sort organization by parentOrgId
        $organizationByParent = array();
        foreach ($organizationList as $organization) {
278
            $parentOrgId = (string)$organization->parentOrgId;
Prosper De Laure's avatar
Prosper De Laure committed
279
280
281
282

            if (empty($parentOrgId) && $owner) {
                $tree[] = $organization;
                continue;
Alexandre Morin's avatar
Alexandre Morin committed
283
284
285
            } elseif (!$owner && (
                    (string)$organization->orgId == (string)$currentOrg->ownerOrgId)
                || (string)$organization->orgId == (string) $user->ownerOrgId) {
Prosper De Laure's avatar
Prosper De Laure committed
286
287
288
289
                $tree[] = $organization;
                continue;
            }

290
291
292
293
294
295
            if ($user->ownerOrgId && $organization->ownerOrgId != $user->ownerOrgId) {
                if (empty($parentOrgId) && $organization->orgId != $user->ownerOrgId) {
                    continue;
                }
            }

Prosper De Laure's avatar
Prosper De Laure committed
296
297
298
299
300
301
302
303
304
305
306
            if (!isset($organizationByParent[$parentOrgId])) {
                $organizationByParent[$parentOrgId] = array();
            }
            $organizationByParent[$parentOrgId][] = $organization;
        }

        return $this->buildTree($tree, $organizationByParent);
    }

    /**
     * Set positions for the organization tree
307
308
     * @param object $roots            The organization tree roots
     * @param array  $organizationList The list of organization sorted by parent organization
309
     *
Dylan's avatar
Dylan committed
310
     * @return object[]
Prosper De Laure's avatar
Prosper De Laure committed
311
312
313
314
     */
    protected function buildTree($roots, $organizationList)
    {
        foreach ($roots as $organization) {
315
            $orgId = (string)$organization->orgId;
Prosper De Laure's avatar
Prosper De Laure committed
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332

            if (isset($organizationList[$orgId])) {
                $organization->organization = $this->buildTree($organizationList[$orgId], $organizationList);
            }
        }

        return $roots;
    }

    /**
     * Create an organization
     * @param organization/organization $organization The organization object to create
     *
     * @return string the new organization's Id
     */
    public function create($organization)
    {
333
334
        $user = $this->accountController->get(\laabs::getToken('AUTH')->accountId);

335
        if ($organization->isOrgUnit) {
336
            $this->accountController->isAuthorized(['func_admin', 'user']);
337

338
339
340
341
342
343
            $securityLevel = null;
            if ($this->hasSecurityLevel) {
                $securityLevel = $user->getSecurityLevel();
            }

            if ($securityLevel == $user::SECLEVEL_FUNCADMIN) {
344
345
346
                $userOrgIds = [$user->ownerOrgId];
                foreach($this->readDescendantOrg($user->ownerOrgId) as $org){
                    $userOrgIds[] = (string) $org->orgId;
347
                }
348
                if (!in_array($organization->ownerOrgId, $userOrgIds)){
349
                    throw new \core\Exception\ForbiddenException("You are not allowed to do this action");
350
351
                }
            }
352

353
        } else {
354
            $this->accountController->isAuthorized('gen_admin');
355
        }
356
357
358


        if (!$organization->parentOrgId && !in_array($user->accountName, \laabs::configuration("auth")["adminUsers"])) {
359
360
            if (\laabs::getToken("ORGANIZATION")) {
                if (!in_array('owner', \laabs::getToken("ORGANIZATION")->orgRoleCodes)) {
361
                    throw new \core\Exception\ForbiddenException("You're not allowed to create an organization");
362
363
                }
            } else {
364
                throw new \core\Exception\ForbiddenException("You're not allowed to create an organization");
365
            }
366
        } else {
367
368
369
370
            if ($organization->parentOrgId) {
                try {
                    $this->read($organization->parentOrgId);
                } catch (\Exception $e) {
371
                    throw new \core\Exception\NotFoundException("Organization identified by " . $organization->parentOrgId . " was not find");
372
                }
373
            }
374
375
        }

Prosper De Laure's avatar
Prosper De Laure committed
376
377
378
379
380
381
382
383
        try {
            if (empty($organization->displayName)) {
                $organization->displayName = $organization->orgName;
            }

            $organization->orgId = \laabs::newId();
            $this->sdoFactory->create($organization, 'organization/organization');
        } catch (\Exception $e) {
384
            throw new \core\Exception\ConflictException("Key already exists");
Prosper De Laure's avatar
Prosper De Laure committed
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
        }

        return $organization->orgId;
    }

    /**
     * Read an organization by his orgId
     * @param string $orgId The Identifier of the organization to read
     *
     * @return organization/organization the organization
     */
    public function read($orgId)
    {
        $organization = $this->sdoFactory->read("organization/organization", $orgId);
        $organization->users = $this->readUserPositions($orgId);
        $organization->services = $this->readServicePositions($orgId);
        $organization->contacts = $this->getContacts($orgId);
402
        $organization->archivalProfileAccess = $this->sdoFactory->find('organization/archivalProfileAccess', "orgId='$orgId'");
Prosper De Laure's avatar
Prosper De Laure committed
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427

        return \laabs::castMessage($organization, "organization/organization");
    }

    /**
     * Get an organization by his regitration number
     * @param string $registrationNumber The registration number of the organization
     *
     * @return organization/organization the organization
     */
    public function getOrgByRegNumber($registrationNumber)
    {
        $organization = $this->sdoFactory->read("organization/organization", array('registrationNumber' => $registrationNumber));

        return \laabs::castMessage($organization, "organization/organization");
    }

    /**
     * Get organizations by their role
     * @param string $role The role of the organizations
     *
     * @return organization/organization[] the organizations
     */
    public function getOrgsByRole($role)
    {
428
        $organizations = $this->sdoFactory->find("organization/organization", "orgRoleCodes = '*$role*'");
Prosper De Laure's avatar
Prosper De Laure committed
429
430
431
432
433

        return \laabs::castMessageCollection($organizations, "organization/organization");
    }

    /**
434
     * List organizations
Prosper De Laure's avatar
Prosper De Laure committed
435
436
     * @param string $role The role of organizations
     *
Dylan's avatar
Dylan committed
437
     * @return organization/organization[] The organizations list
Prosper De Laure's avatar
Prosper De Laure committed
438
439
440
441
442
443
444
445
446
447
448
449
     */
    public function orgList($role = null)
    {
        $queryString = "";

        if ($role) {
            $queryString .= "orgRoleCodes = '*$role*'";
        }

        return $this->sdoFactory->index("organization/organization", array("displayName", "registrationNumber"), $queryString, null, array('registrationNumber'));
    }

450
    /**
451
     * List organizations
Prosper De Laure's avatar
Prosper De Laure committed
452
453
     * @param string $role The role of organizations
     *
Dylan's avatar
Dylan committed
454
     * @return organization/organization[] The organizations list
Prosper De Laure's avatar
Prosper De Laure committed
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
     */
    public function orgUnitList($role = null)
    {
        $queryString = "";

        if ($role) {
            $queryString .= "orgRoleCodes = '*$role*'";
        }

        $queryString .= "isOrgUnit = 'true'";

        return $this->sdoFactory->index("organization/organization", array("displayName", "registrationNumber"), $queryString, null, array('registrationNumber'));
    }

    /**
     * Get organization's user positions
     * @param string $orgId The organization's identifier
     *
     * @return organization/userPositionTree[] The list of user position
     */
    public function readUserPositions($orgId)
    {
        $users = $this->sdoFactory->find("organization/userPosition", "orgId = '$orgId'");
        $users = \laabs::castMessageCollection($users, 'organization/userPositionTree');

        $userAccountController = \laabs::newController('auth/userAccount');

        foreach ($users as $user) {
483
            $user->displayName = $userAccountController->edit((string)$user->userAccountId)->displayName;
Prosper De Laure's avatar
Prosper De Laure committed
484
485
486
487
488
        }

        return $users;
    }

Dylan's avatar
Dylan committed
489
490
491
492
493
494
495
496
497
498
    /**
     * Get user positions
     * @param string $accountId The user identifier
     *
     * @return organization/userPositionTree[] The list of user position
     */
    public function readUserOrgs($accountId)
    {
        $users = $this->sdoFactory->find("organization/userPosition", "userAccountId = '$accountId'");
        $users = \laabs::castMessageCollection($users, 'organization/userPositionTree');
Dylan's avatar
Dylan committed
499
        $organizations = $this->sdoFactory->find("organization/organization");
Dylan's avatar
Dylan committed
500
501
502

        foreach ($users as $user) {
            $user->displayName = $this->sdoFactory->read("organization/organization", $user->orgId)->displayName;
Dylan's avatar
Dylan committed
503
504

            foreach ($organizations as $org) {
505
                if ($user->orgId == $org->orgId) {
Dylan's avatar
Dylan committed
506
                    foreach ($organizations as $orgName) {
507
                        if ($org->ownerOrgId == $orgName->orgId) {
Dylan's avatar
Dylan committed
508
                            $user->ownerOrgName = $orgName->displayName;
509
                            $user->ownerOrgId = $orgName->orgId;
Dylan's avatar
Dylan committed
510
511
512
513
                        }
                    }
                }
            }
Dylan's avatar
Dylan committed
514
515
516
517
518
        }

        return $users;
    }

Prosper De Laure's avatar
Prosper De Laure committed
519
520
521
522
523
524
525
526
527
528
529
    /**
     * Get organization's service positions
     * @param string $orgId The organization's identifier
     *
     * @return organization/servicePosition[] The list of service position
     */
    public function readServicePositions($orgId)
    {
        $services = $this->sdoFactory->find("organization/servicePosition", "orgId = '$orgId'");
        $services = \laabs::castMessageCollection($services, 'organization/servicePositionTree');

530
        $userAccountController = \laabs::newController('auth/userAccount');
Prosper De Laure's avatar
Prosper De Laure committed
531
532

        foreach ($services as $service) {
533
            $service->displayName = $userAccountController->edit((string)$service->serviceAccountId)->displayName;
Prosper De Laure's avatar
Prosper De Laure committed
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
        }

        return $services;
    }

    /**
     * Get organization's contacts
     * @param string $orgId The organization's identifier
     *
     * @return organization/orgContact[] The list of service position
     */
    public function readContact($orgId)
    {
        return $this->sdoFactory->find("organization/orgContact", "orgId = '$orgId'");
    }

    /**
     * Update an organization
     * @param string                    $orgId        The organization identifier
     * @param organization/organization $organization The organization object to update
     *
     * @return boolean The result of the operation
     */
    public function update($orgId, $organization)
    {
559
        if ($organization->isOrgUnit) {
560
            $this->accountController->isAuthorized(['func_admin', 'user']);
561
        } else {
562
            $this->accountController->isAuthorized('gen_admin');
563
564
        }

Prosper De Laure's avatar
Prosper De Laure committed
565
566
        $organization->orgId = $orgId;

567
        if ($organization->beginDate > $organization->endDate) {
568
569
            throw new \core\Exception("The end date is lower than the begin date.");
        }
570

571
572

        if (isset($organization->orgRoleCodes) && $organization->orgRoleCodes->contains("owner")) {
573
            if ($this->sdoFactory->count("organization/organization", "registrationNumber!='$organization->registrationNumber' AND orgRoleCodes='*owner*'")) {
574
575
                throw new \core\Exception("An owner is already defined.");
            }
576
577
        }

578
579
580
581
582
        try {
            if ($this->isUsed($organization->registrationNumber)) {
                $originalOrganization = $this->read($orgId);
                $organization->registrationNumber = $originalOrganization->registrationNumber;
            }
583

584
585
586
587
588
589
            if (empty($organization->displayName)) {
                $organization->displayName = $organization->orgName;
            }
            $res = $this->sdoFactory->update($organization, 'organization/organization');
        } catch (\Exception $e) {
            throw new \core\Exception("Key already exists");
Prosper De Laure's avatar
Prosper De Laure committed
590
591
        }

592
        return $res;
Prosper De Laure's avatar
Prosper De Laure committed
593
    }
594

Cyril Vazquez's avatar
Cyril Vazquez committed
595
596
597
598
599
    /**
     * Sets the status of an org unit
     *
     * @param string $orgId  The org identifier
     * @param bool   $status The new status (enabled = true of false)
600
     *
Cyril Vazquez's avatar
Cyril Vazquez committed
601
602
     * @return bool The result of the change
     */
603
604
    public function changeStatus($orgId, $status)
    {
605
606
        $this->accountController->isAuthorized(['func_admin']);

607
608
        $organization = $this->sdoFactory->read('organization/organization', $orgId);

609
610
611
        $accountToken = \laabs::getToken('AUTH');
        $account = $this->sdoFactory->read("auth/account", $accountToken->accountId);

612
613
614
615
        $securityLevel = null;
        if ($this->hasSecurityLevel) {
            $securityLevel = $account->getSecurityLevel();
        }
616

617
        if ($securityLevel == $account::SECLEVEL_FUNCADMIN) {
618
619
620
621
622
623
            $userOrgIds = [$account->ownerOrgId];
            foreach($this->readDescendantOrg($account->ownerOrgId) as $org){
                $userOrgIds[] = (string) $org->orgId;
            }
            if (!in_array($organization->ownerOrgId, $userOrgIds)){
                throw new \core\Exception\ForbiddenException("You are not allowed to do this action");
624
625
626
            }
        }

627
        if (!$organization->isOrgUnit) {
628
            throw new \core\Exception\ForbiddenException("An organization can't be disabled.");
629
630
631
632
633
634
635
636
637
638
        }

        if ($status == "true") {
            $organization->enabled = 1;
        } else {
            $organization->enabled = 0;
        }

        return $this->sdoFactory->update($organization);
    }
Prosper De Laure's avatar
Prosper De Laure committed
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656

    /**
     * Move an organization to a new ownerOrg
     * @param string $orgId          The organization identifier
     * @param string $newParentOrgId The new parent organization identifier
     * @param string $newOwnerOrgId  The new owner organization identifier
     *
     * @return boolean The result of the operation
     */
    public function move($orgId, $newParentOrgId = null, $newOwnerOrgId = null)
    {
        if ($newParentOrgId == "") {
            $newParentOrgId = null;
        }
        if ($newOwnerOrgId == "") {
            $newOwnerOrgId = null;
        }

657
        if ($newParentOrgId) {
658
659
660
661
662
663
664
665
666
667
668
            $oldParentOrgs = $this->readParentOrg($orgId);
            $newParentOrgs = $this->readParentOrg($newParentOrgId);

            $newParentOrg = $this->sdoFactory->read("organization/organization", $newParentOrgId);
            if ($newParentOrg->isOrgUnit) {
                for ($i = 0; $i < count($newParentOrgs); $i++) {
                    if (!$newParentOrgs[$i]->isOrgUnit) {
                        $newParentOrg = $newParentOrgs[$i];
                        break;
                    }
                }
Alexandre Morin's avatar
Alexandre Morin committed
669
670
            }

671
672
673
674
675
            for ($i = 0; $i < count($oldParentOrgs); $i++) {
                if (!$oldParentOrgs[$i]->isOrgUnit) {
                    $oldParentOrg = $oldParentOrgs[$i];
                    break;
                }
Alexandre Morin's avatar
Alexandre Morin committed
676
            }
Alexandre Morin's avatar
Alexandre Morin committed
677

678
679
680
681
682
            if ($oldParentOrg->orgId === null
                || ($oldParentOrg->orgId !== null
                    && $oldParentOrg->orgId != $newParentOrg->orgId
                )
            ) {
683
684
                throw new \core\Exception("Organization can''t be moved to an other organization");
            }
Alexandre Morin's avatar
Alexandre Morin committed
685
686
        }

Prosper De Laure's avatar
Prosper De Laure committed
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
        $organization = $this->sdoFactory->read("organization/organization", $orgId);
        $descendants = $this->sdoFactory->readDescendants("organization/organization", $organization);

        $transactionControl = !$this->sdoFactory->inTransaction();

        if ($transactionControl) {
            $this->sdoFactory->beginTransaction();
        }

        try {
            $organization->parentOrgId = $newParentOrgId;
            $organization->ownerOrgId = $newOwnerOrgId;

            if (!$newOwnerOrgId) {
                $newOwnerOrgId = $orgId;
            }

            foreach ($descendants as $descendantOrg) {
705
                if ((string)$descendantOrg->orgId == $organization->orgId) {
Alexis Ragot's avatar
Alexis Ragot committed
706
                    throw new \bundle\organization\Exception\orgMoveException("Organization can't be moved to a descendent organization");
Prosper De Laure's avatar
Prosper De Laure committed
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
                }

                if ($descendantOrg->isOrgUnit && $descendantOrg->ownerOrgId != $newOwnerOrgId) {
                    $descendantOrg->ownerOrgId = $newOwnerOrgId;
                    $this->sdoFactory->update($descendantOrg, 'organization/organization');
                }
            }

            $this->sdoFactory->update($organization);
        } catch (\Exception $e) {
            throw $e;
        }

        if ($transactionControl) {
            $this->sdoFactory->commit();
        }

        return true;
    }

    /**
     * Delete an organization
     * @param string $orgId The organization id
     *
     * @return boolean The restult of the operation
     */
    public function delete($orgId)
    {
        $organization = $this->sdoFactory->read("organization/organization", $orgId);
736
        if ($organization->isOrgUnit) {
737
            $this->accountController->isAuthorized(['func_admin', 'user']);
738
        } else {
739
            $this->accountController->isAuthorized('gen_admin');
740
        }
741
742
743
744
745

        $transactionControl = !$this->sdoFactory->inTransaction();

        if ($transactionControl) {
            $this->sdoFactory->beginTransaction();
746
747
        }

748
749
        try {
            if ($this->isUsed($organization->registrationNumber)) {
750
751
752
753
754
755
                throw new \core\Exception\ForbiddenException(
                    "The organization %s is used in archives.",
                    403,
                    null,
                    [$organization->registrationNumber]
                );
756
757
            }

758
759
760
761
            $controlAuthorities = $this->sdoFactory->find(
                'medona/controlAuthority',
                "originatorOrgUnitId = '$orgId' OR controlAuthorityOrgUnitId = '$orgId'"
            );
Cyril Vazquez's avatar
Cyril Vazquez committed
762
            if (count($controlAuthorities) > 0) {
763
764
765
766
767
768
                throw new \core\Exception\ForbiddenException(
                    "The organization %s is used in control authority.",
                    403,
                    null,
                    [$organization->registrationNumber]
                );
769
            }
770

771
772
773
774
775
            $children = $this->sdoFactory->readChildren("organization/organization", $organization);
            $users = $this->sdoFactory->readChildren("organization/userPosition", $organization);
            $services = $this->sdoFactory->readChildren("organization/servicePosition", $organization);
            $contacts = $this->sdoFactory->readChildren("organization/orgContact", $organization);
            $this->sdoFactory->deleteChildren("organization/archivalProfileAccess", $organization);
776

777
            foreach ($contacts as $contact) {
778
                $this->deleteContactPosition($contact->contactId, (string)$organization->orgId);
779
780
781
            }

            foreach ($children as $child) {
782
783
784
785
                $controlAuthorities = $this->sdoFactory->find(
                    'medona/controlAuthority',
                    "originatorOrgUnitId = '$child->orgId' OR controlAuthorityOrgUnitId = '$child->orgId'"
                );
Cyril Vazquez's avatar
Cyril Vazquez committed
786
                if (count($controlAuthorities) > 0) {
787
788
789
790
791
                    throw new \core\Exception\ForbiddenException(
                        "The child organization is used in control authority.",
                        403,
                        null
                    );
792
                }
793
                $this->delete($child->orgId);
794
795
            }

796
797
798
799
800
801
802
803
804
805
806
807
808
809
            foreach ($users as $user) {
                $this->sdoFactory->delete($user);
            }
            foreach ($services as $service) {
                $this->sdoFactory->delete($service);
            }

            $this->sdoFactory->delete($organization);
        } catch (\Exception $exception) {
            if ($transactionControl) {
                $this->sdoFactory->rollback();
            }

            throw $exception;
Prosper De Laure's avatar
Prosper De Laure committed
810
        }
811
812
813

        if ($transactionControl) {
            $this->sdoFactory->commit();
Prosper De Laure's avatar
Prosper De Laure committed
814
815
816
        }


817
818

        return true;
Prosper De Laure's avatar
Prosper De Laure committed
819
820
821
822
823
824
825
826
827
828
829
830
    }

    /**
     * Add a user position to an organization
     * @param string $userAccountId The user account identifier
     * @param string $orgId         The organization identifier
     * @param string $function      The function of the user
     *
     * @return boolean The result of the operation
     */
    public function addUserPosition($userAccountId, $orgId, $function = null)
    {
831
        $this->accountController->isAuthorized('func_admin');
832

Prosper De Laure's avatar
Prosper De Laure committed
833
834
835
836
837
        $userPosition = \laabs::newInstance('organization/userPosition');
        $userPosition->userAccountId = $userAccountId;
        $userPosition->orgId = $orgId;
        $userPosition->function = $function;
        $userPosition->default = false;
838
839
840
841
        $userDefaultPosition = $this->sdoFactory->find(
            'organization/userPosition',
            "userAccountId = '$userAccountId' AND default = true"
        );
Prosper De Laure's avatar
Prosper De Laure committed
842
843
844
845

        if (empty($userDefaultPosition)) {
            $userPosition->default = true;
        }
846

Prosper De Laure's avatar
Prosper De Laure committed
847
848
849
        return $this->sdoFactory->create($userPosition, 'organization/userPosition');
    }

Dylan's avatar
Dylan committed
850
851
    /**
     * Delete an archival pofile accesss from every organization
Dylan's avatar
Dylan committed
852
853
     * @param string $userAccountId The user account identifier
     * @param string $orgId         The organization identifier
Dylan's avatar
Dylan committed
854
855
856
857
858
     *
     * @return bool The result of the operation
     */
    public function updateUserPosition($userAccountId, $orgId = null)
    {
859
        $this->accountController->isAuthorized('func_admin');
860

Dylan's avatar
Dylan committed
861
862
863
864
865
866
867
868
869
870
        $userPositions = $this->sdoFactory->find("organization/userPosition", "userAccountId='$userAccountId'");
        $default = null;

        foreach ($userPositions as $userPosition) {
            if ($userPosition->default) {
                $default = $userPosition->orgId;
            }
        }

        try {
871
            if ($userPositions) {
Dylan's avatar
Dylan committed
872
873
874
                $this->sdoFactory->deleteCollection($userPositions, "organization/userPosition");
            }

875
            foreach ($orgId as $id) {
Dylan's avatar
Dylan committed
876
877
878
879
                $userPosition = \laabs::newInstance('organization/userPosition');
                $userPosition->userAccountId = $userAccountId;
                $userPosition->orgId = $id;

880
                if ($id == $default) {
Dylan's avatar
Dylan committed
881
                    $userPosition->default = true;
882
                } else {
Dylan's avatar
Dylan committed
883
884
885
                    $userPosition->default = false;
                }

886
                $userPosition = $this->sdoFactory->create($userPosition, 'organization/userPosition');
Dylan's avatar
Dylan committed
887
888
889
890
891
892
893
894
895
            }
        } catch (Exception $e) {
            $this->sdoFactory->rollback();
            throw $e;
        }

        return $userPosition;
    }

Jerome Boucher's avatar
Jerome Boucher committed
896
897


Prosper De Laure's avatar
Prosper De Laure committed
898
899
900
    /**
     * Add a service position to an organization
     * @param string $orgId            The organization identifier
901
     * @param string $serviceAccountId The service account identifier
Prosper De Laure's avatar
Prosper De Laure committed
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
     *
     * @return boolean The result of the operation
     */
    public function addServicePosition($orgId, $serviceAccountId)
    {
        $servicePosition = \laabs::newInstance('organization/servicePosition');
        $servicePosition->serviceAccountId = $serviceAccountId;
        $servicePosition->orgId = $orgId;

        return $this->sdoFactory->create($servicePosition);
    }

    /**
     * Set default user position for an user
     * @param string $orgId         The organization identifier
     * @param string $userAccountId The service account identifier
     *
     * @return boolean The result of the operation
     */
    public function setDefaultUserPosition($orgId, $userAccountId)
    {
923
924
        $previousDefaultUserPosition = $this->sdoFactory->find(
            'organization/userPosition', "userAccountId='$userAccountId' AND default=true");
Prosper De Laure's avatar
Prosper De Laure committed
925
926
927
928
929
930
931

        if (!empty($previousDefaultUserPosition)) {
            $previousDefaultUserPosition = $previousDefaultUserPosition[0];
            $previousDefaultUserPosition->default = false;
            $this->sdoFactory->update($previousDefaultUserPosition, 'organization/userPosition');
        }

932
933
934
935
        $userPosition = $this->sdoFactory->read(
            "organization/userPosition",
            array("userAccountId" => $userAccountId, "orgId" => $orgId)
        );
Prosper De Laure's avatar
Prosper De Laure committed
936
937
938
939
940
941
942
943
944
945
        $userPosition->default = true;

        return $this->sdoFactory->update($userPosition, 'organization/userPosition');
    }

    /**
     * Remove a user's position
     * @param string $userAccountId The user account identifier
     * @param string $orgId         The organization account identifier
     *
Dylan's avatar
Dylan committed
946
     * @return boolean The result of the operation
Prosper De Laure's avatar
Prosper De Laure committed
947
948
949
     */
    public function deleteUserPosition($userAccountId, $orgId)
    {
950
        $this->accountController->isAuthorized('func_admin');
951

952
953
954
955
        $userPosition = $this->sdoFactory->read(
            "organization/userPosition",
            array("userAccountId" => $userAccountId, "orgId" => $orgId)
        );
Prosper De Laure's avatar
Prosper De Laure committed
956
957

        if ($userPosition->default) {
958
959
960
961
            $newDefaultUserPosition = $this->sdoFactory->find(
                'organization/userPosition',
                "userAccountId='$userAccountId'"
            );
Prosper De Laure's avatar
Prosper De Laure committed
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978

            if (!empty($newDefaultUserPosition)) {
                $newDefaultUserPosition = $newDefaultUserPosition[0];
                $newDefaultUserPosition->default = true;

                $this->sdoFactory->update($newDefaultUserPosition, 'organization/userPosition');
            }
        }

        return $this->sdoFactory->delete($userPosition);
    }

    /**
     * Remove a contact's position
     * @param string $contactId The user account identifier
     * @param string $orgId     The organization account identifier
     *
Dylan's avatar
Dylan committed
979
     * @return boolean The result of the operation
Prosper De Laure's avatar
Prosper De Laure committed
980
981
982
     */
    public function deleteContactPosition($contactId, $orgId)
    {
983
984
985
986
        $contactPosition = $this->sdoFactory->read(
            "organization/orgContact",
            array("contactId" => $contactId, "orgId" => $orgId)
        );
Prosper De Laure's avatar
Prosper De Laure committed
987
988

        $contactController = \laabs::newController('contact/contact');
989
        $this->sdoFactory->delete($contactPosition);
Prosper De Laure's avatar
Prosper De Laure committed
990

991
        return $contactController->delete($contactId);
Prosper De Laure's avatar
Prosper De Laure committed
992
993
994
995
996
997
998
    }

    /**
     * Remove a service's position
     * @param string $orgId            The organization account identifier
     * @param string $serviceAccountId The service account identifier
     *
Dylan's avatar
Dylan committed
999
     * @return boolean The result of the operation
Prosper De Laure's avatar
Prosper De Laure committed
1000
     */
For faster browsing, not all history is shown. View entire blame