我有2款:
我有以下规则:
我创建了用户和书籍的模型,并定义了属于/一对多realtion,我使用cake bake创建了CRUD视图和控制器。我的书籍编辑控制器操作如下:
public function edit($id = null) {
if (!$this->Book->exists($id)) {
throw new NotFoundException(__('Invalid book'));
}
if ($this->request->is(array('post', 'put'))) {
if ($this->Book->save($this->request->data)) {
$this->Session->setFlash(__('The book has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The book could not be saved. Please, try again.'));
}
} else {
$options = array('conditions' => array('Book.' . $this->Book->primaryKey => $id));
$book = $this->Book->find('first', $options);
if ($this->isUserCanAccessThisRecordActionEdit($book['BookUser']))
$this->request->data = $book;
else
$this->redirectNotAuthorized();
}
$bookUsers = $this->Book->BookUser->find('list');
$this->set(compact('bookUsers'));
}
<div class="input select required"><label for="BookBookUserId">Book User</label>
<select name="data[Book][book_user_id]" disabled="disabled" id="BookBookUserId">
<option value="3">vhb</option>
<option value="5">vhb2</option>
<option value="6">vhb3</option>
<option value="7">vhb4</option>
<option value="8">vhb5</option>
<option value="9">vhb6</option>
<option value="10">vhb7</option>
<option value="11">vhb8</option>
<option value="12">vhb9</option>
<option value="13">vhb10</option>
<option value="14">vhb11</option>
<option value="15">vhb12</option>
<option value="16">vhb13</option>
<option value="17">vhb14</option>
<option value="18">vhb15</option>
<option value="19">vhb16</option>
<option value="20">vhb17</option>
<option value="21">root</option>
<option value="22">vhb19</option>
<option value="23">vhb20</option>
<option value="24">vhb21</option>
<option value="25">vhb1</option>
</select></div>
我建议管理员和用户使用不同的模型。它易于操作和维护。
我还建议在AppController中保留用户级别检查。
对于您当前的代码:
假设您正在使用名为“is_admin”的字段检查用户是否为admin:
在控制器中:
public function edit($id = null) {
if (!$this->Book->exists($id)) {
throw new NotFoundException(__('Invalid book'));
}
$isAdmin = ($this->Auth->user('is_admin') == true) ? true : false;
$this->set(compact('isAdmin'));
if ($this->request->is(array('post', 'put'))) {
if ($isAdmin != true) {
$this->request->data['Book']['book_user_id'] = $this->Auth->user('id');
}
if ($this->Book->save($this->request->data)) {
$this->Session->setFlash(__('The book has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The book could not be saved. Please, try again.'));
}
} else {
$options = array('conditions' => array('Book.' . $this->Book->primaryKey => $id));
$book = $this->Book->find('first', $options);
if ($this->isUserCanAccessThisRecordActionEdit($book['BookUser']))
$this->request->data = $book;
else
$this->redirectNotAuthorized();
}
if ($isAdmin == true) {
$bookUsers = $this->Book->BookUser->find('list');
$this->set(compact('bookUsers'));
}
}
这样可以避免加载不必要的数据。
在你看来:
if ($isAdmin == true) {
$this->Html->input('book_user_id');
} else {
echo AuthComponent::user('name');
}