需求
- 管理员通过 oauth2 进行登录
- 填写问卷的用户使用已有的系统的权限认证
- 导出的问卷结果中需要有用户名
Limesurvey 版本
本文使用 Limesurvey 2.0.5 以上版本
解决方法
1. 管理员通过 oauth2 进行登录
可以通过编写插件来解决问题1和问题2。插件需要继承自AuthPluginBase
。并在插件中订阅相关的Event:beforeLogin
、newUserSession
和beforeLogout
。
其中,beforeLogin
是在管理员登陆之前,可以在这里跳过 limesurvey 的登陆界面、进行自己的oauth2身份验证等。 newUserSession
中进行真正的身份验证,将oauth2得到的用户信息与系统内的用户信息进行验证,并 setAuthSuccess。 beforeLogout
则可以将相关的cookie、session等清理下。
2. 填写问卷的用户使用已有的系统的权限认证
同样是在插件中进行相关逻辑的编写。需要订阅的事件是beforeSurveyPage
。也可以使用afterSurveyComplete
来在用户填写完问卷后进行一些额外的操作。
插件的代码示例如下所示:
class AuthYours extends AuthPluginBase
{
protected $storage = 'DbStorage';
static protected $description = 'Authentication For You';
static protected $name = 'AuthYours';
public function init()
{
$this->subscribe('beforeSurveyPage');
$this->subscribe('afterSurveyComplete');
$this->subscribe('beforeLogin');
$this->subscribe('newUserSession');
$this->subscribe('beforeLogout');
}
public function beforeSurveyPage(){……}
public function afterSurveyComplete(){……}
public function beforeLogin(){……}
public function newUserSession(){
…………
$sUser = $this->getUsername(); //oauth得到的用户名信息
$oUser = $this->api->getUserByName($sUser);
…………
$this->setAuthSuccess($oUser);
…………
}
public function beforeLogout(){……}
3. 问卷导出的结果中添加用户名
直接hack了源代码,插件的方式也是可以的,但是工作量略有些大。
在application/helpers/admin/export/writer.php
的write
方法中,对应的header
数组和elementArray
数组中添加需要的内容。
// header中强行添加 用户id 字段
array_unshift($headers, '用户ID');
// If no empty survey, render/export responses array.
foreach($oSurvey->responses as $response)
{
$elementArray = array();
// 将对应的数据也从数据库取出,放入 elementArray 中
$elementArray[] = $response['user_id'];
ps:
如果在你跟我类似,之前将用户ID存在了survey_{{surveyId}}
这样的数据库中,其实会导致查看问卷结果的时候读不到结果。对应修改application/controller/admin/responses.php
吧。 需要修改对应函数
public function getResponses_json($iSurveyID)