Welcome to Paster, Anonymous Friend!
Added by anonymous on 2008-10-23 17:53:55 Download/View
  1. class Model_User extends Amodel {
  2.         protected $table = 'users';
  3.         protected $table_key = 'id';
  4.         protected $fields = array('id' => '',
  5.                                 'display_name' => '',
  6.                                 'username' => '',
  7.                                 'email' => '',
  8.                                 'password' => '',
  9.                                 'auth_code' => '',
  10.                                 'created_time' => '',
  11.                                 'active' => '',
  12.                                 'deleted' => '',
  13.                                 'banned' => '',
  14.                                 'token' => '',
  15.                                 'token_expires' => '',
  16.                                 'last_active' => '',
  17.                                 'admin_login' => '',
  18.                                 'logins' => '',
  19.                                 'token_user_agent' => ''
  20.         );
  21.         protected $rules = array(
  22.                                         'display_name'  => array('required', 'length[4,100]'),
  23.                                         'username'              => array('required', 'length[4,100]', 'alpha_dash'),
  24.                                         'email'                 => array('required', 'email'),
  25.                                         'password'              => array('required', 'length[8,100]', 'matches[password2]')
  26.         );
  27.         protected $callbacks = array(
  28.                                         'username'      => array('is_unique')
  29.         );
  30.         public function pre_validate($data, $is_update = FALSE, $before = array())
  31.         {
  32.                 if ($is_update)
  33.                 {
  34.                         // Don't check for unique username if they didn't change the username
  35.                         if (isset($data['username'])  AND isset($before['username']) AND $data['username'] == $before['username'])
  36.                         {
  37.                                 unset($this->callbacks['username']);
  38.                         }
  39.                        
  40.                         // We need the create_time for the password salt
  41.                         $data['created_time'] = $before['created_time'];
  42.                        
  43.                         // The password isn't required for updates
  44.                         $this->rules['password'] =  array('length[8,100]', 'matches[password2]');
  45.                        
  46.                 }
  47.                 return $data;
  48.         }
  49. }
  50.