ããªãã¼ã·ã§ã³ã®ãã¹ããã©ã¯ãã£ã¹
ã¤ã³ãããã¯ã·ã§ã³
CakePHPã®ä½¿ãæ¹ã¯å¤ç¨®å¤æ§ã§ããã¡ããä¸ã¤ã®ããæ¹ãæ£è§£ã¨ãããã¨ã¯ããã¾ããã
ããããCakePHPã¯ãã¬ã¼ã ã¯ã¼ã¯ã§ããããã§ãæ³å®ããã使ãæ¹ä»¥å¤ã§ã¯ãã®ç価ããªããªãçºæ®ã§ãã¾ããã
CakePHPã«ãããã¢ãã«ã¯ããã¸ãã¹ãã¸ãã¯ãç½®ãã¬ã¤ã¤ã¨ãã¦æ³å®ããã¦ãã¾ãã
ããã¦ãããªãã¼ã·ã§ã³ãç¨ãããã¨ã«ãã£ã¦ä¿åã®ãã¸ãã¯ãæ§ç¯ããã¨ãããã¨ãæ³å®ããã¦ãã¾ãã
ããã¯ãä½æ
Cookbookã§ç´¹ä»ãããã³ã¼ãããã»ã¨ãã©ããªãã¼ã·ã§ã³ã¨Model::save()ã®çµã¿åããã§ãããã¨ãããã¨ãã®çãã«ããªã£ã¦ãã¾ãã
æªãä¾
ããªãã¯Model::save()ã®ä»£ããã¨ãã¦ã以ä¸ã®ãããªadd()ã¡ã½ãããã¢ãã«ã«å®ç¾©ãã¦ããããããã¾ãã:
<?php class Post extends AppModel { var $validate = array( 'user_id' => array( 'numeric' => array( 'rule' => array('numeric'), ), ), // ... ); var $belongsTo = array('User'); // $user_id ã¯ã³ã³ããã¼ã©ã®ã¢ã¯ã·ã§ã³å´ã§æå®ï¼$this->Auth->user('id')ï¼ function add($user_id, $data) { $this->User->id = $user_id; if (!$user_id || !$this->User->exists()) { throw new NonValidUserError(); return false; } $data[$this->alias]['user_id'] = $user_id; // ... $this->create($data); return $this->save(); } }
ããã¯åç´ã«ããã°ã¤ã³ãã¦ããã¦ã¼ã¶ã§ãããã¨ããã¸ãã¯ã¨ãã¦ä¿éããããã®ã³ã¼ãã§ãã
user_idã®ããªãã¼ã·ã§ã³ã¯bakeããã ãã®ã³ã¼ãã§ããã¾ãæå³ãæã¡ã¾ããã
ãã¦ããã®ã³ã¼ãã®ä½ãæªããã¨ããã¨ã
- æ´æ°ã®æã®ãã¸ãã¯ã§ã¾ãåãã³ã¼ããæ¸ãå¿ è¦ãåºã¦ãã => ã³ããã®æ¸©åº
- ä¾å¤ãæãããããªæ±ãå¦çãå¢ããï¼Cakeã®æ¨æºæ©è½ã§å¦çããããªããªãï¼ => ãããããã
- ããªãã¼ã·ã§ã³ã¡ãã»ã¼ã¸ä»¥å¤ãèæ ®ã«å ¥ããªããã°ãªããªããããã³ã³ããã¼ã©ããã¥ã¼ãæ±ããªã
ã¨ãã£ãç¹ãæãããã¾ãã
è¯ãä¾
<?php class Post extends AppModel { var $validate = array( 'user_id' => array( 'isCurrentUser' => array( 'rule' => array('isCurrentUser'), 'message' => 'ãã°ã¤ã³ã¦ã¼ã¶ã§ã¯ããã¾ãã', ), ), // ... ); var $belongsTo = array('User'); function isCurrentUser($check) { $user_id = current($check); // AppControllerãã©ããã§Configure::write('CurrentUser' = $this->Auth->User())ãªã©ã¨ãã if($user_id !== Configure::read('CurrentUser.id') { return false; } $this->User->id = $user_id if (!$this->User->exists()) { return 'ãµã¼ãå é¨ã¨ã©ã¼ã§ãã管çè ã«ãåãåãããã ãã'; } return true; } }
æ©æµ
ããã§ãåºæ¬çã«ã³ã³ããã¼ã©ã§ã¯Model::save()ãå¼ã¶ã ãã§ãã
è¦æ
£ããã³ã¼ãã§ãã¹ã¦è¨è¿°ã§ããã¨ããç¹ã§ãååé«ééçºã«è²¢ç®ãã¾ãã
ã¾ããããªãã¼ã·ã§ã³ã¡ãã»ã¼ã¸ã¯ãæååãè¿ããã¨ã«ããå¤æ´ãå¯è½ãªãããã©ã®ãããªã¨ã©ã¼ããå¶å¾¡ãããã¨ãã§ãã¾ãã
ããè¤éãªã±ã¼ã¹ã®ä¾
<?php class Post extends AppModel { var $validate = array( 'user_id' => array( 'isCurrentUser' => array( 'rule' => array('isCurrentUser'), 'message' => 'ãã°ã¤ã³ã¦ã¼ã¶ã§ã¯ããã¾ãã', ), 'userHasThis' => array( 'rule' => array('userHasThis'), 'message' => 'ãã®åçã¯ããªãã®ãã®ã§ã¯ããã¾ãã', 'on' => 'update', ), ), // ... ); var $belongsTo = array('User'); function isCurrentUser($check) { $user_id = current($check); // AppControllerãã©ããã§Configure::write('CurrentUser' = $this->Auth->User())ãªã©ã¨ãã if($user_id !== Configure::read('CurrentUser.id') { return false; } $this->User->id = $user_id if (!$this->User->exists()) { return 'ãµã¼ãå é¨ã¨ã©ã¼ã§ãã管çè ã«ãåãåãããã ãã'; } return true; } function userHasThis($check) { $user_id = current($check); if ($this->field('user_id') !== $user_id) { return false; } return true; } }
ãã®ããã«ãã¦ãæ´æ°æã®ã¿ã®ãã§ãã¯ããããã¨ãã§ãã¾ãã
ããã¯bakeã§çæãããã³ã¼ãã«ãå«ã¾ããã®ã§ããåç¥ã®æ¹ãå¤ãã¨æãã¾ãã
whitelist
Model::save()ã¨Model::validate()ã«ã¯ãfieldListã¨ããåã®ãªãã·ã§ã³ãããã¾ãã
ãããæå®ããã¨ãä»»æã®ãã£ã¼ã«ãã®ã¿ããªãã¼ã·ã§ã³ã¨ä¿åãã§ãã¾ãã
ã¾ããããã©ã«ãã¨ãã¦Model::whitelistã«ãåæ§ã«æå®ãã§ãã¾ãã
ä¾ãã°ãã¹ã¯ã¼ãã ãå¤æ´ããã¨ãã¯ãã¹ã¯ã¼ãã ãã®ããªãã¼ã·ã§ã³ããããã¨ãã«ããã使ãã¾ãã
æ°ããªã¡ã½ãããç¨æããå¿
è¦ã¯ããã¾ããã
æ´ã«ãåè¿°ã®'on' => 'update'ãªã©ã¨çµã¿åãããã°ãã»ã¨ãã©ã®ã±ã¼ã¹ã«å¯¾ãã¦å¯¾å¿ãããã¨ãã§ãã¾ãã
ããããããã ãã§ã¯å¯¾å¿ã§ããªãã±ã¼ã¹ããã¡ããããã¾ãã
ä¾ãã°åé¤æãéè¨ãç¹å®ã®æ¡ä»¶ãæºããã¬ã³ã¼ãå
¨ã¦ãæ´æ°ããã¨ãã§ãã
ãã®å ´åãã¢ãã«ã®ã¡ã½ããã¨ãã¦ã¾ã¨ãããã¨ã¯ä½ãåé¡ããã¾ããã
ã¾ã¨ã
- Cakeã®åºæ¬æ©è½ããé¸è±ãããããã¨ã¯ããªã
- ããªãã¼ã·ã§ã³ããã¾ã使ãã
- åºæ¬ã¯ããªãã¼ã·ã§ã³ã®'on'ãªãã·ã§ã³ãwhitelistã使ã£ã¦æè»ã«å¯¾å¿
- å ¨ã¦ã®ã¡ã½ãããä½è¨ã¨ãªãããã§ã¯ãªã
ç§æã§ãããCakePHPã¯ããããæ¯ãã¦ããææã«å
¥ããã¨ãã¦ãã¾ãã
ããã©ã³ãããã¦ã¿ãã°ãããã®ãã¨ã¯å½ç¶ã®ãããªãã¨ããããã¾ããã
ããããæµ·å¤ã§ã¯æµ¸éãã¦ãããã¹ããã©ã¯ãã£ã¹ã¯ãæ¥æ¬ã§ã¯æ
å ±ãå°ãªãã§ããããã¯ä»æ¹ãç¡ããã¨ãªã®ããããã¾ããã
CakePHPã®æå±ããé«ééçºãå®ç¾ããããã«ããããããã¯å°ããã¤ãã¹ããã©ã¯ãã£ã¹ãã¾ã¨ãã¦ãããããããªã¨æãã¾ãã