2014-11-14 166 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; ?>