Skip to content

Instantly share code, notes, and snippets.

@Stanton
Created July 20, 2012 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Stanton/3150890 to your computer and use it in GitHub Desktop.
Save Stanton/3150890 to your computer and use it in GitHub Desktop.
HoM Users Controller
<?php
class UsersController extends AppController {
var $name = 'Users';
var $scaffold = 'admin';
var $components = array('JqImgcrop');
var $helpers = array('Javascript', 'Thumbnail', 'CropImage');
function beforeFilter() {
$this->Auth->allow('index','view','login','logout','add');
parent::beforeFilter();
$this->Auth->autoRedirect = false;
}
function login() {
if ($this->Auth->user()) {
if (!empty($this->data) && $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']);
}
$referer = $this->referer();
if($referer != '/users/login') {
$this->redirect($this->referer());
} else {
$this->redirect($this->Auth->redirect());
}
}
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());
} else { // Delete invalid Cookie
$this->Cookie->del('Auth.User');
}
}
}
}
function logout() {
$this->Cookie->destroy('Auth.User');
$this->redirect($this->Auth->logout());
}
function admin_logout() {
$this->redirect($this->Auth->logout());
}
function index() {
}
function add() {
if (!empty($this->data)) {
$this->User->set($this->data);
if ($this->User->validates()) {
$this->set('password',$this->Auth->password($this->data['User']['password']));
if($this->User->save($this->data)) {
$this->Session->setFlash('Your account has been created! You can now sign in.');
$this->redirect('/users/login');
} else {
// because it gets hashed, clear out the password if we've failed
unset($this->data['User']['password']);
$this->Session->setFlash('Oh noes! There was a problem creating your account!');
}
} else {
// because it gets hashed, clear out the password if we've failed
unset($this->data['User']['password']);
$this->Session->setFlash('Oh noes! there were problems creating your account, please check the messages below.');
}
}
}
function edit($function=null) {
$this->layout = 'usersEdit';
$this->User->id = $this->Session->read('Auth.User.id');
if (isset($this->data['User']['function'])) {
$view = $this->data['User']['function'];
} elseif ($function != null && !is_numeric($function)) {
$view = $function;
} else {
$view = 'edit';
}
if (empty($this->data)) {
$this->data = $this->User->read();
$this->render($view);
} else {
if ($this->User->saveAll($this->data)) {
if($view=='edit' || isset($this->data['User']['function'])) {
if (isset($this->data['User']['username'])) {
$this->Session->write('Auth.User.username',$this->data['User']['username']);
}
}
$this->Session->setFlash('Your details have been updated.');
$this->render($view);
}
}
}
function avatar_step_2() {
$this->layout = 'usersEdit';
if (!empty($this->data)) {
$uploaded = $this->JqImgcrop->uploadImage($this->data['User']['avatar'], '/img/avatars/200px/', time() . '_');
$this->set('uploaded', $uploaded);
}
}
function avatar_step_3() {
$this->JqImgcrop->cropImage(151, $this->data['User']['x1'], $this->data['User']['y1'], $this->data['User']['x2'], $this->data['User']['y2'], $this->data['User']['w'], $this->data['User']['h'], $this->data['User']['imagePath'], $this->data['User']['imagePath']);
$this->User->id = $this->Session->read('Auth.User.id');
$this->User->saveField('avatar', $this->data['User']['avatar']);
$this->Session->setFlash('Your avatar has been updated, you look nice!');
$this->redirect('/users/edit/avatar/');
}
function edit_twitter() {
$this->layout = 'usersEdit';
$this->User->id = $this->Session->read('Auth.User.id');
$view = 'edit';
if (empty($this->data)) {
$this->data = $this->User->read();
$this->redirect('/users/edit/twitter/');
} else {
if ($this->User->save($this->data, array('validate' => false))) {
if (isset($this->data['User']['username'])) {
$this->Session->write('Auth.User.username',$this->data['User']['username']);
}
$this->Session->setFlash('Your details have been updated.');
$this->redirect('/users/edit/twitter/');
} else {
$this->Session->setFlash('There was a problem saving your changes, please try again...');
$this->redirect('/users/edit/twitter/');
}
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment