Skip to content

Instantly share code, notes, and snippets.

@Tjoosten
Created January 4, 2017 16:02
Show Gist options
  • Save Tjoosten/fd70de4bd6b0ffae77eb4825df79d308 to your computer and use it in GitHub Desktop.
Save Tjoosten/fd70de4bd6b0ffae77eb4825df79d308 to your computer and use it in GitHub Desktop.
<?php
namespace ActivismBe\Controllers;
use Silex\Application;
use ActivismBe\Models\User;
use Silex\Api\ControllerProviderInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Validator\Constraints as Assert;
class AuthController implements ControllerProviderInterface
{
/**
* Variable used to put the app instance in.
*
* @var mixed
*/
private $app;
/**
* IndexController instance.
*
* @param \Silex\Application $app
* @return void.
*/
public function __construct($app)
{
$this->app = $app;
}
/**
* Factory routing instance.
*
* @param Application $app
* @return mixed
*/
public function connect(Application $app)
{
$factory = $app['controllers_factory'];
$factory->get('/login', [$this, 'loginView'])->bind('login.view');
$factory->get('/register', [$this, 'registerView'])->bind('register.view');
$factory->post('/login', [$this, 'authenticate'])->bind('login.authenticate');
$factory->post('/register', [$this, 'registerMethod'])->bind('register.method');
return $factory;
}
/**
*
*/
public function loginView()
{
$data['errors'] = [];
return $this->app['twig']->render('auth\login.html.twig', $data);
}
/**
* Authencation controller with the given user input.
*
* @see POST: http://www.domain.tld/auth/login
* @param Request $input
* @return Redirect
*/
public function authenticate(Request $input)
{
$constraints['email'] = [ new Assert\NotBlank(), new Assert\Email() ];
$constraints['password'] = [ new Assert\NotBlank() ];
$errors['validation'] = $this->app['validator']->validate($input->request->all(), new Assert\Collection($constraints));
$MySQL['credentials'] = User::where('email', $input->get('email'))
->with(['permissions'])
->where('password', md5($input->get('password')))
->where('blocked', 'N');
if (count($errors['validation']) > 0 || $MySQL['credentials']->count() === 0) { // There are errors
$data['errors'] = $errors;
return $this->app['twig']->render('auth\login.html.twig', $data);
}
// No errors so we can move on with the auth method.
$authencation = [];
$permissions = [];
foreach ($MySQL['credentials']->get() as $user) { // Define the data to the session array's.
// Building up the session.
foreach ($user->permissions as $perm) {
array_push($permissions, $perm->role); // Push every key invidual to the permissions array.
}
$authencation['id'] = $user->id;
$authencation['name'] = $user->name;
$authencation['email'] = $user->email;
$authencation['permissions'] = $permissions;
}
$this->app['session']->set('logged_in', $authencation);
return $this->app->redirect('backend');
}
/**
* Register view for a new user.
*
* @see GET|HEAD: http://www.domain.tld/auth/register
* @return Twig view.
*/
public function registerView()
{
$data['title'] = 'Register';
return $this->app['twig']->render('auth\register', $data);
}
/**
*
*/
public function registerMethod()
{
}
}
{% if errors %}
<ul>
{% for error in errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
<form class="http://localhost:8000/auth/login" method="post">
<input type="text" name="email" placeholder="email">
<input type="text" name="password" value="password">
<button type="submit">Submmit</button>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment