Skip to content

Commit

Permalink
Merge pull request #66 from mateusKoppe/externalSubscription
Browse files Browse the repository at this point in the history
Inscrição para usuário externo
  • Loading branch information
mateusKoppe authored Oct 6, 2019
2 parents 89ba8b0 + 376dc0a commit e06f2ab
Show file tree
Hide file tree
Showing 26 changed files with 299 additions and 925 deletions.
153 changes: 110 additions & 43 deletions App/Controllers/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Controllers;

use App\Models\User;
use App\Helpers\View;
use App\Helpers\AuthHelper;
use App\Helpers\UtilsHelper;
Expand All @@ -14,50 +15,50 @@ public function logout ($request, $response, $args) {
->withStatus(302);
}

public function loginForm ($request, $response, $args) {
public function subscription ($request, $response, $args) {
$user = AuthHelper::getAuthenticatedUser();

if ($user) {
return $response
->withHeader('Location', UtilsHelper::base_url("/perfil"))
->withStatus(302);
}

View::render('layout/website/header');
View::render('login');
View::render('auth/subscription');
View::render('layout/website/footer');
return $response;
}
}

public function profileUpdate ($request, $response, $args) {
public function studantLoginForm ($request, $response, $args) {
$user = AuthHelper::getAuthenticatedUser();
$user->registration = @$_POST['registration'];
$user->save();
return $response
->withHeader('Location', UtilsHelper::base_url("/perfil"))
->withStatus(302);

if ($user) {
return $response
->withHeader('Location', UtilsHelper::base_url("/perfil"))
->withStatus(302);
}

View::render('layout/website/header');
View::render('auth/studant-login');
View::render('layout/website/footer');
return $response;
}

public function login ($request, $response, $args) {
$aLoginError = false;
$aHasAccount = false;

public function studantLogin ($request, $response, $args) {
if (!isset($_POST['user'], $_POST['password'])) {
View::render('layout/admin/header', $data);
View::render('auth/login', array(
'loginError' => true
));
View::render('layout/admin/footer', $data);
return $response;
return $response
->withHeader('Location', UtilsHelper::base_url("/inscricao/aluno"))
->withStatus(302);
}

$username = $_POST['user'];

$user = AuthHelper::loginUsingPortal($username, $_POST['password']);

/* TODO: Use flash messages and redirect */
if (!$user) {
View::render('layout/admin/header', $data);
View::render('auth/login', array(
View::render('auth/studant-login', array(
'loginError' => true
));
View::render('layout/admin/footer', $data);
Expand All @@ -72,12 +73,98 @@ public function login ($request, $response, $args) {
->withStatus(302);
}

public function externalRegisterForm ($request, $response, $args) {
$user = AuthHelper::getAuthenticatedUser();

if ($user) {
return $response
->withHeader('Location', UtilsHelper::base_url("/perfil"))
->withStatus(302);
}

View::render('layout/website/header');
View::render('auth/external-register');
View::render('layout/website/footer');
return $response;
}

public function externalRegister ($request, $response, $args) {
if (empty($_POST['user']) || empty($_POST['name']) || empty($_POST['password']) || empty($_POST['password_confirm'])) {
return $response
->withHeader('Location', UtilsHelper::base_url("/inscricao"))
->withStatus(302);
}
$user = new User();
$user->name = $_POST['name'];
$user->login = $_POST['user'];
$user->email = $_POST['user'];
$user->password = AuthHelper::hash($_POST['password']);
$user->type = User::USER_LEVEL_EXTERNAL;
$user->save();
$_SESSION['user'] = $user->id;
return $response
->withHeader('Location', UtilsHelper::base_url("/perfil"))
->withStatus(302);
}

public function externalLoginForm ($request, $response, $args) {
$user = AuthHelper::getAuthenticatedUser();

if ($user) {
return $response
->withHeader('Location', UtilsHelper::base_url("/perfil"))
->withStatus(302);
}

View::render('layout/website/header');
View::render('auth/external-login');
View::render('layout/website/footer');
return $response;
}

public function externalLogin ($request, $response, $args) {
$user = AuthHelper::getAuthenticatedUser();
if ($user) {
return $response
->withHeader('Location', UtilsHelper::base_url("/perfil"))
->withStatus(302);
}

if (!isset($_POST['user'], $_POST['password'])) {
return $response
->withHeader('Location', UtilsHelper::base_url("/inscricao/visitante/login"))
->withStatus(302);
}

$user = User::findByCredentials($_POST['user'], $_POST['password']);
if (!$user) {
return $response
->withHeader('Location', UtilsHelper::base_url("/inscricao/visitante/login"))
->withStatus(302);
}

$_SESSION['user'] = $user->id;
return $response
->withHeader('Location', UtilsHelper::base_url("/perfil"))
->withStatus(302);
}


public function profileUpdate ($request, $response, $args) {
$user = AuthHelper::getAuthenticatedUser();
$user->registration = @$_POST['registration'];
$user->save();
return $response
->withHeader('Location', UtilsHelper::base_url("/perfil"))
->withStatus(302);
}

public function profile ($request, $response, $args) {
$user = AuthHelper::getAuthenticatedUser();

if (!$user) {
return $response
->withHeader('Location', UtilsHelper::base_url("/login"))
->withHeader('Location', UtilsHelper::base_url("/inscricao"))
->withStatus(302);
}

Expand All @@ -88,24 +175,4 @@ public function profile ($request, $response, $args) {
View::render('layout/website/footer', $data);
return $response;
}

public function subscriptionForm ($request, $response, $args) {
AuthHelper::allowNonAuthenticated();

$aLoginError = false;
$aIsUFFS = isset($_POST['uffs']) && $_POST['uffs'] == '1';
$aHasAccount = false;

View::render('auth/register', array(
'loginError' => $aLoginError,
'user' => @$_POST['user'],
'uffs' => !isset($_POST['uffs']) ? '1' : $_POST['uffs'],
'email' => @$_POST['email'],
'name' => @$_POST['name'],
'passworde' => @$_POST['passworde'],
'password' => @$_POST['password'],
'isLogin' => false
));
return $response;
}
}
}
6 changes: 0 additions & 6 deletions App/Controllers/CompetitionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ public function index ($request, $response, $args) {
public function create ($request, $response, $args) {
AuthHelper::restrictToPermission(User::USER_LEVEL_ADMIN);
$user = AuthHelper::getAuthenticatedUser();
$isAdmin = $user->isLevel(User::USER_LEVEL_ADMIN);

if (!$isAdmin) {
View::render('restricted');
return $response;
}

$competitions = Competition::findAll();

Expand Down
51 changes: 0 additions & 51 deletions App/Controllers/EventController.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,55 +144,4 @@ public function store ($request, $response, $args) {
->withHeader('Location', UtilsHelper::base_url("/admin/evento/$id"))
->withStatus(302);
}

/* TODO: Implement this */
public function attempt($request, $response, $args)
{
AuthHelper::restrictToPermission(User::USER_LEVEL_ADMIN);

$data = [];
$user = User::getById($_SESSION['user']);
$isAdmin = $user->isLevel(User::USER_LEVEL_ADMIN);

if (!$isAdmin) {
header("Location: restricted.php");
exit();
}

$event = Event::getById($args['id']);
$users = [];
$attending = [];
$paidCredit = Payment::findUsersWithPaidCredit();
$emailsPaid = [];
$emailsNonPaid = [];

if($event) {
if (isset($_REQUEST['remove'])) {
$data['createdOrUpdated'] = attendingRemove($_REQUEST['remove'], $event['id']);
}

$attending = Subscription::findByUserId($args['id']);
$users = userFindByIds(array_keys($attending));
}

foreach($users as $aId => $aInfo) {
$users[$aId]['admin'] = $aInfo['type'] == User::USER_LEVEL_ADMIN;
$users[$aId]['source'] = $aInfo['type'] == User::USER_LEVEL_UFFS || $aInfo['type'] == User::USER_LEVEL_ADMIN ? 'UFFS' : 'Externo';
$users[$aId]['paid'] = isset($paidCredit[$aId]) && $paidCredit[$aId] >= userGetConferencePrice($aInfo);

if ($users[$aId]['paid']) {
$emailsPaid[] = $aInfo['email'];
} else {
$emailsNonPaid[] = $aInfo['email'];
}
}

$data['users'] = $users;
$data['event'] = $event;
$data['attending'] = $attending;
$data['emailsPaid'] = $emailsPaid;
$data['emailsNonPaid'] = $emailsNonPaid;

View::render('attending-event', $data);
}
}
27 changes: 0 additions & 27 deletions App/Controllers/PaymentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,31 +79,4 @@ public function delete ($request, $response, $args) {
->withHeader('Location', UtilsHelper::base_url("/admin/pagamento"))
->withStatus(302);
}

public function apiIndex ($request, $response, $args)
{
AuthHelper::AllowAuthenticated();

$aUser = AuthHelper::getAuthenticatedUser();
$aMustPay = Payment::calculateUserDept($aUser);
$aCredit = Payment::calculateUserCredit($aUser->id);
$aDebit = $aMustPay - $aCredit;

$aData = array();
$aData['dept'] = $aDebit;
$aData['payments'] = Payment::findByUser($aUser->id);
$aData['showPayButton'] = $aDebit > 0;
$aData['noDept'] = $aDebit <= 0;

$aPaymentIsBeingAnalyzed = true;

foreach($aData['payments'] as $aId => $aPayment) {
$aPaymentIsBeingAnalyzed = false;
}

$aData['beingAnalyzed'] = $aPaymentIsBeingAnalyzed;

\App\Helpers\View::render('ajax-payments', $aData);
return $response;
}
}
18 changes: 3 additions & 15 deletions App/Helpers/AuthHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
require_once dirname(__FILE__). '/../../vendor/rmccue/requests/library/Requests.php';

use App\Models\User;
use App\Helpers\DatabaseHelper;
use App\Helpers\AuthHelper;
use App\Helpers\View;

class AuthHelper {
public static function hash($thePassword) {
return md5($thePassword . PASSWORD_SALT);
return sha1($thePassword . PASSWORD_SALT);
}

public static function getAuthenticatedUser() {
Expand All @@ -27,17 +25,6 @@ public static function allowNonAuthenticated() {
}
}

public static function allowAdmin() {
if(!AuthHelper::isAuthenticated()) {
header('Location: login.php');
exit();

} else {
header('Location: restricted.php');
exit();
}
}

public static function allowAuthenticated() {
$user = AuthHelper::getAuthenticatedUser();
$title = '401';
Expand Down Expand Up @@ -67,7 +54,7 @@ public static function logout() {
unset($_SESSION['user']);
}

public function isAuthenticated() {
public static function isAuthenticated() {
return isset($_SESSION['user']);
}

Expand Down Expand Up @@ -103,6 +90,7 @@ public static function loginUsingPortal($username, $password) {
if(is_numeric($username[0])){
$user->cpf = str_replace(array('.', '-'),'', $username);
}
$user->type = User::USER_LEVEL_UFFS;
$user->save();
return $user;
}
Expand Down
Loading

0 comments on commit e06f2ab

Please sign in to comment.