Skip to content

Instantly share code, notes, and snippets.

@Stanton
Created January 13, 2012 14:15
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/1606415 to your computer and use it in GitHub Desktop.
Save Stanton/1606415 to your computer and use it in GitHub Desktop.
Add method
<?php
App::uses('AppController', 'Controller');
/**
* Users Controller
*
* @property User $User
*/
class UsersController extends AppController {
var $scaffold = 'admin';
public $uses = array('Twitter');
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('add', 'login', 'logout', 'edit'); // not sure edit belongs here
}
/**
* login method
*
* @return void
*/
public function login() {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirect());
} elseif ($this->request->is('post')) {
$this->Session->setFlash(__('Invalid username or password, try again.'));
}
}
/**
* logout method
*
* @return void
*/
public function logout() {
$this->Session->setFlash(__('You\'ve been logged out. Come back soon, we might know more of your Twitter friends\' gamer handles!'));
$this->redirect($this->Auth->logout());
}
/**
* index method
*
* @return void
*/
public function index() {$this->layout = 'default';
if ($this->Auth->user('id')) {
$this->User->id = $this->Auth->user('id');
if (!$this->User->exists()) {
throw new NotFoundException(__('Invalid user'));
}
$this->set('user', $this->User->read(null, $this->User->id));
}
}
// /**
// * view method
// *
// * @param string $id
// * @return void
// */
// public function view() {
// $this->User->id = $this->Auth->user('id');
// if (!$this->User->exists()) {
// throw new NotFoundException(__('Invalid user'));
// }
// $this->set('user', $this->User->read(null, $id));
// }
/**
* add method
*
* @return void
*/
public function add() {
if ($this->request->is('post')) {
$this->User->set($this->request->data);
if (!$this->User->validates(array('fieldList' => array('username', 'email')))) {
$errors = $this->User->invalidFields('username');
if (
(isset($errors['username']) && $errors['username'][0] == 'Sorry, this username has already been taken.') ||
(isset($errors['email']) && $errors['email'][0] == 'We already have an account with this email address.')
) {
$this->Session->setFlash(__('It looks like you may already have an account, would you like to <a href="/user/login">try logging in instead?</a>'));
return false;
}
}
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('Oh dear! We had a problem creating your account, please see the errors below.'));
}
}
}
/**
* edit method
*
* @param string $id
* @return void
*/
public function edit() {
$this->User->id = $this->Auth->user('id');
if (!$this->User->exists()) {
throw new NotFoundException(__('Invalid user'));
}
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
} else {
$this->request->data = $this->User->read(null, $this->User->id);
}
}
/**
* delete method
*
* @param string $id
* @return void
*/
public function delete($id = null) {
// if (!$this->request->is('post')) {
// throw new MethodNotAllowedException();
// }
// $this->User->id = $id;
// if (!$this->User->exists()) {
// throw new NotFoundException(__('Invalid user'));
// }
// if ($this->User->delete()) {
// $this->Session->setFlash(__('User deleted'));
// $this->redirect(array('action' => 'index'));
// }
// $this->Session->setFlash(__('User was not deleted'));
// $this->redirect(array('action' => 'index'));
}
public function friends() {
$account = $this->Twitter->find('first', array(
'conditions' => array(
'user_id' => $this->Auth->user('id')
),
'fields' => 'Twitter.friends'
));
$friends = unserialize($account['Twitter']['friends']);
return $friends;
}
public function find_handles() {
$this->User->id = $this->Auth->user('id');
print_r($this->User);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment