Hi Everyone,
This is what I did:
- I have a custom login/registration/confirmation script that uses another database to store details, such as username and password.
When users authenticate, a session variable will be set:
and
$_SESSION['user_login']['username']
and whatever else I'm using for users.
- controllers/index.php:
just after if ( $this->_surveyCantBeViewedWithCurrentPreviewAccess($surveyid, $isSurveyActive, $surveyExists) ) I added the following code;
// Check if user logged in. If not, redirect to login page
if((!isset($_SESSION['user_login']) || !isset($_SESSION['user_login']['username'])){
// redirect to the login page
$login_url = '/limesurvey/userlogin/login.php';
Yii::app()->request->redirect($login_url);
}
- helpers/frontend_helper.php:
Instead of displaying a new registration form, just create a new token and save in the database. Where you have "if (isset($thissurvey) && $thissurvey == "Y")"
//echo templatereplace(file_get_contents($sTemplatePath."register.pstpl"),array(),$redata,'frontend_helper[1599]');
$tokentable = $dbprefix."tokens_".$surveyid;
$user_email = $_SESSION['user_login']['email'];
$baselang = Survey::model()->findByPk($surveyid)->language;
// check if the token exists in a database token_XXX for this user
$qry = "SELECT * FROM {{tokens_$surveyid}} WHERE email='$user_email'";
$qryrow = Yii::app()->db->createCommand($qry)->queryRow();
if (!$qryrow){
// it doesn't, then create a token in the database token_XXX with $_SESSION[user_login][details]
$tokenlength = 15;
while ($mayinsert != true)
{
$newtoken = randomChars($tokenlength);
$ntquery = "SELECT * FROM {{tokens_$surveyid}} WHERE token='$newtoken'";
$usrow = Yii::app()->db->createCommand($ntquery)->queryRow();
if (!$usrow) {$mayinsert = true;}
}
// Insert new entry into tokens db
Tokens_dynamic::sid($thissurvey['sid']);
$token = new Tokens_dynamic;
$token->firstname = $_SESSION['user_login']['firstname'];
$token->lastname = $_SESSION['user_login']['lastname'];
$token->email = $user_email;
$token->emailstatus = 'OK';
$token->token = $newtoken;
$result = $token->save();
$token=$token->token;
// then redirect to the survey XXX witht the token id
$surveylink = Yii::app()->createAbsoluteUrl("/survey/index/sid/{$surveyid}",array('lang'=>$baselang,'token'=>$newtoken));
}
else
{
// it does, then redirect to the survey XXX with the token id
$surveylink = Yii::app()->createAbsoluteUrl("/survey/index/sid/{$surveyid}",array('lang'=>$baselang,'token'=>$qryrow['token']));
}
// redirect
header("Location: $surveylink");
- in RegisterController.php:
using a similar workaround to stop sending users a confirmation email
So now when users want to participate in a survey, they have to login (username and password from separate DB) or register.
Tokens are still enabled to track responses.
Public registration is turned on. But now instead of seeing a token registration form (the one that asks you for name and email address) a new token is created automatically.
This works just as it should. But I would like to integrate the whole registration/login process into the LimeSurvey and have it as a plugin so I don't have to modify much source code.
I am still finding it hard to get around the Yii framework.
Can you please point me in the right direction?
Do I need to have a new Controller etc. for this?
I'm thinking:
- controllers/userauth/login.php - to control login process? check DB, display error message, etc.
- controllers/userauth/register.php - same as above, but registration functions
- controllers/userauth/logout.php - well, clear the session, etc.
models/Userauth.php - does this need to represent my additional database table for users?
What other files I need to modify in order to get this all working together with limeSurvey?
And what about displaying all this stuff? Do I need to have any other files to manage how this all is displayed?
Am I on the right path with this one?