Skip to content

Commit

Permalink
code for admin login and refactored user login a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
dogmatic69 committed Feb 17, 2010
1 parent ef27526 commit 0f0e5e0
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 32 deletions.
4 changes: 4 additions & 0 deletions app_controller.php
Expand Up @@ -98,6 +98,10 @@ function __setupAuth(){

$this->Auth->autoRedirect = false;
$this->Auth->loginRedirect = '/';

if (isset($this->params['prefix']) && $this->params['prefix'] == 'admin') {
$this->Auth->loginRedirect = '/admin';
}
$this->Auth->logoutRedirect = '/';
}

Expand Down
103 changes: 71 additions & 32 deletions infinitas/management/controllers/users_controller.php
Expand Up @@ -37,43 +37,23 @@ function beforeFilter(){
* @access public
*/
function login(){
if ($this->Auth->user()) {
if (!empty($this->data['User']['remember_me'])) {
$cookie = array();
$cookie['username'] = $this->data['User']['username'];
$cookie['password'] = $this->data['User']['password'];
$this->Cookie->write('Auth.User', $cookie, true, '+2 weeks');
unset($this->data['User']['remember_me']);
}
//$this->redirect($this->Auth->redirect());
}
$this->_createCookie();

if(!(empty($this->data)) && $this->Auth->user()){
$this->User->recursive = -1;
$lastLogon = $this->User->read(
array(
'User.ip_address',
'User.last_login',
'User.country',
'User.city'
),
$this->Auth->user('id')
);

$data['User']['id'] = $this->Auth->user('id');
$data['User']['ip_address'] = $this->RequestHandler->getClientIP();
$data['User']['last_login'] = date('Y-m-d H:i:s');
$data['User']['modified'] = false;
$data['User']['browser'] = $this->Infinitas->getBrowser();
$data['User']['operating_system'] = $this->Infinitas->getOperatingSystem();

$data['User']['country'] = $this->Infinitas->getCountry();
$data['User']['city'] = $this->Infinitas->getCity();
$data['User']['is_mobile'] = $this->RequestHandler->isMobile();

$data = $this->_getUserData();

if ($this->User->save($data)) {
$currentUser = $this->Session->read('Auth.User');

$lastLogon = $this->User->getLastLogon($this->Auth->user('id'));

// there is something wrong
if ($lastLogon === false) {
$this->redirect('/logout');
}

$this->Session->write('Auth.User', array_merge($data['User'], $currentUser));
$this->Session->setFlash(
sprintf(
Expand All @@ -88,19 +68,46 @@ function login(){
}
$this->redirect($this->Auth->redirect());
}
}

function _getUserData(){
$data['User']['id'] = $this->Auth->user('id');
$data['User']['ip_address'] = $this->RequestHandler->getClientIP();
$data['User']['last_login'] = date('Y-m-d H:i:s');
$data['User']['modified'] = false;
$data['User']['browser'] = $this->Infinitas->getBrowser();
$data['User']['operating_system'] = $this->Infinitas->getOperatingSystem();

$data['User']['country'] = $this->Infinitas->getCountry();
$data['User']['city'] = $this->Infinitas->getCity();
$data['User']['is_mobile'] = $this->RequestHandler->isMobile();

return $data;
}

function _checkCookie(){
if (!empty($this->data)) {
$cookie = $this->Cookie->read('Auth.User');
if (!is_null($cookie)) {
if ($this->Auth->login($cookie)) {
// Clear auth message, just in case we use it.
$this->Session->del('Message.auth');
$this->redirect($this->Auth->redirect());
}
}
}
}

function _createCookie(){
if ($this->Auth->user()) {
if (!empty($this->data['User']['remember_me'])) {
$cookie = array();
$cookie['username'] = $this->data['User']['username'];
$cookie['password'] = $this->data['User']['password'];
$this->Cookie->write('Auth.User', $cookie, true, '+2 weeks');
unset($this->data['User']['remember_me']);
}
}
}

/**
* Logout method.
*
Expand All @@ -124,7 +131,39 @@ function register(){


function admin_login(){
$this->layout = 'admin_login';

$this->_createCookie();

if(!(empty($this->data)) && $this->Auth->user()){
$this->User->recursive = -1;

$data = $this->_getUserData();

if ($this->User->save($data)) {
$currentUser = $this->Session->read('Auth.User');

$lastLogon = $this->User->getLastLogon($this->Auth->user('id'));

// there is something wrong
if ($lastLogon === false) {
$this->redirect('/logout');
}

$this->Session->write('Auth.User', array_merge($data['User'], $currentUser));
$this->Session->setFlash(
sprintf(
__('Welcome back %s, your last login was from %s, %s on %s. (%s)', true),
$currentUser['username'],
$lastLogon['User']['country'],
$lastLogon['User']['city'],
$lastLogon['User']['last_login'],
$lastLogon['User']['ip_address']
)
);
}
$this->redirect($this->Auth->redirect());
}
}

function admin_logout(){
Expand Down
16 changes: 16 additions & 0 deletions infinitas/management/models/user.php
Expand Up @@ -108,6 +108,22 @@ function validPassword($field = null){
return true; preg_match('/'.Configure::read('Website.password_regex').'/', $field['confirm_password']);
}

function getLastLogon($user_id){
if (!$user_id) {
return false;
}

return $this->read(
array(
'User.ip_address',
'User.last_login',
'User.country',
'User.city'
),
$user_id
);
}

function parentNode() {
if (!$this->id && empty($this->data)) {
return null;
Expand Down

0 comments on commit 0f0e5e0

Please sign in to comment.