Passed
Pull Request — master (#1128)
by René
06:43
created

User::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
eloc 10
dl 0
loc 11
rs 9.9332
c 2
b 0
f 2
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <[email protected]>
4
 *
5
 * @author René Gieling <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 *  This program is free software: you can redistribute it and/or modify
10
 *  it under the terms of the GNU Affero General Public License as
11
 *  published by the Free Software Foundation, either version 3 of the
12
 *  License, or (at your option) any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU Affero General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU Affero General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
25
namespace OCA\Polls\Model;
26
27
use OCP\IUser;
28
use OCA\Polls\Interfaces\IUserObj;
29
30
class User implements \JsonSerializable, IUserObj {
31
	public const TYPE = 'user';
32
	public const TYPE_USER = 'user';
33
	public const TYPE_GROUP = 'group';
34
	public const TYPE_CONTACTGROUP = 'contactGroup';
35
	public const TYPE_CONTACT = 'contact';
36
	public const TYPE_EMAIL = 'email';
37
	public const TYPE_CIRCLE = 'circle';
38
	public const TYPE_EXTERNAL = 'external';
39
	public const TYPE_INVALID = 'invalid';
40
41
	/** @var string */
42
	private $id;
43
44
	/** @var IUser */
45
	private $user;
46
47
	/**
48
	 * User constructor.
49
	 * @param $type
50
	 * @param $id
51
	 * @param $emailAddress
52
	 * @param $displayName
53
	 */
54
	public function __construct(
55
		$id
56
	) {
57
		$this->id = $id;
58
		$this->load();
59
	}
60
61
	/**
62
	 * Get userId
63
	 * @NoAdminRequired
64
	 * @return String
65
	 */
66
	public function getUserId() {
67
		return $this->id;
68
	}
69
70
	/**
71
	 * Get userId
72
	 * @NoAdminRequired
73
	 * @return String
74
	 */
75
	public function getId() {
76
		return $this->id;
77
	}
78
79
	/**
80
	 * getUser
81
	 * @NoAdminRequired
82
	 * @return String
83
	 */
84
	public function getUser() {
85
		return $this->id;
86
	}
87
88
	/**
89
	 * Get user type
90
	 * @NoAdminRequired
91
	 * @return String
92
	 */
93
	public function getType() {
94
		return self::TYPE;
95
	}
96
97
	/**
98
	 * @NoAdminRequired
99
	 * @return String
100
	 */
101
	public function getLanguage() {
102
		return \OC::$server->getConfig()->getUserValue($this->id, 'core', 'lang');
103
	}
104
105
	/**
106
	 * Get displayName
107
	 * @NoAdminRequired
108
	 * @return String
109
	 */
110
	public function getDisplayName() {
111
		return \OC::$server->getUserManager()->get($this->id)->getDisplayName();
112
	}
113
114
	/**
115
	 * @NoAdminRequired
116
	 * @return String
117
	 */
118
	public function getOrganisation() {
119
		return '';
120
	}
121
122
	/**
123
	 * Get email address
124
	 * @NoAdminRequired
125
	 * @return String
126
	 */
127
	public function getEmailAddress() {
128
		return $this->user->getEMailAddress();
129
	}
130
131
	/**
132
	 * Get additional description, if available
133
	 * @NoAdminRequired
134
	 * @return String
135
	 */
136
	public function getDescription() {
137
		return \OC::$server->getL10N('polls')->t('User');
138
	}
139
140
	/**
141
	 * Get icon class
142
	 * @NoAdminRequired
143
	 * @return String
144
	 */
145
	public function getIcon() {
146
		return 'icon-user';
147
	}
148
149
	/**
150
	 * Get icon class
151
	 * @NoAdminRequired
152
	 * @return String
153
	 */
154
	public function getUserIsDisabled() {
155
		return !\OC::$server->getUserManager()->get($this->id)->isEnabled();
156
	}
157
158
	/**
159
	 * listRaw
160
	 * @NoAdminRequired
161
	 * @param string $query
162
	 * @return Array
163
	 */
164
	public static function listRaw($query = '') {
165
		return \OC::$server->getUserManager()->search($query);
166
	}
167
168
	/**
169
	 * search
170
	 * @NoAdminRequired
171
	 * @param string $query
172
	 * @param array $skip - group names to skip in return array
173
	 * @return Group[]
174
	 */
175
	public static function search($query = '', $skip = []) {
176
		$users = [];
177
		foreach (self::listRaw($query) as $user) {
178
			if (!in_array($user->getUID(), $skip)) {
179
				$users[] = new Self($user->getUID());
180
			}
181
		}
182
		return $users;
183
	}
184
185
	private function load() {
186
		$this->user = \OC::$server->getUserManager()->get($this->id);
187
	}
188
189
	/**
190
	 * @return array
191
	 */
192
	public function jsonSerialize(): array {
193
		return	[
194
			'user'          => $this->id,
195
			'id'        	=> $this->id,
196
			'userId'        => $this->id,
197
			'type'       	=> $this->getType(),
198
			'displayName'	=> $this->getDisplayName(),
199
			'organisation'	=> $this->getOrganisation(),
200
			'emailAddress'	=> $this->getEmailAddress(),
201
			'desc' 			=> $this->getDescription(),
202
			'icon'			=> $this->getIcon(),
203
		];
204
	}
205
}
206