dongsheng8664 2014-04-27 12:30
浏览 50

如何根据用户角色隐藏部分HTML表单

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.

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

  • 写回答

1条回答 默认 最新

  • dp7311 2014-04-27 15:46
    关注

    Ok, so you want to distinguish three different cases

    • visitor
    • admin
    • 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);
    

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

    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>
    

    And index/visitor

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

    index/user

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

    index/admin

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

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c