Skip to content

Commit

Permalink
plugins: Support Multi-instance Services
Browse files Browse the repository at this point in the history
Support ability to register multiple instances of the same backend
  • Loading branch information
protich committed Jul 1, 2022
1 parent a2e78bf commit 0b93b48
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 30 deletions.
34 changes: 20 additions & 14 deletions include/class.auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ function attemptAutoRegister() {
);
if ($bk->supportsInteractiveAuthentication())
// User can only be authenticated against this backend
$defaults['backend'] = $bk::$id;
$defaults['backend'] = $bk->getBkId();
if ($this_form->isValid(function($f) { return !$f->isVisibleToUsers(); })
&& ($U = User::fromVars($this_form->getClean()))
&& ($acct = ClientAccount::createForUser($U, $defaults))
Expand Down Expand Up @@ -177,7 +177,7 @@ function attemptAutoRegister() {
abstract class AuthenticationBackend extends ServiceRegistry {
static $name;
static $id;

protected $config;

static function register($class) {
if (is_string($class) && class_exists($class))
Expand All @@ -187,7 +187,7 @@ static function register($class) {
|| !($class instanceof AuthenticationBackend))
return false;

static::$registry[$class->getId()] = $class;
static::$registry[$class->getBkId()] = $class;
}

static function allRegistered() {
Expand Down Expand Up @@ -330,11 +330,11 @@ static function getSearchDirectories() {
$backends = array();
foreach (StaffAuthenticationBackend::allRegistered() as $bk)
if ($bk instanceof AuthDirectorySearch)
$backends[$bk::$id] = $bk;
$backends[$bk->getBkId()] = $bk;

foreach (UserAuthenticationBackend::allRegistered() as $bk)
if ($bk instanceof AuthDirectorySearch)
$backends[$bk::$id] = $bk;
$backends[$bk->getBkId()] = $bk;

return array_unique($backends);
}
Expand Down Expand Up @@ -570,7 +570,7 @@ function login($staff, $bk) {
}

// Tag the authkey.
$authkey = $bk->getId().':'.$authkey;
$authkey = $bk->getBkId().':'.$authkey;
// Now set session crap and lets roll baby!
$authsession = &$_SESSION['_auth']['staff'];
$authsession = array(); //clear.
Expand Down Expand Up @@ -677,7 +677,8 @@ function renderExternalLink() {
$this->getServiceName());
?>
<a class="external-sign-in" title="<?php echo $service; ?>"
href="login.php?do=ext&amp;bk=<?php echo urlencode(static::$id); ?>">
href="login.php?do=ext&amp;bk=<?php echo
urlencode($this->getBkId()); ?>">
<?php if (static::$sign_in_image_url) { ?>
<img class="sign-in-image" src="<?php echo static::$sign_in_image_url;
?>" alt="<?php echo $service; ?>"/>
Expand All @@ -695,15 +696,17 @@ function renderExternalLink() {
}

function triggerAuth() {
$_SESSION['ext:bk:id'] = $this->getBkId();
// For legacy plugins prior to v1.17
$_SESSION['ext:bk:class'] = get_class($this);
}
}
Signal::connect('api', function($dispatcher) {
$dispatcher->append(
url('^/auth/ext$', function() {
if ($class = $_SESSION['ext:bk:class']) {
$bk = StaffAuthenticationBackend::getBackend($class::$id)
?: UserAuthenticationBackend::getBackend($class::$id);
if ($id = $_SESSION['ext:bk:id']) {
$bk = StaffAuthenticationBackend::getBackend($id)
?: UserAuthenticationBackend::getBackend($id);
if ($bk instanceof ExternalAuthentication)
$bk->triggerAuth();
}
Expand Down Expand Up @@ -751,7 +754,7 @@ function login($user, $bk) {
global $ost;

if (!$user || !$bk
|| !$bk::$id //Must have ID
|| !$bk->getBkId() //Must have ID
|| !($authkey = $bk->getAuthKey($user)))
return false;

Expand Down Expand Up @@ -796,7 +799,7 @@ function setAuthKey($user, $bk, $key=false) {
$authkey = $key ?: $bk->getAuthKey($user);

//Tag the authkey.
$authkey = $bk::$id.':'.$authkey;
$authkey = $bk->getBkId().':'.$authkey;

//Set the session goodies
$authsession = &$_SESSION['_auth']['user'];
Expand Down Expand Up @@ -882,10 +885,11 @@ function renderExternalLink() {

?>
<a class="external-sign-in" title="<?php echo $service; ?>"
href="login.php?do=ext&amp;bk=<?php echo urlencode(static::$id); ?>">
href="login.php?do=ext&amp;bk=<?php echo
urlencode($this->getBkId()); ?>">
<?php if (static::$sign_in_image_url) { ?>
<img class="sign-in-image" src="<?php echo static::$sign_in_image_url;
?>" alt="<?php $service; ?>"/>
?>" alt="<?php echo $service; ?>"/>
<?php } else { ?>
<div class="external-auth-box">
<span class="external-auth-icon">
Expand All @@ -900,6 +904,8 @@ function renderExternalLink() {
}

function triggerAuth() {
$_SESSION['ext:bk:id'] = $this->getBkId();
// Legacy for plugins prior to v1.17
$_SESSION['ext:bk:class'] = get_class($this);
}
}
Expand Down
4 changes: 4 additions & 0 deletions include/class.plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ function getInfo() {
return array_merge($this->config, $this->defaults ?? []);
}

function getInstance() {
return $this->instance;
}

function get($key, $default=null) {
if (isset($this->config[$key]))
return $this->config[$key];
Expand Down
25 changes: 21 additions & 4 deletions include/class.util.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,29 @@ static function getVarScope() {
abstract class ServiceRegistry {
static protected $registry = array();

public function __isset($property) {
return isset($this->$property);
}

function getId() {
if (isset($this->config)
&& is_a($this->config, 'PluginConfig'))
return $this->config->getId();
return static::$id;
}

/*
* getBkId
*
* Get service id used to register the service. Plugins adds a tag
* making it possible to register multiple instances of the same
* plugin.
*
*/
function getBkId() {
$id = $this->getId();
if (isset($this->config)
&& is_a($this->config, 'PluginConfig'))
$id =sprintf('%s.%s', $id, $this->config->getId());

return static::$id;
return $id;
}

function getName() {
Expand Down
4 changes: 2 additions & 2 deletions include/client/register.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
if (isset($user) && $user instanceof ClientCreateRequest) {
$bk = $user->getBackend();
$info = array_merge($info, array(
'backend' => $bk::$id,
'backend' => $bk->getBkId(),
'username' => $user->getUsername(),
));
}
Expand Down Expand Up @@ -61,7 +61,7 @@
<input type="hidden" name="backend" value="<?php echo $info['backend']; ?>"/>
<input type="hidden" name="username" value="<?php echo $info['username']; ?>"/>
<?php foreach (UserAuthenticationBackend::allRegistered() as $bk) {
if ($bk::$id == $info['backend']) {
if ($bk->getBkId() == $info['backend']) {
echo $bk->getName();
break;
}
Expand Down
4 changes: 2 additions & 2 deletions include/staff/templates/user-register.tmpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
<option value="">&mdash; <?php echo __('Use any available backend'); ?> &mdash;</option>
<?php foreach (UserAuthenticationBackend::allRegistered() as $ab) {
if (!$ab->supportsInteractiveAuthentication()) continue; ?>
<option value="<?php echo $ab::$id; ?>" <?php
if ($info['backend'] == $ab::$id)
<option value="<?php echo $ab->getBkId(); ?>" <?php
if ($info['backend'] == $ab->getBkId())
echo 'selected="selected"'; ?>><?php
echo $ab->getName(); ?></option>
<?php } ?>
Expand Down
16 changes: 8 additions & 8 deletions include/upgrader/streams/core/c37e1656-e2b4e5cb.task.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,35 +33,35 @@ function run($time) {
$ost->plugins->bootstrap();
// Staff auth backends
foreach (StaffAuthenticationBackend::allRegistered() as $p) {
if ($p::$id && $p->getId() == $p::$id)
if ($p::$id && $p->getBkId() == $p::$id)
continue;
Staff::objects()
->filter(['backend' => $p::$id])
->update(['backend' => $p->getId()]);
->update(['backend' => $p->getBkId()]);
}
// User auth backends
foreach (UserAuthenticationBackend::allRegistered() as $p) {
if ($p::$id && $p->getId() == $p::$id)
if ($p::$id && $p->getBkId() == $p::$id)
continue;
UserAccount::objects()
->filter(['backend' => $p::$id])
->update(['backend' => $p->getId()]);
->update(['backend' => $p->getBkId()]);
}
// 2fa backends
foreach (Staff2FABackend::allRegistered() as $p) {
if ($p::$id && $p->getId() == $p::$id)
if ($p::$id && $p->getBkId() == $p::$id)
continue;
ConfigItems::objects()
->filter(['default_2fa' => $p::$id])
->update(['default_2fa' => $p->getId()]);
->update(['default_2fa' => $p->getBkId()]);
}
// Password Policies
$config = $ost->getConfig();
foreach (PasswordPolicy::allActivePolicies() as $p) {
if ($config->get('agent_passwd_policy') == $p::$id)
$config->set('agent_passwd_policy', $p->getId());
$config->set('agent_passwd_policy', $p->getBkId());
if ($config->get('client_passwd_policy') == $p::$id)
$config->set('client_passwd_policy', $p->getId());
$config->set('client_passwd_policy', $p->getBkId());
}
}
}
Expand Down

0 comments on commit 0b93b48

Please sign in to comment.