2014-11-14 115 views
0

我有这样的代码在我admin_index视图CakePHP的foreach循环条件

<?php foreach ($users as $user): ?> 
<tr> 
    <?php if ($user['User']['account_type']=='admin'): ?> 
    <td><?php echo h($user['User']['ID']); ?>&nbsp;</td> 
    <td><?php echo h($user['User']['username']); ?>&nbsp;</td> 
    <td><?php echo h($user['User']['account_type']); ?>&nbsp;</td> 
    <td class="actions"> 
     <?php echo $this->Html->link(__('View'), array('action' => 'admin_view', $user['User']['ID'])); ?> 
     <?php echo $this->Html->link(__('Edit'), array('action' => 'admin_edit', $user['User']['ID'])); ?> 
    </td> 
    <?php else: ?> 
    <td><?php echo h($user['User']['ID']); ?>&nbsp;</td> 
    <td><?php echo h($user['User']['username']); ?>&nbsp;</td> 
    <td><?php echo h($user['User']['account_type']); ?>&nbsp;</td> 
    <td class="actions"> 
     <?php echo $this->Html->link(__('View'), array('action' => 'admin_view', $user['User']['ID'])); ?> 
     <?php echo $this->Html->link(__('Edit'), array('action' => 'admin_edit', $user['User']['ID'])); ?> 
     <?php echo $this->Form->postLink(__('Delete'), array('action' => 'admin_delete', $user['User']['ID']), array(), __('Are you sure you want to delete # %s?', $user['User']['ID'])); ?> 
    </td> 
</tr> 
<?php endif; ?> 
<?php endforeach; ?> 

我想要做的就是打印的行动基于账户类型* ERGO如果帐户TD手机不同的行类型是'用户'它将打印一个删除按钮,如果'管理员,不允许删除..现在的事情是一个管理员是'超级用户'布尔列指定的超级用户,我试图将其整合到if条件,如果当前登录的管理员是指定的超级用户,他自己的账户行将不具有类似于上述代码的删除按钮,但也能够删除其他管理员。并且如果当前登录的用户不是超级用户上面的代码将被显示出来其他管理员无法查看超级用户配置文件

打过电话身份验证和会话中的if语句

this>auth/session->user('ID') 

并没有真正顺利

更新

<?php foreach ($users as $user): ?> 
<tr> 
    <?php if ($this->Session->read('User.super_user')=== 1): ?> 
     <?php if ($this->Session->read('User.ID')===$user['User']['ID']): ?> 
      <td><?php echo h($user['User']['ID']); ?>&nbsp;</td> 
      <td><?php echo h($user['User']['username']); ?>&nbsp;</td> 
      <td><?php echo h($user['User']['account_type']); ?>&nbsp;</td> 
      <td class="actions"> 
       <?php echo $this->Html->link(__('View'), array('action' => 'admin_view', $user['User']['ID'])); ?> 
       <?php echo $this->Html->link(__('Edit'), array('action' => 'admin_edit', $user['User']['ID'])); ?> 
      </td> 
     <?php else: ?> 
      <td><?php echo h($user['User']['ID']); ?>&nbsp;</td> 
      <td><?php echo h($user['User']['username']); ?>&nbsp;</td> 
      <td><?php echo h($user['User']['account_type']); ?>&nbsp;</td> 
      <td class="actions"> 
       <?php echo $this->Html->link(__('View'), array('action' => 'admin_view', $user['User']['ID'])); ?> 
       <?php echo $this->Html->link(__('Edit'), array('action' => 'admin_edit', $user['User']['ID'])); ?> 
       <?php echo $this->Form->postLink(__('Delete'), array('action' => 'admin_delete', $user['User']['ID']), array(), __('Are you sure you want to delete # %s?', $user['User']['ID'])); ?> 
      </td> 
     <?php endif; ?> 
    <?php else: ?> 

     <?php if ($this->Session->read('User.ID')=== $user['User']['ID']): ?> 
      <td><?php echo h($user['User']['ID']); ?>&nbsp;</td> 
      <td><?php echo h($user['User']['username']); ?>&nbsp;</td> 
      <td><?php echo h($user['User']['account_type']); ?>&nbsp;</td> 
      <td class="actions"> 
       <?php echo $this->Html->link(__('View'), array('action' => 'admin_view', $user['User']['ID'])); ?> 
       <?php echo $this->Html->link(__('Edit'), array('action' => 'admin_edit', $user['User']['ID'])); ?> 
      </td> 
     <?php elseif ($user['User']['super_user'] ===1): ?> 
      <td><?php echo h($user['User']['ID']); ?>&nbsp;</td> 
      <td><?php echo h($user['User']['username']); ?>&nbsp;</td> 
      <td><?php echo h($user['User']['account_type']); ?>&nbsp;</td> 
      <td class="actions"> 
       <?php echo "no altering allowed";?> 
      </td> 
     <?php else: ?> 
      <td><?php echo h($user['User']['ID']); ?>&nbsp;</td> 
      <td><?php echo h($user['User']['username']); ?>&nbsp;</td> 
      <td><?php echo h($user['User']['account_type']); ?>&nbsp;</td> 
      <td class="actions"> 
       <?php echo $this->Html->link(__('View'), array('action' => 'admin_view', $user['User']['ID'])); ?> 
       <?php echo $this->Html->link(__('Edit'), array('action' => 'admin_edit', $user['User']['ID'])); ?> 
       <?php echo $this->Form->postLink(__('Delete'), array('action' => 'admin_delete', $user['User']['ID']), array(), __('Are you sure you want to delete # %s?', $user['User']['ID'])); ?> 
      </td> 
     <?php endif; ?> 
<?php endif; ?> 
    </tr> 

我现在的问题来自我的第一层if语句。它的自动无视我检查的情况,如果该会话的当前SUPER_USER设置为1,它总是与else语句去......说不上来是怎么回事

+0

到目前为止您尝试了什么,以及这些尝试的结果如何?你已经发布的代码,但不要说它是否产生不正确的结果(如果是这样,什么)等等等 – Dave 2014-11-14 16:13:05

+0

多数民众赞成的事情..试图找出如何将当前登录的用户数据传递到视图将它整合到代码中......在if语句中随机调用auth和session并不是我最聪明的时刻 – 2014-11-14 16:17:44

回答

0

Accessing the logged user

在你的控制器:

$iAmsuperAdmin = (bool)$this->Auth->user('super_user'); 
$myId = (int)$this->Auth->user('ID'); 
$this->set('iAmsuperAdmin', $iAmsuperAdmin); 
$this->set('myID', $myID); 

查看:

<?php foreach ($users as $user): ?> 
    <?php 
    $canDelete = false; 

    // admin users should be able to delete 
    if ($user['User']['account_type'] == 'admin') { 
     $canDelete = true; 
    } 

    // if I am the super-admin, I should not be able to delete myself 
    if ($user['User']['account_type'] == 'admin' && $iAmSuperAdmin === true && $myID == $user['User']['ID']) { 
     $canDelete = false; 
    } 
    ?> 
    <tr> 
    <?php ?> 
     <td><?php echo h($user['User']['ID']); ?>&nbsp;</td> 
     <td><?php echo h($user['User']['username']); ?>&nbsp;</td> 
     <td><?php echo h($user['User']['account_type']); ?>&nbsp;</td> 
     <td class="actions"> 
      <?php echo $this->Html->link(__('View'), array('action' => 'admin_view', $user['User']['ID'])); ?> 
      <?php echo $this->Html->link(__('Edit'), array('action' => 'admin_edit', $user['User']['ID'])); ?> 
      <?php if ($canDelete === true) { echo $this->Form->postLink(__('Delete'), array('action' => 'admin_delete', $user['User']['ID']), array(), __('Are you sure you want to delete # %s?', $user['User']['ID'])); } ?> 
     </td> 
     </tr> 
<?php endforeach; ?>