mediaãã©ã°ã¤ã³å¿ç¨(11) ã¢ãããã¼ãã¦ã¼ã¶æ å ±ã®è¿½å
Mediaãã©ã°ã¤ã³ã¯ããã©ã®ã¦ã¼ã¶ãã¢ãããã¼ãããããã®æ
å ±ãä¿ç®¡ãã¾ããã
ãã®ãããå¥ã®ã¦ã¼ã¶ãã¢ãããã¼ããããã¡ã¤ã«ã§ãã£ã¦ããIDãæ£ããæå®ããã°åé¤ã§ãã¦ãã¾ãã¾ãã
attachmentsãã¼ãã«ã«user_idã追å ãã¦ããã¡ã¤ã«ä¿åæã®ç»é²ã¨ãã¡ã¤ã«åé¤æã®æ¨©éãã§ãã¯ã追å ãã¾ããã
ã¦ã¼ã¶ID追å
user_idã®ã«ã©ã ã追å ãã¦ãattachmentsãã¼ãã«ãä½æããªããã¾ãã
mysql> DESCRIBE attachments; +-------------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+------------------+------+-----+---------+----------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | user_id | int(10) unsigned | NO | MUL | NULL | | | model | varchar(255) | NO | MUL | NULL | | | foreign_key | int(10) | NO | | NULL | | | dirname | varchar(255) | YES | | NULL | | | basename | varchar(255) | NO | | NULL | | | checksum | varchar(255) | NO | | NULL | | | alternative | varchar(50) | YES | | NULL | | | group | varchar(255) | YES | | NULL | | | created | datetime | YES | | NULL | | | modified | datetime | YES | | NULL | | +-------------+------------------+------+-----+---------+----------------+ 12 rows in set (0.00 sec)
$fieldListã«user_idã«ã©ã ã追å ãã¾ãã
app/controllers/users_controller.php
function edit_image() { ï¼ä¸ç¥ï¼ $fieldList = array( + 'user_id', 'model', 'foreign_key', 'dirname', 'basename', 'checksum', 'group', 'alternative', 'file', ); if ($this->User->saveAll($this->data, array(
Mediaãã©ã°ã¤ã³ã®ã¢ãã«ã«ãUserã¨ã®é¢é£ä»ãï¼$belongsTo)ã¨ãbeforeSave()ã§user_idã®ã»ããã追å ãã¾ãã
ã¢ãã«ããAuthã³ã³ãã¼ãã³ãã®èªè¨¼æ
å ±ã¯åç
§ãè¤éã«ãªãã®ã§ãã»ãã·ã§ã³æ
å ±ããã¦ã¼ã¶IDãã»ãããã¾ããã
app/plugins/media/models/attachment_ex.php
+ var $belongsTo = array( + 'User' => array( + 'className' => 'User', + 'foreignKey' => 'user_id', + 'conditions' => '', + 'fields' => '', + 'order' => '' + ) + ); (ä¸ç¥ï¼ + function beforeSave() { + if (!isset($this->data['Attachment']['delete'])) { + // ç»é²è ID + if (isset($_SESSION['Auth']['User']['id']) && + (!isset($this->data[$this->alias]['delete']) || $this->data[$this->alias]['delete'] === '0') + $this->data[$this->alias]['user_id'] = $_SESSION['Auth']['User']['id']; + } + } + + return parent::beforeSave(); + }
ããã§ãattachmentsã«å®è¡è ã®ã¦ã¼ã¶IDãç»é²ãããããã«ãªãã¾ãã
mysql> SELECT id, user_id, model, foreign_key, basename FROM attachments\G *************************** 1. row *************************** id: 26 user_id: 1 model: User foreign_key: 1 basename: 4b876687-fefc-4fc4-9c1e-0a72c0a80b08.jpg *************************** 2. row *************************** id: 27 user_id: 25 model: User foreign_key: 25 basename: 4b876695-6720-4aa3-ad7f-0a77c0a80b08.jpg 2 rows in set (0.00 sec)
åé¤æ¨©éãã§ãã¯
åé¤åã®ã¦ã¼ã¶IDãã§ãã¯ã¯ãMediaãã©ã°ã¤ã³ã®ã¢ãã«ã«beforeDeleteã追å ãã¦ããã®ä¸ã§è¡ãªãã¾ãã
+ function beforeDelete($cascade = true) { + if (!isset($this->data['Attachment']['id']) || empty($this->data['Attachment']['id'])) { + return false; + } + $result = $this->find('first', array( + 'conditions' => array('id' => $this->data['Attachment']['id']), + 'fields' => array('user_id'), + 'recursive' => -1, + )); + + if ($result["Attachment"]["user_id"] != $_SESSION['Auth']['User']['id']) { + return false; + } + + return parent::beforeDelete($cascade); + }
beforeDelete()ã§falseãè¿ãã¨ãåé¤ã¯è¡ãªããã¾ãããsaveAll()ã¯ã¨ã©ã¼ã«ãªããªãã®ã§ãããåç¬ã§ã¯ã³ã³ããã¼ã©å´ã§ã¨ã©ã¼ã¡ãã»ã¼ã¸è¡¨ç¤ºãªã©ã¯è¡ãªããã¾ããã
ä»åã¯ãã¨ã©ã¼ã¡ãã»ã¼ã¸è¡¨ç¤ºã¯ç¹ã«è¦ããªãã¨å¤æãã¾ããããã¡ãã»ã¼ã¸ã欲ããå ´åã¯ãã³ã³ããã¼ã©ãããªãã¼ã·ã§ã³æ®µéã§ã®ãã§ãã¯ã®æ¹ãè¦ãã¾ãã