MVC 设计 - 包含用户组内容的视图
MVC Design - views with content for a usergroup
我正在 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 解析器解析文件,检查是否允许用户使用数据属性调用该函数。
如果不允许用户调用该函数,则该元素将被删除。
这尚未实施,因为我不确定这是否是一个好的解决方案,或者我如何才能做得更好?
我的建议是根据您认为有用的内容实施 ACL (Access Control List) or 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)
)
现在,当您显示这些操作的按钮时,您可以检查当前用户是否具有允许他访问这些操作的任何角色。如果答案是 "yes" 则显示它们,否则根本不显示它们。
$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>
我正在 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 解析器解析文件,检查是否允许用户使用数据属性调用该函数。 如果不允许用户调用该函数,则该元素将被删除。 这尚未实施,因为我不确定这是否是一个好的解决方案,或者我如何才能做得更好?
我的建议是根据您认为有用的内容实施 ACL (Access Control List) or 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)
)
现在,当您显示这些操作的按钮时,您可以检查当前用户是否具有允许他访问这些操作的任何角色。如果答案是 "yes" 则显示它们,否则根本不显示它们。
$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>