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

난 할 노력하고있어 조치가 계정 유형 * 에고 계정이 경우에 따라 다른 셀을 TD 인쇄 할 수있는 행을 얻을 수있다 형식은 '사용자'입니다 삭제 버튼을 인쇄하고 'admin'은 삭제를 허용하지 않습니다 .. 이제는 한 관리자가 'super_user'부울 열로 지정된 수퍼 유저이고 if를 if에 통합하려고합니다. 조건 여기서 현재 로그인 한 관리자가 지정된 수퍼 유저 인 경우 자신의 계정 행에 위의 코드와 비슷한 삭제 버튼이 없으며 다른 관리자를 삭제할 수도 있습니다. 그리고 현재 로그인 한 사용자가 수퍼 유저가 아닌 경우 위의 코드가 표시되고 다른 관리자는 슈퍼 사용자가

가 정말 잘

갱신

<?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> 
을하지 않았다

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

으로 if 문에서 인증과 세션을 호출하려고 프로필을 볼 수 없습니다

내 문제는 이제 if 문의 첫 번째 레이어에서 발생합니다. 세션의 현재 super_user 1로 설정되어있는 경우 체크는 자동으로 무시하고 내 상태는 항상 다른 문으로 간다 ... 몰라 컨트롤러에서

+0

지금까지 시도한 내용과 그 시도의 결과는 무엇입니까? 코드를 게시했지만 잘못된 결과가 나오는 경우 (예일 경우) ... 등등을 말하지 마십시오. – Dave

+0

thats ... 현재 로그인 한 사용자의 데이터를보기에 전달하는 방법을 찾으려고합니다. 그것을 코드에 포함시키기 위해서 ... if 문에서 무작위로 auth와 session을 호출하는 것이 나의 가장 밝은 순간이 아니었다. –

답변

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; ?>