cakePHP:在根据用户角色传递给视图之前过滤行

时间:2014-07-19 12:00:12

标签: php cakephp authorization user-roles

我开发了一个用于管理一些书籍的cakePHP应用程序,它使用了“Auth'其认证机制的组件。我有2个用户角色:admin&用户。在这个应用程序中,我有以下规则Book及其索引视图:

  1. 每个管理员都可以查看所有图书,编辑它们......
  2. 每个普通用户只能查看他/她的书籍,只能编辑他/她的书(不是所有书籍)
  3. 换句话说,我想知道是否可以在传递模型信息之前根据用户角色过滤数据?

    我想将所有数据传递给视图然后根据视图中的用户角色过滤它们可能会导致数据库开销,但我不确定我是否正确。 目前我的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']); ?>&nbsp;</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']); ?>&nbsp;</td>
                            <td><?php echo h($book['Book']['revision']); ?>&nbsp;</td>
                            <td><?php echo h($book['Book']['price']); ?>&nbsp;</td>
                            <td><?php echo h($book['Book']['publication_year']); ?>&nbsp;</td>
                            <td><?php echo h($book['Book']['url_cover_page']); ?>&nbsp;</td>
                            <td><?php echo h($book['Book']['url_last_page']); ?>&nbsp;</td>
                            <td><?php echo h($book['Book']['author']); ?>&nbsp;</td>
                            <td><?php echo h($book['Book']['publisher']); ?>&nbsp;</td>
                            <td><?php echo h($book['Book']['translator']); ?>&nbsp;</td>
                            <td><?php echo h($book['Book']['tags']); ?>&nbsp;</td>
                            <td><?php echo h($book['Book']['created']); ?>&nbsp;</td>
                            <td><?php echo h($book['Book']['modified']); ?>&nbsp;</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-&gt;链接(__(&#39; New Book&#39;),数组(&#39; action&#39; =&gt;&#39; add&#39;)); ?&GT;
    •         
    • Html-&gt;链接(__(&#39;列出用户&#39;),数组(&#39;控制器&#39; =&gt;&#39;用户&#39;,&#39;操作&# 39; =&gt;&#39; index&#39;)); ?&GT;
    •         
    • Html-&gt;链接(__(&#39;新书用户&#39;),数组(&#39;控制器&#39; =&gt;&#39;用户&#39;,&#39;行动& #39; =&gt;&#39;添加&#39;)); ?&GT;
    •         
    • Html-&gt;链接(__(&#39; List Pgs&#39;),数组(&#39;控制器&#39; =&gt;&#39; pgs&#39;,&#39;行动&# 39; =&gt;&#39; index&#39;)); ?&GT;
    •         
    • Html-&gt;链接(__(&#39; New Book Pgs&#39;),数组(&#39;控制器&#39; =&gt;&#39; pgs&#39;,&#39; action& #39; =&gt;&#39;添加&#39;)); ?&GT;
    •     

1 个答案:

答案 0 :(得分:0)

您不应该检索数据,更不用说将所有数据发送到视图。您应该根据当前登录用户的角色过滤模型查询。然后传递它。

如果由于某种原因很难在实际查询中过滤(不确定为什么会这样),我建议仍然使用Cake的Hash过滤模型方法中的数据或php数组函数....等等,根据你的需要传回Controller之前。

在您看来,为不同的显示块编写if语句是可以接受的,但这基于用户的角色。