我正在开发一个功能,只允许客户和医生访问检查结果。但我不太确定我的代码在确保仅授权用户查看方面的优势。
我已经创建了“检查”帖子类型来处理信息,并添加了元盒以在每次检查中保存医生和患者信息(医生和患者也是用户的自定义角色,其功能类似于订阅者)。
然后,我创建了此函数来检查试图查看内容的用户是否有权使用它:
/*
* Checks if user is logged in and has access to that specific exam
*/
function rm_userauth_check() {
global $current_user;
get_currentuserinfo();
$doctor = get_post_custom_values(\'doctor\');
$patient = get_post_custom_values(\'patient\');
$loggeduser = $current_user->user_login;
$nicename = $current_user->display_name;
$mainrole = $current_user->roles;
if ($current_user->data !== null) {
if ($mainrole[0] == \'doctor\' && $loggeduser == $doctor[0]) {
return true; // this user is a doctor and is assigned to this exam
} elseif ($mainrole[0] == \'patient\' && $loggeduser == $patient[0]) {
return true; // this user is a patient and is assigned to this exam
} else {
return false; // this user is not assigned to this exam
}
} else {
return false; // user is not logged in
}
现在我要在我的一次考试中宣布。php文件为
<?php if (function_exists(\'rm_userauth_check\')) : ?>
<?php if (rm_userauth_check()) : ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<?php get_template_part( \'content\', \'single\' ); ?>
<?php endwhile; // end of the loop. ?>
<?php else : ?>
<?php wp_redirect( home_url() ); exit; ?>
<?php endif; ?>
<?php endif; ?>
你觉得怎么样?我是走对了路还是应该尝试另一种方法?