Passed
Push — master ( 1738e1...673267 )
by Morris
36:22 queued 25:43
created

AvatarManager::getAvatar()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 1
dl 0
loc 16
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2016, ownCloud, Inc.
7
 *
8
 * @author Arthur Schiwon <[email protected]>
9
 * @author John Molakvoæ (skjnldsv) <[email protected]>
10
 * @author Lukas Reschke <[email protected]>
11
 * @author Michael Weimann <[email protected]>
12
 * @author Morris Jobke <[email protected]>
13
 * @author Robin Appelman <[email protected]>
14
 * @author Roeland Jago Douma <[email protected]>
15
 * @author Thomas Müller <[email protected]>
16
 *
17
 * @license AGPL-3.0
18
 *
19
 * This code is free software: you can redistribute it and/or modify
20
 * it under the terms of the GNU Affero General Public License, version 3,
21
 * as published by the Free Software Foundation.
22
 *
23
 * This program is distributed in the hope that it will be useful,
24
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26
 * GNU Affero General Public License for more details.
27
 *
28
 * You should have received a copy of the GNU Affero General Public License, version 3,
29
 * along with this program. If not, see <http://www.gnu.org/licenses/>
30
 *
31
 */
32
33
namespace OC\Avatar;
34
35
use OC\User\Manager;
36
use OC\User\NoUserException;
37
use OCP\Files\IAppData;
38
use OCP\Files\NotFoundException;
39
use OCP\Files\NotPermittedException;
40
use OCP\IAvatar;
41
use OCP\IAvatarManager;
42
use OCP\IConfig;
43
use OCP\IL10N;
44
use OCP\ILogger;
45
46
/**
47
 * This class implements methods to access Avatar functionality
48
 */
49
class AvatarManager implements IAvatarManager {
50
51
	/** @var Manager */
52
	private $userManager;
53
54
	/** @var IAppData */
55
	private $appData;
56
57
	/** @var IL10N */
58
	private $l;
59
60
	/** @var ILogger  */
61
	private $logger;
62
63
	/** @var IConfig */
64
	private $config;
65
66
	/**
67
	 * AvatarManager constructor.
68
	 *
69
	 * @param Manager $userManager
70
	 * @param IAppData $appData
71
	 * @param IL10N $l
72
	 * @param ILogger $logger
73
	 * @param IConfig $config
74
	 */
75
	public function __construct(
76
			Manager $userManager,
77
			IAppData $appData,
78
			IL10N $l,
79
			ILogger $logger,
80
			IConfig $config) {
81
		$this->userManager = $userManager;
82
		$this->appData = $appData;
83
		$this->l = $l;
84
		$this->logger = $logger;
85
		$this->config = $config;
86
	}
87
88
	/**
89
	 * return a user specific instance of \OCP\IAvatar
90
	 * @see \OCP\IAvatar
91
	 * @param string $userId the ownCloud user id
92
	 * @return \OCP\IAvatar
93
	 * @throws \Exception In case the username is potentially dangerous
94
	 * @throws NotFoundException In case there is no user folder yet
95
	 */
96
	public function getAvatar(string $userId) : IAvatar {
97
		$user = $this->userManager->get($userId);
98
		if ($user === null) {
99
			throw new \Exception('user does not exist');
100
		}
101
102
		// sanitize userID - fixes casing issue (needed for the filesystem stuff that is done below)
103
		$userId = $user->getUID();
104
105
		try {
106
			$folder = $this->appData->getFolder($userId);
107
		} catch (NotFoundException $e) {
108
			$folder = $this->appData->newFolder($userId);
109
		}
110
111
		return new UserAvatar($folder, $this->l, $user, $this->logger, $this->config);
112
	}
113
114
	/**
115
	 * Clear generated avatars
116
	 */
117
	public function clearCachedAvatars() {
118
		$users = $this->config->getUsersForUserValue('avatar', 'generated', 'true');
119
		foreach ($users as $userId) {
120
			try {
121
				$folder = $this->appData->getFolder($userId);
122
				$folder->delete();
123
			} catch (NotFoundException $e) {
124
				$this->logger->debug("No cache for the user $userId. Ignoring...");
125
			}
126
			$this->config->setUserValue($userId, 'avatar', 'generated', 'false');
127
		}
128
	}
129
130
	public function deleteUserAvatar(string $userId): void {
131
		try {
132
			$folder = $this->appData->getFolder($userId);
133
			$folder->delete();
134
		} catch (NotFoundException $e) {
135
			$this->logger->debug("No cache for the user $userId. Ignoring avatar deletion");
136
		} catch (NotPermittedException $e) {
137
			$this->logger->error("Unable to delete user avatars for $userId. gnoring avatar deletion");
138
		} catch (NoUserException $e) {
139
			$this->logger->debug("User $userId not found. gnoring avatar deletion");
140
		}
141
		$this->config->deleteUserValue($userId, 'avatar', 'generated');
142
	}
143
144
	/**
145
	 * Returns a GuestAvatar.
146
	 *
147
	 * @param string $name The guest name, e.g. "Albert".
148
	 * @return IAvatar
149
	 */
150
	public function getGuestAvatar(string $name): IAvatar {
151
		return new GuestAvatar($name, $this->logger);
152
	}
153
}
154