/**
* LOGIN
*
* If you wanted to do any custom checks in the database for the user,
* here is where you'd do it. For example, below you can see the basic calls to retrieve
* the user id using the $user object passed in to the method then get the user profile data
*
* @param obj $user The user object
* @param obj $options The login options
* @return bool true/false The 'success' for login
*/
function onUserLogin($user, $options)
{
//Get App & User
$app =& JFactory::getApplication();
//only run in site
if($app->isSite()){
//INIT some vars to use
//Get the user ID with helper method and the $user object
$userId = intval(JUserHelper::getUserId($user['username']));
//Get Profile with helper method and userId
$profile = JUserHelper::getProfile($userId);
// Check the users groups against the configured Investor/Creator groups to get login redirect
$redirect = $this->runCrowdfundingLoginOverrides($userId);
// Only redirect if the config is not empty (fallback to default behaviour)
if($redirect != ""){
// Get config to pass in the site's SEF setting in redirect
$config = JFactory::getConfig();
// Set the redirect
$app->setUserState(
'users.login.form.return',
JRoute::_($redirect, false, $config->get('usesecure'))
);
}//END IF redirect isn't empty
}// END IF IN SITE
//RETURN
return true;
}/**
* Get the custom user/group data to use
*
* Method to get the Custom Crowdfunding user groups & their custom redirect urls for each from config
* and compare with the users current groups to determine the redirect
*
* As its name suggests, this method (function) You could conceivably add additional 'sub' routines in here to get additional info &
* 'Do' stuff with it. You could do this by writing your logic (code) or 'trigger'
* other public or private methods (in this file) - both of which are demonstrated in the
* function 'runCrowdfundingLoginOverrides($userId, $userGroups)'.
*
* Please see bottom of this file for examples.
*
* @param integer $userId The user id whose groups to chec
* @return bool true/false True on success/False on fail
*/
public function runCrowdfundingLoginOverrides($userId)
{ // INIT Vars
$return=""; // to return
// Login Type - first OR any
$loginType = "any";
// Groups
$userGroups = JUserHelper::getUserGroups($userId);
// Get the configured custom user groups to check against;
$check = array(
"custom_groups" => array(
$this->params->get('investor_group', 2),
$this->params->get('creator_group', 3)
),
"first" => array(
$this->params->get('investor_group', 2) => $this->params->get('investorredirect1', ''),
$this->params->get('creator_group', 3) => $this->params->get('creatorredirect1', '')
),
"any" => array(
$this->params->get('investor_group', 2) => $this->params->get('investorredirect2', ''),
$this->params->get('creator_group', 3) => $this->params->get('creatorredirect2', '')
)
);
// Get user type
$profile = JUserHelper::getProfile($userId);
$userType = ((int)$profile->profile['usertype'] > 0) ? (int)$profile->profile['usertype'] : 0;
$otherGroup = ((int)$userType > 0 && (int)$userType == $check['custom_groups'][0]) ? $check['custom_groups'][1] : $check['custom_groups'][0];
// =============== Check & Set the user's groups =====================//
// Check usergroups is array & not empty
if(is_array($userGroups) && !empty($userGroups)){
// The user is in their correct group
if($userType > 0){
if(in_array($userType, $userGroups)){
//Check if they are alo in the opposing group
if(in_array($otherGroup, $userGroups)){
JUserHelper::removeUserFromGroup($userId, $otherGroup);
//unset($userGroups[$otherGroup]);
}
}else{// User isn't in their correct group
// Add the user to their group
JUserHelper::addUserToGroup($userId, $userType);
//$userGroups[$userType] = $userType;
//Remove from opposing
if(in_array($otherGroup, $userGroups)){
JUserHelper::removeUserFromGroup($userId, $otherGroup);
//unset($userGroups[$otherGroup]);
}
}
}else{
// User Doesn't have a usertype set - ADD IT based on their group
//If creator
if(in_array($this->params->get('creator_group', 3), $userGroups))
// Add to creator type
$profile->profile['usertype'] = $this->params->get('creator_group', 3);
//If investor
if(in_array($this->params->get('investor_group', 2), $userGroups))
//Set to investor type
$profile->profile['usertype'] = $this->params->get('investor_group', 2);
// RESET usertype var
$userType = ((int)$profile->profile['usertype'] > 0) ? (int)$profile->profile['usertype'] : 0;
if($userType > 0) // if their field is set
// Set the Group based on the usertype field
JUserHelper::addUserToGroup($userId, $userType);
}//END Else (usertype <= 0)
}//END IF $userGroups is array and not empty
else{ //The user is not in any groups - check if they have a usertype in profile info and add to that group
if($userType > 0){
// Add the user to their group
JUserHelper::addUserToGroup($userId, $userType);
}else{
// Add the users to the default Registration Group
jimport('joomla.application.component.helper');
$config = JComponentHelper::getParams('com_users');
// Default to Registered.
$defaultUserGroup = $config->get('new_usertype', 2);
JUserHelper::addUserToGroup($userId, $defaultUserGroup);
}
}
// Get the groups again
$userGroups = JUserHelper::getUserGroups($userId);
// Get fresh session/user object
$session = JFactory::getSession();
$session->set('user', new JUser($userId));
$user = JFactory::getUser($userId);
//Check if this is the first login or not and set $loginType
if($user->guest || $user->lastvisitDate == "0000-00-00 00:00:00" || $user->lastvisitDate == NULL){
$loginType = "first";
}
//Check the user groups again now we've added/removed
if(is_array($userGroups) && !empty($userGroups)){
//Loop through the users groups to check which group they are in
foreach($userGroups as $k => $v){
if(in_array($k, $check['custom_groups'])) //&& $g = $userType
$return = $check[$loginType][$k];
}
}
// Return $return
return $return;
}Join our newsletter to get alerts for Joomla releases, tips and tricks and extension updates.
