Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,12 @@ public function registerLDAPPlugins(): void {

$this->ldapGroupManager = new LDAPGroupManager(
$s->getGroupManager(),
$s->getUserSession(),
$ldapConnect,
$s->getLogger(),
$provider
$provider,
$c->query(Configuration::class),
$s->getL10N(self::APP_ID),
$s->getLogger()
);

/** @var UserPluginManager $userPluginManager */
Expand Down
72 changes: 56 additions & 16 deletions lib/LDAPGroupManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,26 @@
use Exception;
use OC\Group\Backend;
use OCA\LdapWriteSupport\AppInfo\Application;
use OCA\LdapWriteSupport\Service\Configuration;
use OCA\User_LDAP\Group_Proxy;
use OCA\User_LDAP\ILDAPGroupPlugin;
use OCA\User_LDAP\LDAPProvider;
use OCP\AppFramework\QueryException;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\ILogger;
use OCP\IUser;
use OCP\IUserSession;
use OCP\LDAP\ILDAPProvider;

class LDAPGroupManager implements ILDAPGroupPlugin {

/** @var ILDAPProvider */
private $ldapProvider;

/** @var IUserSession */
private $userSession;

/** @var IGroupManager */
private $groupManager;

Expand All @@ -49,11 +56,14 @@ class LDAPGroupManager implements ILDAPGroupPlugin {
/** @var ILogger */
private $logger;

public function __construct(IGroupManager $groupManager, LDAPConnect $ldapConnect, ILogger $logger, ILDAPProvider $ldapProvider) {
public function __construct(IGroupManager $groupManager, IUserSession $userSession, LDAPConnect $ldapConnect, ILDAPProvider $ldapProvider, Configuration $configuration, IL10N $l10n, ILogger $logger) {
$this->groupManager = $groupManager;
$this->userSession = $userSession;
$this->ldapConnect = $ldapConnect;
$this->logger = $logger;
$this->ldapProvider = $ldapProvider;
$this->configuration = $configuration;
$this->l10n = $l10n;
$this->logger = $logger;

if($this->ldapConnect->groupsEnabled()) {
$this->makeLdapBackendFirst();
Expand Down Expand Up @@ -84,15 +94,27 @@ public function respondToActions() {
* @return string|null
*/
public function createGroup($gid) {
$adminUser = $this->userSession->getUser();
$requireActorFromLDAP = $this->configuration->isLdapActorRequired();
if ($requireActorFromLDAP && !$adminUser instanceof IUser) {
throw new Exception('Acting user is not from LDAP');
}
try {
$connection = $this->ldapProvider->getLDAPConnection($adminUser->getUID());
// TODO: what about multiple bases?
$base = $this->ldapProvider->getLDAPBaseGroups($adminUser->getUID());
} catch (Exception $e) {
if ($requireActorFromLDAP) {
if ($this->configuration->isPreventFallback()) {
throw new \Exception('Acting admin is not from LDAP', 0, $e);
}
return false;
}
$connection = $this->ldapConnect->getLDAPConnection();
$base = $this->ldapConnect->getLDAPBaseGroups()[0];
}

/**
* FIXME could not create group using LDAPProvider, because its methods rely
* on passing an already inserted [ug]id, which we do not have at this point.
*/

$newGroupEntry = $this->buildNewEntry($gid);
$connection = $this->ldapConnect->getLDAPConnection();
$newGroupDN = "cn=$gid," . $this->ldapConnect->getLDAPBaseGroups()[0];
list($newGroupDN, $newGroupEntry) = $this->buildNewEntry($gid, $base);
$newGroupDN = $this->ldapProvider->sanitizeDN([$newGroupDN])[0];

if ($ret = ldap_add($connection, $newGroupDN, $newGroupEntry)) {
Expand Down Expand Up @@ -223,12 +245,30 @@ public function isLDAPGroup($gid) {
}
}

private function buildNewEntry($gid) {
return [
'objectClass' => ['groupOfNames', 'top'],
'cn' => $gid,
'member' => ['']
];
private function buildNewEntry($gid, $base) {
$ldif = $this->configuration->getGroupTemplate();

$ldif = str_replace('{GID}', $gid, $ldif);
$ldif = str_replace('{BASE}', $base, $ldif);

$entry = [];
$lines = explode(PHP_EOL, $ldif);
foreach ($lines as $line) {
$split = explode(':', $line, 2);
$key = trim($split[0]);
$value = trim($split[1]);
if (!isset($entry[$key])) {
$entry[$key] = $value;
} else if (is_array($entry[$key])) {
$entry[$key][] = $value;
} else {
$entry[$key] = [$entry[$key], $value];
}
}
$dn = $entry['dn'];
unset($entry['dn']);

return [$dn, $entry];
}

public function makeLdapBackendFirst() {
Expand Down
6 changes: 6 additions & 0 deletions lib/LDAPUserManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ public function respondToActions() {
* @throws ServerNotAvailableException
*/
public function setDisplayName($uid, $displayName) {
trigger_error(__METHOD__);
$this->logger->error(__METHOD__, ['app' => 'ldap_write_support']);
$userDN = $this->getUserDN($uid);

$connection = $this->ldapProvider->getLDAPConnection($uid);
Expand All @@ -141,6 +143,10 @@ public function setDisplayName($uid, $displayName) {
if (ldap_mod_replace($connection, $userDN, [$displayNameField => $displayName])) {
return $displayName;
}
trigger_error(print_r(['conn' => $connection,
'user' => $userDN,
'dpyField' => $displayNameField,
'dpy' => $displayName], true));
throw new HintException('Failed to set display name');
} catch (ConstraintViolationException $e) {
throw new HintException(
Expand Down
16 changes: 16 additions & 0 deletions lib/Service/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ public function getUserTemplate() {
);
}

public function getGroupTemplate() {
return $this->config->getAppValue(
Application::APP_ID,
'template.group',
$this->getGroupTemplateDefault()
);
}

public function getUserTemplateDefault() {
return
'dn: uid={UID},{BASE}' . PHP_EOL .
Expand All @@ -67,6 +75,14 @@ public function getUserTemplateDefault() {
'userPassword: {PWD}';
}

public function getGroupTemplateDefault() {
return
'dn: cn={GID},{BASE}' . PHP_EOL .
'objectClass: groupOfNames' . PHP_EOL .
'cn: {GID}' . PHP_EOL .
'member:';
}

public function isRequireEmail(): bool {
// this core settings flag is not exposed anywhere else
return $this->config->getAppValue('core', 'newUser.requireEmail', 'no') === 'yes';
Expand Down
4 changes: 3 additions & 1 deletion lib/Settings/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ public function getForm() {
'templates',
[
'user' => $this->config->getUserTemplate(),
'userDefault' => $this->config->getUserTemplateDefault(),
'userDefault' => $this->config->getGroupTemplateDefault(),
'group' => $this->config->getGroupTemplate(),
'groupDefault' => $this->config->getGroupTemplateDefault(),
]
);
$this->initialStateService->provideInitialState(
Expand Down
21 changes: 21 additions & 0 deletions src/components/AdminSettings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@
<li><span class="mono">{BASE}</span> – {{ t('ldap_write_support', 'the LDAP node of the acting (sub)admin or the configured user base') }}</li>
</ul>
<textarea class="mono" v-on:change="setUserTemplate" v-model="templates.user">{{templates.user}}</textarea>
<h3>{{ t('ldap_write_support', 'Group template') }}</h3>
<p>{{ t('ldap_write_support', 'LDIF template for creating groups. Following placeholders may be used') }}</p>
<ul class="disc">
<li><span class="mono">{GID}</span> – {{ t('ldap_write_support', 'the group id provided by the (sub)admin') }}</li>
<li><span class="mono">{BASE}</span> – {{ t('ldap_write_support', 'the LDAP node of the acting (sub)admin or the configured group base') }}</li>
</ul>
<textarea class="mono" v-on:change="setGroupTemplate" v-model="templates.group">{{templates.group}}</textarea>
</div>
</template>

Expand All @@ -67,6 +74,8 @@
templates: {
user: Object,
userDefault: Object,
group: Object,
groupDefault: Object,
},
switches: {
createRequireActorFromLdap: Boolean,
Expand All @@ -92,6 +101,18 @@
}
OCP.AppConfig.setValue('ldap_write_support', 'template.user', this.templates.user);
},
setGroupTemplate() {
if(this.templates.group === "") {
let self = this;
OCP.AppConfig.deleteKey('ldap_write_support', 'template.group', {
success: function() {
self.templates.group = self.templates.groupDefault;
}
});
return;
}
OCP.AppConfig.setValue('ldap_write_support', 'template.group', this.templates.group);
},
toggleSwitch(prefKey, state, appId = 'ldap_write_support') {
this.switches[prefKey] = state;
let value = (state | 0).toString();
Expand Down