Commit 4fb3079b by Juan Montilla

Register a medias

parent cdb4f7f8
......@@ -21,6 +21,9 @@ class Filters extends BaseConfig
'honeypot' => Honeypot::class,
'invalidchars' => InvalidChars::class,
'secureheaders' => SecureHeaders::class,
'user_auth' => \App\Filters\UserAuth::class,
'admin_auth' => \App\Filters\AdminAuth::class,
];
/**
......
......@@ -33,7 +33,9 @@ use App\Controllers\User;
$routes->match(['get'], '/', [User::class, 'login']);
$routes->match(['get', 'post'], '/login', [User::class, 'login']);
$routes->match(['get'], '/logged', [User::class, 'user_ok']);
$routes->match(['get', 'post'], '/register', [User::class, 'register']);
$routes->match(['get'], '/logged', [User::class, 'user_ok'], ['filter' => 'user_auth']);
......
......@@ -17,7 +17,7 @@ class User extends BaseController
$userModel = new \App\Models\UserModel();
$data['users'] = $userModel->findAll();
return view('templates/header')
. view('user/admin_list', $data)
. view('user/list', $data)
. view('templates/footer');
}
......@@ -48,7 +48,7 @@ class User extends BaseController
$session->set('user', $user);
return redirect()->to(base_url('/logged'));
} else {
$session->setFlashdata('msg', 'Credenciales');
$session->setFlashdata('msg', 'Credenciales incorrectas');
}
} else {
$data["errors"] = $validation->getErrors();
......@@ -65,10 +65,60 @@ class User extends BaseController
. view('templates/footer');
}
public function logout()
{
# To Do.
}
public function unauthorized()
{
return view('templates/header')
. view('user/unauthorized')
. view('templates/footer');
}
public function register()
{
$validation = \Config\Services::validation(); //Comprueba que los datos sean únicos en la base de datos, etc.
$rules = [
"username" => [
"label" => "Username",
"rules" => "required"
],
"email" => [
"label" => "Email",
"rules" => "required|valid_email|is_unique[user.email]"
],
"password" => [
"label" => "Password",
"rules" => "required"
]
];
$data = [];
$session = session();
$userModel = model('UserModel');
if ($this->request->getMethod() == "post") {
if ($this->validate($rules)) {
$username = $this->request->getVar('username');
$email = $this->request->getVar('email');
$password = $this->request->getVar('password');
$user = [
'username' => $username,
'email' => $email,
'password' => password_hash($password, PASSWORD_DEFAULT),
];
$userModel->saveUser($email, $username, $password);
$session->setFlashdata('msg', 'Registro exitoso');
return redirect()->to(base_url('/login'));
} else {
$data["errors"] = $validation->getErrors();
}
}
return view('templates/header')
. view('pages/home')
. view('templates/footer');
}
}
\ No newline at end of file
<?php
namespace App\Filters;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Filters\FilterInterface;
class AdminAuth implements FilterInterface
{
public function before(RequestInterface $request, $arguments = null)
{
if (!session('logged_in'))
return redirect()->to(site_url('/login'));
elseif ((session('user')->role & 2) == 0)
return redirect()->to(site_url('/unauthorized'));
}
public function after(
RequestInterface $request, ResponseInterface $response,
$arguments = null
) {
}
}
\ No newline at end of file
<?php
namespace App\Filters;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Filters\FilterInterface;
class UserAuth implements FilterInterface
{
public function before(RequestInterface $request, $arguments = null)
{
if (!session('logged_in'))
return redirect()->to(site_url('/login'));
elseif ((session('user')->role & 1) == 0)
return redirect()->to(site_url('/unauthorized'));
}
public function after(
RequestInterface $request, ResponseInterface $response,
$arguments = null
) {
}
}
\ No newline at end of file
......@@ -26,5 +26,15 @@ class UserModel extends Model
return FALSE;
}
public function saveUser($email, $username, $password)
{
$data = [
'email' => $email,
'username' => $username,
'password' => password_hash($password, PASSWORD_DEFAULT),
];
return $this->insert($data);
}
}
\ No newline at end of file
......@@ -29,7 +29,7 @@
<div class="col-md-8">
<div class="container" id="container">
<div class="form-container sign-up-container">
<form action="#" style="flex-direction: column; padding: 0 30px;">
<form action=<?= base_url('/register'); ?> method="post" style="flex-direction: column; padding: 0 30px;">
<h1>Crear cuenta</h1>
<div class="social-container">
<a href="https://accounts.google.com/" class="social"><img
......@@ -40,10 +40,23 @@
src="<?= base_url("iconos/apple.ico") ?>" width="52" height="52"></a>
</div>
<span>o usa tu correo</span>
<input style="background-color: #eee;" type="text" placeholder="Nombre" />
<input style="background-color: #eee;" type="email" placeholder="Email" />
<input style="background-color: #eee;" type="password" placeholder="Contraseña" />
<button id="signup-button">Registrarse</button>
<input style="background-color: #eee;" class="form-control" name="username" type="text" placeholder="Nombre" />
<input style="background-color: #eee;" class="form-control" name="email" type="email" placeholder="Email" />
<input style="background-color: #eee;" class="form-control" name="password" type="password" placeholder="Contraseña" />
<span class="error">
<?= \Config\Services::validation()->listErrors(); ?>
</span>
<span class="error">
<?php if (session()->getFlashdata('register_error')): ?>
<div class="alert alert-danger">
<?= session()->getFlashdata('msg') ?>
</div>
<?php endif; ?>
</span>
<button id="signup-button" type="submit">Registrarse</button>
</form>
</div>
<div class="form-container sign-in-container">
......
......@@ -37,7 +37,7 @@
<header id="header" class="header fixed-top d-flex align-items-center">
<div class="d-flex align-items-center justify-content-between">
<a href="index.html" class="logo d-flex align-items-center">
<a href="/home" class="logo d-flex align-items-center">
<img src="<?= base_url("iconos/logo.png") ?>" alt="" style="margin-right: 0px;">
<img class="d-none d-lg-block" src="<?= base_url("iconos/logo_a_medias.png") ?> " style="margin-left: 0px;"></img>
</a>
......
h2 class="alert alert-danger">Access denied</h2>
<p>You are not authorized to access this page.</p>
__ci_last_regenerate|i:1679676447;_ci_previous_url|s:32:"http://localhost/index.php/login";logged_in|b:1;user|O:8:"stdClass":3:{s:5:"email";s:14:"admin@admin.es";s:8:"username";s:13:"Pruebausuario";s:8:"password";s:60:"$2y$10$cg2bjfW6UXlmOPMgwB2Ate.uGnpGzInHeACcN0sDEz.TJcS3LZCiO";}
\ No newline at end of file
__ci_last_regenerate|i:1679672792;_ci_previous_url|s:32:"http://localhost/index.php/login";logged_in|b:1;user|O:8:"stdClass":3:{s:5:"email";s:14:"admin@admin.es";s:8:"username";s:13:"Pruebausuario";s:8:"password";s:60:"$2y$10$cg2bjfW6UXlmOPMgwB2Ate.uGnpGzInHeACcN0sDEz.TJcS3LZCiO";}
\ No newline at end of file
__ci_last_regenerate|i:1679670509;_ci_previous_url|s:32:"http://localhost/index.php/login";
\ No newline at end of file
__ci_last_regenerate|i:1679670892;_ci_previous_url|s:32:"http://localhost/index.php/login";logged_in|b:1;user|O:8:"stdClass":3:{s:5:"email";s:14:"admin@admin.es";s:8:"username";s:13:"Pruebausuario";s:8:"password";s:60:"$2y$10$cg2bjfW6UXlmOPMgwB2Ate.uGnpGzInHeACcN0sDEz.TJcS3LZCiO";}
\ No newline at end of file
__ci_last_regenerate|i:1679656981;_ci_previous_url|s:32:"http://localhost/index.php/login";logged_in|b:1;user|O:8:"stdClass":3:{s:5:"email";s:14:"admin@admin.es";s:8:"username";s:13:"Pruebausuario";s:8:"password";s:60:"$2y$10$cg2bjfW6UXlmOPMgwB2Ate.uGnpGzInHeACcN0sDEz.TJcS3LZCiO";}msg|s:17:"Wrong credentials";__ci_vars|a:1:{s:3:"msg";s:3:"new";}
\ No newline at end of file
__ci_last_regenerate|i:1679659193;_ci_previous_url|s:32:"http://localhost/index.php/login";logged_in|b:1;user|O:8:"stdClass":3:{s:5:"email";s:14:"admin@admin.es";s:8:"username";s:13:"Pruebausuario";s:8:"password";s:60:"$2y$10$cg2bjfW6UXlmOPMgwB2Ate.uGnpGzInHeACcN0sDEz.TJcS3LZCiO";}msg|s:17:"Wrong credentials";__ci_vars|a:1:{s:3:"msg";s:3:"new";}
\ No newline at end of file
__ci_last_regenerate|i:1679673794;_ci_previous_url|s:32:"http://localhost/index.php/login";logged_in|b:1;user|O:8:"stdClass":3:{s:5:"email";s:14:"admin@admin.es";s:8:"username";s:13:"Pruebausuario";s:8:"password";s:60:"$2y$10$cg2bjfW6UXlmOPMgwB2Ate.uGnpGzInHeACcN0sDEz.TJcS3LZCiO";}msg|s:12:"Credenciales";__ci_vars|a:1:{s:3:"msg";s:3:"old";}
\ No newline at end of file
__ci_last_regenerate|i:1679673521;_ci_previous_url|s:32:"http://localhost/index.php/login";
\ No newline at end of file
__ci_last_regenerate|i:1679675225;_ci_previous_url|s:32:"http://localhost/index.php/login";logged_in|b:1;user|O:8:"stdClass":3:{s:5:"email";s:14:"admin@admin.es";s:8:"username";s:13:"Pruebausuario";s:8:"password";s:60:"$2y$10$cg2bjfW6UXlmOPMgwB2Ate.uGnpGzInHeACcN0sDEz.TJcS3LZCiO";}
\ No newline at end of file
__ci_last_regenerate|i:1679676447;_ci_previous_url|s:32:"http://localhost/index.php/login";logged_in|b:1;user|O:8:"stdClass":3:{s:5:"email";s:14:"admin@admin.es";s:8:"username";s:13:"Pruebausuario";s:8:"password";s:60:"$2y$10$cg2bjfW6UXlmOPMgwB2Ate.uGnpGzInHeACcN0sDEz.TJcS3LZCiO";}
\ No newline at end of file
__ci_last_regenerate|i:1679675747;_ci_previous_url|s:35:"http://localhost/index.php/register";logged_in|b:1;user|O:8:"stdClass":3:{s:5:"email";s:14:"admin@admin.es";s:8:"username";s:13:"Pruebausuario";s:8:"password";s:60:"$2y$10$cg2bjfW6UXlmOPMgwB2Ate.uGnpGzInHeACcN0sDEz.TJcS3LZCiO";}
\ No newline at end of file
__ci_last_regenerate|i:1679676069;_ci_previous_url|s:32:"http://localhost/index.php/login";logged_in|b:1;user|O:8:"stdClass":3:{s:5:"email";s:14:"admin@admin.es";s:8:"username";s:13:"Pruebausuario";s:8:"password";s:60:"$2y$10$cg2bjfW6UXlmOPMgwB2Ate.uGnpGzInHeACcN0sDEz.TJcS3LZCiO";}
\ No newline at end of file
__ci_last_regenerate|i:1679659193;_ci_previous_url|s:32:"http://localhost/index.php/login";logged_in|b:1;user|O:8:"stdClass":3:{s:5:"email";s:14:"admin@admin.es";s:8:"username";s:13:"Pruebausuario";s:8:"password";s:60:"$2y$10$cg2bjfW6UXlmOPMgwB2Ate.uGnpGzInHeACcN0sDEz.TJcS3LZCiO";}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment