Passed
Pull Request — master (#1234)
by René
03:57
created

Share::getValidAuthenticated()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 5
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 12
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
namespace OCA\Polls\Db;
25
26
use JsonSerializable;
27
28
use OCP\AppFramework\Db\Entity;
29
use OCA\Polls\Model\UserGroupClass;
30
31
/**
32
 * @method string getId()
33
 * @method void setId(integer $value)
34
 * @method string getToken()
35
 * @method void setToken(string $value)
36
 * @method string getType()
37
 * @method void setType(string $value)
38
 * @method int getPollId()
39
 * @method void setPollId(integer $value)
40
 * @method string getUserId()
41
 * @method void setUserId(string $value)
42
 * @method string getEmailAddress()
43
 * @method void setEmailAddress(string $value)
44
 * @method int getInvitationSent()
45
 * @method void setInvitationSent(integer $value)
46
 * @method int getDisplayName()
47
 * @method void setDisplayName(string $value)
48
 */
49
class Share extends Entity implements JsonSerializable {
50
51
	// Only authenticated access
52
	public const TYPE_USER = 'user';
53
	public const TYPE_GROUP = 'group';
54
55
	// Public and authenticated Access
56
	public const TYPE_PUBLIC = 'public';
57
58
	// Only public access
59
	public const TYPE_EMAIL = 'email';
60
	public const TYPE_CONTACT = 'contact';
61
	public const TYPE_EXTERNAL = 'external';
62
63
	// no direct Access
64
	public const TYPE_CIRCLE = 'circle';
65
	public const TYPE_CONTACTGROUP = 'contactGroup';
66
67
	/** @var string $token */
68
	protected $token;
69
70
	/** @var string $type */
71
	protected $type;
72
73
	/** @var int $pollId */
74
	protected $pollId;
75
76
	/** @var string $userId */
77
	protected $userId;
78
79
	/** @var string $emailAddress */
80
	protected $emailAddress;
81
82
	/** @var string $invitationSent */
83
	protected $invitationSent;
84
85
	/** @var string $displayName */
86
	protected $displayName;
87
88
	public function jsonSerialize() {
89
		return [
90
			'id' => intval($this->id),
91
			'token' => $this->token,
92
			'type' => $this->type,
93
			'pollId' => intval($this->pollId),
94
			'userId' => $this->getUserId(),
95
			'emailAddress' => $this->emailAddress,
96
			'invitationSent' => intval($this->invitationSent),
97
			'displayName' => $this->displayName,
98
			'isNoUser' => !($this->type === self::TYPE_USER),
99
			'validPublic' => $this->getValidPublic(),
100
			'validAuthenticated' => $this->getValidAuthenticated(),
101
			'URL' => $this->getURL()
102
		];
103
	}
104
105
	public function getURL() {
106
		if ($this->type === self::TYPE_USER || $this->type === self::TYPE_GROUP) {
107
			return \OC::$server->getUrlGenerator()->linkToRouteAbsolute(
108
				'polls.page.vote',
109
				['id' => $this->pollId]
110
			);
111
		} else {
112
			return \OC::$server->getUrlGenerator()->linkToRouteAbsolute(
113
				'polls.page.vote_publicpublic',
114
				['token' => $this->token]
115
			);
116
		}
117
	}
118
	public function getUserId() {
119
		if ($this->type === self::TYPE_CONTACTGROUP) {
120
			// contactsgroup had the prefix contactgroup_ until version 1.5
121
			// strip it out
122
			$parts = explode("contactgroup_", $this->userId);
123
			$userId = end($parts);
124
			return $userId;
125
		}
126
		return $this->userId;
127
	}
128
129
	/**
130
	 * @return UserGroupClass
131
	 */
132
	public function getUserObject() {
133
		return UserGroupClass::getUserGroupChild(
134
			$this->type,
135
			$this->userId,
136
			$this->displayName,
137
			$this->emailAddress
138
		);
139
	}
140
141
	/**
142
	 * @return UserGroupClass[]
143
	 */
144
	public function getMembers() {
145
		if ($this->type === self::TYPE_GROUP
146
		|| $this->type === self::TYPE_CONTACTGROUP
147
		|| $this->type === self::TYPE_CIRCLE) {
148
			$group = UserGroupClass::getUserGroupChild($this->type, $this->getUserId());
149
			return $group->getMembers();
150
		} else {
151
			return [UserGroupClass::getUserGroupChild(
152
				$this->type,
153
				$this->userId,
154
				$this->displayName,
155
				$this->emailAddress
156
			)];
157
		}
158
	}
159
160
	/**
161
	 * @return bool
162
	 */
163
	public function getValidPublic() {
164
		return (
165
			   $this->type === self::TYPE_PUBLIC
166
			|| $this->type === self::TYPE_EMAIL
167
			|| $this->type === self::TYPE_CONTACT
168
			|| $this->type === self::TYPE_EXTERNAL);
169
	}
170
171
	/**
172
	 * @return bool
173
	 */
174
	public function getValidAuthenticated() {
175
		return (
176
			   $this->type === self::TYPE_PUBLIC
177
			|| $this->type === self::TYPE_USER
178
			|| $this->type === self::TYPE_GROUP);
179
	}
180
}
181