Skip to content

Commit

Permalink
Dev: Call limesurvey.org with curl to login to com_api (CintLink)
Browse files Browse the repository at this point in the history
  • Loading branch information
olleharstedt committed Jul 14, 2016
1 parent e73fece commit 6310a50
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
38 changes: 37 additions & 1 deletion application/core/plugins/CintLink/CintLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,41 @@ public function getLoginForm(LSHttpRequest $request)
return $content;
}

/**
* Login to limesurvey.org
*/
public function login(LSHttpRequest $request)
{
$username = $request->getParam('username');
$password = $request->getParam('password');

$curl = new Curl();
$response = $curl->post(
$this->baseURL,
array(
'app' => 'cintlinklimesurveyrestapi',
'format' => 'raw',
'resource' => 'login',
'username' => $username,
'password' => $password
)
);
$result = json_decode($response->body);

if ($result->code == 403)
{
return json_encode(array('result' => false));
}
else if ($result->code == 200)
{
return json_encode(array('result' => true));
}
else
{
return json_encode(array('error' => 'Unknown return code: ' . $result->code));
}
}

public function newDirectRequest()
{
$event = $this->event;
Expand All @@ -187,7 +222,8 @@ public function newDirectRequest()
$event->setContent($this, $content);
}
else if ($functionToCall == 'checkIfUserIsLoggedInOnLimesurveyorg'
|| $functionToCall == 'getLoginForm')
|| $functionToCall == 'getLoginForm'
|| $functionToCall == "login")
{
echo $this->$functionToCall($request);
}
Expand Down
31 changes: 30 additions & 1 deletion application/core/plugins/CintLink/js/cintlink.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,35 @@ $(document).ready(function() {
url: LS.plugin.cintlink.pluginBaseUrl + '&function=getLoginForm'
}).done(function(response) {
$('#cintlink-container').html(response);

$('#cintlink-login-submit').on('click', function(ev) {
ev.preventDefault();

var formValues = $('#cintlink-login-form').serialize();

$.ajax({
method: 'POST',
url: LS.plugin.cintlink.pluginBaseUrl + '&function=login&' + formValues
}).done(function(response) {
console.log(response);

var response = JSON.parse(response);

if (response.error) {
$('#error-modal .modal-body-text').html(response.error);
$('#error-modal').modal();
}
else if (response.result) {
// Login OK
}
else {
$('#error-modal .modal-body-text').html("Could not login. Please make sure username and password is correct.");
$('#error-modal').modal();
}

});

});
});
}

Expand All @@ -29,7 +58,7 @@ $(document).ready(function() {
}).done(function(response) {
console.log(response);

response = JSON.parse(response);
var response = JSON.parse(response);

if (response.result)
{
Expand Down
4 changes: 2 additions & 2 deletions application/core/plugins/CintLink/views/loginform.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<form class='form-horizontal'>
<form id='cintlink-login-form' class='form-horizontal'>
<div class='col-sm-4'></div>
<div class='col-sm-8'>
<p class='help-block'><?php eT("Log in to your limesurvey.org account to buy participants."); ?></p>
Expand All @@ -18,7 +18,7 @@
<div class='form-group'>
<div class='col-sm-4'></div>
<div class='col-sm-4'>
<input class='btn btn-default' type='submit' value='Login' />
<input id='cintlink-login-submit' class='btn btn-default' type='submit' value='Login' />
</div>
</div>
</form>

0 comments on commit 6310a50

Please sign in to comment.