pictcode / app / Model / User.php @ 001918d1
履歴 | 表示 | アノテート | ダウンロード (3.328 KB)
| 1 | 
      <?php
     | 
  
|---|---|
| 2 | 
      App::uses('AppModel', 'Model');  | 
  
| 3 | 
      App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth');  | 
  
| 4 | 
       | 
  
| 5 | 
       | 
  
| 6 | 
      /**
     | 
  
| 7 | 
       * User Model
     | 
  
| 8 | 
       *
     | 
  
| 9 | 
       * @property Login $Login
     | 
  
| 10 | 
       * @property Program $Program
     | 
  
| 11 | 
       */
     | 
  
| 12 | 
      class User extends AppModel {  | 
  
| 13 | 
       | 
  
| 14 | 
       | 
  
| 15 | 
      /**
     | 
  
| 16 | 
       * Validation rules
     | 
  
| 17 | 
       *
     | 
  
| 18 | 
       * @var array
     | 
  
| 19 | 
       */
     | 
  
| 20 | 
      public $validate = array(  | 
  
| 21 | 
      'login_id' => array(  | 
  
| 22 | 
      'rule' => array('notBlank'),  | 
  
| 23 | 
      'message' => 'この項目は入力必須です'  | 
  
| 24 | 
      ),  | 
  
| 25 | 
      'email' => array(  | 
  
| 26 | 
                      array(
     | 
  
| 27 | 
      'rule' => array('notBlank'),  | 
  
| 28 | 
      'message' => 'メールアドレスを入力してください'  | 
  
| 29 | 
      ),  | 
  
| 30 | 
                  // メールアドレスであること。
     | 
  
| 31 | 
      'isEmail' => array(  | 
  
| 32 | 
      'rule' => 'Email',  | 
  
| 33 | 
      'message' => '正しいメールアドレスを入力してください'  | 
  
| 34 | 
      ),  | 
  
| 35 | 
                  array(
     | 
  
| 36 | 
      'rule' => 'DuplicateEmailCheck',  | 
  
| 37 | 
      'message' => 'このメールアドレスは既に登録されています'  | 
  
| 38 | 
      ),  | 
  
| 39 | 
                  // 一意性チェック
     | 
  
| 40 | 
                  // 'emailExists' => array( 'rule' => 'isUnique', 'message' => '既に登録されています'),
     | 
  
| 41 | 
      ),  | 
  
| 42 | 
      'password' => array(  | 
  
| 43 | 
                  array(
     | 
  
| 44 | 
      'rule' => array('notBlank'),  | 
  
| 45 | 
      'message' => 'パスワードを入力してください'  | 
  
| 46 | 
      ),  | 
  
| 47 | 
                  array(
     | 
  
| 48 | 
      'rule' => array('minLength', 8),  | 
  
| 49 | 
      'message' => 'パスワードは8文字以上入力してください',  | 
  
| 50 | 
      ),  | 
  
| 51 | 
                  array(
     | 
  
| 52 | 
      'rule' => 'passwordConfirm',  | 
  
| 53 | 
      'message' => 'パスワードが一致していません'  | 
  
| 54 | 
      ),  | 
  
| 55 | 
      ),  | 
  
| 56 | 
      'password_confirm' => array(  | 
  
| 57 | 
                  array(
     | 
  
| 58 | 
      'rule' => array('notBlank'),  | 
  
| 59 | 
      'message' => 'パスワード(確認)を入力してください'  | 
  
| 60 | 
      ),  | 
  
| 61 | 
      ),  | 
  
| 62 | 
      'status' => array(  | 
  
| 63 | 
      'numeric' => array(  | 
  
| 64 | 
      'rule' => array('numeric'),  | 
  
| 65 | 
                                      //'message' => 'Your custom message here',
     | 
  
| 66 | 
                                      //'allowEmpty' => false,
     | 
  
| 67 | 
                                      //'required' => false,
     | 
  
| 68 | 
                                      //'last' => false, // Stop validation after this rule
     | 
  
| 69 | 
                                      //'on' => 'create', // Limit validation to 'create' or 'update' operations
     | 
  
| 70 | 
      ),  | 
  
| 71 | 
      ),  | 
  
| 72 | 
      );  | 
  
| 73 | 
       | 
  
| 74 | 
       | 
  
| 75 | 
      public function DuplicateEmailCheck(){  | 
  
| 76 | 
      $query = array('conditions' => array('User.email' => $this->data['User']['email'],'User.status' => 1));  | 
  
| 77 | 
      $num = $this->find('count',$query);  | 
  
| 78 | 
                      //$this->log($recodes);
     | 
  
| 79 | 
                      //var_dump($num);
     | 
  
| 80 | 
                      //exit;
     | 
  
| 81 | 
      if($num > 0){  | 
  
| 82 | 
      return false; //登録済み  | 
  
| 83 | 
                      } else{
     | 
  
| 84 | 
      return true; //未登録  | 
  
| 85 | 
      }  | 
  
| 86 | 
      }  | 
  
| 87 | 
       | 
  
| 88 | 
      public function passwordConfirm($check){  | 
  
| 89 | 
              //2つのパスワードフィールドが一致する事を確認する
     | 
  
| 90 | 
      if($this->data['User']['password'] === $this->data['User']['password_confirm']){  | 
  
| 91 | 
      return true;  | 
  
| 92 | 
              }else{
     | 
  
| 93 | 
      return false;  | 
  
| 94 | 
      }  | 
  
| 95 | 
       | 
  
| 96 | 
      }  | 
  
| 97 | 
       | 
  
| 98 | 
       | 
  
| 99 | 
      public function getActivationHash() {  | 
  
| 100 | 
                  // ユーザIDの有無確認
     | 
  
| 101 | 
      if (!isset($this->id)) {  | 
  
| 102 | 
      return false;  | 
  
| 103 | 
      }  | 
  
| 104 | 
                  // 更新日時をハッシュ化
     | 
  
| 105 | 
      return Security::hash( $this->field('updated'), 'md5', true);  | 
  
| 106 | 
      }  | 
  
| 107 | 
       | 
  
| 108 | 
       | 
  
| 109 | 
      public function beforeSave($options = array()) {  | 
  
| 110 | 
      if (isset($this->data[$this->alias]['password'])) {  | 
  
| 111 | 
      $passwordHasher = new BlowfishPasswordHasher();  | 
  
| 112 | 
      $this->data[$this->alias]['password'] = $passwordHasher->hash(  | 
  
| 113 | 
      $this->data[$this->alias]['password']  | 
  
| 114 | 
      );  | 
  
| 115 | 
      }  | 
  
| 116 | 
      return true;  | 
  
| 117 | 
      }  | 
  
| 118 | 
       | 
  
| 119 | 
       | 
  
| 120 | 
      }  |