我开发了一个cakePHP应用程序来管理一些图书,它使用'Auth'组件作为其身份验证机制。我有2个用户角色:admin &用户。在这个应用程序中,我为Book及其索引视图设置了以下规则:
- 每个管理员都可以查看所有书籍,编辑它们和…
- 每个普通用户只能查看他/她的书,只能编辑他/她的书(不是所有的书)
,我想知道在将模型信息传递给视图之前,是否可以根据用户角色过滤数据?
我猜传递所有的数据到视图,然后根据用户角色在视图内过滤它们,可能会导致DB开销,但我不确定我是否正确。目前我的索引。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>
- Html ->链接(__("新书"),数组('行动' => '添加'));?>
- Html ->链接(__("用户列表"),数组("控制器"=>"用户的"行动"=>"索引"));?>
- Html ->链接(__("新书用户"),数组("控制器"=>"用户的'行动' => '添加'));?>
- Html ->链接(__(动力分配列表),数组("控制器"=>"后卫","行动"=>"索引"));?>
- Html ->链接(__(新书后卫),数组("控制器"=>"后卫",'行动' => '添加'));?>
您不应该检索数据,更不用说将所有数据发送到视图了。您应该根据当前登录用户的角色过滤模型查询。然后把它传递下去。
如果由于某种原因难以在实际查询中过滤(不确定为什么会这样),我建议仍然使用Cake的Hash或php数组函数....过滤Model方法中的数据等,然后传回控制器。
在您看来,为不同的显示块编写if语句是可以接受的,但这取决于用户的角色。