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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\UsersManager\tests\Integration;
use Piwik\Access;
use Piwik\Plugins\UsersManager\Model;
use Piwik\Plugins\UsersManager\UserAccessFilter;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\Mock\FakeAccess;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
/**
* @group UsersManager
* @group UserAccessFilterTest
* @group UserAccessFilter
* @group Plugins
*/
class UserAccessFilterTest extends IntegrationTestCase
{
/**
* @var Model
*/
private $model;
/**
* @var Access
*/
private $access;
/**
* @var UserAccessFilter
*/
private $filter;
/**
* @var \ReflectionMethod
*/
private $isNonSuperUserAllowedToSeeThisLogin;
private static $users = [
'login2' => ['view' => [1,3,5], 'write' => [], 'admin' => [2,6]],
'login3' => ['view' => [], 'write' => [], 'admin' => []], // no access to any site
'login4' => ['view' => [6], 'write' => [], 'admin' => []], // only access to one with view
'login5' => ['view' => [], 'write' => [], 'admin' => [3]], // only access to one with admin
'login6' => ['view' => [], 'write' => [], 'admin' => [6,3]], // access to a couple of sites with admin
'login7' => ['view' => [2,1,6,3], 'write' => [], 'admin' => []], // access to a couple of sites with view
'login8' => ['view' => [4,7], 'write' => [], 'admin' => [2,5]], // access to a couple of sites with admin and view
'login9' => ['view' => [], 'write' => [2,5], 'admin' => []], // access to a couple of sites with write
'login10' => ['view' => [1,3], 'write' => [6], 'admin' => []], // access to a couple of sites with write and view
];
public function setUp(): void
{
parent::setUp();
// set up your test here if needed
$this->model = new Model();
$this->access = new FakeAccess();
FakeAccess::clearAccess();
$this->filter = new UserAccessFilter($this->model, $this->access);
$method = new \ReflectionMethod($this->filter, 'isNonSuperUserAllowedToSeeThisLogin');
if (PHP_VERSION_ID < 80100) {
$method->setAccessible(true);
}
$this->isNonSuperUserAllowedToSeeThisLogin = $method;
}
protected static function beforeTableDataCached()
{
parent::beforeTableDataCached();
self::createManyWebsites();
self::createManyUsers();
}
public function testFilterUserWithSuperUserAccessShouldAlwaysReturnTrue()
{
$this->configureAccessForLogin('login1');
foreach ($this->getAllLogins() as $login) {
$this->assertSame(['login' => $login], $this->filter->filterUser(['login' => $login]));
}
}
public function testFilterUserWithViewUserAccessShouldOnlyReturnUserForOwnLogin()
{
$identity = 'login4';
$this->configureAccessForLogin($identity);
$this->assertSame(['login' => $identity], $this->filter->filterUser(['login' => $identity]));
foreach ($this->getAllLogins() as $login) {
if ($login !== $identity) {
$this->assertNull($this->filter->filterUser(['login' => $login]));
}
}
}
/**
* @dataProvider getIsUserAllowedToSeeThisLoginWithAdminAccess
*/
public function testFilterUserWithAdminAccessShouldOnlyReturnUserForOwnLogin($expectedAllowed, $loginToSee)
{
$this->configureAccessForLogin('login2');
if ($expectedAllowed) {
$this->assertSame(['login' => $loginToSee], $this->filter->filterUser(['login' => $loginToSee]));
} else {
$this->assertSame(null, $this->filter->filterUser(['login' => $loginToSee]));
}
}
/**
* @dataProvider getIsUserAllowedToSeeThisLoginWithAdminAccess
*/
public function testIsNonSuperUserAllowedToSeeThisLoginWithAdminAccessIsAllowedToSeeAnyUserHavingAccessToSameAdminSites($expectedAllowed, $loginToSee)
{
$this->configureAccessForLogin('login2');
$this->assertSame($expectedAllowed, $this->isNonSuperUserAllowedToSeeThisLogin->invoke($this->filter, $loginToSee));
}
public function getIsUserAllowedToSeeThisLoginWithAdminAccess()
{
return array(
array($expectedAllowed = false, 'login1'), // not allowed to see this user as it has super user access
array($expectedAllowed = true, 'login10'),
array($expectedAllowed = true, 'login2'), // it is the own user so visible anyway
array($expectedAllowed = false, 'login3'), // not allowed to see this user as this one does not have access to any site
array($expectedAllowed = true, 'login4'),
array($expectedAllowed = false, 'login5'), // this user doesn't share any site id where the user has admin access
array($expectedAllowed = true, 'login6'),
array($expectedAllowed = true, 'login7'),
array($expectedAllowed = true, 'login8'),
array($expectedAllowed = true, 'login9'),
);
}
public function testIsNonSuperUserAllowedToSeeThisLoginWithAdminAccessIsAllowedToSeeAnyUserHavingAccessToSameAdminSitesUserHasAccessToOnlyOneAdminSite()
{
$this->configureAccessForLogin('login5');
$this->assertTrue($this->isNonSuperUserAllowedToSeeThisLogin->invoke($this->filter, 'login2'));
$this->assertTrue($this->isNonSuperUserAllowedToSeeThisLogin->invoke($this->filter, 'login5'));
$this->assertTrue($this->isNonSuperUserAllowedToSeeThisLogin->invoke($this->filter, 'login7'));
$this->assertTrue($this->isNonSuperUserAllowedToSeeThisLogin->invoke($this->filter, 'login6'));
$this->assertTrue($this->isNonSuperUserAllowedToSeeThisLogin->invoke($this->filter, 'login10'));
$this->assertFalse($this->isNonSuperUserAllowedToSeeThisLogin->invoke($this->filter, 'login1'));
$this->assertFalse($this->isNonSuperUserAllowedToSeeThisLogin->invoke($this->filter, 'login3'));
$this->assertFalse($this->isNonSuperUserAllowedToSeeThisLogin->invoke($this->filter, 'login4'));
$this->assertFalse($this->isNonSuperUserAllowedToSeeThisLogin->invoke($this->filter, 'login8'));
$this->assertFalse($this->isNonSuperUserAllowedToSeeThisLogin->invoke($this->filter, 'login9'));
}
public function testIsNonSuperUserWithOnlyViewAccessAllowedToSeeOnlyOwnUser()
{
$this->configureAccessForLogin('login7');
$this->assertTrue($this->isNonSuperUserAllowedToSeeThisLogin->invoke($this->filter, 'login7'));
$this->assertFalse($this->isNonSuperUserAllowedToSeeThisLogin->invoke($this->filter, 'login1'));
$this->assertFalse($this->isNonSuperUserAllowedToSeeThisLogin->invoke($this->filter, 'login2'));
$this->assertFalse($this->isNonSuperUserAllowedToSeeThisLogin->invoke($this->filter, 'login3'));
$this->assertFalse($this->isNonSuperUserAllowedToSeeThisLogin->invoke($this->filter, 'login4'));
$this->assertFalse($this->isNonSuperUserAllowedToSeeThisLogin->invoke($this->filter, 'login5'));
$this->assertFalse($this->isNonSuperUserAllowedToSeeThisLogin->invoke($this->filter, 'login6'));
$this->assertFalse($this->isNonSuperUserAllowedToSeeThisLogin->invoke($this->filter, 'login8'));
$this->assertFalse($this->isNonSuperUserAllowedToSeeThisLogin->invoke($this->filter, 'login9'));
$this->assertFalse($this->isNonSuperUserAllowedToSeeThisLogin->invoke($this->filter, 'login10'));
}
public function testIsNonSuperUserWithoutAnyAccessAllowedToSeeOnlyOwnUser()
{
$this->configureAccessForLogin('login3');
$this->assertTrue($this->isNonSuperUserAllowedToSeeThisLogin->invoke($this->filter, 'login3'));
$this->assertFalse($this->isNonSuperUserAllowedToSeeThisLogin->invoke($this->filter, 'login1'));
$this->assertFalse($this->isNonSuperUserAllowedToSeeThisLogin->invoke($this->filter, 'login2'));
$this->assertFalse($this->isNonSuperUserAllowedToSeeThisLogin->invoke($this->filter, 'login4'));
$this->assertFalse($this->isNonSuperUserAllowedToSeeThisLogin->invoke($this->filter, 'login5'));
$this->assertFalse($this->isNonSuperUserAllowedToSeeThisLogin->invoke($this->filter, 'login7'));
$this->assertFalse($this->isNonSuperUserAllowedToSeeThisLogin->invoke($this->filter, 'login6'));
$this->assertFalse($this->isNonSuperUserAllowedToSeeThisLogin->invoke($this->filter, 'login8'));
$this->assertFalse($this->isNonSuperUserAllowedToSeeThisLogin->invoke($this->filter, 'login9'));
$this->assertFalse($this->isNonSuperUserAllowedToSeeThisLogin->invoke($this->filter, 'login10'));
}
/**
* @dataProvider getTestFilterLogins
*/
public function testFilterLogins($expectedLogins, $loginIdentity, $logins)
{
$this->configureAccessForLogin($loginIdentity);
$this->assertSame($expectedLogins, $this->filter->filterLogins($logins));
}
/**
* @dataProvider getTestFilterLogins
*/
public function testFilterUsers($expectedLogins, $loginIdentity, $logins)
{
$this->configureAccessForLogin($loginIdentity);
$users = [];
$expectedUsers = [];
foreach ($logins as $login) {
$user = ['login' => $login, 'password' => md5('pass')];
$users[] = $user;
if (in_array($login, $expectedLogins)) {
$expectedUsers[] = $user;
}
}
$this->assertSame($expectedUsers, $this->filter->filterUsers($users)); // a view user is allowed to see itself
}
/**
* @dataProvider getTestFilterLogins
*/
public function testFilterLoginIndexedArray($expectedLogins, $loginIdentity, $logins)
{
$this->configureAccessForLogin($loginIdentity);
$testArray = [];
$expectedTestArray = [];
foreach ($logins as $login) {
$anything = ['foo' . $login];
$testArray[$login] = $anything;
if (in_array($login, $expectedLogins)) {
$expectedTestArray[$login] = $anything;
}
}
$this->assertSame($expectedTestArray, $this->filter->filterLoginIndexedArray($testArray));
}
public function getTestFilterLogins()
{
return [
[$expectedLogins = $this->getAllLogins(), $identity = 'login1', $this->getAllLogins()], // a super user is allowed to see all logins
[$expectedLogins = ['login2', 'foobar'], $identity = 'login1', ['login2', 'foobar']], // for super users we do not even check if they actually exist
[$expectedLogins = $this->buildLogins([2,4]), $identity = 'login2', ['login2', 'foobar', 'login4', 'login3']], // should remove logins that do not actually exist when user has admin permission
[$expectedLogins = $this->buildLogins([10,2,4,6,7,8,9]), $identity = 'login2', $this->getAllLogins()], // an admin user can see users having access to the admin sites
[$expectedLogins = $this->buildLogins([3]), $identity = 'login3', $this->getAllLogins()], // a user with no access to any site can only see itself
[$expectedLogins = ['foobar'], $identity = 'foobar', ['foobar']], // doesn't check whether user exists when not having access to any site and user doesn't actually exist
[$expectedLogins = $this->buildLogins([4]), $identity = 'login4', $this->getAllLogins()], // a user with only view access to a site can only see itself
[$expectedLogins = $this->buildLogins([10,2,5,6,7]), $identity = 'login5', $this->getAllLogins()], // has access to one admin site
[$expectedLogins = $this->buildLogins([10,2,4,5,6,7]), $identity = 'login6', $this->getAllLogins()], // has access to multiple admin sites
[$expectedLogins = $this->buildLogins([7]), $identity = 'login7', $this->getAllLogins()], // has only access to multiple view sites
[$expectedLogins = $this->buildLogins([2,7,8,9]), $identity = 'login8', $this->getAllLogins()], // has access to multiple view & admin sites
[$expectedLogins = $this->buildLogins([9]), $identity = 'login9', $this->getAllLogins()], // a user with write access only can only see itself
[$expectedLogins = $this->buildLogins([10]), $identity = 'login10', $this->getAllLogins()], // a user with view and write access to a site can only see itself
[$expectedLogins = [], $identity = 'login1', []], // no users given, should return empty array for user with super user access
[$expectedLogins = [], $identity = 'login2', []], // no users given, should return empty array for user with admin access
[$expectedLogins = [], $identity = 'login9', []], // no users given, should return empty array for user with write access
[$expectedLogins = [], $identity = 'login3', []], // no users given, should return empty array for user with no access
[$expectedLogins = [], $identity = 'login4', []], // no users given, should return empty array for user with only view access
[$expectedLogins = ['anonymous'], $identity = 'anonymous', ['anonymous']], // anonymous user can see itself
];
}
public function testGetAllLoginsShouldBeUpToDate()
{
$this->assertSame($this->model->getUsersLogin(), $this->getAllLogins());
$this->assertNotEmpty($this->getAllLogins());
}
public function testBuildLogins()
{
$this->assertSame(['login2', 'login3', 'login7'], $this->buildLogins([2,3,7]));
$this->assertSame([], $this->buildLogins([]));
}
private static function createManyWebsites()
{
for ($i = 0; $i < 10; $i++) {
Fixture::createWebsite('2014-01-01 00:00:00');
}
}
private function buildLogins($ids)
{
$logins = [];
foreach ($ids as $id) {
$logins[] = 'login' . $id;
}
return $logins;
}
private function getAllLogins()
{
$logins = $this->buildLogins([1, 10, 2,3, 4, 5, 6, 7, 8, 9]);
array_unshift($logins, 'anonymous');
return $logins;
}
private static function createManyUsers()
{
$model = new Model();
$model->addUser('login1', md5('pass'), 'email1@example.com', '2008-01-01 00:00:00');
$model->addUser('login2', md5('pass'), 'email2@example.com', '2008-01-01 00:00:00');
// login3 won't have access to any site
$model->addUser('login3', md5('pass'), 'email3@example.com', '2008-01-01 00:00:00');
$model->addUser('login4', md5('pass'), 'email4@example.com', '2008-01-01 00:00:00');
$model->addUser('login5', md5('pass'), 'email5@example.com', '2008-01-01 00:00:00');
$model->addUser('login6', md5('pass'), 'email6@example.com', '2008-01-01 00:00:00');
$model->addUser('login7', md5('pass'), 'email7@example.com', '2008-01-01 00:00:00');
$model->addUser('login8', md5('pass'), 'email8@example.com', '2008-01-01 00:00:00');
$model->addUser('login9', md5('pass'), 'email9@example.com', '2008-01-01 00:00:00');
$model->addUser('login10', md5('pass'), 'email10@example.com', '2008-01-01 00:00:00');
$model->addUser('anonymous', '', 'ano@example.com', '2008-01-01 00:00:00');
$model->setSuperUserAccess('login1', true); // we treat this one as our superuser
foreach (self::$users as $login => $permissions) {
foreach ($permissions as $access => $idSites) {
$model->addUserAccess($login, $access, $idSites);
}
}
}
private function configureAccessForLogin($login)
{
$hasSuperUser = false;
$idSitesAdmin = [];
$idSitesWrite = [];
$idSitesView = [];
if ($login === 'login1') {
$hasSuperUser = true;
} elseif (isset(self::$users[$login])) {
$idSitesAdmin = self::$users[$login]['admin'];
$idSitesWrite = self::$users[$login]['write'];
$idSitesView = self::$users[$login]['view'];
}
FakeAccess::clearAccess($hasSuperUser, $idSitesAdmin, $idSitesView, $login, $idSitesWrite);
}
}
|