Completed
Push — stable8.2 ( b4bbd4...3350d6 )
by
unknown
59:02
created

User::getHomePath()   C

Complexity

Conditions 15
Paths 12

Size

Total Lines 45
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 15
Metric Value
dl 0
loc 45
ccs 32
cts 32
cp 1
rs 5.0504
cc 15
eloc 25
nc 12
nop 1
crap 15

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @author Arthur Schiwon <[email protected]>
4
 * @author Morris Jobke <[email protected]>
5
 *
6
 * @copyright Copyright (c) 2015, ownCloud, Inc.
7
 * @license AGPL-3.0
8
 *
9
 * This code is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License, version 3,
11
 * as published by the Free Software Foundation.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License, version 3,
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
20
 *
21
 */
22
23
namespace OCA\user_ldap\lib\user;
24
25
use OCA\user_ldap\lib\user\IUserTools;
26
use OCA\user_ldap\lib\Connection;
27
use OCA\user_ldap\lib\FilesystemHelper;
28
use OCA\user_ldap\lib\LogWrapper;
29
30
/**
31
 * User
32
 *
33
 * represents an LDAP user, gets and holds user-specific information from LDAP
34
 */
35
class User {
36
	/**
37
	 * @var IUserTools
38
	 */
39
	protected $access;
40
	/**
41
	 * @var Connection
42
	 */
43
	protected $connection;
44
	/**
45
	 * @var \OCP\IConfig
46
	 */
47
	protected $config;
48
	/**
49
	 * @var FilesystemHelper
50
	 */
51
	protected $fs;
52
	/**
53
	 * @var \OCP\Image
54
	 */
55
	protected $image;
56
	/**
57
	 * @var LogWrapper
58
	 */
59
	protected $log;
60
	/**
61
	 * @var \OCP\IAvatarManager
62
	 */
63
	protected $avatarManager;
64
65
	/**
66
	 * @var string
67
	 */
68
	protected $dn;
69
	/**
70
	 * @var string
71
	 */
72
	protected $uid;
73
	/**
74
	 * @var string[]
75
	 */
76
	protected $refreshedFeatures = array();
77
	/**
78
	 * @var string
79
	 */
80
	protected $avatarImage;
81
82
	/**
83
	 * DB config keys for user preferences
84
	 */
85
	const USER_PREFKEY_FIRSTLOGIN  = 'firstLoginAccomplished';
86
	const USER_PREFKEY_LASTREFRESH = 'lastFeatureRefresh';
87
88
	/**
89
	 * @brief constructor, make sure the subclasses call this one!
90
	 * @param string the internal username
91
	 * @param string the LDAP DN
92
	 * @param IUserTools $access an instance that implements IUserTools for
93
	 * LDAP interaction
94
	 * @param \OCP\IConfig
95
	 * @param FilesystemHelper
96
	 * @param \OCP\Image any empty instance
97
	 * @param LogWrapper
98
	 * @param \OCP\IAvatarManager
99
	 */
100 43
	public function __construct($username, $dn, IUserTools $access,
101
		\OCP\IConfig $config, FilesystemHelper $fs, \OCP\Image $image,
102
		LogWrapper $log, \OCP\IAvatarManager $avatarManager) {
103
104 43
		$this->access        = $access;
105 43
		$this->connection    = $access->getConnection();
106 43
		$this->config        = $config;
107 43
		$this->fs            = $fs;
108 43
		$this->dn            = $dn;
109 43
		$this->uid           = $username;
110 43
		$this->image         = $image;
111 43
		$this->log           = $log;
112 43
		$this->avatarManager = $avatarManager;
113 43
	}
114
115
	/**
116
	 * @brief updates properties like email, quota or avatar provided by LDAP
117
	 * @return null
118
	 */
119 9
	public function update() {
120 9
		if(is_null($this->dn)) {
121
			return null;
122
		}
123
124 9
		$hasLoggedIn = $this->config->getUserValue($this->uid, 'user_ldap',
125 9
				self::USER_PREFKEY_FIRSTLOGIN, 0);
126
127 9
		if($this->needsRefresh()) {
128 8
			$this->updateEmail();
129 8
			$this->updateQuota();
130 8
			if($hasLoggedIn !== 0) {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison !== seems to always evaluate to true as the types of $hasLoggedIn (string) and 0 (integer) can never be identical. Maybe you want to use a loose comparison != instead?
Loading history...
131
				//we do not need to try it, when the user has not been logged in
132
				//before, because the file system will not be ready.
133 7
				$this->updateAvatar();
134
				//in order to get an avatar as soon as possible, mark the user
135
				//as refreshed only when updating the avatar did happen
136 7
				$this->markRefreshTime();
137 7
			}
138 8
		}
139 9
	}
140
141
	/**
142
	 * processes results from LDAP for attributes as returned by getAttributesToRead()
143
	 * @param array $ldapEntry the user entry as retrieved from LDAP
144
	 */
145 3
	public function processAttributes($ldapEntry) {
146 3
		$this->markRefreshTime();
147
		//Quota
148 3
		$attr = strtolower($this->connection->ldapQuotaAttribute);
0 ignored issues
show
Documentation introduced by
The property ldapQuotaAttribute does not exist on object<OCA\user_ldap\lib\Connection>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
149 3
		if(isset($ldapEntry[$attr])) {
150 1
			$this->updateQuota($ldapEntry[$attr][0]);
151 1
		}
152 3
		unset($attr);
153
154
		//Email
155 3
		$attr = strtolower($this->connection->ldapEmailAttribute);
0 ignored issues
show
Documentation introduced by
The property ldapEmailAttribute does not exist on object<OCA\user_ldap\lib\Connection>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
156 3
		if(isset($ldapEntry[$attr])) {
157 1
			$this->updateEmail($ldapEntry[$attr][0]);
158 1
		}
159 3
		unset($attr);
160
161
		//displayName
162 3
		$displayName = $displayName2 = '';
163 3
		$attr = strtolower($this->connection->ldapUserDisplayName);
164 3
		if(isset($ldapEntry[$attr])) {
165 1
			$displayName = $ldapEntry[$attr][0];
166 1
		}
167 3
		$attr = strtolower($this->connection->ldapUserDisplayName2);
168 3
		if(isset($ldapEntry[$attr])) {
169
			$displayName2 = $ldapEntry[$attr][0];
170
		}
171 3
		if(!empty($displayName)) {
172 1
			$this->composeAndStoreDisplayName($displayName);
173 1
			$this->access->cacheUserDisplayName(
174 1
				$this->getUsername(),
175 1
				$displayName,
176
				$displayName2
177 1
			);
178 1
		}
179 3
		unset($attr);
180
181
		// LDAP Username, needed for s2s sharing
182 3
		if(isset($ldapEntry['uid'])) {
183 1
			$this->storeLDAPUserName($ldapEntry['uid'][0]);
184 3
		} else if(isset($ldapEntry['samaccountname'])) {
185
			$this->storeLDAPUserName($ldapEntry['samaccountname'][0]);
186
		}
187
188
		//homePath
189 3
		if(strpos($this->connection->homeFolderNamingRule, 'attr:') === 0) {
0 ignored issues
show
Documentation introduced by
The property homeFolderNamingRule does not exist on object<OCA\user_ldap\lib\Connection>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
190 1
			$attr = strtolower(substr($this->connection->homeFolderNamingRule, strlen('attr:')));
0 ignored issues
show
Documentation introduced by
The property homeFolderNamingRule does not exist on object<OCA\user_ldap\lib\Connection>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
191 1
			if(isset($ldapEntry[$attr])) {
192 1
				$this->access->cacheUserHome(
193 1
					$this->getUsername(), $this->getHomePath($ldapEntry[$attr][0]));
194 1
			}
195 1
		}
196
197
		//memberOf groups
198 3
		$cacheKey = 'getMemberOf'.$this->getUsername();
199 3
		$groups = false;
200 3
		if(isset($ldapEntry['memberof'])) {
201 1
			$groups = $ldapEntry['memberof'];
202 1
		}
203 3
		$this->connection->writeToCache($cacheKey, $groups);
204
205
		//Avatar
206 3
		$attrs = array('jpegphoto', 'thumbnailphoto');
207 3
		foreach ($attrs as $attr)  {
208 3
			if(isset($ldapEntry[$attr])) {
209 1
				$this->avatarImage = $ldapEntry[$attr][0];
210 1
				$this->updateAvatar();
211 1
				break;
212
			}
213 3
		}
214 3
	}
215
216
	/**
217
	 * @brief returns the LDAP DN of the user
218
	 * @return string
219
	 */
220 10
	public function getDN() {
221 10
		return $this->dn;
222
	}
223
224
	/**
225
	 * @brief returns the ownCloud internal username of the user
226
	 * @return string
227
	 */
228 12
	public function getUsername() {
229 12
		return $this->uid;
230
	}
231
232
	/**
233
	 * returns the home directory of the user if specified by LDAP settings
234
	 * @param string $valueFromLDAP
235
	 * @return bool|string
236
	 * @throws \Exception
237
	 */
238 6
	public function getHomePath($valueFromLDAP = null) {
239 6
		$path = $valueFromLDAP;
240 6
		$attr = null;
241
242 6
		if(   is_null($path)
243 6
		   && strpos($this->access->connection->homeFolderNamingRule, 'attr:') === 0
0 ignored issues
show
Bug introduced by
Accessing connection on the interface OCA\user_ldap\lib\user\IUserTools suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
244 6
		   && $this->access->connection->homeFolderNamingRule !== 'attr:')
0 ignored issues
show
Bug introduced by
Accessing connection on the interface OCA\user_ldap\lib\user\IUserTools suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
245 6
		{
246 4
			$attr = substr($this->access->connection->homeFolderNamingRule, strlen('attr:'));
0 ignored issues
show
Bug introduced by
Accessing connection on the interface OCA\user_ldap\lib\user\IUserTools suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
247 4
			$homedir = $this->access->readAttribute(
248 4
				$this->access->username2dn($this->getUsername()), $attr);
249 4
			if ($homedir && isset($homedir[0])) {
250 2
				$path = $homedir[0];
251 2
			}
252 4
		}
253
254 6
		if(!empty($path)) {
255
			//if attribute's value is an absolute path take this, otherwise append it to data dir
256
			//check for / at the beginning or pattern c:\ resp. c:/
257 2
			if(   '/' !== $path[0]
258 2
			   && !(3 < strlen($path) && ctype_alpha($path[0])
259 1
			       && $path[1] === ':' && ('\\' === $path[2] || '/' === $path[2]))
260 2
			) {
261 1
				$path = $this->config->getSystemValue('datadirectory',
262 1
						\OC::$SERVERROOT.'/data' ) . '/' . $path;
263 1
			}
264
			//we need it to store it in the DB as well in case a user gets
265
			//deleted so we can clean up afterwards
266 2
			$this->config->setUserValue(
267 2
				$this->getUsername(), 'user_ldap', 'homePath', $path
268 2
			);
269 2
			return $path;
270
		}
271
272 4
		if(    !is_null($attr)
273 4
			&& $this->config->getAppValue('user_ldap', 'enforce_home_folder_naming_rule', true)
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
274 4
		) {
275
			// a naming rule attribute is defined, but it doesn't exist for that LDAP user
276 1
			throw new \Exception('Home dir attribute can\'t be read from LDAP for uid: ' . $this->getUsername());
277
		}
278
279
		//false will apply default behaviour as defined and done by OC_User
280 3
		$this->config->setUserValue($this->getUsername(), 'user_ldap', 'homePath', '');
281 3
		return false;
282
	}
283
284
	public function getMemberOfGroups() {
285
		$cacheKey = 'getMemberOf'.$this->getUsername();
286
		if($this->connection->isCached($cacheKey)) {
287
			return $this->connection->getFromCache($cacheKey);
288
		}
289
		$groupDNs = $this->access->readAttribute($this->getDN(), 'memberOf');
290
		$this->connection->writeToCache($cacheKey, $groupDNs);
291
		return $groupDNs;
292
	}
293
294
	/**
295
	 * @brief reads the image from LDAP that shall be used as Avatar
296
	 * @return string data (provided by LDAP) | false
297
	 */
298 11
	public function getAvatarImage() {
299 11
		if(!is_null($this->avatarImage)) {
300 1
			return $this->avatarImage;
301
		}
302
303 11
		$this->avatarImage = false;
0 ignored issues
show
Documentation Bug introduced by
The property $avatarImage was declared of type string, but false is of type false. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
304 11
		$attributes = array('jpegPhoto', 'thumbnailPhoto');
305 11
		foreach($attributes as $attribute) {
306 11
			$result = $this->access->readAttribute($this->dn, $attribute);
307 11
			if($result !== false && is_array($result) && isset($result[0])) {
308 3
				$this->avatarImage = $result[0];
309 3
				break;
310
			}
311 11
		}
312
313 11
		return $this->avatarImage;
314
	}
315
316
	/**
317
	 * @brief marks the user as having logged in at least once
318
	 * @return null
319
	 */
320 3
	public function markLogin() {
321 3
		$this->config->setUserValue(
322 3
			$this->uid, 'user_ldap', self::USER_PREFKEY_FIRSTLOGIN, 1);
323 3
	}
324
325
	/**
326
	 * @brief marks the time when user features like email have been updated
327
	 * @return null
328
	 */
329 9
	public function markRefreshTime() {
330 9
		$this->config->setUserValue(
331 9
			$this->uid, 'user_ldap', self::USER_PREFKEY_LASTREFRESH, time());
332 9
	}
333
334
	/**
335
	 * @brief checks whether user features needs to be updated again by
336
	 * comparing the difference of time of the last refresh to now with the
337
	 * desired interval
338
	 * @return bool
339
	 */
340 9
	private function needsRefresh() {
341 9
		$lastChecked = $this->config->getUserValue($this->uid, 'user_ldap',
342 9
			self::USER_PREFKEY_LASTREFRESH, 0);
343
344
		//TODO make interval configurable
345 9
		if((time() - intval($lastChecked)) < 86400 ) {
346 1
			return false;
347
		}
348 8
		return  true;
349
	}
350
351
	/**
352
	 * Stores a key-value pair in relation to this user
353
	 *
354
	 * @param string $key
355
	 * @param string $value
356
	 */
357 5
	private function store($key, $value) {
358 5
		$this->config->setUserValue($this->uid, 'user_ldap', $key, $value);
359 5
	}
360
361
	/**
362
	 * Composes the display name and stores it in the database. The final
363
	 * display name is returned.
364
	 *
365
	 * @param string $displayName
366
	 * @param string $displayName2
367
	 * @returns string the effective display name
368
	 */
369 5
	public function composeAndStoreDisplayName($displayName, $displayName2 = '') {
370 5
		if(!empty($displayName2)) {
371 1
			$displayName .= ' (' . $displayName2 . ')';
372 1
		}
373 5
		$this->store('displayName', $displayName);
374 5
		return $displayName;
375
	}
376
377
	/**
378
	 * Stores the LDAP Username in the Database
379
	 * @param string $userName
380
	 */
381
	public function storeLDAPUserName($userName) {
382
		$this->store('uid', $userName);
383
	}
384
385
	/**
386
	 * @brief checks whether an update method specified by feature was run
387
	 * already. If not, it will marked like this, because it is expected that
388
	 * the method will be run, when false is returned.
389
	 * @param string email | quota | avatar (can be extended)
390
	 * @return bool
391
	 */
392 20
	private function wasRefreshed($feature) {
393 20
		if(isset($this->refreshedFeatures[$feature])) {
394 1
			return true;
395
		}
396 20
		$this->refreshedFeatures[$feature] = 1;
397 20
		return false;
398
	}
399
400
	/**
401
	 * fetches the email from LDAP and stores it as ownCloud user value
402
	 * @param string $valueFromLDAP if known, to save an LDAP read request
403
	 * @return null
404
	 */
405 11
	public function updateEmail($valueFromLDAP = null) {
406 11
		if($this->wasRefreshed('email')) {
407 1
			return;
408
		}
409 11
		$email = $valueFromLDAP;
410 11 View Code Duplication
		if(is_null($valueFromLDAP)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
411 11
			$emailAttribute = $this->connection->ldapEmailAttribute;
0 ignored issues
show
Documentation introduced by
The property ldapEmailAttribute does not exist on object<OCA\user_ldap\lib\Connection>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
412 11
			if(!empty($emailAttribute)) {
413 2
				$aEmail = $this->access->readAttribute($this->dn, $emailAttribute);
414 2
				if(is_array($aEmail) && (count($aEmail) > 0)) {
415 1
					$email = $aEmail[0];
416 1
				}
417 2
			}
418 11
		}
419 11
		if(!is_null($email)) {
420 1
			$this->config->setUserValue(
421 1
				$this->uid, 'settings', 'email', $email);
422 1
		}
423 11
	}
424
425
	/**
426
	 * fetches the quota from LDAP and stores it as ownCloud user value
427
	 * @param string $valueFromLDAP the quota attribute's value can be passed,
428
	 * to save the readAttribute request
429
	 * @return null
430
	 */
431 14
	public function updateQuota($valueFromLDAP = null) {
432 14
		if($this->wasRefreshed('quota')) {
433 1
			return;
434
		}
435
		//can be null
436 14
		$quotaDefault = $this->connection->ldapQuotaDefault;
0 ignored issues
show
Documentation introduced by
The property ldapQuotaDefault does not exist on object<OCA\user_ldap\lib\Connection>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
437 14
		$quota = $quotaDefault !== '' ? $quotaDefault : null;
438 14
		$quota = !is_null($valueFromLDAP) ? $valueFromLDAP : $quota;
439
440 14 View Code Duplication
		if(is_null($valueFromLDAP)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
441 13
			$quotaAttribute = $this->connection->ldapQuotaAttribute;
0 ignored issues
show
Documentation introduced by
The property ldapQuotaAttribute does not exist on object<OCA\user_ldap\lib\Connection>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
442 13
			if(!empty($quotaAttribute)) {
443 4
				$aQuota = $this->access->readAttribute($this->dn, $quotaAttribute);
444 4
				if($aQuota && (count($aQuota) > 0)) {
445 2
					$quota = $aQuota[0];
446 2
				}
447 4
			}
448 13
		}
449 14
		if(!is_null($quota)) {
450 4
			$this->config->setUserValue($this->uid, 'files', 'quota', $quota);
451 4
		}
452 14
	}
453
454
	/**
455
	 * @brief attempts to get an image from LDAP and sets it as ownCloud avatar
456
	 * @return null
457
	 */
458 10
	public function updateAvatar() {
459 10
		if($this->wasRefreshed('avatar')) {
460 1
			return;
461
		}
462 10
		$avatarImage = $this->getAvatarImage();
463 10
		if($avatarImage === false) {
464
			//not set, nothing left to do;
465 8
			return;
466
		}
467 2
		$this->image->loadFromBase64(base64_encode($avatarImage));
468 2
		$this->setOwnCloudAvatar();
469 2
	}
470
471
	/**
472
	 * @brief sets an image as ownCloud avatar
473
	 * @return null
474
	 */
475 2
	private function setOwnCloudAvatar() {
476 2 View Code Duplication
		if(!$this->image->valid()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
477
			$this->log->log('user_ldap', 'jpegPhoto data invalid for '.$this->dn,
478
				\OCP\Util::ERROR);
0 ignored issues
show
Unused Code introduced by
The call to LogWrapper::log() has too many arguments starting with \OCP\Util::ERROR.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
479
			return;
480
		}
481
		//make sure it is a square and not bigger than 128x128
482 2
		$size = min(array($this->image->width(), $this->image->height(), 128));
483 2 View Code Duplication
		if(!$this->image->centerCrop($size)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
484
			$this->log->log('user_ldap',
485
				'croping image for avatar failed for '.$this->dn,
486
				\OCP\Util::ERROR);
0 ignored issues
show
Unused Code introduced by
The call to LogWrapper::log() has too many arguments starting with \OCP\Util::ERROR.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
487
			return;
488
		}
489
490 2
		if(!$this->fs->isLoaded()) {
491
			$this->fs->setup($this->uid);
492
		}
493
494 2
		$avatar = $this->avatarManager->getAvatar($this->uid);
495
		try {
496 2
			$avatar->set($this->image);
497 2
		} catch (\Exception $e) {
498
			\OC::$server->getLogger()->notice(
499
				'Could not set avatar for ' . $this->dn	. ', because: ' . $e->getMessage(),
500
				['app' => 'user_ldap']);
501
		}
502 2
	}
503
504
}
505