Created
March 3, 2014 22:24
-
-
Save mckelvey/9335931 to your computer and use it in GitHub Desktop.
How to use the Public group for LDAP/SSO community submissions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$_LW->REGISTERED_APPS['community_public_group'] = array( // configure this application module | |
'title' => 'Community Public Group', | |
'handlers' => array( | |
'onLoad', | |
'onLoginSuccess', | |
'onLoginFailure', | |
'onSubnavs', | |
'onManagerQuery', | |
'onAfterEdit', | |
'onAfterCreate', | |
'onOutput', | |
), | |
); | |
class LiveWhaleApplicationCommunityPublicGroup { | |
/* PROTECTED CLASS VARIABLES */ | |
protected $DIRECTORY_HOST = 'domain.tld'; // set this to the main directory host, specifically for email (e.g. [email protected]) | |
/* PUBLIC HANDLER METHODS */ | |
public function onLoad() { | |
global $_LW; | |
if (!$this->is_community_user_in_public()) return; // exit if not the community_user | |
$_LW->REGISTERED_CSS[] = '/live/resource/css/community_public_group%5Csimplify.css'; | |
} | |
public function onLoginSuccess($username) { // called when a user logs in successfully | |
global $_LW; | |
if (!in_array($_LW->CONFIG['LOGIN_MODE'], array('LDAP', 'SSO'))) return NULL; // exit if not SSO or LDAP | |
if (preg_match('~^/livewhale/\?(news|news_edit|events|events_list|events_edit|images|images_edit)~i', @$_SESSION['livewhale']['login_redirect'], $redirect_page)) { | |
switch ($redirect_page[1]) { | |
case 'news': | |
case 'news_edit': | |
if ($_LW->dbo->query('select', 'username', 'livewhale_users', 'username = '.$_LW->escape($username).' AND authorized_modules LIKE \'%news%\'')->firstRow()->run()) return NULL; // allow normal behavior if authorized for news | |
break; | |
case 'events': | |
case 'events_list': | |
case 'events_edit': | |
if ($_LW->dbo->query('select', 'username', 'livewhale_users', 'username = '.$_LW->escape($username).' AND authorized_modules LIKE \'%events%\'')->firstRow()->run()) return NULL; // allow normal behavior if authorized for events | |
break; | |
case 'images': | |
case 'images_edit': | |
if ($_LW->userSetting('core_edit')) return NULL; // allow normal behavior if authorized for any dynamic module | |
break; | |
} | |
} else { | |
return NULL; | |
} | |
if ($gid = $_LW->dbo->query('select', 'id', 'livewhale_groups', 'fullname = \'Public\'')->firstRow()->run()) { | |
if (empty($_SESSION['livewhale']['manage']['switch_groups'])) { | |
$_SESSION['livewhale']['manage']['switch_groups'] = array($_SESSION['livewhale']['manage']['gid']); // add switch groups array if not present, pre-loaded with the base group | |
} | |
$_SESSION['livewhale']['manage']['switch_groups'][] = $gid['id']; // add Public group to the available switch_groups | |
if (empty($_SESSION['livewhale']['manage']['user_settings']['core_switch'])) $_SESSION['livewhale']['manage']['user_settings']['core_switch'] = 1; // and allow group switching | |
$_LW->d_groups->switchGroup($gid['id'], TRUE); // so that the actual switch will work | |
} | |
} | |
public function onLoginFailure($username) { // called when a user fails to login, or does not have a LiveWhale account | |
global $_LW; | |
if (!in_array($_LW->CONFIG['LOGIN_MODE'], array('LDAP', 'SSO'))) return NULL; // exit if not SSO or LDAP | |
if ($_LW->dbo->query('select', 'username', 'livewhale_users', 'username = '.$_LW->escape($username))->firstRow()->run()) return NULL; // allow normal behavior if a LiveWhale account exists | |
if (preg_match('~^/livewhale/\?(news|news_edit|events|events_list|events_edit|images|images_edit)~i', @$_SESSION['livewhale']['login_redirect'])) { | |
$_LW->d_login->livewhaleLogin('community_user'); | |
} | |
} | |
public function onSubnavs($type, $subnavs) { | |
global $_LW; | |
if (!$this->is_community_user_in_public()) return $subnavs; // exit if not the community_user | |
if (!in_array($type, array('news', 'events', 'images'))) return; // this check should not really be a necessary given everything else, but best to be sure | |
$subnavs = array(); // remove the subnav for all accessible types, since you cannot search beyond your own content anyway | |
return $subnavs; | |
} | |
public function onManagerQuery($manager, $query) { | |
global $_LW; | |
if (!$this->is_community_user_in_public()) return $query; // exit if not the community_user | |
if (empty($_SESSION['livewhale']['login_username'])) return $query; // exit if the login_username is not available (TODO: better choice?) | |
if ($manager === 'manager_public_submissions') { | |
$join = "livewhale_public_submissions.submission_id=livewhale_events.id"; // allow any type | |
} else if (preg_match('~_?(news|events|images)_?~', $manager, $match)) { | |
$join = "livewhale_public_submissions.submission_type='{$match[1]}' AND livewhale_public_submissions.submission_id=livewhale_events.id"; // match the type | |
} else { | |
return $query; // not a great choice to exit here, but some type is needed to filter correctly (TODO: better choice?) | |
} | |
$query->innerJoin('livewhale_public_submissions', $join); // join the submissions table | |
$query->innerJoin('livewhale_public_submitters', 'livewhale_public_submitters.id=livewhale_public_submissions.submitter_id'); // so that we can subsequently join the submitter table | |
$query->where('livewhale_public_submitters.email='.$_LW->escape($this->email())); // only show submissions made by this login username | |
return $query; | |
} | |
public function onAfterEdit($type, $page, $id) { | |
global $_LW; | |
if (!$this->is_community_user_in_public()) return; // exit if not the community_user | |
if (empty($id)) return; // exit if no id, e.g. creating a new event | |
$submitter = $_LW->dbo->query('select', 'email', 'livewhale_public_submitters')->innerJoin('livewhale_public_submissions', 'livewhale_public_submissions.submitter_id=livewhale_public_submitters.id')->where('livewhale_public_submissions.submission_type='.$_LW->escape($type).' AND livewhale_public_submissions.submission_id='.((int) $id))->firstRow()->run(); | |
if (empty($submitter) || $submitter['email'] !== $this->email()) { // if we did not create this event or it has no submitter | |
$_SESSION['livewhale']['manage']['messages']['failure'][]='You may not edit an event which you did not create.'; // tell them what is happening | |
die(header("Location: /livewhale/?events_list")); // and redirect to the events list | |
} | |
} | |
public function onAfterCreate($type, $id) { | |
global $_LW; | |
if (!$this->is_community_user_in_public()) return; // exit if not the community_user | |
if (!in_array($type, array('news', 'events', 'images'))) return; // this check should not really be a necessary given everything else, but best to be sure | |
if (!((int) $id)) return; // you bet we need an id | |
$this->saveSubmission($_SESSION['livewhale']['manage']['firstname'], $this->email(), $type, $id, ((empty($_LW->_POST['title'])) ? 'Unknown Title' : $_LW->_POST['title']), NULL); // save the submitter for this item | |
} | |
public function onOutput($buffer) { | |
global $_LW; | |
if (!$this->is_community_user_in_public()) return $buffer; // exit if not the community_user | |
if (!in_array($_LW->page, array('news', 'events', 'events_list', 'images'))) return $buffer; // exit if not a manager page we care about; should not be necessary, but best to be sure | |
$buffer = preg_replace('~"addnew([^"]*)lw_hidden([^"]*)"~i', 'addnew$1$2', $buffer); // make add an new event button visible again | |
return $buffer; | |
} | |
/* PRIVATE METHODS */ | |
private function is_community_user_in_public() { | |
return (@$_SESSION['livewhale']['manage']['username'] === 'community_user' && @$_SESSION['livewhale']['manage']['grouptitle'] === 'Public'); | |
} | |
private function email() { | |
return ((empty($_SESSION['livewhale']['login_username'])) ? @$_SESSION['livewhale']['manage']['email'] : "{$_SESSION['livewhale']['login_username']}@{$this->DIRECTORY_HOST}"); | |
} | |
/* TODO: | |
This code was copied from /livewhale/core/modules/public/live/public.php. | |
It should be moved into the data module to be more available and | |
updated to use the new dbo. */ | |
private function saveSubmission($name, $email, $type, $id, $title, $mission) { // saves a submitter account | |
global $_LW; | |
if ($res=$_LW->query('SELECT 1 FROM livewhale_public_submitters WHERE email='.$_LW->escape($email).';')) { // insert submitter record if it doesn't already exist | |
if (!$res->num_rows) { | |
$_LW->query('INSERT INTO livewhale_public_submitters VALUES(NULL,'.$_LW->escape($name).','.$_LW->escape($email).');'); | |
} | |
} | |
$_LW->query('INSERT INTO livewhale_public_submissions VALUES(NULL,(SELECT id FROM livewhale_public_submitters WHERE email='.$_LW->escape($email).' LIMIT 1),'.(int)$id.','.$_LW->escape($type).','.$_LW->escape($title).',NOW(),'.(!empty($mission) ? (int)$mission : 'NULL').','.(!empty($mission) ? '(SELECT title FROM livewhale_missions WHERE id='.(int)$mission.')' : 'NULL').',NULL,'.(strpos(@$_SERVER['HTTP_USER_AGENT'], 'iPhone')!==false ? 1 : 'NULL').');'); // insert submission | |
@touch($_LW->INCLUDES_DIR_PATH.'/data/public/last_submission'); // record time of last submission | |
} | |
} | |
/* TODO for existing users: | |
1) onLoginSuccess works, but we need to address how to persist it | |
2) enable necessary tabs in Public not otherwise present in home group(s) | |
3) disable create links in Public that are available in their home group(s) | |
*/ | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#toolbar, | |
#meta_bar, | |
#search { | |
display: none; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment