Skip to content

Instantly share code, notes, and snippets.

@ThomasHambach
Created March 14, 2013 04:41
Show Gist options
  • Save ThomasHambach/5158852 to your computer and use it in GitHub Desktop.
Save ThomasHambach/5158852 to your computer and use it in GitHub Desktop.
Example implementation of authenticating users to Invision Powerboard using ssoPublicSessions. Will create a new user if this user is not found. This file is located in admin/sources/classes/session and should be overwritten by you.
<?php
/**
* Class ssoPublicSessions
*/
class ssoPublicSessions extends publicSessions
{
protected $ci_user = null;
public function __construct()
{
/* Finish loading publicSessions constructor */
parent::__construct();
// bots can be skipped
if ($this->_member->is_not_human) {
return;
}
if (!$this->ci_user = $this->_loginCheck()) {
// we do not have a valid session in codeigniter
self::setMember(0);
$this->_updateMemberSession();
return false;
}
/** Already logged in, but we had to verify with CI */
$member = false;
if (self::$data_store['member_id'] > 0) {
/** But only if we have our frontend id saved */
$res = ipsRegistry::DB()->buildAndFetch( array( 'select' => 'field_11 as frontendid',
'from' => 'pfields_content',
'where' => "member_id = '" . self::$data_store['member_id'] . "'" ) );
if($res['frontendid'] > 0) {
// frontend id is set, all is good
return;
} else {
//echo 'saving ;linked id';
// no frontend id, life is bad, and sad...
IPSMember::save(self::$data_store['member_id'],array(
'customFields' => array(
'field_11' => $this->ci_user['userid']
)
));
return;
}
}
// we are not logged in, but we would like to!
$res = ipsRegistry::DB()->buildAndFetch( array( 'select' => 'member_id',
'from' => 'pfields_content',
'where' => "field_11 = '" . $this->ci_user['userid'] . "'" ) );
if($res['member_id'] > 0) {
// echo 'loaded by member id';
$member = IPSMember::load($res['member_id'], 'all');
} else {
// echo 'loaded by username';
$member = IPSMember::load($this->ci_user['username'], 'all', 'username');
}
// IPB didnt find the user
if (!isset($member['member_id'])) {
$member = IPSMember::create(array(
'members' => array(
'name' => $this->ci_user['username'],
'members_display_name' => $this->ci_user['username'],
'password' => rand(111111, 999999),
'email' => $this->ci_user['email'],
), /*
'customFields' => array(
'frontendid' => $this->ci_user['id']
) */
), false, true, false);
IPSMember::save($member['member_id'],array(
'customFields' => array(
'field_11' => $this->ci_user['userid']
)
));
}
self::setMember($member['member_id']);
$this->_updateMemberSession();
return true;
}
/**
* Check if the user is logged in
*/
protected function _loginCheck()
{
if (!$this->session) $this->session = new FG_Session();
if ($this->session->userdata('loggedin')) {
$user = $this->session->all_userdata();
return $user;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment