This repository has been archived by the owner on Feb 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hqscoutnetauthenticator.php
109 lines (103 loc) · 4.42 KB
/
hqscoutnetauthenticator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
* @version $Id: hqscoutnetauthenticator.php $
* @package Joomla.Tutorials
* @subpackage Plugins
* @license GNU/GPL
*/
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();
/**
* Authentication Plugin. Based on the example.php plugin in the Joomla! Core installation
*
* @package Joomla.Tutorials
* @subpackage Plugins
* @license GNU/GPL
*/
class plgAuthenticationhqscoutnetauthenticator extends JPlugin
{
/**
* This method should handle any authentication and report back to the subject
* This example uses simple authentication - it checks if the password is the reverse
* of the username (and the user exists in the database).
*
* @access public
* @param array $credentials Array holding the user credentials ('username' and 'password')
* @param array $options Array of extra options
* @param object $response Authentication response object
* @return boolean
* @since 1.5
*/
function onUserAuthenticate( $credentials, $options, &$response )
{
/*
* Here you would do whatever you need for an authentication routine with the credentials
*
* In this example the mixed variable $return would be set to false
* if the authentication routine fails or an integer userid of the authenticated
* user if the routine passes
*/
$db = JFactory::getDbo();
$query = $db->getQuery(true)
->select('id')
->from('#__users')
->where('username=' . $db->quote($credentials['username']) . ' OR email=' . $db->quote($credentials['username']));
$db->setQuery($query);
$result = $db->loadResult();
if (!$result) { //om username eller email INTE finns i joomlas user-tabell
$response->status = 'STATUS_FAILURE';
$response->error_message = 'Användarnamnet hittades inte. Kontrollera användarnamnet och försök igen.';
error_log("HQ: Username or email does not exist in Joomla. Aborting login.", 0);
} else { // username eller email finns i joomla -> testa inloggning mot scoutnet..
$authUrl = $this->params->get('loginUrl');
$postdata = http_build_query(
array(
$this->params->get('usernameParameterName') => $credentials['username'],
$this->params->get('passwordParameterName') => $credentials['password']
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$authResult = file_get_contents($authUrl, false, $context);
if ($authResult <>"") {
$authResultObj=json_decode($authResult);
// var_dump($authResultObj); //DEBUG
if (isset($authResultObj->member->member_no)) { //login success
// echo $authResultObj->token; //DEBUG
// echo "Välkommen ".$authResultObj->member->first_name ." ".$authResultObj->member->last_name."!"; //DEBUG
error_log("HQ Login TRUE for: ".$credentials['username'], 0);
$authMedlemsnr = $authResultObj->member->member_no;
error_log("HQ Medlemsnr FOUND: ".$authMedlemsnr, 0);
$query = $db->getQuery(true) //CMJ: REDOING the query on the correct username, found in Scoutnet.
->select('id')
->from('#__users')
->where('username=' . $db->quote($authMedlemsnr));
$db->setQuery($query);
$result = $db->loadResult();
$userLoggedIn = JUser::getInstance($result); // Bring this in line with the rest of the system
$response->email = $userLoggedIn->email;
$response->username = $userLoggedIn->username;
$response->type = "hqscoutnetauthenticator";
$response->status = JAuthentication::STATUS_SUCCESS;
} else if (isset($authResultObj->err)) { //login failure
$response->status = JAuthentication::STATUS_FAILURE;
$response->error_message = 'Felaktigt användarnamn ('.$credentials['username'].') eller lösenord. Kontrollera uppgifterna och försök igen.';
error_log("HQ Login FALSE for ".$username, 0);
// error_log($buffer, 0); //DEBUG
}
} else { //inloggningen misslyckades
$response->status = JAuthentication::STATUS_FAILURE;
$response->error_message = 'Uppkopplingen mot Scoutnet misslyckades. Vänligen försök senare.';
error_log("HQ Login CONNECTION FAILURE TO SCOUTNET for ".$username, 0);
// error_log($buffer, 0); //DEBUG
}
}
}
}
?>