1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* @copyright Copyright (c) 2018, Michael Weimann <[email protected]> |
6
|
|
|
* |
7
|
|
|
* @author Michael Weimann <[email protected]> |
8
|
|
|
* |
9
|
|
|
* @license AGPL-3.0 |
10
|
|
|
* |
11
|
|
|
* This code is free software: you can redistribute it and/or modify |
12
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
13
|
|
|
* as published by the Free Software Foundation. |
14
|
|
|
* |
15
|
|
|
* This program is distributed in the hope that it will be useful, |
16
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18
|
|
|
* GNU Affero General Public License for more details. |
19
|
|
|
* |
20
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
21
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace OC\Avatar; |
25
|
|
|
|
26
|
|
|
use OCP\Files\SimpleFS\InMemoryFile; |
27
|
|
|
use OCP\ILogger; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* This class represents a guest user's avatar. |
31
|
|
|
*/ |
32
|
|
|
class GuestAvatar extends Avatar { |
33
|
|
|
/** |
34
|
|
|
* Holds the guest user display name. |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $userDisplayName; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* GuestAvatar constructor. |
42
|
|
|
* |
43
|
|
|
* @param string $userDisplayName The guest user display name |
44
|
|
|
* @param ILogger $logger The logger |
45
|
|
|
*/ |
46
|
|
|
public function __construct(string $userDisplayName, ILogger $logger) { |
47
|
|
|
parent::__construct($logger); |
48
|
|
|
$this->userDisplayName = $userDisplayName; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Tests if the user has an avatar. |
53
|
|
|
* |
54
|
|
|
* @return true Guests always have an avatar. |
55
|
|
|
*/ |
56
|
|
|
public function exists() { |
57
|
|
|
return true; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Returns the guest user display name. |
62
|
|
|
* |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
public function getDisplayName(): string { |
66
|
|
|
return $this->userDisplayName; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Setting avatars isn't implemented for guests. |
71
|
|
|
* |
72
|
|
|
* @param \OCP\IImage|resource|string $data |
73
|
|
|
* @return void |
74
|
|
|
*/ |
75
|
|
|
public function set($data) { |
76
|
|
|
// unimplemented for guest user avatars |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Removing avatars isn't implemented for guests. |
81
|
|
|
*/ |
82
|
|
|
public function remove() { |
83
|
|
|
// unimplemented for guest user avatars |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Generates an avatar for the guest. |
88
|
|
|
* |
89
|
|
|
* @param int $size The desired image size. |
90
|
|
|
* @return InMemoryFile |
91
|
|
|
*/ |
92
|
|
|
public function getFile($size) { |
93
|
|
|
$avatar = $this->generateAvatar($this->userDisplayName, $size); |
94
|
|
|
return new InMemoryFile('avatar.png', $avatar); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Updates the display name if changed. |
99
|
|
|
* |
100
|
|
|
* @param string $feature The changed feature |
101
|
|
|
* @param mixed $oldValue The previous value |
102
|
|
|
* @param mixed $newValue The new value |
103
|
|
|
* @return void |
104
|
|
|
*/ |
105
|
|
|
public function userChanged($feature, $oldValue, $newValue) { |
106
|
|
|
if ($feature === 'displayName') { |
107
|
|
|
$this->userDisplayName = $newValue; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Guests don't have custom avatars. |
113
|
|
|
* |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
|
|
public function isCustomAvatar(): bool { |
117
|
|
|
return false; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: