-
Notifications
You must be signed in to change notification settings - Fork 6
/
Controller.php
161 lines (123 loc) · 4.88 KB
/
Controller.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
/**
* Matomo - free/libre analytics platform
*
* @link http://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\GroupPermissions;
use Piwik\API\Request;
use Piwik\Common;
use Piwik\Piwik;
use Piwik\Plugin\ControllerAdmin;
use Piwik\Site;
use Piwik\Translation\Translator;
use Piwik\View;
class Controller extends ControllerAdmin
{
/**
* @var Translator
*/
private $translator;
public function __construct(Translator $translator)
{
$this->translator = $translator;
parent::__construct();
}
static function orderByName($a, $b)
{
return strcmp($a['name'], $b['name']);
}
/**
* The "Manage Users and Permissions" Admin UI screen
*/
public function index()
{
Piwik::checkUserIsNotAnonymous();
Piwik::checkUserHasSomeAdminAccess();
$view = new View('@GroupPermissions/index');
$view->activeTab = Common::getRequestVar('show', 'access');
$validTabs = array('access', 'groups');
if (!in_array($view->activeTab, $validTabs)) {
$view->activeTab = 'access';
}
$view->hasOnlyAdminAccess = Piwik::isUserHasSomeAdminAccess() && !Piwik::hasUserSuperUserAccess();
if ($view->activeTab === 'access') {
$this->tabAccess($view);
}
else if (Piwik::hasUserSuperUserAccess()) {
if ($view->activeTab === 'groups') {
$this->tabGroups($view);
}
}
$this->setBasicVariablesView($view);
return $view->render();
}
private function tabAccess(&$view)
{
$IdSitesAdmin = Request::processRequest('SitesManager.getSitesIdWithAdminAccess');
$idSiteSelected = 1;
if (count($IdSitesAdmin) > 0) {
$defaultWebsiteId = $IdSitesAdmin[0];
$idSiteSelected = Common::getRequestVar('idSite', $defaultWebsiteId);
}
if ($idSiteSelected === 'all') {
$defaultReportSiteName = $this->translator->translate('UsersManager_ApplyToAllWebsites');
$groups = Request::processRequest('GroupPermissions.getAllGroups');
$groupAccessByWebsite = array();
foreach ($groups as $group) {
$groupAccessByWebsite[$group['name']] = '';
}
} else {
if (!Piwik::isUserHasAdminAccess($idSiteSelected) && count($IdSitesAdmin) > 0) {
// make sure to show a website where user actually has admin access
$idSiteSelected = $IdSitesAdmin[0];
}
$defaultReportSiteName = Site::getNameFor($idSiteSelected);
$groupAccessByWebsite = Request::processRequest('GroupPermissions.getGroupAccessFromSite', array('idSite' => $idSiteSelected));
}
ksort($groupAccessByWebsite);
$view->idSiteSelected = $idSiteSelected;
$view->defaultReportSiteName = $defaultReportSiteName;
$view->groupAccessByWebsite = $groupAccessByWebsite;
$websites = Request::processRequest('SitesManager.getSitesWithAdminAccess');
uasort($websites, array('Piwik\Plugins\GroupPermissions\Controller', 'orderByName'));
$view->websites = $websites;
}
private function tabGroups(&$view)
{
$defaultGroupId = -1;
$rawGroups = Request::processRequest('GroupPermissions.getAllGroups');
$groups = array();
if (!empty($rawGroups)) {
$defaultGroupId = current($rawGroups)['idgroup'];
foreach ($rawGroups as $group) {
$groups[$group['idgroup']] = $group['name'];
}
asort($groups);
}
$idGroup = Common::getRequestVar('idGroup', $defaultGroupId);
$view->groups = $groups;
if ($idGroup > 0) {
$rawGroupUsers = Request::processRequest('GroupPermissions.getMembersOfGroup', array('idGroup' => $idGroup));
$groupUsers = array();
if (!empty($rawGroupUsers)) {
foreach ($rawGroupUsers as $user) {
$groupUsers[] = $user['login'];
}
natcasesort($groupUsers);
}
$view->groupUsers = $groupUsers;
}
else {
$view->groupUsers = array();
}
$allUsers = Request::processRequest('UsersManager.getUsersLogin', ['filter_limit' => '-1']);
// Create a named array using the user login as the key as well as the value
$allUsers = array_combine($allUsers, $allUsers);
$view->users = $allUsers;
$view->selectedIdGroup = $idGroup;
$view->selectedGroupName = isset($groups[$idGroup]) ? $groups[$idGroup] : '';
}
}