如何根据用户角色隐藏HTML表单的一部分 [英] how to hide part of HTML form depending on user role

查看:347
本文介绍了如何根据用户角色隐藏HTML表单的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Kohana 3.3开发一个网站,我想根据用户的角色有选择地显示HTML UI元素. e:-如果用户是管理员,则显示编辑"超链接,当管理员单击编辑"按钮时,将文本框从只读"更改为普通".

I am developing a website using Kohana 3.3 and i want to selectively display HTML UI elements depending on the role of the user. e:- If user is an admin then show the 'edit' hyperlink, and when the admin clicks the edit button change the textbox's from 'readonly' to 'normal'.

如果用户是注册普通用户,则启用按钮提问".

If user is an registered normal user than enable the button to 'ask a question'.

如果用户是访客,则没有特权.

If user is a visitor then he have no priviliges.

现在我正在使用单个视图文件,并在检查php变量的状态后更改可见性.不知何故,我觉得我做的不正确,建议使用什么方法来处理这种情况(任何插件?)?

Right now i am using a single view file and changing the visbility after checking the status of php variables. Somehow, i feel that i am not doing it correctly, what is the suggested method to handle such scenarios( any plugins?) ?

推荐答案

好,所以您想区分三种不同的情况

Ok, so you want to distinguish three different cases

  • 访客
  • 管理员
  • 用户

处理此问题的地方是您的控制器.在此,您可以访问 Auth::instance()->get_user() .

The place to handle this, is your controller. In this you have access to Auth::instance()->get_user().

$user = Auth::instance()->get_user();
if ($user === null) {
    //visitor
} else {
    if ($user->has('roles', ORM::factory('Role', array('name' => 'admin')))) {
        //admin
    } else {
        //user
    }
}

现在,您知道如何处理案件了,您需要以某种方式告诉您您的观点.为此,您可以创建一个新变量,在其中加载三个视图之一-每种情况一个.

Now that you know how to handle the cases, you somehow need to tell your view. To do that, you can create a new variable in which you load either one of three views - one for each case.

$specificViewName = "";
$user = Auth::instance()->get_user();
if ($user === null) {
    $specificViewName = "visitor";
} else {
    if ($user->has('roles', ORM::factory('Role', array('name' => 'admin')))) {
        $specificViewName = "admin";
    } else {
        $specificViewName = "user";
    }
}
$specificView = View::factory("index/".$specificViewName);

如果您在Controller_Template中,则可以使用$this->template->set("specificView", $specificView);.

If you are in a Controller_Template, you can now use $this->template->set("specificView", $specificView);.

在这种情况下,您将有一个像这样的 index 模板

In this case you'd have a index template like this

<html><!--etc.-->
<h1>Welcome to my website</h1>
<!--stuff all sites share like navigation-->
<?php print $specificView; ?>
<!--more-->
</html>

索引/访问者

<span class="sadtext">Nothing special for you here</span>

索引/用户

<form>
<button>ask a question!
</form>

索引/管理员

<a href="edit">hyperlink</a>

这篇关于如何根据用户角色隐藏HTML表单的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆