【问题标题】:MVC Design - views with content for a usergroupMVC 设计 - 包含用户组内容的视图
【发布时间】:2016-04-05 09:41:57
【问题描述】:

我正在用 PHP 编写我的第一个 MVC 应用程序。我不想使用任何框架。

现在我正在寻找一个好的解决方案或最佳实践,以便在视图中为用户显示元素,前提是他们被允许与特定元素调用的函数进行交互。

应用在视图中加载一个模板文件。 在模板文件中,我展示了模型中的数据,这些数据是从控制器传递到视图的。

来自控制器

public function showUserList() {
    $userList = $this->model->getUserList();
    $this->view->loadTemplate($userList, 'user_list.php');
}

从视图:

class user_view  {
    public function loadTemplate($data, $file, $buffer= false){
        if($buffer === true){
            ob_start();
            include dirname(__FILE__).'/templates/'.$file;
            $c = ob_get_contents();
            ob_end_clean();
            return $c;
        }else{
            include dirname(__FILE__).'/templates/'.$file;
        }
    }
}

模板:

<table class="table" id="data-table">
    <thead>
        <tr>
        <th>Name</th>
        <th>Username</th>
        <th>E-Mail</th>
        <th>Group</th>
        <th>Action</th>
        </tr>
    </thead>
    <tbody>
    <?php
       if(is_array($data)){

          foreach($data as $key => $value){
          ?>
            <tr>
                <td><?php echo $data['name'].' '.$data['surname']; ?></td>
                    <td><?php echo $data['abbr']; ?></td>
                    <td><?php echo $data['email']; ?></td>
                    <td><?php echo $data['gr_name']; ?></td>
                    <td>

                    <div data-access="," onclick="$user_controller.showUserChangeView('<?php echo $data['id']; ?>')" class="btn btn-primary">edit</div>
                    <div data-access="checkAccess" onclick="$user_controller.deleteUser('<?php echo $data['id']; ?>')" class="btn btn-danger">delete</div>

                    </td>
                </tr>

          <?php
          }

       }
    ?>
    </tbody>
</table>
</div>

如果允许用户调用按钮或菜单项的功能,我只想显示按钮或菜单项。

我的第一个方法是创建一个包含所有可以显示的元素的模板文件。并非所有用户都可以访问的元素获得数据属性。

视图加载模板,DOM 解析器解析文件,检查是否允许用户使用数据属性调用该函数。 如果不允许用户调用该函数,则该元素将被删除。 这尚未实施,因为我不确定这是否是一个好的解决方案,或者我如何才能做得更好?

【问题讨论】:

    标签: php model-view-controller web-applications software-design


    【解决方案1】:

    我的建议是实现ACL (Access Control List)RBAC (Role Based Access Control),具体取决于您认为有用的内容。我更喜欢 RBAC,所以我将举一个简单的例子。

    创建用户可以执行的所有可能操作的列表,并定义可能有权访问这些操作的角色。

    // List of permissions (the actual term to define resources in RBAC) for the users
    $actions = array(
        'view' => array(ROLE_USER, ROLE_OWNER, ROLE_MANAGER, ROLE_ADMIN),
        'edit' => array(ROLE_OWNER, ROLE_ADMIN),
        'delete' => array(ROLE_OWNER, ROLE_ADMIN),
        'change_pass' => array(ROLE_OWNER, ROLE_MANAGER, ROLE_ADMIN)
    )
    

    现在,当您显示这些操作的按钮时,您可以检查当前用户是否具有任何允许他访问操作的角色。如果答案是“是”,则显示它们,否则根本不显示它们。

    $currentUserRole = '..'; //Retrieve however you want
    foreach($actions as $allowed_roles) {
        if(in_array($currentUserRole, $allowed_roels)) {
            echo "<div ...>"; // Basically show the action
        }
        // If not carry on to next action with out showing
    }
    

    注意:不显示可能不足以保护它,所以我们在执行操作时也必须检查然后)

    更新:您可以将上面的代码放在以下部分:

    <table class="table" id="data-table">
        <thead>
            <tr>
            <th>Name</th>
            <th>Username</th>
            <th>E-Mail</th>
            <th>Group</th>
            <th>Action</th>
            </tr>
        </thead>
        <tbody>
        <?php
           if(is_array($data)){
    
              foreach($data as $key => $value){
              ?>
                <tr>
                    <td><?php echo $data['name'].' '.$data['surname']; ?></td>
                        <td><?php echo $data['abbr']; ?></td>
                        <td><?php echo $data['email']; ?></td>
                        <td><?php echo $data['gr_name']; ?></td>
                        <td>
                            $currentUserRole = '..'; //Retrieve however you want
                            foreach($actions as $allowed_roles) {
                                if(in_array($currentUserRole, $allowed_roels)) {
                                   echo "<div ...>"; // Basically show the action
                                }
                                // If not carry on to next action with out showing
                            }
                        </td>
                    </tr>
    
              <?php
              }
    
           }
        ?>
        </tbody>
    </table>
    </div>
    

    【讨论】:

    • 谢谢你的方法。我应该在哪里实现模板文件中的最后一个代码?
    • @h0mebrewer,我试图在上面的 for 循环中的 if 语句中显示该部分。您具体指的是哪个代码,因为我可能不确定?
    • 我的意思是把这些代码行放在哪里: foreach($actions as $allowed_roles) { if(in_array($currentUserRole, $allowed_roels)) { echo "
      "; // 基本上显示动作 } // 如果不继续下一个动作而不显示 }
    猜你喜欢
    • 2017-01-05
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-26
    • 2020-09-01
    • 2021-08-28
    相关资源
    最近更新 更多