Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable10] Admins can change display name when flag set to false #29442

Merged
merged 1 commit into from
Nov 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions lib/private/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ class User implements IUser {
/** @var AccountMapper */
private $mapper;

/** @var \OC\Group\Manager */
private $groupManager;

/** @var Session */
private $userSession;

/**
* User constructor.
*
Expand All @@ -76,9 +82,12 @@ class User implements IUser {
* @param IConfig|null $config
* @param null $urlGenerator
* @param EventDispatcher|null $eventDispatcher
* @param \OC\Group\Manager|null $groupManager
* @param Session|null $userSession
*/
public function __construct(Account $account, AccountMapper $mapper, $emitter = null, IConfig $config = null,
$urlGenerator = null, EventDispatcher $eventDispatcher = null
$urlGenerator = null, EventDispatcher $eventDispatcher = null,
\OC\Group\Manager $groupManager = null, Session $userSession = null
) {
$this->account = $account;
$this->mapper = $mapper;
Expand All @@ -92,6 +101,14 @@ public function __construct(Account $account, AccountMapper $mapper, $emitter =
if (is_null($this->urlGenerator)) {
$this->urlGenerator = \OC::$server->getURLGenerator();
}
$this->groupManager = $groupManager;
if (is_null($this->groupManager)) {
$this->groupManager = \OC::$server->getGroupManager();
}
$this->userSession = $userSession;
if (is_null($this->userSession)) {
$this->userSession = \OC::$server->getUserSession();
}
}

/**
Expand Down Expand Up @@ -307,7 +324,9 @@ public function canChangePassword() {
* @return bool
*/
public function canChangeDisplayName() {
if ($this->config->getSystemValue('allow_user_to_change_display_name') === false) {
if (($this->config->getSystemValue('allow_user_to_change_display_name') === false) &&
(!$this->groupManager->isAdmin($this->userSession->getUser()->getUID())) &&
(!$this->groupManager->getSubAdmin()->isSubAdmin($this->userSession->getUser()))) {
return false;
}
$backend = $this->account->getBackendInstance();
Expand Down
74 changes: 74 additions & 0 deletions tests/lib/User/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@

namespace Test\User;

use OC\Group\Manager;
use OC\Hooks\PublicEmitter;
use OC\SubAdmin;
use OC\User\Account;
use OC\User\AccountMapper;
use OC\User\Backend;
use OC\User\Database;
use OC\User\Session;
use OC\User\User;
use OCP\IConfig;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\User\IChangePasswordBackend;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Test\TestCase;
Expand Down Expand Up @@ -44,6 +48,12 @@ class UserTest extends TestCase {
private $eventDispatcher;
/** @var IURLGenerator | \PHPUnit_Framework_MockObject_MockObject */
private $urlGenerator;
/** @var Manager | \PHPUnit_Framework_MockObject_MockObject */
private $groupManager;
/** @var SubAdmin | \PHPUnit_Framework_MockObject_MockObject */
private $subAdmin;
/** @var Session | \PHPUnit_Framework_MockObject_MockObject */
private $sessionUser;

public function setUp() {
parent::setUp();
Expand All @@ -57,6 +67,9 @@ public function setUp() {
->setMethods(['getAbsoluteURL'])
->disableOriginalConstructor()
->getMock();
$this->groupManager = $this->createMock('\OC\Group\Manager');
$this->subAdmin = $this->createMock('\OC\SubAdmin');
$this->sessionUser = $this->createMock(Session::class);

$this->user = new User($this->account, $this->accountMapper, $this->emitter, $this->config, $this->urlGenerator, $this->eventDispatcher);
}
Expand Down Expand Up @@ -170,6 +183,67 @@ public function providesChangeDisplayName() {
[false, false]
];
}

public function providesAdminOrSubAdmin() {
return [
[false, false, false, false],
[true, false, true, true],
[true, true, true, true],
[true, true, true, true]
];
}

/**
* @dataProvider providesAdminOrSubAdmin
* @param $isAdmin
* @param $isSubaDmin
* @param $expected
* @param $implements
*/
public function testAdminSubadminCanChangeDisplayName($isAdmin, $isSubaDmin, $expected, $implements) {

$this->config->method('getSystemValue')
->with('allow_user_to_change_display_name')
->willReturn(false);

$user = $this->createMock('\OCP\IUser');
$user->method('getUID')->willReturn('admin');

$subAdmin = $this->createMock(SubAdmin::class);
$subAdmin->method('isSubAdmin')->willReturn($isSubaDmin);

$this->sessionUser->method('getUser')
->willReturn($user);

$this->groupManager->method('isAdmin')->willReturn($isAdmin);
$this->groupManager->method('getSubAdmin')->willReturn($subAdmin);

$backend = $this->getMockBuilder(Database::class)
->setMethods(['implementsActions'])
->getMock();

/** @var Account | \PHPUnit_Framework_MockObject_MockObject $account */
$account = $this->getMockBuilder(Account::class)
->setMethods(['getBackendInstance', 'getDisplayName', 'setDisplayName'])
->getMock();
$account->expects($this->any())->method('getBackendInstance')->willReturn($backend);
$account->expects($this->any())->method('getDisplayName')->willReturn('admin');
$account->expects($this->any())->method('setDisplayName')->willReturn($implements);

$backend->expects($this->any())
->method('implementsActions')
->will($this->returnCallback(function ($actions) use ($implements) {
if ($actions === Backend::SET_DISPLAYNAME) {
return $implements;
} else {
return false;
}
}));

$user = new User($account, $this->accountMapper, null, $this->config, null, null, $this->groupManager, $this->sessionUser);

$this->assertEquals($expected, $user->canChangeDisplayName());
}
/**
* @dataProvider providesChangeDisplayName
*/
Expand Down