Php 我应该如何为应用程序I';中的安全角色编写HTML etc;你继承了什么?
正如标题所示,我继承了一个php/MySQL应用程序,它有相当好的编写过程代码,但只有一个管理员用户登录。客户机自然希望将功能划分为不同的用户/角色,并最终使用最佳实践技术更新应用程序代码 对于在“管理”部分更新某人用户名的示例,原始版本仅显示一个预先填充用户名的文本字段(为简洁起见,代码简化): 我的问题是,它在更大的代码块中的伸缩性如何?有两个“交互式功能”,它们使用大量的HTML和JavaScript一次发回多个值。对于每个角色,我们实际上都有一个单独的自定义控件版本。将每个版本粘贴到Php 我应该如何为应用程序I';中的安全角色编写HTML etc;你继承了什么?,php,security,permissions,coding-style,Php,Security,Permissions,Coding Style,正如标题所示,我继承了一个php/MySQL应用程序,它有相当好的编写过程代码,但只有一个管理员用户登录。客户机自然希望将功能划分为不同的用户/角色,并最终使用最佳实践技术更新应用程序代码 对于在“管理”部分更新某人用户名的示例,原始版本仅显示一个预先填充用户名的文本字段(为简洁起见,代码简化): 我的问题是,它在更大的代码块中的伸缩性如何?有两个“交互式功能”,它们使用大量的HTML和JavaScript一次发回多个值。对于每个角色,我们实际上都有一个单独的自定义控件版本。将每个版本粘贴到开关
开关中
有点异味(无论是内联代码还是作为单独的文件包含)。。。类似地,在后端,根据角色的不同,处理帖子的脚本将有多个版本
我知道一定有一百种方法可以做到这一点,但其他人过去都做了些什么呢?如果你有一些见解,你可以用过程的方式来演示它,因为在这个项目中没有OO!谢谢 您可以使用以下内容:
// $access_type can be the result of a function call, an array value, etc.
switch($access_type){
case 'edit':
echo '<input type="text" name="username" value="' . $username . '">';
break;
case 'readonly':
echo $username;
break;
case NULL:
default:
// Don't display anything.
}
interface User {
/* stores several roles */
/**
* Combines the permissions of the several roles
* of the user and returns the result.
* @return Permission */
function getPermission($key);
}
interface Role {
/* stores several permissions */
/* if there's no record of this kind of this
* particular permission, return an empty one */
/** @return Permission */
function getPermission($key);
}
interface Permission {
/** @return Permission */
function combine(array $permissions);
/** @return bool */
function canEdit();
function canRead();
}
然后做:
/* user factory takes care of creating the instances and
* wiring them together */
$u = UserFactory::loadCurrentUser();
if ($u->getPermission('admin_page')->canWrite()) {
//...
}
您可以使用以下内容:
// $access_type can be the result of a function call, an array value, etc.
switch($access_type){
case 'edit':
echo '<input type="text" name="username" value="' . $username . '">';
break;
case 'readonly':
echo $username;
break;
case NULL:
default:
// Don't display anything.
}
interface User {
/* stores several roles */
/**
* Combines the permissions of the several roles
* of the user and returns the result.
* @return Permission */
function getPermission($key);
}
interface Role {
/* stores several permissions */
/* if there's no record of this kind of this
* particular permission, return an empty one */
/** @return Permission */
function getPermission($key);
}
interface Permission {
/** @return Permission */
function combine(array $permissions);
/** @return bool */
function canEdit();
function canRead();
}
然后做:
/* user factory takes care of creating the instances and
* wiring them together */
$u = UserFactory::loadCurrentUser();
if ($u->getPermission('admin_page')->canWrite()) {
//...
}
嗨,谢谢你的回答。然而,我正在寻找关于如何最好地将HTML/JavaScript块包含到if/else或开关代码中的建议,以及如何处理访问类型“canWrite()”不够精细的情况-canWrite()返回布尔值,而我正在寻找一种可能返回多种访问类型之一的情况('edit'、'readonly'和潜在的'Constraint_to_8_characters'、'lowercase_only'例如)都将使用不同的HTML表单和后端验证。谢谢!@Bendos您可以通过添加额外的权限来实现额外的粒度。例如,
if($u->getPermission('admin_page_uppercase_stuff')->canWrite()){…}
@Atrefacto-我想根据返回的权限是edit
、readonly
还是lowercase\u only
,显示一组不同的HTML/JavaScript。如果您的示例更像开关($u->getPermission('username')->getEditability(){…},那么它会起作用吗
?您好,感谢您的回答。不过,我想了解如何最好地将HTML/JavaScript块包含到if/else或switch代码中,以及如何处理访问类型“canWrite()”不够精细的情况-canWrite()返回布尔值,而我所看到的场景可能返回多种访问类型中的一种(“编辑”、“只读”和可能的“将\u限制为\u 8个字符”、“仅小写”)这将使用不同的HTML表单和后端验证。谢谢!@Bendos您可以通过添加额外的权限来实现额外的粒度。例如,if($u->getPermission('admin\u page\u uppercase\u stuff')->canWrite(){…}
@Atrefacto-我想根据返回的权限是edit
、readonly
还是lowercase\u only
,显示一组不同的HTML/JavaScript。如果您的示例更像开关($u->getPermission('username')->getEditability(){…}
,它会起作用吗?