Newer
Older
<?php
/**
* Copyright Maarch since 2008 under licence GPLv3.
* See LICENCE.txt file at the root folder for more details.
* This file is part of Maarch software.
*/
/**
* @brief Authentication Controller
*
* @author dev@maarch.org
*/
namespace SrcCore\controllers;
use Respect\Validation\Validator;
use Slim\Http\Request;
use Slim\Http\Response;
class AuthenticationController
{
public static function authentication()
{
if (!empty($_SERVER['PHP_AUTH_USER']) && !empty($_SERVER['PHP_AUTH_PW'])) {
if (AuthenticationModel::authentication(['login' => $_SERVER['PHP_AUTH_USER'], 'password' => $_SERVER['PHP_AUTH_PW']])) {
$user = UserModel::getByLogin(['select' => ['id'], 'login' => $_SERVER['PHP_AUTH_USER']]);
}
} else {
$cookie = AuthenticationModel::getCookieAuth();
if (!empty($cookie) && AuthenticationModel::cookieAuthentication($cookie)) {
AuthenticationModel::setCookieAuth(['id' => $cookie['id']]);
$id = $cookie['id'];
public static function log(Request $request, Response $response)
{
$check = Validator::stringType()->notEmpty()->validate($body['login']);
$check = $check && Validator::stringType()->notEmpty()->validate($body['password']);
if (!$check) {
return $response->withStatus(400)->withJson(['errors' => 'Bad Request']);
}
if (!AuthenticationModel::authentication(['login' => $body['login'], 'password' => $body['password']])) {
return $response->withStatus(401)->withJson(['errors' => 'Authentication Failed']);
}
$user = UserModel::getByLogin(['login' => $body['login'], 'select' => ['id', 'mode']]);
if ($user['mode'] != 'standard') {
return $response->withStatus(403)->withJson(['errors' => 'Login unauthorized']);
}
AuthenticationModel::setCookieAuth(['id' => $user['id']]);
UserModel::update(['set' => ['reset_token' => json_encode(['token' => null, 'until' => null])], 'where' => ['id = ?'], 'data' => [$user['id']]]);
'code' => 'OK',
'objectType' => 'users',
'objectId' => $user['id'],
'type' => 'LOGIN',
return $response->withJson(['user' => UserController::getUserInformationsById(['id' => $user['id']])]);
public static function logout(Request $request, Response $response)
{
AuthenticationModel::deleteCookieAuth();
'code' => 'OK',
'objectType' => 'users',
'objectId' => $GLOBALS['id'],
'type' => 'LOGOUT',
return $response->withJson(['success' => 'success']);
}