SW/panel_user/includes/formChangePass.php

96 lines
5.0 KiB
PHP
Raw Normal View History

2021-05-09 16:14:52 +02:00
<?php
2021-05-09 16:27:21 +02:00
require_once('../assets/php/form.php');
include_once('../assets/php/common/user.php');
2021-05-09 16:14:52 +02:00
2021-05-09 16:27:21 +02:00
class FormChangePass extends Form {
public function __construct() {
$options = array("action" => "./?option=manage_profile");
parent::__construct('formChangeUserPass', $options);
}
protected function generaCamposFormulario($datos, $errores = array()){
$nombre = $datos['nombreUsuario'] ?? '';
// Se generan los mensajes de error si existen.
$htmlErroresGlobales = self::generaListaErroresGlobales($errores);
2021-05-10 11:52:45 +02:00
$errorOldPass = self::createMensajeError($errores, 'old_pass', 'span', array('class' => 'error'));
2021-05-09 16:27:21 +02:00
$errorPassword = self::createMensajeError($errores, 'pass', 'span', array('class' => 'error'));
$errorPassword2 = self::createMensajeError($errores, 'repass', 'span', array('class' => 'error'));
2021-05-10 11:52:45 +02:00
$html = "<div class='row'>
<fieldset id='contraseña_usuario'><pre>".$htmlErroresGlobales."</pre>
<legend>Nueva Contraseña</legend>
<input type='password' name='old_pass' id='old_pass' value='' placeholder='Contraseña Actual' required/><pre>".$errorOldPass."</pre>
<input type='password' name='pass' id='pass' value='' placeholder='Nueva Contraseña' required/><pre>".$errorPassword."</pre>
<input type='password' name='repass' id='repass' value='' placeholder='Repita la nueva contraseña' required/><pre>".$errorPassword2."</pre>
2021-05-09 20:31:54 +02:00
</fieldset>
2021-05-10 11:52:45 +02:00
<div class='actions'>
<input type='submit' id='submit' value='Cambiar Contraseña' class='primary' />
<input type='reset' id='reset' value='Borrar' />
2021-05-09 20:31:54 +02:00
</div>
2021-05-10 11:52:45 +02:00
</div>";
2021-05-09 16:27:21 +02:00
return $html;
}
protected function procesaFormulario($datos){
$result = array();
2021-05-09 16:46:12 +02:00
$old_pass = $datos['old_pass'] ?? null;
2021-05-09 21:59:52 +02:00
if ( empty($old_pass) || mb_strlen($old_pass) < 4 ) {
2021-05-10 11:52:45 +02:00
$result['old_pass'] = "El password tiene que tener\n una longitud de al menos\n 4 caracteres.";
2021-05-09 16:27:21 +02:00
}
$password = $datos['pass'] ?? null;
2021-05-09 21:59:52 +02:00
if ( empty($password) || mb_strlen($password) < 4 ) {
2021-05-10 11:52:45 +02:00
$result['pass'] = "El password tiene que tener una\n longitud de al menos\n 4 caracteres.";
2021-05-09 16:27:21 +02:00
}
$password2 = $datos['repass'] ?? null;
if ( empty($password2) || strcmp($password, $password2) !== 0 ) {
2021-05-09 16:46:12 +02:00
$result['repass'] = "Los passwords deben coincidir.";
2021-05-09 16:27:21 +02:00
}
if (count($result) === 0) {
$bd = new UserDAO("complucine");
2021-05-10 11:52:45 +02:00
$user = $bd->selectUser(unserialize($_SESSION['user'])->getName(), $old_pass);
2021-05-09 16:27:21 +02:00
if (!$user) {
2021-05-10 11:52:45 +02:00
$result[] = "Ha ocurrido un problema\nal actualizar la contraseña.";
$_SESSION['message'] = "<div class='row'>
<div class='column side'></div>
<div class='column middle'>
<div class='code info'>
<h1>Ha ocurrido un probrema</h1><hr />
2021-05-10 12:30:15 +02:00
<p>No hemos podido actualizar su contraseña de usuario.
Comprueba que la contraseña actual sea correcta.</p>
2021-05-10 11:52:45 +02:00
<a href=''><button>Cerrar Mensaje</button></a>
</div>
2021-05-09 21:59:52 +02:00
</div>
2021-05-10 11:52:45 +02:00
<div class='column side'></div>
2021-05-09 21:59:52 +02:00
</div>
2021-05-10 11:52:45 +02:00
";
} else {
$bd->changeUserPass(unserialize($_SESSION['user'])->getId(), $password);
2021-05-09 16:46:12 +02:00
$_SESSION['message'] = "<div class='row'>
<div class='column side'></div>
<div class='column middle'>
<div class='code info'>
<h1>Operacion realizada con exito</h1><hr />
2021-05-10 11:52:45 +02:00
<p>Se ha modificado su contraseña de usuario correctamente.</p>
2021-05-09 16:46:12 +02:00
<a href=''><button>Cerrar Mensaje</button></a>
</div>
</div>
<div class='column side'></div>
</div>
";
2021-05-09 16:27:21 +02:00
$result = './?option=manage_profile';
}
}
return $result;
}
protected function test_input($input){
return htmlspecialchars(trim(strip_tags($input)));
}
}
2021-05-09 16:14:52 +02:00
?>