Skip to content

Commit fec2fa2

Browse files
committed
add DI and clean uo setDisplayNameRoutine
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
1 parent 6a2f094 commit fec2fa2

File tree

2 files changed

+52
-12
lines changed

2 files changed

+52
-12
lines changed

lib/AppInfo/Application.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ public function registerLDAPPlugins(): void {
5151
new LDAPConnect($s->getConfig()),
5252
$s->getConfig(),
5353
$p,
54-
$c->query(Configuration::class)
54+
$c->query(Configuration::class),
55+
$s->getL10N(self::APP_ID),
56+
$s->getLogger()
5557
);
5658

5759
// $this->ldapUserManager = $c->query(LDAPUserManager::class);

lib/LDAPUserManager.php

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828

2929
use OC\HintException;
30+
use OC\ServerNotAvailableException;
3031
use OC\User\Backend;
3132
use OCA\LdapWriteSupport\Service\Configuration;
3233
use OCA\User_LDAP\Exceptions\ConstraintViolationException;
@@ -36,6 +37,8 @@
3637
use OCP\IGroup;
3738
use OCP\IGroupManager;
3839
use OCP\IImage;
40+
use OCP\IL10N;
41+
use OCP\ILogger;
3942
use OCP\IUser;
4043
use OCP\IUserManager;
4144
use OCP\IUserSession;
@@ -62,8 +65,25 @@ class LDAPUserManager implements ILDAPUserPlugin {
6265
private $ocConfig;
6366
/** @var Configuration */
6467
private $configuration;
68+
/** @var IL10N */
69+
private $l10n;
70+
/** @var ILogger */
71+
private $logger;
6572

66-
public function __construct(IUserManager $userManager, IGroupManager $groupManager, IUserSession $userSession, LDAPConnect $ldapConnect, IConfig $ocConfig, ILDAPProvider $ldapProvider, Configuration $configuration) {
73+
/**
74+
* LDAPUserManager constructor.
75+
*
76+
* @param IUserManager $userManager
77+
* @param IGroupManager $groupManager
78+
* @param IUserSession $userSession
79+
* @param LDAPConnect $ldapConnect
80+
* @param IConfig $ocConfig
81+
* @param ILDAPProvider $ldapProvider
82+
* @param Configuration $configuration
83+
* @param IL10N $l10n
84+
* @param ILogger $logger
85+
*/
86+
public function __construct(IUserManager $userManager, IGroupManager $groupManager, IUserSession $userSession, LDAPConnect $ldapConnect, IConfig $ocConfig, ILDAPProvider $ldapProvider, Configuration $configuration, IL10N $l10n, ILogger $logger) {
6787
$this->userManager = $userManager;
6888
$this->groupManager = $groupManager;
6989
$this->userSession = $userSession;
@@ -75,6 +95,8 @@ public function __construct(IUserManager $userManager, IGroupManager $groupManag
7595
$this->makeLdapBackendFirst();
7696
$this->ldapProvider = $ldapProvider;
7797
$this->configuration = $configuration;
98+
$this->l10n = $l10n;
99+
$this->logger = $logger;
78100
}
79101

80102
/**
@@ -93,28 +115,44 @@ public function respondToActions() {
93115
}
94116

95117
/**
96-
* set display name of the user
97118
*
98119
* @param string $uid user ID of the user
99120
* @param string $displayName new user's display name
100-
* @return bool
121+
* @return string
122+
* @throws HintException
123+
* @throws ServerNotAvailableException
101124
*/
102125
public function setDisplayName($uid, $displayName) {
103126
$userDN = $this->getUserDN($uid);
104127

105128
$connection = $this->ldapProvider->getLDAPConnection($uid);
106129

107-
$displayNameField = $this->ldapProvider->getLDAPDisplayNameField($uid);
130+
try {
131+
$displayNameField = $this->ldapProvider->getLDAPDisplayNameField($uid);
132+
// The LDAP backend supports a second display name field, but it is
133+
// not exposed at this time. So it is just ignored for now.
134+
} catch (\Exception $e) {
135+
throw new HintException(
136+
'Corresponding LDAP User not found',
137+
$this->l10n->t('Could not find related LDAP entry')
138+
);
139+
}
108140

109141
if (!is_resource($connection)) {
110-
//LDAP not available
111-
\OCP\Util::writeLog('user_ldap', 'LDAP resource not available.', \OCP\Util::DEBUG);
112-
return false;
142+
$this->logger->debug('LDAP resource not available', ['app' => 'ldap_write_support']);
143+
throw new ServerNotAvailableException('LDAP server is not available');
113144
}
114145
try {
115-
return ldap_mod_replace($connection,$userDN, [$displayNameField => $displayName]);
116-
} catch(ConstraintViolationException $e) {
117-
throw new HintException('DisplayName change rejected.', \OC::$server->getL10N('user_ldap')->t('DisplayName change rejected. Hint: ').$e->getMessage(), $e->getCode());
146+
if (ldap_mod_replace($connection, $userDN, [$displayNameField => $displayName])) {
147+
return $displayName;
148+
}
149+
throw new HintException('Failed to set display name');
150+
} catch (ConstraintViolationException $e) {
151+
throw new HintException(
152+
$e->getMessage(),
153+
$this->l10n->t('DisplayName change rejected'),
154+
$e->getCode()
155+
);
118156
}
119157
}
120158

@@ -180,7 +218,7 @@ public function createUser($username, $password) {
180218
$requireActorFromLDAP = (bool)$this->ocConfig->getAppValue('ldap_write_support', 'create.requireActorFromLDAP', '1');
181219
$adminUser = $this->userSession->getUser();
182220
if($requireActorFromLDAP && !$adminUser instanceof IUser) {
183-
throw new \Exception('Acting user is not a user');
221+
throw new \Exception('Acting user is not from LDAP');
184222
}
185223
try {
186224
$connection = $this->ldapProvider->getLDAPConnection($adminUser->getUID());

0 commit comments

Comments
 (0)