Completed
Push — master ( 527ecb...217492 )
by René
04:32
created

PollService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 8
ccs 0
cts 8
cp 0
crap 2
rs 10
c 0
b 0
f 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
namespace OCA\Polls\Service;
25
26
use OCP\IGroupManager;
27
28
use OCA\Polls\Db\PollMapper;
29
use OCA\Polls\Db\ShareMapper;
30
31
class PollService {
32
	private $mapper;
33
	private $shareMapper;
34
	private $groupManager;
35
36
	public function __construct(
37
		PollMapper $mapper,
38
		ShareMapper $shareMapper,
39
		IGroupManager $groupManager
40
	) {
41
		$this->mapper = $mapper;
42
		$this->shareMapper = $shareMapper;
43
		$this->groupManager = $groupManager;
44
	}
45
46
	/**
47
	 * Check if current user is in the access list
48
	 * @param Array $accessList
49
	 * @return bool
50
	 */
51
	public function checkUserAccess($accessList) {
52
		foreach ($accessList as $accessItem) {
53
			if ($accessItem['type'] === 'user' && $accessItem['id'] === \OC::$server->getUserSession()->getUser()->getUID()) {
54
				return true;
55
			}
56
		}
57
58
		return false;
59
	}
60
61
	/**
62
	 * Check If current user is member of a group in the access list
63
	 * @param Array $accessList
64
	 * @return bool
65
	 */
66
	public function checkGroupAccess($accessList) {
67
		foreach ($accessList as $accessItem) {
68
			if ($accessItem['type'] === 'group' && $this->groupManager->isInGroup(\OC::$server->getUserSession()->getUser()->getUID(), $accessItem['id'])) {
69
				return true;
70
			}
71
		}
72
73
		return false;
74
	}
75
76
	/**
77
	 * Set the access right of the current user for the poll
78
	 * @param Array $poll
79
	 * @param Array $shares
80
	 * @return String
81
	 */
82
	public function grantAccessAs($pollId, $currentUser = '') {
83
		if (\OC::$server->getUserSession()->isLoggedIn()) {
84
			$currentUser = \OC::$server->getUserSession()->getUser()->getUID();
85
		}
86
87
		$poll = $this->mapper->find($pollId);
88
89
		$grantAccessAs = 'none';
90
91
		if ($poll->getOwner() === $currentUser) {
92
			$grantAccessAs = 'owner';
93
		} elseif ($poll->getAccess() === 'public') {
94
			$grantAccessAs = 'public';
95
		} elseif ($poll->getAccess() === 'registered' && \OC::$server->getUserSession()->isLoggedIn()) {
96
			$grantAccessAs = 'registered';
97
		} elseif ($poll->getAccess() === 'hidden' && ($poll->getowner() === \OC::$server->getUserSession()->getUser())) {
98
			$grantAccessAs = 'hidden';
99
		// } elseif ($this->checkUserAccess($shares)) {
100
		// 	$grantAccessAs = 'userInvitation';
101
		// } elseif ($this->checkGroupAccess($shares)) {
102
		// 	$grantAccessAs = 'groupInvitation';
103
		} elseif ($this->groupManager->isAdmin($currentUser)) {
104
			$grantAccessAs = 'admin';
105
		}
106
107
		return $grantAccessAs;
108
	}
109
110
}
111