Blame view

src/Controller/FormulesController.php 4.44 KB
b3dceb2a   Alexis Proust   Ajout nouveaux fi...
1
2
3
<?php
namespace App\Controller;

b3dceb2a   Alexis Proust   Ajout nouveaux fi...
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
use Cake\ORM\TableRegistry;

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

	/**
	 * @param $user
	 *
	 * Give authorization for formules
	 *
	 * @return boolean
	 */
	public function isAuthorized($user)
	{
aaf7558a   Thibaud Ajas   modifications de ...
23
		$configuration = $this->confLabinvent;
b3dceb2a   Alexis Proust   Ajout nouveaux fi...
24
25
		$role = TableRegistry::get('Users')->find()->where(['username' => $user[$configuration->authentificationType_ldap][0]])->first()['role'];
		 
a0fefb3d   Thibaud Ajas   bugfixes suite au...
26
		$action = $this->request->getAttribute('params')['action'];
b3dceb2a   Alexis Proust   Ajout nouveaux fi...
27
		 
164ad0a0   Etienne Pallier   Grosse ameliorati...
28
		if($this->userHasRoleAtLeast('Administration')) return true;
b3dceb2a   Alexis Proust   Ajout nouveaux fi...
29
30
31
		
		//Pour un "utilisateur"
		if (in_array($action, ['edit', 'delete'])) {
a0fefb3d   Thibaud Ajas   bugfixes suite au...
32
			$id = (int)$this->request->getAttribute('params')['pass'][0];
b3dceb2a   Alexis Proust   Ajout nouveaux fi...
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
			if($this->isOwnedBy($id, $user['sn'][0].' '.$user['givenname'][0])) return true;
			if($role == 'Responsable' && $this->isRespGroup($id, $user[$configuration->authentificationType_ldap][0])) return true;
		}
		 
		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')) {
a0fefb3d   Thibaud Ajas   bugfixes suite au...
86
            $formule = $this->Formules->patchEntity($formule, $this->request->getData ,['associated' => 'Variables']);
b3dceb2a   Alexis Proust   Ajout nouveaux fi...
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
            
            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->data);
            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']);
    }
    
    
}