Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // UsersController
- class UsersController extends AppController {
- var $name = "Users";
- public function login() {
- if (empty($this->data)) {
- return;
- }
- $user = $this->User->authsomeLogin('credentials', $this->data['User']);
- if (!$user) {
- $this->Session->setFlash('Unknown user or wrong password');
- return;
- }
- }
- public function register() {
- if (empty($this->data)) {
- return;
- }
- if ($this->User->save($this->data)) {
- $this->Session->setFlash('Your entry has been saved.');
- if($this->login()) {
- $this->redirect(array('controller'=>'users', 'action'=>'setup'));
- }
- }
- }
- function setup() {
- if(empty($this->data)) {
- return;
- }
- $this->validate = false;
- if ($this->User->save($this->data, array('validate' => false))) {
- $this->Session->setFlash('Your entry has been saved.');
- $this->redirect(array('controller'=>'users', 'action'=>'view'));
- } else {
- $this->Session->setFlash('Data not saved');
- debug($this->data);
- }
- }
- function edit() {
- if(empty($this->data)) {
- return;
- }
- if ($this->User->save($this->data)) {
- $this->Session->setFlash('Your entry has been saved.');
- $this->redirect(array('controller'=>'users', 'action'=>'view'));
- }
- }
- }
- // User Model
- class User extends AppModel {
- var $validate = array(
- 'username' => array(
- 'alphaNumeric' => array(
- 'rule'=>'alphaNumeric',
- 'message'=>'Letters and numbers only'
- ),
- 'between'=>array(
- 'rule'=>array('between', 6, 25),
- 'message'=>'Must be between 6 and 25 characters'
- ),
- 'isUnique'=>array(
- 'rule'=>'isUnique',
- 'message'=>'That username is already taken'
- )
- ),
- 'password'=>array(
- 'minLength'=>array(
- 'rule'=>array('minLength', 6),
- 'message'=>'Password must be at least 6 characters'
- ),
- 'passwordMatch'=>array(
- 'rule'=> 'passwordMatch',
- 'message'=>'Your passwords must match'
- )
- ),
- 'email'=>array(
- 'email'=>array(
- 'rule'=>'email',
- 'message'=>'Not a valid email address'
- ),
- 'emailMatch'=>array(
- 'rule'=> 'emailMatch',
- 'message'=>'Your email addresses must match'
- )
- ),
- 'state'=>array(
- 'alphaNumeric'=>array(
- 'rule'=> 'alphaNumeric',
- 'message'=>'Not a valid state'
- )
- ),
- 'city'=>array(
- 'alphaNumeric'=>array(
- 'rule'=>'alphaNumeric',
- 'message'=>'Not a valid city'
- )
- ),
- 'zip'=>array(
- 'zip'=>array(
- 'rule'=>'postal',
- 'message'=>'Not a valid postal code'
- )
- )
- );
- function passwordMatch() {
- if ($this->data['User']['password'] == $this->data['User']['confirm_password']) {
- return true;
- }
- return false;
- }
- function emailMatch() {
- if ($this->data['User']['email'] == $this->data['User']['confirm_email']) {
- return true;
- }
- return false;
- }
- function beforeSave() {
- if(!empty($this->data['User']['password'])) {
- $this->data['User']['password'] = Authsome::hash($this->data['User']['password']);
- return true;
- }
- }
- public function authsomeLogin($type, $credentials = array()) {
- switch ($type) {
- case 'guest':
- return array('it'=>'works');
- case 'credentials':
- $password = Authsome::hash($credentials['password']);
- $conditions = array(
- 'User.username' => $credentials['username'],
- 'User.password' => $password,
- );
- break;
- default:
- return null;
- }
- return $this->find('first', compact('conditions'));
- }
- }
- // Setup (update) view
- echo $form->create('User', array('controller'=>'users', 'action'=>'setup'));
- echo $form->input('firstname', array('label'=>'First Name'));
- echo $form->input('lastname', array('label'=>'Last Name'));
- echo $form->input('email', array('label'=>'Email'));
- echo $form->input('confirm_email', array('label'=>'Confirm Email'));
- echo $form->input('phone', array('label'=>'Phone'));
- echo $form->input('street', array('label'=>'Street'));
- echo $form->input('city', array('label'=>'City'));
- echo $form->input('state', array('label'=>'State'));
- echo $form->input('zip', array('label'=>'Postal Code'));
- echo $form->input('id', array('type'=>'hidden', 'value'=>Authsome::get('User.id')));
- echo $form->end('Save');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement