Passed
Pull Request — master (#1254)
by René
04:14
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
			'URL' => $this->getURL()
100
		];
101
	}
102
103
	/**
104
	 * @return string
105
	 */
106
	public function getURL() {
107
		if ($this->type === self::TYPE_USER || $this->type === self::TYPE_GROUP) {
108
			return \OC::$server->getUrlGenerator()->linkToRouteAbsolute(
109
				'polls.page.vote',
110
				['id' => $this->pollId]
111
			);
112
		} else {
113
			return \OC::$server->getUrlGenerator()->linkToRouteAbsolute(
114
				'polls.page.vote_publicpublic',
115
				['token' => $this->token]
116
			);
117
		}
118
	}
119
120
	/**
121
	 * getUserId
122
	 * @return string
123
	 */
124
	public function getUserId() {
125
		if ($this->type === self::TYPE_CONTACTGROUP) {
126
			// contactsgroup had the prefix contactgroup_ until version 1.5
127
			// strip it out
128
			$parts = explode("contactgroup_", $this->userId);
129
			$userId = end($parts);
130
			return $userId;
131
		}
132
		return $this->userId;
133
	}
134
135
	/**
136
	 * getUserObject
137
	 * @return UserGroupClass
138
	 */
139
	public function getUserObject() {
140
		return UserGroupClass::getUserGroupChild(
141
			$this->type,
142
			$this->userId,
143
			$this->displayName,
144
			$this->emailAddress
145
		);
146
	}
147
148
	/**
149
	 * getMembers
150
	 * @return UserGroupClass[]
151
	 */
152
	public function getMembers() {
153
		if ($this->type === self::TYPE_GROUP
154
		|| $this->type === self::TYPE_CONTACTGROUP
155
		|| $this->type === self::TYPE_CIRCLE) {
156
			$group = UserGroupClass::getUserGroupChild($this->type, $this->getUserId());
157
			return $group->getMembers();
158
		} else {
159
			return [UserGroupClass::getUserGroupChild(
160
				$this->type,
161
				$this->userId,
162
				$this->displayName,
163
				$this->emailAddress
164
			)];
165
		}
166
	}
167
}
168