bonsoir à tous,
je suis encore en phase de test:
je n'arrive pas à me logger avec les utilisateurs que j'ai crée à partir de l'action add();
voir piece jointe
User.php
UsersController.php
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200 <?php App::uses('AuthComponent', 'Controller/Component'); class User extends AppModel { //public $avatarUploadDir = 'img/avatars'; //var $displayField = 'email'; var $belongsTo = 'Group'; public $validate = array( /* 'username' => array( 'nonEmpty' => array( 'rule' => array('notEmpty'), 'message' => 'A username is required', 'allowEmpty' => false ), 'between' => array( 'rule' => array('between', 5, 15), 'required' => true, 'message' => 'Usernames must be between 5 to 15 characters' ), 'unique' => array( 'rule' => array('isUniqueUsername'), 'message' => 'This username is already in use' ), 'alphaNumericDashUnderscore' => array( 'rule' => array('alphaNumericDashUnderscore'), 'message' => 'Username can only be letters, numbers and underscores' ), ), */ 'password' => array( 'required' => array( 'rule' => array('notEmpty'), 'message' => 'A password is required' ), 'min_length' => array( 'rule' => array('minLength', '6'), 'message' => 'Password must have a mimimum of 6 characters' ) ), 'password_confirm' => array( 'required' => array( 'rule' => array('notEmpty'), 'message' => 'Please confirm your password' ), 'equaltofield' => array( 'rule' => array('equaltofield','password'), 'message' => 'Both passwords must match.' ) ), 'email' => array( 'required' => array( 'rule' => array('email', true), 'message' => 'Please provide a valid email address.' ) , 'unique' => array( 'rule' => array('isUniqueEmail'), 'message' => 'This email is already in use', ), 'between' => array( 'rule' => array('between', 6, 60), 'message' => 'Usernames must be between 6 to 60 characters' ) ) , /*'role' => array( 'valid' => array( 'rule' => array('inList', array('king', 'queen', 'bishop', 'rook', 'knight', 'pawn')), 'message' => 'Please enter a valid role', 'allowEmpty' => false ) ), */ 'password_update' => array( 'min_length' => array( 'rule' => array('minLength', '6'), 'message' => 'Password must have a mimimum of 6 characters', 'allowEmpty' => true, 'required' => false ) ), 'password_confirm_update' => array( 'equaltofield' => array( 'rule' => array('equaltofield','password_update'), 'message' => 'Both passwords must match.', 'required' => false, ) ) ); /** * Before isUniqueUsername * @param array $options * @return boolean */ /* function isUniqueUsername($check) { $username = $this->find( 'first', array( 'fields' => array( 'User.id', 'User.username' ), 'conditions' => array( 'User.username' => $check['username'] ) ) ); if(!empty($username)){ if($this->data[$this->alias]['id'] == $username['User']['id']){ return true; }else{ return false; } }else{ return true; } } */ /** * Before isUniqueEmail * @param array $options * @return boolean */ function isUniqueEmail($check) { $email = $this->find( 'first', array( 'fields' => array( 'User.id' ), 'conditions' => array( 'User.email' => $check['email'] ) ) ); if(!empty($email)){ if($this->data[$this->alias]['id'] == $email['User']['id']){ return true; }else{ return false; } }else{ return true; } } public function alphaNumericDashUnderscore($check) { // $data array is passed using the form field name as the key // have to extract the value to make the function generic $value = array_values($check); $value = $value[0]; return preg_match('/^[a-zA-Z0-9_ \-]*$/', $value); } public function equaltofield($check,$otherfield) { //get name of field $fname = ''; foreach ($check as $key => $value){ $fname = $key; break; } return $this->data[$this->name][$otherfield] === $this->data[$this->name][$fname]; } /** * Before Save * @param array $options * @return boolean */ public function beforeSave($options = array()) { // hash our password if (isset($this->data[$this->alias]['password'])) { $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']); } // if we get a new password, hash it if (isset($this->data[$this->alias]['password_update'])) { $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password_update']); } // fallback to our parent return parent::beforeSave($options); } } ?>
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
144 <?php class UsersController extends AppController { public $paginate = array( 'limit' => 25, 'conditions' => array('active' => '1'), 'order' => array('User.email' => 'asc' ) ); public function beforeFilter() { parent::beforeFilter(); $this->Auth->allow('login','add'); } public function login() { //if already logged-in, redirect if($this->Session->check('Auth.User')){ $this->redirect(array('action' => 'index')); } // if we get the post information, try to authenticate if ($this->request->is('post')) { //debug($this->Auth->login()); if ($this->Auth->login()) { //debug('login succeeded !'); //$this->Session->setFlash(__('Welcome, '. $this->Auth->user('name'))); //$this->redirect($this->Auth->redirectUrl()); } else { //debug('login unsucceeded !'); //$this->Session->setFlash(__('Invalid email or password')); } debug($this->data); } } public function logout() { $this->redirect($this->Auth->logout()); } public function index() { $this->paginate = array( 'limit' => 6, 'order' => array('User.name' => 'asc' ) ); $users = $this->paginate('User'); $this->set(compact('users')); } public function add() { if ($this->request->is('post')) { $this->User->create(); if ($this->User->save($this->request->data)) { $this->Session->setFlash(__('The user has been created')); $this->redirect(array('action' => 'index')); } else { $this->Session->setFlash(__('The user could not be created. Please, try again.')); } } $groups = $this->User->Group->find('list'); $this->set(compact('groups')); } public function edit($id = null) { if (!$id) { $this->Session->setFlash('Please provide a user id'); $this->redirect(array('action'=>'index')); } $user = $this->User->findById($id); if (!$user) { $this->Session->setFlash('Invalid User ID Provided'); $this->redirect(array('action'=>'index')); } if ($this->request->is('post') || $this->request->is('put')) { $this->User->id = $id; if ($this->User->save($this->request->data)) { $this->Session->setFlash(__('The user has been updated')); $this->redirect(array('action' => 'edit', $id)); }else{ $this->Session->setFlash(__('Unable to update your user.')); } } if (!$this->request->data) { $this->request->data = $user; } } public function delete($id = null) { if (!$id) { $this->Session->setFlash('Please provide a user id'); $this->redirect(array('action'=>'index')); } $this->User->id = $id; if (!$this->User->exists()) { $this->Session->setFlash('Invalid user id provided'); $this->redirect(array('action'=>'index')); } if ($this->User->saveField('active', 0)) { $this->Session->setFlash(__('User deleted')); $this->redirect(array('action' => 'index')); } $this->Session->setFlash(__('User was not deleted')); $this->redirect(array('action' => 'index')); } public function activate($id = null) { if (!$id) { $this->Session->setFlash('Please provide a user id'); $this->redirect(array('action'=>'index')); } $this->User->id = $id; if (!$this->User->exists()) { $this->Session->setFlash('Invalid user id provided'); $this->redirect(array('action'=>'index')); } if ($this->User->saveField('active', 1)) { $this->Session->setFlash(__('User re-activated')); $this->redirect(array('action' => 'index')); } $this->Session->setFlash(__('User was not re-activated')); $this->redirect(array('action' => 'index')); } } ?>
/views/users/add.ctp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 <!-- app/View/Users/add.ctp --> <div class="users form"> <?php echo $this->Form->create('User');?> <fieldset> <legend><?php echo __('Add User'); ?></legend> <?php echo $this->Form->input('name'); echo $this->Form->input('email'); echo $this->Form->input('password'); echo $this->Form->input('password_confirm', array('label' => 'Confirm Password *', 'maxLength' => 255, 'title' => 'Confirm password', 'type'=>'password')); echo $this->Form->input('group_id', array('label'=>'Groupe')); echo $this->Form->submit('Add User', array('class' => 'form-submit', 'title' => 'Click here to add the user') ); ?> </fieldset> <?php echo $this->Form->end(); ?> </div> <?php if($this->Session->check('Auth.User')){ echo $this->Html->link( "Return to Dashboard", array('action'=>'index') ); echo "<br>"; echo $this->Html->link( "Logout", array('action'=>'logout') ); }else{ echo $this->Html->link( "Return to Login Screen", array('action'=>'login') ); } ?>
/views/users/login.ctp
à un moment, quand j'entre l'email, à l'ajout, on me demande un email valide;
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 <div class="users form"> <?php echo $this->Session->flash('auth'); ?> <?php echo $this->Form->create('User'); ?> <fieldset> <legend><?php echo __('Please enter your email and password'); ?></legend> <?php echo $this->Form->input('email'); echo $this->Form->input('password'); //debug($this->data); ?> </fieldset> <?php echo $this->Form->end(__('Login')); ?> </div> <?php echo $this->Html->link( "Add A New User", array('action'=>'add') ); ?>
j'ai desactivé le require sur email , j'arrive à enregistré les users
maintenant quand je me loggue on me dit email or password false????....
question: es ce normal que mon debug affiche le password en etoile?
merci pour votre aide
Partager