mirror of
https://github.com/e107inc/e107.git
synced 2025-08-01 04:10:38 +02:00
New API to detect if social login is enabled site-wide
The site-wide social login system is now backwards-compatible with how
the social_login_active core pref worked since commit
3b2d8333b6
.
social_login_active's least significant bit is now treated as a global
bit, so if it's not set, no other bits are allowed to be set. This
un-breaks all existing checks for whether social_login_active is empty.
Except in themes, the social_login_active check has been replaced with
an e_user_provider API to check if social login is enabled site-wide.
This commit is contained in:
@@ -65,8 +65,7 @@ class signup_shortcodes extends e_shortcode
|
|||||||
// TODO - template
|
// TODO - template
|
||||||
function sc_signup_xup_login($parm)
|
function sc_signup_xup_login($parm)
|
||||||
{
|
{
|
||||||
$pref = e107::getPref('social_login_active');
|
if (!e107::getUserProvider()->isSocialLoginEnabled()) return '';
|
||||||
if (empty($pref)) return '';
|
|
||||||
|
|
||||||
$size = empty($parm['size']) ? '3x' : $parm['size'];
|
$size = empty($parm['size']) ? '3x' : $parm['size'];
|
||||||
$class = empty($parm['class']) ? 'btn btn-primary' : $parm['class'] ;
|
$class = empty($parm['class']) ? 'btn btn-primary' : $parm['class'] ;
|
||||||
@@ -77,8 +76,7 @@ class signup_shortcodes extends e_shortcode
|
|||||||
// TODO - template
|
// TODO - template
|
||||||
function sc_signup_xup_signup($parm)
|
function sc_signup_xup_signup($parm)
|
||||||
{
|
{
|
||||||
$pref = e107::getPref('social_login_active');
|
if (!e107::getUserProvider()->isSocialLoginEnabled()) return '';
|
||||||
if (empty($pref)) return '';
|
|
||||||
|
|
||||||
$size = empty($parm['size']) ? '2x' : $parm['size'];
|
$size = empty($parm['size']) ? '2x' : $parm['size'];
|
||||||
$class = empty($parm['class']) ? 'btn btn-primary' : $parm['class'] ;
|
$class = empty($parm['class']) ? 'btn btn-primary' : $parm['class'] ;
|
||||||
|
@@ -313,7 +313,7 @@ class comment
|
|||||||
else
|
else
|
||||||
{ // Comment entry not allowed - point to signup link
|
{ // Comment entry not allowed - point to signup link
|
||||||
$userReg = intval(e107::pref('core','user_reg'));
|
$userReg = intval(e107::pref('core','user_reg'));
|
||||||
$socialLogin = e107::pref('core','social_login_active');
|
$socialLogin = e107::getUserProvider()->isSocialLoginEnabled();
|
||||||
|
|
||||||
$text = "<div class='comments-form-login'>";
|
$text = "<div class='comments-form-login'>";
|
||||||
|
|
||||||
|
@@ -1260,13 +1260,22 @@ class e_user_provider
|
|||||||
return $type;
|
return $type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if social logins are enabled site-wide
|
||||||
|
* @return bool TRUE if the site has social logins enabled; FALSE otherwise
|
||||||
|
*/
|
||||||
|
public function isSocialLoginEnabled()
|
||||||
|
{
|
||||||
|
return $this->social_login_config_manager->isFlagActive(SocialLoginConfigManager::ENABLE_BIT_GLOBAL);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* XUP Signup Method (falls-back to XUP login when existing user is detected).
|
* XUP Signup Method (falls-back to XUP login when existing user is detected).
|
||||||
* May be used as a simple XUP login link for existing and non-existing users.
|
* May be used as a simple XUP login link for existing and non-existing users.
|
||||||
*/
|
*/
|
||||||
public function signup($redirectUrl = true, $loginAfterSuccess = true, $emailAfterSuccess = true)
|
public function signup($redirectUrl = true, $loginAfterSuccess = true, $emailAfterSuccess = true)
|
||||||
{
|
{
|
||||||
if (!e107::getPref('social_login_active', false))
|
if (!$this->isSocialLoginEnabled())
|
||||||
{
|
{
|
||||||
throw new Exception("Signup failed! This feature is disabled.", 100); // TODO lan
|
throw new Exception("Signup failed! This feature is disabled.", 100); // TODO lan
|
||||||
}
|
}
|
||||||
@@ -1449,7 +1458,7 @@ class e_user_provider
|
|||||||
public function login($redirectUrl = true)
|
public function login($redirectUrl = true)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (!e107::getPref('social_login_active', false))
|
if (!$this->isSocialLoginEnabled())
|
||||||
{
|
{
|
||||||
throw new Exception("Login failed! This feature is disabled.", 100); // TODO lan
|
throw new Exception("Login failed! This feature is disabled.", 100); // TODO lan
|
||||||
}
|
}
|
||||||
|
@@ -1609,7 +1609,7 @@ class e_user extends e_user_model
|
|||||||
*/
|
*/
|
||||||
final public function loginProvider($xup)
|
final public function loginProvider($xup)
|
||||||
{
|
{
|
||||||
if(!e107::getPref('social_login_active', false)) return false;
|
if(!e107::getUserProvider()->isSocialLoginEnabled()) return false;
|
||||||
|
|
||||||
if($this->isUser()) return true;
|
if($this->isUser()) return true;
|
||||||
|
|
||||||
@@ -1724,7 +1724,7 @@ class e_user extends e_user_model
|
|||||||
public function tryProviderSession($deniedAs)
|
public function tryProviderSession($deniedAs)
|
||||||
{
|
{
|
||||||
// don't allow if main admin browse front-end or there is already user session
|
// don't allow if main admin browse front-end or there is already user session
|
||||||
if((!$deniedAs && $this->getSessionDataAs()) || null !== $this->_session_data || !e107::getPref('social_login_active', false)) return $this;
|
if((!$deniedAs && $this->getSessionDataAs()) || null !== $this->_session_data || !e107::getUserProvider()->isSocialLoginEnabled()) return $this;
|
||||||
|
|
||||||
$hybrid = e107::getHybridAuth(); // init the auth class
|
$hybrid = e107::getHybridAuth(); // init the auth class
|
||||||
|
|
||||||
@@ -1883,7 +1883,7 @@ class e_user extends e_user_model
|
|||||||
$this->_initConstants();
|
$this->_initConstants();
|
||||||
|
|
||||||
// init any available external user provider
|
// init any available external user provider
|
||||||
if(e107::getPref('social_login_active', false)) $this->initProvider();
|
if(e107::getUserProvider()->isSocialLoginEnabled()) $this->initProvider();
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
@@ -35,16 +35,24 @@ class SocialLoginConfigManager
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Check a social login boolean (toggle) setting
|
* Check a social login boolean (toggle) setting
|
||||||
|
*
|
||||||
|
* For backwards compatibility, if the global bit (0) is off, no other bits can be on.
|
||||||
|
*
|
||||||
* @param int $bit Which setting to check
|
* @param int $bit Which setting to check
|
||||||
* @return boolean TRUE if the setting is enabled, FALSE otherwise
|
* @return boolean TRUE if the setting is enabled, FALSE otherwise
|
||||||
*/
|
*/
|
||||||
public function isFlagActive($bit = self::ENABLE_BIT_GLOBAL)
|
public function isFlagActive($bit = self::ENABLE_BIT_GLOBAL)
|
||||||
{
|
{
|
||||||
return (bool)($this->config->get(self::SOCIAL_LOGIN_FLAGS) & 1 << $bit);
|
$flags = $this->config->get(self::SOCIAL_LOGIN_FLAGS);
|
||||||
|
if (!($flags & 1 << self::ENABLE_BIT_GLOBAL)) return false;
|
||||||
|
return (bool)($flags & 1 << $bit);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set a social login boolean (toggle) setting
|
* Set a social login boolean (toggle) setting
|
||||||
|
*
|
||||||
|
* For backwards compatibility, if the global bit (0) is off, no other bits can be on.
|
||||||
|
*
|
||||||
* @param int $bit Which setting to change
|
* @param int $bit Which setting to change
|
||||||
* @param boolean $active TRUE to enable the setting, FALSE to disable the setting
|
* @param boolean $active TRUE to enable the setting, FALSE to disable the setting
|
||||||
*/
|
*/
|
||||||
@@ -54,7 +62,9 @@ class SocialLoginConfigManager
|
|||||||
if (!is_numeric($flags)) $flags = 0x0;
|
if (!is_numeric($flags)) $flags = 0x0;
|
||||||
|
|
||||||
$flags = $flags & ~(1 << $bit) | ($active << $bit);
|
$flags = $flags & ~(1 << $bit) | ($active << $bit);
|
||||||
$this->config->set(self::SOCIAL_LOGIN_FLAGS, $flags);
|
|
||||||
|
if (!($flags & 1 << self::ENABLE_BIT_GLOBAL)) $this->config->set(self::SOCIAL_LOGIN_FLAGS, 0x0);
|
||||||
|
else $this->config->set(self::SOCIAL_LOGIN_FLAGS, $flags);
|
||||||
$this->saveConfig();
|
$this->saveConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -225,7 +225,9 @@ class social_ui extends e_admin_ui
|
|||||||
return "<p>" . LAN_SOCIAL_UPDATE_REQUIRED . "</p>";
|
return "<p>" . LAN_SOCIAL_UPDATE_REQUIRED . "</p>";
|
||||||
}
|
}
|
||||||
|
|
||||||
$text = "<table class='table adminform'>
|
$text = $this->generateAdminFormJs();
|
||||||
|
|
||||||
|
$text .= "<table class='table adminform'>
|
||||||
<colgroup>
|
<colgroup>
|
||||||
<col class='col-label' />
|
<col class='col-label' />
|
||||||
<col class='col-control' />
|
<col class='col-control' />
|
||||||
@@ -466,6 +468,53 @@ class social_ui extends e_admin_ui
|
|||||||
|
|
||||||
return $text;
|
return $text;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function generateAdminFormJs()
|
||||||
|
{
|
||||||
|
return <<<EOD
|
||||||
|
<script type='text/javascript'>
|
||||||
|
var e107 = e107 || {'settings': {}, 'behaviors': {}};
|
||||||
|
|
||||||
|
let socialLoginSwitches = {
|
||||||
|
'social-login-test-page__switch': null,
|
||||||
|
};
|
||||||
|
|
||||||
|
function socialLoginSwitchesHighstate(element) {
|
||||||
|
if (element === undefined) return;
|
||||||
|
|
||||||
|
let isActive = element.checked;
|
||||||
|
|
||||||
|
if (isActive) {
|
||||||
|
for (let key in socialLoginSwitches) {
|
||||||
|
let toggle = $('[name='+key+']');
|
||||||
|
toggle.bootstrapSwitch('disabled', false);
|
||||||
|
if (socialLoginSwitches[key] !== null) toggle.bootstrapSwitch('state', socialLoginSwitches[key]);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for (let key in socialLoginSwitches) {
|
||||||
|
let toggle = $('[name='+key+']');
|
||||||
|
socialLoginSwitches[key] = toggle.bootstrapSwitch('state');
|
||||||
|
toggle.bootstrapSwitch('state', false);
|
||||||
|
toggle.bootstrapSwitch('disabled', true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
(function ($)
|
||||||
|
{
|
||||||
|
e107.behaviors.manageSocialLoginSwitches = {
|
||||||
|
attach: function (context, settings) {
|
||||||
|
let globalSwitch = $('[name=social-login-active__switch]');
|
||||||
|
socialLoginSwitchesHighstate(globalSwitch.get(0));
|
||||||
|
globalSwitch.on('switchChange.bootstrapSwitch', function(event) {
|
||||||
|
socialLoginSwitchesHighstate(event.target);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
};
|
||||||
|
})(jQuery);
|
||||||
|
</script>
|
||||||
|
EOD;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -164,7 +164,7 @@ class social_shortcodes extends e_shortcode
|
|||||||
|
|
||||||
function sc_social_login($parm=null)
|
function sc_social_login($parm=null)
|
||||||
{
|
{
|
||||||
$pref = e107::pref('core', 'social_login_active');
|
$pref = e107::getUserProvider()->isSocialLoginEnabled();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@@ -55,22 +55,48 @@ class SocialLoginConfigManagerTest extends \Codeception\Test\Unit
|
|||||||
$this->manager = new SocialLoginConfigManager($this->pref);
|
$this->manager = new SocialLoginConfigManager($this->pref);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testFlagSetting()
|
public function testFlagSettingOff()
|
||||||
{
|
{
|
||||||
$this->pref->set(SocialLoginConfigManager::SOCIAL_LOGIN_FLAGS, 0x0);
|
$this->pref->set(SocialLoginConfigManager::SOCIAL_LOGIN_FLAGS, 0x0);
|
||||||
$this->manager = new SocialLoginConfigManager($this->pref);
|
$this->manager = new SocialLoginConfigManager($this->pref);
|
||||||
$this->assertFalse($this->manager->isFlagActive(SocialLoginConfigManager::ENABLE_BIT_GLOBAL));
|
$this->assertFalse($this->manager->isFlagActive(SocialLoginConfigManager::ENABLE_BIT_GLOBAL));
|
||||||
$this->assertFalse($this->manager->isFlagActive(SocialLoginConfigManager::ENABLE_BIT_TEST_PAGE));
|
$this->assertFalse($this->manager->isFlagActive(SocialLoginConfigManager::ENABLE_BIT_TEST_PAGE));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFlagSettingGlobalOffPreventsOthersOn()
|
||||||
|
{
|
||||||
$this->manager->setFlag(SocialLoginConfigManager::ENABLE_BIT_GLOBAL, 0);
|
$this->manager->setFlag(SocialLoginConfigManager::ENABLE_BIT_GLOBAL, 0);
|
||||||
$this->manager->setFlag(SocialLoginConfigManager::ENABLE_BIT_TEST_PAGE, 1);
|
$this->manager->setFlag(SocialLoginConfigManager::ENABLE_BIT_TEST_PAGE, 1);
|
||||||
$this->assertFalse($this->manager->isFlagActive(SocialLoginConfigManager::ENABLE_BIT_GLOBAL));
|
$this->assertFalse($this->manager->isFlagActive(SocialLoginConfigManager::ENABLE_BIT_GLOBAL));
|
||||||
$this->assertTrue($this->manager->isFlagActive(SocialLoginConfigManager::ENABLE_BIT_TEST_PAGE));
|
$this->assertFalse($this->manager->isFlagActive(SocialLoginConfigManager::ENABLE_BIT_TEST_PAGE));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFlagSettingGlobalOnAllowsOtherToggles()
|
||||||
|
{
|
||||||
$this->manager->setFlag(SocialLoginConfigManager::ENABLE_BIT_GLOBAL, 1);
|
$this->manager->setFlag(SocialLoginConfigManager::ENABLE_BIT_GLOBAL, 1);
|
||||||
$this->manager->setFlag(SocialLoginConfigManager::ENABLE_BIT_TEST_PAGE, 0);
|
$this->manager->setFlag(SocialLoginConfigManager::ENABLE_BIT_TEST_PAGE, 0);
|
||||||
$this->assertTrue($this->manager->isFlagActive(SocialLoginConfigManager::ENABLE_BIT_GLOBAL));
|
$this->assertTrue($this->manager->isFlagActive(SocialLoginConfigManager::ENABLE_BIT_GLOBAL));
|
||||||
$this->assertFalse($this->manager->isFlagActive(SocialLoginConfigManager::ENABLE_BIT_TEST_PAGE));
|
$this->assertFalse($this->manager->isFlagActive(SocialLoginConfigManager::ENABLE_BIT_TEST_PAGE));
|
||||||
|
|
||||||
|
$this->manager->setFlag(SocialLoginConfigManager::ENABLE_BIT_TEST_PAGE, 1);
|
||||||
|
$this->assertTrue($this->manager->isFlagActive(SocialLoginConfigManager::ENABLE_BIT_GLOBAL));
|
||||||
|
$this->assertTrue($this->manager->isFlagActive(SocialLoginConfigManager::ENABLE_BIT_TEST_PAGE));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Don't break existing client code that checks if social_login_active is 0 or not!
|
||||||
|
* If the global bit is 0, all the other bits should be 0, too.
|
||||||
|
*/
|
||||||
|
public function testFlagGlobalOffTurnsAllOff()
|
||||||
|
{
|
||||||
|
$this->pref->set(SocialLoginConfigManager::SOCIAL_LOGIN_FLAGS, ~0);
|
||||||
|
$this->manager = new SocialLoginConfigManager($this->pref);
|
||||||
|
$this->assertTrue($this->manager->isFlagActive(SocialLoginConfigManager::ENABLE_BIT_GLOBAL));
|
||||||
|
$this->assertTrue($this->manager->isFlagActive(SocialLoginConfigManager::ENABLE_BIT_TEST_PAGE));
|
||||||
|
|
||||||
|
$this->manager->setFlag(SocialLoginConfigManager::ENABLE_BIT_GLOBAL, 0);
|
||||||
|
$this->assertFalse($this->manager->isFlagActive(SocialLoginConfigManager::ENABLE_BIT_GLOBAL));
|
||||||
|
$this->assertFalse($this->manager->isFlagActive(SocialLoginConfigManager::ENABLE_BIT_TEST_PAGE));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testIsProviderEnabled()
|
public function testIsProviderEnabled()
|
||||||
|
@@ -120,7 +120,7 @@
|
|||||||
|
|
||||||
// unset($_SESSION['E:SOCIAL']);
|
// unset($_SESSION['E:SOCIAL']);
|
||||||
|
|
||||||
if(vartrue($_GET['provider']) && !isset($_SESSION['E:SOCIAL']) && e107::getPref('social_login_active', false) && (e_ADMIN_AREA !== true))
|
if(vartrue($_GET['provider']) && !isset($_SESSION['E:SOCIAL']) && e107::getUserProvider()->isSocialLoginEnabled() && (e_ADMIN_AREA !== true))
|
||||||
{
|
{
|
||||||
$hybridauth = e107::getHybridAuth();
|
$hybridauth = e107::getHybridAuth();
|
||||||
|
|
||||||
|
@@ -13,7 +13,7 @@
|
|||||||
require_once("class2.php");
|
require_once("class2.php");
|
||||||
|
|
||||||
|
|
||||||
if ((USER || e_LOGIN != e_SELF || (empty($pref['user_reg']) && empty($pref['social_login_active']))) && e_QUERY !== 'preview' && !getperms('0') ) // Disable page if user logged in, or some custom e_LOGIN value is used.
|
if ((USER || e_LOGIN != e_SELF || (empty($pref['user_reg']) && !e107::getUserProvider()->isSocialLoginEnabled())) && e_QUERY !== 'preview' && !getperms('0') ) // Disable page if user logged in, or some custom e_LOGIN value is used.
|
||||||
{
|
{
|
||||||
$prev = e107::getRedirect()->getPreviousUrl();
|
$prev = e107::getRedirect()->getPreviousUrl();
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user