Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // funcion que genera un string random
- function random_str($length, $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
- {
- $pieces = [];
- $max = mb_strlen($keyspace, '8bit') - 1;
- for ($i = 0; $i < $length; ++$i) {
- $pieces []= $keyspace[random_int(0, $max)];
- }
- return implode('', $pieces);
- }
- public function forget_pass()
- {
- // verifica que es un metodo post
- if ($this->request->('get')){
- $email_user = $this->request->data['email'];
- // verificar que el mail existe
- $user = $this->Users->find('all', array('conditions' => array('Users.email' => $email_user)))->firt();
- // si el usuario no existe
- if ($user == null){
- $this->Flash->success('Este email no se encuentra registrado.');
- }
- else{
- // genera una nueva contraseña random de cuatro caracteres
- $new_password = random_str(4);
- // se envia el correo a la direccion del usuario
- $email = new Email('default');
- $email->setFrom(['[email protected]' => 'ServiBat'])
- ->setTo($email_user);
- ->setSubject('Nueva contraseña'),
- ->send($new_password);
- $user->password = $new_password;
- //si la nueva contraseña se guarda con exito
- if($this->Users->save($user)){
- $this->Flash->success('Error al cambiar la nueva pasword.');
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment