Skip to content

Commit

Permalink
#4733 Okta connect
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexTr committed Aug 5, 2024
1 parent a9ee6a9 commit 570ba23
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
46 changes: 44 additions & 2 deletions modules/boonex/okta_connect/classes/BxOktaConCustom.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ function onConfig ($oConfig)
{
}

function onRegister ($aRemoteProfileInfo)
function onRegister ($iProfileId, $aRemoteProfileInfo)
{
bx_log('bx_oktacon', $aRemoteProfileInfo);

if (!empty($aRemoteProfileInfo['communities']))
$this->syncCommunities(bx_get_logged_profile_id(), explode(',', $aRemoteProfileInfo['communities']));
$this->syncCommunities($iProfileId, explode(',', $aRemoteProfileInfo['communities']));

$this->syncLocation($iProfileId, $aRemoteProfileInfo);
}

function onLogin ($oProfile, $aRemoteProfileInfo)
Expand All @@ -33,11 +35,51 @@ function onLogin ($oProfile, $aRemoteProfileInfo)

if (!empty($aRemoteProfileInfo['communities']))
$this->syncCommunities($oProfile ? $oProfile->id() : bx_get_logged_profile_id(), explode(',', $aRemoteProfileInfo['communities']));

$this->syncLocation($oProfile->id(), $aRemoteProfileInfo);
}

function onConvertRemoteFields($aProfileInfo, &$aProfileFields)
{
if (empty($aProfileInfo['grade']))
return;

$a = explode(',', $aProfileInfo['grade']);

$v = 0;
foreach ($a as $i) {
$v += pow(2, (int)$i - 1);
}

$aProfileFields['grades'] = $v;
}

function syncLocation($iProfileId, $aRemoteProfileInfo)
{
if (empty($aRemoteProfileInfo['state']) || empty($aRemoteProfileInfo['city']))
return;

$oProfile = BxDolProfile::getInstance($iProfileId);
if (!$oProfile)
return;

$oMetatags = BxDolMetatags::getObjectInstance('bx_persons');
if (!$oMetatags || !$oMetatags->locationsIsEnabled())
return;

$sEndpoint = bx_append_url_params('https://nominatim.openstreetmap.org', [
'format' => 'json',
'limit' => 1,
'country' => empty($aRemoteProfileInfo['country']) ? 'US' : $aRemoteProfileInfo['country'],
'state' => $aRemoteProfileInfo['state'],
'city' => $aRemoteProfileInfo['city'],
]);
$sResults = bx_file_get_contents($sEndpoint);
if (!($a = @json_decode($sResults, true)))
return;
$a = array_shift($a);

$oMetatags->locationsAdd($oProfile->getContentId(), $a['lat'], $a['lon'], 'US', $aRemoteProfileInfo['state'], $aRemoteProfileInfo['city']);
}

function syncCommunities($iProfileId, $aCommunities, $sModule = 'bx_groups', $sObjConn = 'bx_groups_fans')
Expand Down
19 changes: 15 additions & 4 deletions modules/boonex/okta_connect/classes/BxOktaConModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,28 +117,39 @@ function actionHandle()
*/
if ($aRemoteProfileInfo) {

bx_import('Custom', $this->_aModule);
$oCustom = new BxOktaConCustom($this->_aModule);

// check if user logged in before
$iLocalProfileId = $this->_oDb->getProfileId($aRemoteProfileInfo['id']);

if ($iLocalProfileId && $oProfile = BxDolProfile::getInstance($iLocalProfileId)) {
// user already exists
$this->setLogged($oProfile->id(), '', true, getParam('bx_oktacon_remember_session')); // remember user
bx_import('Custom', $this->_aModule);
$oCustom = new BxOktaConCustom($this->_aModule);
$oCustom->onLogin($oProfile, $aRemoteProfileInfo);
}
else {
// register new user
$this->_createProfile($aRemoteProfileInfo);
$oCustom->onRegister($aRemoteProfileInfo);

}
}
else {
$this->_oTemplate->getPage(_t('_Error'), MsgBox(_t('_sys_connect_profile_error_info')));
}
}

function _createProfileRaw($aProfileInfo, $sAlternativeName = '', $isAutoFriends = true, $isSetLoggedIn = true)
{
$mixed = parent::_createProfileRaw($aProfileInfo, $sAlternativeName, $isAutoFriends, $isSetLoggedIn);
if (is_array($mixed) && isset($mixed['profile_id']) && (!isset($mixed['join_page_redirect']) || !$mixed['join_page_redirect'])) {
bx_import('Custom', $this->_aModule);
$oCustom = new BxOktaConCustom($this->_aModule);
$oCustom->onRegister($mixed['profile_id'], $aProfileInfo);
}

return $mixed;
}

/**
* @param $aProfileInfo - remote profile info
* @param $sAlternativeName - suffix to add to NickName to make it unique
Expand Down

0 comments on commit 570ba23

Please sign in to comment.