我开发了一个用于管理一些书籍的cakePHP应用程序,它使用了“Auth'其认证机制的组件。我有2个用户角色:admin&用户。在这个应用程序中,我有以下规则Book及其索引视图:
换句话说,我想知道是否可以在传递模型信息之前根据用户角色过滤数据?
我想将所有数据传递给视图然后根据视图中的用户角色过滤它们可能会导致数据库开销,但我不确定我是否正确。 目前我的index.ctp是这样的,它只显示与登录用户相关的书籍(不关心他的角色):
<div class="books index">
<h2><?php echo __('Books'); ?></h2>
<?php if ($loggedIn) { ?>
<table cellpadding="0" cellspacing="0">
<thead>
<tr>
<th><?php echo $this->Paginator->sort('id'); ?></th>
<th><?php echo $this->Paginator->sort('book_user_id'); ?></th>
<th><?php echo $this->Paginator->sort('name'); ?></th>
<th><?php echo $this->Paginator->sort('revision'); ?></th>
<th><?php echo $this->Paginator->sort('price'); ?></th>
<th><?php echo $this->Paginator->sort('publication_year'); ?></th>
<th><?php echo $this->Paginator->sort('url_cover_page'); ?></th>
<th><?php echo $this->Paginator->sort('url_last_page'); ?></th>
<th><?php echo $this->Paginator->sort('author'); ?></th>
<th><?php echo $this->Paginator->sort('publisher'); ?></th>
<th><?php echo $this->Paginator->sort('translator'); ?></th>
<th><?php echo $this->Paginator->sort('tags'); ?></th>
<th><?php echo $this->Paginator->sort('created'); ?></th>
<th><?php echo $this->Paginator->sort('modified'); ?></th>
<th class="actions"><?php echo __('Actions'); ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($books as $book): ?>
<?php if ($book['BookUser']['id'] == $loggedIn) { ?>
<tr>
<td><?php echo h($book['Book']['id']); ?> </td>
<td>
<?php echo $this->Html->link($book['BookUser']['username'], array('controller' => 'users', 'action' => 'view', $book['BookUser']['id'])); ?>
</td>
<td><?php echo h($book['Book']['name']); ?> </td>
<td><?php echo h($book['Book']['revision']); ?> </td>
<td><?php echo h($book['Book']['price']); ?> </td>
<td><?php echo h($book['Book']['publication_year']); ?> </td>
<td><?php echo h($book['Book']['url_cover_page']); ?> </td>
<td><?php echo h($book['Book']['url_last_page']); ?> </td>
<td><?php echo h($book['Book']['author']); ?> </td>
<td><?php echo h($book['Book']['publisher']); ?> </td>
<td><?php echo h($book['Book']['translator']); ?> </td>
<td><?php echo h($book['Book']['tags']); ?> </td>
<td><?php echo h($book['Book']['created']); ?> </td>
<td><?php echo h($book['Book']['modified']); ?> </td>
<td class="actions">
<?php echo $this->Html->link(__('View'), array('action' => 'view', $book['Book']['id'])); ?>
<?php echo $this->Html->link(__('Edit'), array('action' => 'edit', $book['Book']['id'])); ?>
<?php echo $this->Form->postLink(__('Delete'), array('action' => 'delete', $book['Book']['id']), array(), __('Are you sure you want to delete # %s?', $book['Book']['id'])); ?>
</td>
</tr>
<?php } ?>
<?php endforeach; ?>
</tbody>
</table>
<?php } else { ?>
<p>To view your books, please login or Sign Up.</p>
<?php } ?>
<p>
<?php
echo $this->Paginator->counter(array(
'format' => __('Page {:page} of {:pages}, showing {:current} records out of {:count} total, starting on record {:start}, ending on {:end}')
));
?> </p>
<div class="paging">
<?php
echo $this->Paginator->prev('< ' . __('previous'), array(), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers(array('separator' => ''));
echo $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled'));
?>
</div>
答案 0 :(得分:0)
您不应该检索数据,更不用说将所有数据发送到视图。您应该根据当前登录用户的角色过滤模型查询。然后传递它。
如果由于某种原因很难在实际查询中过滤(不确定为什么会这样),我建议仍然使用Cake的Hash过滤模型方法中的数据或php数组函数....等等,根据你的需要传回Controller之前。
在您看来,为不同的显示块编写if语句是可以接受的,但这基于用户的角色。