cakephp 3 根据用户角色显示 html

cakephp 3 display html according to users role

我正在用 cakephp 3 制作一个应用程序。我的用户有 2 个角色,管理员和学生。管理员可以看到所有内容,而学生只能查看和编辑他的个人资料。角色是数据库中用户 table 中的一个字段。

管理员的 ctp 文件有很多链接来执行不同的操作,比如添加新产品或删除用户,我想做的是仅向管理员角色显示此链接,但使用相同的 ctp 文件对于学生和管理员。我不知道该怎么做,如果有人可以给我一个例子。

例如,这是 add.ctp 用户的文件:

<div class="actions columns large-2 medium-3">
<h3><?= __('Actions') ?></h3>
THIS ARE THE LINKS
<ul class="side-nav">
    <li><?= $this->Html->link(__('List Users'), ['action' => 'index']) ?></li>
    <li><?= $this->Html->link(__('New Clase'), ['controller' => 'Clases', 'action' => 'add']) ?></li>
    <li><?= $this->Html->link(__('List Convenios Usuarios'), ['controller' => 'ConveniosUsuarios', 'action' => 'index']) ?></li>
    <li><?= $this->Html->link(__('New Convenios Usuario'), ['controller' => 'ConveniosUsuarios', 'action' => 'add']) ?></li>
    <li><?= $this->Html->link(__('List Desvinculaciones'), ['controller' => 'Desvinculaciones', 'action' => 'index']) ?></li>
    <li><?= $this->Html->link(__('New Desvinculacione'), ['controller' => 'Desvinculaciones', 'action' => 'add']) ?></li>
    <li><?= $this->Html->link(__('List Historial Alumnos'), ['controller' => 'HistorialAlumnos', 'action' => 'index']) ?></li>
    <li><?= $this->Html->link(__('New Historial Alumno'), ['controller' => 'HistorialAlumnos', 'action' => 'add']) ?></li>
    <li><?= $this->Html->link(__('List Pagos'), ['controller' => 'Pagos', 'action' => 'index']) ?></li>
    <li><?= $this->Html->link(__('New Pago'), ['controller' => 'Pagos', 'action' => 'add']) ?></li>
    <li><?= $this->Html->link(__('List Pedidos'), ['controller' => 'Pedidos', 'action' => 'index']) ?></li>
    <li><?= $this->Html->link(__('New Pedido'), ['controller' => 'Pedidos', 'action' => 'add']) ?></li>
</ul>
<div class="users form large-10 medium-9 columns">
<?= $this->Form->create($user) ?>
<fieldset>
    <legend><?= __('Agrega Nuevo Usuario') ?></legend>
    <?php
        echo $this->Form->input('nombre', ['label'=>'Nombre Completo']);
        echo $this->Form->input('fecha_nac',['label'=>'Fecha de Nacimiento']);
        echo $this->Form->input('username',['label'=>'Nombre de Usuario']);
        echo $this->Form->input('email');
        echo $this->Form->input('password', ['label'=>'Contraseña']);
        echo $this->Form->input('telefono');
        echo $this->Form->input('rol', ['options' =>['Alumno'=>'Alumno', 'Monitor'=>'Monitor','Instructor'=>'Instructor']]);
        echo $this->Form->input('fecha_ing',['label'=>'Fecha de Ingreso']);
        echo $this->Form->input('profesion');
        echo $this->Form->input('grado_id', ['options' => $grados]);
        echo $this->Form->input('referencia');
        echo $this->Form->input('estado', ['options' =>['Activo','Inactivo']]);
        echo $this->Form->input('fecha_ult_acenso', ['label'=>'Fecha último ascenso','empty' => true, 'default' => '']);
        echo $this->Form->input('nombre_apoderado', ['empty' => true, 'default' => 'No tiene Apoderado']);
        echo $this->Form->input('telefono_apoderado',  ['empty' => true, 'default' => '']);
        echo $this->Form->input('nota_salud',['label'=>'Información de Salud', 'empty' => true, 'default' => 'No presenta Complicaciones']);
        echo $this->Form->input('llevar_a', ['label'=>'En caso de Emergencia', 'empty' => true, 'default' => '']);
        echo $this->Form->input('monto_paga',['label'=>'Mensualidad']);
        echo $this->Form->input('id_user_referencia', ['label'=>'Quien Paga']);
        echo $this->Form->input('observaciones', ['empty' => true, 'default' => 'No tiene observaciones']);
        echo $this->Form->input('fecha_cambio_password', ['empty' => true, 'default' => '']);
        echo $this->Form->file('foto');
    ?>
</fieldset>
<?= $this->Form->button(__('Agregar')) ?>
<?= $this->Form->end() ?>

我的第一个想法是在 ctp 文件中使用 if 并且有两个链接列表并根据用户的角色显示一个或另一个,但我不知道是否有更好的形式来做到这一点,使用cakephp的工具。

如果你只需要show/hide some/all contents(links) in your View(.ctp) file,那我有个更好的主意。

只需从该页面的控制器方法传递一个视图变量,它决定当前用户是管理员还是学生。

例如,

在你的控制器方法中,

if( check if user is admin ) {
// set the view variable here
    $this->set('is_admin', 1);
}

在您的视图中(.ctp 文件):

<? if( isset($is_admin) && $is_admin === 1 ) { ?>
<!-- All the link codes for admin goes here as html -->
    <li><?= $this->Html->link(__('_ _ _'), ['controller' => 'Clases', 'action' => 'add']) ?></li>
<? } ?>

NOTE: Please note that this way only hides the links for students in the view, and not the access to these links.