UsersController.php
4.08 KB
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
70
71
72
73
74
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
138
139
140
141
142
143
<?php
namespace App\Controller;
use App\Controller\AppController;
use Cake\Event\Event;
/**
* Users Controller
*
* @property \App\Model\Table\UsersTable $Users
*/
class UsersController extends AppController
{
public function beforeFilter(Event $event)
{
parent::beforeFilter($event);
$this->Auth->allow(['logout']);
}
public function login()
{
if ($this->request->is('post')) {
$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
return $this->redirect($this->Auth->redirectUrl());
}
$this->Flash->error(__('Login ou mot de passe invalide, réessayez'));
}
}
public function logout()
{
return $this->redirect($this->Auth->logout());
}
/**
* Index method
*
* @return \Cake\Network\Response|null
*/
public function index()
{
$this->paginate = [
'contain' => ['GroupesMetiers']
];
$users = $this->paginate($this->Users);
$this->set(compact('users'));
$this->set('_serialize', ['users']);
}
/**
* View method
*
* @param string|null $id User id.
* @return \Cake\Network\Response|null
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function view($id = null)
{
$user = $this->Users->get($id, [
'contain' => ['GroupesMetiers']
]);
$this->set('user', $user);
$this->set('_serialize', ['user']);
}
/**
* Add method
*
* @return \Cake\Network\Response|void Redirects on successful add, renders view otherwise.
*/
public function add()
{
$user = $this->Users->newEntity();
if ($this->request->is('post')) {
$user = $this->Users->patchEntity($user, $this->request->data);
if ($this->Users->save($user)) {
$this->Flash->success(__('L\'utilisateur a bien été ajouté.'));
return $this->redirect([
'controller' => 'Pages',
'action' => 'display'
]);
} else {
$this->Flash->error(__('L\utilisateur n\'a pas pu être ajouté.'));
}
}
$groupesMetiers = $this->Users->GroupesMetiers->find('list', [ 'keyField' => 'id', 'valueField' => 'nom']);
$this->set(compact('user', 'groupesMetiers'));
$this->set('_serialize', ['user']);
}
/**
* Edit method
*
* @param string|null $id User 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)
{
$user = $this->Users->get($id, [
'contain' => []
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$user = $this->Users->patchEntity($user, $this->request->data);
if ($this->Users->save($user)) {
$this->Flash->success(__('L\utilisateur a bien été édité.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('L\utilisateur n\'a pas pu être édité.'));
}
}
$groupesMetiers = $this->Users->GroupesMetiers->find('list', [ 'keyField' => 'id', 'valueField' => 'nom']);
$this->set(compact('user', 'groupesMetiers'));
$this->set('_serialize', ['user']);
}
/**
* Delete method
*
* @param string|null $id User 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']);
$user = $this->Users->get($id);
if ($this->Users->delete($user)) {
$this->Flash->success(__('L\utilisateur a bien été supprimé.'));
} else {
$this->Flash->error(__('L\utilisateur n\'a pas pu être supprimé.'));
}
return $this->redirect(['action' => 'index']);
}
}