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;
use SrcCore\models\CoreConfigModel;
use SrcCore\models\LangModel;
class AuthenticationController
{
public static function authentication()
{
if (!empty($_SERVER['PHP_AUTH_USER']) && !empty($_SERVER['PHP_AUTH_PW'])) {
if (AuthenticationModel::authentication(['email' => $_SERVER['PHP_AUTH_USER'], 'password' => $_SERVER['PHP_AUTH_PW']])) {
$user = UserModel::getByEmail(['select' => ['id'], 'email' => $_SERVER['PHP_AUTH_USER']]);
$id = $user['id'];
new LangModel(['language' => CoreConfigModel::getLanguage()]);
}
} 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)
{
$data = $request->getParams();
$check = Validator::stringType()->notEmpty()->validate($data['email']);
$check = $check && Validator::stringType()->notEmpty()->validate($data['password']);
if (!$check) {
return $response->withStatus(400)->withJson(['errors' => 'Bad Request']);
}
if (!AuthenticationModel::authentication(['email' => $data['email'], 'password' => $data['password']])) {
return $response->withStatus(401)->withJson(['errors' => 'Authentication Failed']);
}
$user = UserModel::getByEmail(['email' => $data['email'], 'select' => ['id', 'mode']]);
if ($user['mode'] != 'standard') {
return $response->withStatus(403)->withJson(['errors' => 'Login unauthorized']);
}
AuthenticationModel::setCookieAuth(['id' => $user['id']]);
'eventType' => 'AUTHENTICATION',
'info' => "userLogin"
]);
return $response->withJson(['user' => UserController::getUserInformationsById(['id' => $user['id']])]);
public static function logout(Request $request, Response $response)
{
AuthenticationModel::deleteCookieAuth();
'eventType' => 'AUTHENTICATION',
'info' => "userLogout"
]);
return $response->withJson(['success' => 'success']);
}