2727
2828
2929use OC \HintException ;
30+ use OC \ServerNotAvailableException ;
3031use OC \User \Backend ;
3132use OCA \LdapWriteSupport \Service \Configuration ;
3233use OCA \User_LDAP \Exceptions \ConstraintViolationException ;
3637use OCP \IGroup ;
3738use OCP \IGroupManager ;
3839use OCP \IImage ;
40+ use OCP \IL10N ;
41+ use OCP \ILogger ;
3942use OCP \IUser ;
4043use OCP \IUserManager ;
4144use 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