FormulesController.php 5.5 KB
<?php
namespace App\Controller;

use Cake\ORM\TableRegistry;

/**
 * Formules Controller
 *
 * @property \App\Model\Table\FormulesTable $Formules
 */
class FormulesController extends AppController
{

    /**
     * Give authorization for formules
     *
     * @param
     *            $user
     * @return boolean
     */
    //public function isAuthorized($user)
    public function isAuthorized($user,
        $action = null, $id=null, $role=null, $userCname=null) {
        // $configuration = $this->confLabinvent;
        // $action = $this->request->getAttribute('params')['action'];
        $action = $this->getActionPassed();
        // $role = TableRegistry::get('Users')->find()->where(['username' => $user[$configuration->authentificationType_ldap][0]])->first()['role'];
        $role = $this->getUserRole($user);
        
        // Admin + peut tout faire
        // if($this->userHasRoleAtLeast('Administration')) return true;
        if ($this->USER_IS_ADMIN_AT_LEAST())
            return true;
        
        // Les autres users
        if (in_array($action, [
            'edit',
            'delete'
        ])) {
            // $id = (int)$this->request->getAttribute('params')['pass'][0];
            $id = $this->getIdPassed();
            if ($this->isOwnedBy($id, $user['sn'][0] . ' ' . $user['givenname'][0]))
                return true;
            if ($role == 'Responsable' && $this->isRespGroup($id, $user[$configuration->ldap_authenticationType][0]))
                return true;
        }
        
        // Par défaut
        return parent::isAuthorized($user);
    }

    /**
     * Index method
     *
     * @return \Cake\Network\Response|null
     */
    public function index()
    {
        $this->paginate = [
            'contain' => []
        ];
        $formules = $this->paginate($this->Formules->find('all'));
        $nbFormules = $this->Formules->find('all')->count();
        
        $this->set(compact('formules', 'nbFormules'));
        $this->set('_serialize', [
            'formule'
        ]);
    }

    /**
     * View method
     *
     * @param string|null $id
     *            Formule id.
     * @return \Cake\Network\Response|null
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function view($id = null)
    {
        $formule = $this->Formules->get($id, [
            'contain' => [
                'Variables'
            ]
        ]);
        
        $this->set('formule', $formule);
        $this->set('_serialize', [
            'formule'
        ]);
    }

    /**
     * Add method
     *
     * @return \Cake\Network\Response|void Redirects on successful add, renders view otherwise.
     */
    public function add()
    {
        $formule = $this->Formules->newEntity();
        if ($this->request->is('post')) {
            $formule = $this->Formules->patchEntity($formule, $this->request->getData, [
                'associated' => 'Variables'
            ]);
            
            if ($this->Formules->save($formule)) {
                $this->Flash->success(__('La formule a bien été ajouté.'));
                return $this->redirect([
                    'controller' => 'Formules',
                    'action' => 'view',
                    $formule->id
                ]);
            } else {
                $this->Flash->error(__('La formule n\'a pas pu être ajouté.'));
                return $this->redirect([
                    'controller' => 'Formules',
                    'action' => 'add',
                    $formule->id
                ]);
            }
        }
        
        $this->set(compact('formule'));
        $this->set('_serialize', [
            'formule'
        ]);
    }

    /**
     * Edit method
     *
     * @param string|null $id
     *            Suivi id.
     * @return \Cake\Network\Response|void Redirects on successful edit, renders view otherwise.
     * @throws \Cake\Network\Exception\NotFoundException When record not found.
     */
    public function edit($id = null)
    {
        $formule = $this->Formules->get($id, [
            'contain' => []
        ]);
        if ($this->request->is([
            'patch',
            'post',
            'put'
        ])) {
            $formule = $this->Formules->patchEntity($formule, $this->request->getData());
            if ($this->Formules->save($formule)) {
                $this->Flash->success(__('La formule a bien été édité.'));
                return $this->redirect([
                    'action' => 'view',
                    $id
                ]);
            } else {
                $this->Flash->error(__('La formule n\'a pas pu être édité.'));
            }
        }
        
        $this->set(compact('formule'));
        $this->set('_serialize', [
            'formule'
        ]);
    }

    /**
     * Delete method
     *
     * @param string|null $id
     *            Formule id.
     * @return \Cake\Network\Response|null Redirects to index.
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function delete($id = null)
    {
        $this->request->allowMethod([
            'post',
            'delete'
        ]);
        $formule = $this->Formules->get($id);
        if ($this->Formules->delete($formule)) {
            $this->Flash->success(__('La formule a bien été supprimé.'));
        } else {
            $this->Flash->error(__('La formule n\'a pas pu être supprimé.'));
        }
        return $this->redirect([
            'action' => 'index'
        ]);
    }
}