Skip to content

Instantly share code, notes, and snippets.

@pmache
Created January 28, 2014 12:54
Show Gist options
  • Save pmache/17a8d037b89f275235ef to your computer and use it in GitHub Desktop.
Save pmache/17a8d037b89f275235ef to your computer and use it in GitHub Desktop.
user.php
<?php
class User_Controller extends Base_Controller{
public $restful = true;
public function __construct() {
$this->filter( 'before', 'guest' )->except( array( 'logout', 'validate' ) );
// Note: We may not always require CSRF on login for system based logins so ignore it here.
$this->filter( 'before', 'csrf' )->on( 'post' )->except( array( 'login' ) );
}
public function get_login(){
return View::make('user.login');
}
public function post_login()
{
$rules = array(
'username' => Input::get('username|required'),
'password' => Input::get('password|required')
);
if ( Auth::attempt($rules) )
{
Locker::trigger_event(1, Auth::user()->id);
return Redirect::to('home');
}
else
{
return Redirect::to('login')
->with('login_errors', true);
}
}
public function get_register(){
return View::make('user.register');
}
public function post_register($email_confirm = true)
{
$input = Input::all();
$rules = array(
'username' => 'required|unique:users',
'first_name' => 'alpha|max:80',
'last_name' => 'alpha|max:120',
'email' => 'required|unique:users|email',
'password' => 'required|confirmed|min:6'
);
$validator = Validator::make($input, $rules);
if ($validator->fails())
return Redirect::to('register')
->with_errors($validator)
->with_input('except', array('password', 'password_confirm'));
$password = $input['password'];
$password = Hash::make($password);
$user = new User;
$user->username = $input['username'];
$user->name = $input['first_name'].' '.$input['last_name'];
$user->email = $input['email'];
$user->password = $password;
unset($user->password_confirmation);
unset($user->csrf_token);
if(!$email_confirm){
$user->validated = 0;
$user->save();
$user->validation_code = Str::random(array_get($arguments, 0, 32));
$mailer = IoC::resolve('mailer');
$message = Swift_Message::newInstance('Postman delivery from Developers Project')
->setFrom(array(Config::get('email.from')=>Config::get('email.name')))
->setTo(array($user->email_adress=>$user->first_name.' '.$user->last_name))
->setBody(
'<html>'.
'<body>'.
'<h2>Welcome to Developers Project</h2>'.
'<p>You need to confirm, that you want to join community</p>'
'<p>Please, <a href="doNotClick"'.$user->id.'/'.htmlentities($user->validation_code).
'"click here</a>.</p>'.
'</body>'.'</html>','text/html');
$result=$mailer->send($message);}
else
$user->validated=1
}
$user->save();
$_POST['csrf_token'] = Session::token();
$this->post_login(Input::all());
return Redirect::to('index');
}
public function get_validate($id, $code)
{
$key = $html_entity_decode($code);
$validation_code = DB::table('users')->where('id', '=', $id)->only('validation_code');
if($validation_code === $key){
$affected = DB::table('users')
->where('id', '=', $id)
->update(array(
'validated' => 1,
'validation_code => NULL'));
} else
return Redirect::to('index')->with('errors', 'Invalid validation code.');
Session::flash( 'notice', 'You have confirmed your e-mail.' );
return Redirect::to( 'login' );
}
public function get_profile()
{
$title = ucwords(Auth::user()->username."'s Page");
return View::make('user.profile')
->with('title', $title);
}
public function get_logout()
{
Auth::logout();
return Redirect::to('/');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment