RecettesController.php 4.79 KB
Newer Older
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 61 62 63 64 65 66 67 68 69
<?php

class RecettesController extends AppController {

    public $uses = [
        'Organisation'
    ];

    /**
     * Vérification de l'accès aux actions en fonction du profil de l'utilisateur connecté.
     */
    public function beforeFilter() {
        parent::beforeFilter();

        // Seul le rôle Superutilisateur peut accéder à ce contrôleur, uniquement en mode debug et sur une instance de recette
        $recette = preg_match('/^https{0,1}:\/\/(localhost|.*\.recette\.libriciel\.fr)[:\/]/', Router::url( $this->here, true )) === 1;
        if ($this->Droits->isSu() !== true || (int)Configure::read('debug') < 1 || $recette === false) {
            throw new ForbiddenException(__d('default', 'default.flasherrorPasDroitPage'));
        }
    }

    protected function _getOrganisations() {
        $query = [
            'fields' => [
                'Organisation.id',
                'Organisation.raisonsociale'
            ],
            'contain' => [
                'Responsable' => [
                    'fields' => ['Responsable.nom_complet'],
                    'order' => ['Responsable.nom_complet']
                ],
                'Role' => [
                    'fields' => ['Role.libelle'],
                    'order' => ['Role.libelle']
                ],
                'Service' => [
                    'fields' => ['Service.libelle'],
                    'order' => ['Service.libelle']
                ],
                'Soustraitant' => [
                    'fields' => ['Soustraitant.raisonsociale'],
                    'order' => ['Soustraitant.raisonsociale']
                ],
                'Typage' => [
                    'fields' => ['Typage.libelle'],
                    'order' => ['Typage.libelle']
                ],
                'User' => [
                    'fields' => ['User.username'],
                    'order' => ['User.username']
                ],
            ],
            'order' => [
                'Organisation.raisonsociale'
            ]
        ];

        return $this->Organisation->find('all', $query);
    }

    protected function _getUsers() {
        $query = [
            'fields' => array_merge(
                [
                    'User.username',
                    'User.nom_complet',
                    'User.email',
                    '("Admin"."id" IS NOT NULL) AS "User__is_superadmin"',
70 71 72 73 74
                    '(CASE
                        WHEN "User"."username" = \'superadmin\' THEN \'admin\'
                        WHEN "Admin"."id" IS NOT NULL THEN \'Voitures2ColoSoupersos\'
                        ELSE \'bateauavionmotovoiture\'
                    END) AS "User__pwd"',
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
                ],
                $this->Organisation->User->Admin->fields()
            ),
            'contain' => [
                'Organisation' => [
                    'fields' => [
                        'Organisation.raisonsociale',
                        '("Organisation"."dpo" = "OrganisationUser"."user_id") AS "User__is_dpo"',
                    ],
                    'order' => [
                        'Organisation.raisonsociale'
                    ],
                ],

            ],
            'joins' => [
                $this->Organisation->User->join('Admin', ['type' => 'LEFT OUTER'])
            ],
            'conditions' => [],
            'order' => [
                'User.username'
            ]
        ];
        $users = $this->Organisation->User->find('all', $query);

        foreach ($users as $idx => $user) {
            foreach ((array)Hash::extract($user, 'Organisation.{n}.OrganisationUser') as $i => $ou) {
                $query = [
                    'conditions' => [
                        'OrganisationUser.id' => $ou['id']
                    ],
                    'contain' => [
                        'OrganisationUserRole' => [
                            'Role' => [
                                'fields' => 'Role.libelle',
                                'order' => 'Role.libelle',
                            ]
                        ],
                        'OrganisationUserService' => [
                            'Service' => [
                                'fields' => 'Service.libelle',
                                'order' => 'Service.libelle',
                            ]
                        ],
                    ]
                ];
                $users[$idx]['Organisation'][$i]['OrganisationUser'] = array_merge(
                    $users[$idx]['Organisation'][$i],
                    $this->Organisation->OrganisationUser->find('first', $query)
                );
            }
        }

        return $users;
    }

    public function index() {
        $organisations = $this->_getOrganisations();
        $users = $this->_getUsers();

        $this->set(compact('organisations', 'users'));
    }
}