Created
April 26, 2011 20:00
-
-
Save kemo/943000 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php defined('SYSPATH') or die('No direct script access.'); | |
/** | |
* User controller | |
* Handles login, signup, post-signup pages | |
* | |
* @author Kemal Delalic <kemal.delalic@gmail.com> | |
* @version 1.0.0 | |
*/ | |
class Controller_User extends Controller_Template { | |
public function action_login() | |
{ | |
$user = ORM::factory('user'); | |
$values = $user->as_array(); | |
// If the form has been posted | |
if ($this->request->post()) | |
{ | |
// Merge empty values with posted ones | |
$values = Arr::merge($values, $this->request->post()); | |
// Extract email, password and remember from values | |
list($email, $password, $remember) = Arr::values($values, | |
'email','password','remember' | |
); | |
// If login succeeds, redirect to ... | |
if ($this->auth->login($email, $password, (bool) $remember)) | |
{ | |
// ... "after_signup/2" if this is the first login | |
if ($user->logins == 1) | |
{ | |
$this->request->redirect('after_signup/2'); | |
} | |
// ... "dashboard" | |
else | |
{ | |
$this->request->redirect('dashboard'); | |
} | |
} | |
} | |
$this->content = View::factory('user/login', array( | |
'values' => $values, | |
)); | |
} | |
public function action_logout() | |
{ | |
$this->request->redirect('login'); | |
} | |
public function action_signup() | |
{ | |
$errors = array(); | |
$user = new Model_User; | |
$values = $user->as_array(); | |
if ($this->request->post('email')) | |
{ | |
// Try creating a new user (email and stuff is done in the model | |
try | |
{ | |
$values = Arr::merge($values, $this->request->post(), array( | |
'password' => Text::random('alnum', 8), | |
)); | |
// Create the additional validation object for CSRF protection | |
$validation = Validation::factory($values) | |
->rule('csrf', 'not_empty') | |
->rule('csrf', 'Security::check'); | |
// Fill the user object with allowed values and try creating | |
$user->values($values, array('email','password')) | |
->create($validation); | |
// Get the login role and add it to user | |
$login_role = ORM::factory('role', array('name' => 'login')); | |
$user->add('roles', $login_role); | |
// Save the user object into session for later usage | |
Session::instance()->set('user', $user); | |
// Redirect the request to "after signup" page | |
$this->request->redirect('after_signup/1'); | |
} | |
catch (ORM_Validation_Exception $e) | |
{ | |
// Extract error messages from signup file | |
$errors += $e->errors('signup'); | |
} | |
} | |
$this->content = View::factory('user/signup', array( | |
'errors' => $errors, | |
'user' => $user, | |
'values' => $values, | |
)); | |
} | |
/** | |
* After registration action | |
* @param int $step of the 'post signup' process - passed from route | |
*/ | |
public function action_after_signup($step = 0) | |
{ | |
switch ($step) | |
{ | |
// After signup fail? | |
default: | |
// Redirect to homepage | |
$this->request->redirect(); | |
break; | |
// "We're waiting for you to confirm your account" | |
case 1: | |
// Get the user object from session | |
$user = Session::instance()->get('user'); | |
// If no user object, redirect to signup | |
if ($user === NULL) | |
$this->request->redirect('signup'); | |
$this->content = View::factory('user/signup/1', array( | |
'user' => $user, | |
)); | |
break; | |
// After login - "Welcome to our website, you're so awesome" | |
case 2: | |
if ( ! $this->auth->logged_in()) | |
$this->request->redirect(); | |
$user = $this->auth->get_user(); | |
$this->content = View::factory('user/signup/2', array( | |
'user' => $user, | |
)); | |
break; | |
// Buy credits intro | |
case 3: | |
$this->content = View::factory('user/signup/3', array( | |
)); | |
break; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php defined('SYSPATH') or die('No direct script access.'); | |
/** | |
* User controller | |
* Handles login, signup, post-signup pages | |
* | |
* @author Kemal Delalic <kemal.delalic@gmail.com> | |
* @version 1.0.0 | |
*/ | |
class Controller_User extends Controller_Template { | |
public function action_login() | |
{ | |
$user = ORM::factory('user'); | |
$values = $user->as_array(); | |
// If the form has been posted | |
if ($this->request->post()) | |
{ | |
// Merge empty values with posted ones | |
$values = Arr::merge($values, $this->request->post()); | |
// Extract email, password and remember from values | |
list($email, $password, $remember) = Arr::values($values, | |
'email','password','remember' | |
); | |
// If login succeeds, redirect to ... | |
if ($this->auth->login($email, $password, (bool) $remember)) | |
{ | |
// ... "after_signup/2" if this is the first login | |
if ($user->logins == 1) | |
{ | |
$this->request->redirect('after_signup/2'); | |
} | |
// ... "dashboard" | |
else | |
{ | |
$this->request->redirect('dashboard'); | |
} | |
} | |
} | |
$this->content = View::factory('user/login', array( | |
'values' => $values, | |
)); | |
} | |
public function action_logout() | |
{ | |
$this->request->redirect('login'); | |
} | |
public function action_signup() | |
{ | |
$errors = array(); | |
$user = new Model_User; | |
$values = $user->as_array(); | |
if ($this->request->post('email')) | |
{ | |
// Try creating a new user (email and stuff is done in the model | |
try | |
{ | |
$values = Arr::merge($values, $this->request->post(), array( | |
'password' => Text::random('alnum', 8), | |
)); | |
// Create the additional validation object for CSRF protection | |
$validation = Validation::factory($values) | |
->rule('csrf', 'not_empty') | |
->rule('csrf', 'Security::check'); | |
// Fill the user object with allowed values and try creating | |
$user->values($values, array('email','password')) | |
->create($validation); | |
// Get the login role and add it to user | |
$login_role = ORM::factory('role', array('name' => 'login')); | |
$user->add('roles', $login_role); | |
// Save the user object into session for later usage | |
Session::instance()->set('user', $user); | |
// Redirect the request to "after signup" page | |
$this->request->redirect('after_signup/1'); | |
} | |
catch (ORM_Validation_Exception $e) | |
{ | |
// Extract error messages from signup file | |
$errors += $e->errors('signup'); | |
} | |
} | |
$this->content = View::factory('user/signup', array( | |
'errors' => $errors, | |
'user' => $user, | |
'values' => $values, | |
)); | |
} | |
/** | |
* After registration action | |
* @param int $step of the 'post signup' process - passed from route | |
*/ | |
public function action_after_signup($step = 0) | |
{ | |
switch ($step) | |
{ | |
// After signup fail? | |
default: | |
// Redirect to homepage | |
$this->request->redirect(); | |
break; | |
// "We're waiting for you to confirm your account" | |
case 1: | |
// Get the user object from session | |
$user = Session::instance()->get('user'); | |
// If no user object, redirect to signup | |
if ($user === NULL) | |
$this->request->redirect('signup'); | |
$this->content = View::factory('user/signup/1', array( | |
'user' => $user, | |
)); | |
break; | |
// After login - "Welcome to our website, you're so awesome" | |
case 2: | |
if ( ! $this->auth->logged_in()) | |
$this->request->redirect(); | |
$user = $this->auth->get_user(); | |
$this->content = View::factory('user/signup/2', array( | |
'user' => $user, | |
)); | |
break; | |
// Buy credits intro | |
case 3: | |
$this->content = View::factory('user/signup/3', array( | |
)); | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment