provAPI to use only public API of IAccountManager
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
This commit is contained in:
parent
0ac4563ae4
commit
1e271e9f76
|
@ -31,12 +31,13 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace OCA\Provisioning_API\Controller;
|
namespace OCA\Provisioning_API\Controller;
|
||||||
|
|
||||||
use OC\Accounts\AccountManager;
|
|
||||||
use OC\Group\Manager;
|
use OC\Group\Manager;
|
||||||
use OC\User\Backend;
|
use OC\User\Backend;
|
||||||
use OC\User\NoUserException;
|
use OC\User\NoUserException;
|
||||||
use OC_Helper;
|
use OC_Helper;
|
||||||
use OCP\Accounts\IAccountManager;
|
use OCP\Accounts\IAccountManager;
|
||||||
|
use OCP\Accounts\PropertyDoesNotExistException;
|
||||||
|
use OCP\AppFramework\Http;
|
||||||
use OCP\AppFramework\OCS\OCSException;
|
use OCP\AppFramework\OCS\OCSException;
|
||||||
use OCP\AppFramework\OCS\OCSNotFoundException;
|
use OCP\AppFramework\OCS\OCSNotFoundException;
|
||||||
use OCP\AppFramework\OCSController;
|
use OCP\AppFramework\OCSController;
|
||||||
|
@ -61,7 +62,7 @@ abstract class AUserData extends OCSController {
|
||||||
protected $groupManager;
|
protected $groupManager;
|
||||||
/** @var IUserSession */
|
/** @var IUserSession */
|
||||||
protected $userSession;
|
protected $userSession;
|
||||||
/** @var AccountManager */
|
/** @var IAccountManager */
|
||||||
protected $accountManager;
|
protected $accountManager;
|
||||||
/** @var IFactory */
|
/** @var IFactory */
|
||||||
protected $l10nFactory;
|
protected $l10nFactory;
|
||||||
|
@ -72,7 +73,7 @@ abstract class AUserData extends OCSController {
|
||||||
IConfig $config,
|
IConfig $config,
|
||||||
IGroupManager $groupManager,
|
IGroupManager $groupManager,
|
||||||
IUserSession $userSession,
|
IUserSession $userSession,
|
||||||
AccountManager $accountManager,
|
IAccountManager $accountManager,
|
||||||
IFactory $l10nFactory) {
|
IFactory $l10nFactory) {
|
||||||
parent::__construct($appName, $request);
|
parent::__construct($appName, $request);
|
||||||
|
|
||||||
|
@ -140,30 +141,35 @@ abstract class AUserData extends OCSController {
|
||||||
$data['subadmin'] = $this->getUserSubAdminGroupsData($targetUserObject->getUID());
|
$data['subadmin'] = $this->getUserSubAdminGroupsData($targetUserObject->getUID());
|
||||||
$data['quota'] = $this->fillStorageInfo($targetUserObject->getUID());
|
$data['quota'] = $this->fillStorageInfo($targetUserObject->getUID());
|
||||||
|
|
||||||
if ($includeScopes) {
|
try {
|
||||||
$data[IAccountManager::PROPERTY_AVATAR . self::SCOPE_SUFFIX] = $userAccount->getProperty(IAccountManager::PROPERTY_AVATAR)->getScope();
|
|
||||||
}
|
|
||||||
|
|
||||||
$data[IAccountManager::PROPERTY_EMAIL] = $targetUserObject->getEMailAddress();
|
|
||||||
if ($includeScopes) {
|
|
||||||
$data[IAccountManager::PROPERTY_EMAIL . self::SCOPE_SUFFIX] = $userAccount->getProperty(IAccountManager::PROPERTY_EMAIL)->getScope();
|
|
||||||
}
|
|
||||||
$data[IAccountManager::PROPERTY_DISPLAYNAME] = $targetUserObject->getDisplayName();
|
|
||||||
if ($includeScopes) {
|
|
||||||
$data[IAccountManager::PROPERTY_DISPLAYNAME . self::SCOPE_SUFFIX] = $userAccount->getProperty(IAccountManager::PROPERTY_DISPLAYNAME)->getScope();
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ([
|
|
||||||
IAccountManager::PROPERTY_PHONE,
|
|
||||||
IAccountManager::PROPERTY_ADDRESS,
|
|
||||||
IAccountManager::PROPERTY_WEBSITE,
|
|
||||||
IAccountManager::PROPERTY_TWITTER,
|
|
||||||
] as $propertyName) {
|
|
||||||
$property = $userAccount->getProperty($propertyName);
|
|
||||||
$data[$propertyName] = $property->getValue();
|
|
||||||
if ($includeScopes) {
|
if ($includeScopes) {
|
||||||
$data[$propertyName . self::SCOPE_SUFFIX] = $property->getScope();
|
$data[IAccountManager::PROPERTY_AVATAR . self::SCOPE_SUFFIX] = $userAccount->getProperty(IAccountManager::PROPERTY_AVATAR)->getScope();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$data[IAccountManager::PROPERTY_EMAIL] = $targetUserObject->getEMailAddress();
|
||||||
|
if ($includeScopes) {
|
||||||
|
$data[IAccountManager::PROPERTY_EMAIL . self::SCOPE_SUFFIX] = $userAccount->getProperty(IAccountManager::PROPERTY_EMAIL)->getScope();
|
||||||
|
}
|
||||||
|
$data[IAccountManager::PROPERTY_DISPLAYNAME] = $targetUserObject->getDisplayName();
|
||||||
|
if ($includeScopes) {
|
||||||
|
$data[IAccountManager::PROPERTY_DISPLAYNAME . self::SCOPE_SUFFIX] = $userAccount->getProperty(IAccountManager::PROPERTY_DISPLAYNAME)->getScope();
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ([
|
||||||
|
IAccountManager::PROPERTY_PHONE,
|
||||||
|
IAccountManager::PROPERTY_ADDRESS,
|
||||||
|
IAccountManager::PROPERTY_WEBSITE,
|
||||||
|
IAccountManager::PROPERTY_TWITTER,
|
||||||
|
] as $propertyName) {
|
||||||
|
$property = $userAccount->getProperty($propertyName);
|
||||||
|
$data[$propertyName] = $property->getValue();
|
||||||
|
if ($includeScopes) {
|
||||||
|
$data[$propertyName . self::SCOPE_SUFFIX] = $property->getScope();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (PropertyDoesNotExistException $e) {
|
||||||
|
// hard coded properties should exist
|
||||||
|
throw new OCSException($e->getMessage(), Http::STATUS_INTERNAL_SERVER_ERROR, $e);
|
||||||
}
|
}
|
||||||
|
|
||||||
$data['groups'] = $gids;
|
$data['groups'] = $gids;
|
||||||
|
|
|
@ -34,7 +34,7 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace OCA\Provisioning_API\Controller;
|
namespace OCA\Provisioning_API\Controller;
|
||||||
|
|
||||||
use OC\Accounts\AccountManager;
|
use OCP\Accounts\IAccountManager;
|
||||||
use OCP\AppFramework\Http\DataResponse;
|
use OCP\AppFramework\Http\DataResponse;
|
||||||
use OCP\AppFramework\OCS\OCSException;
|
use OCP\AppFramework\OCS\OCSException;
|
||||||
use OCP\AppFramework\OCS\OCSForbiddenException;
|
use OCP\AppFramework\OCS\OCSForbiddenException;
|
||||||
|
@ -61,7 +61,7 @@ class GroupsController extends AUserData {
|
||||||
IConfig $config,
|
IConfig $config,
|
||||||
IGroupManager $groupManager,
|
IGroupManager $groupManager,
|
||||||
IUserSession $userSession,
|
IUserSession $userSession,
|
||||||
AccountManager $accountManager,
|
IAccountManager $accountManager,
|
||||||
IFactory $l10nFactory,
|
IFactory $l10nFactory,
|
||||||
LoggerInterface $logger) {
|
LoggerInterface $logger) {
|
||||||
parent::__construct($appName,
|
parent::__construct($appName,
|
||||||
|
|
|
@ -46,7 +46,6 @@ use libphonenumber\NumberParseException;
|
||||||
use libphonenumber\PhoneNumber;
|
use libphonenumber\PhoneNumber;
|
||||||
use libphonenumber\PhoneNumberFormat;
|
use libphonenumber\PhoneNumberFormat;
|
||||||
use libphonenumber\PhoneNumberUtil;
|
use libphonenumber\PhoneNumberUtil;
|
||||||
use OC\Accounts\AccountManager;
|
|
||||||
use OC\Authentication\Token\RemoteWipe;
|
use OC\Authentication\Token\RemoteWipe;
|
||||||
use OC\HintException;
|
use OC\HintException;
|
||||||
use OC\KnownUser\KnownUserService;
|
use OC\KnownUser\KnownUserService;
|
||||||
|
@ -102,7 +101,7 @@ class UsersController extends AUserData {
|
||||||
IAppManager $appManager,
|
IAppManager $appManager,
|
||||||
IGroupManager $groupManager,
|
IGroupManager $groupManager,
|
||||||
IUserSession $userSession,
|
IUserSession $userSession,
|
||||||
AccountManager $accountManager,
|
IAccountManager $accountManager,
|
||||||
IURLGenerator $urlGenerator,
|
IURLGenerator $urlGenerator,
|
||||||
LoggerInterface $logger,
|
LoggerInterface $logger,
|
||||||
IFactory $l10nFactory,
|
IFactory $l10nFactory,
|
||||||
|
@ -734,13 +733,14 @@ class UsersController extends AUserData {
|
||||||
case IAccountManager::PROPERTY_ADDRESS:
|
case IAccountManager::PROPERTY_ADDRESS:
|
||||||
case IAccountManager::PROPERTY_WEBSITE:
|
case IAccountManager::PROPERTY_WEBSITE:
|
||||||
case IAccountManager::PROPERTY_TWITTER:
|
case IAccountManager::PROPERTY_TWITTER:
|
||||||
$userAccount = $this->accountManager->getUser($targetUser);
|
$userAccount = $this->accountManager->getAccount($targetUser);
|
||||||
if ($userAccount[$key]['value'] !== $value) {
|
$userProperty = $userAccount->getProperty($key);
|
||||||
$userAccount[$key]['value'] = $value;
|
if ($userProperty->getValue() !== $value) {
|
||||||
try {
|
try {
|
||||||
$this->accountManager->updateUser($targetUser, $userAccount, true);
|
$userProperty->setValue($value);
|
||||||
|
$this->accountManager->updateAccount($userAccount);
|
||||||
|
|
||||||
if ($key === IAccountManager::PROPERTY_PHONE) {
|
if ($userProperty->getName() === IAccountManager::PROPERTY_PHONE) {
|
||||||
$this->knownUserService->deleteByContactUserId($targetUser->getUID());
|
$this->knownUserService->deleteByContactUserId($targetUser->getUID());
|
||||||
}
|
}
|
||||||
} catch (\InvalidArgumentException $e) {
|
} catch (\InvalidArgumentException $e) {
|
||||||
|
@ -756,11 +756,12 @@ class UsersController extends AUserData {
|
||||||
case IAccountManager::PROPERTY_TWITTER . self::SCOPE_SUFFIX:
|
case IAccountManager::PROPERTY_TWITTER . self::SCOPE_SUFFIX:
|
||||||
case IAccountManager::PROPERTY_AVATAR . self::SCOPE_SUFFIX:
|
case IAccountManager::PROPERTY_AVATAR . self::SCOPE_SUFFIX:
|
||||||
$propertyName = substr($key, 0, strlen($key) - strlen(self::SCOPE_SUFFIX));
|
$propertyName = substr($key, 0, strlen($key) - strlen(self::SCOPE_SUFFIX));
|
||||||
$userAccount = $this->accountManager->getUser($targetUser);
|
$userAccount = $this->accountManager->getAccount($targetUser);
|
||||||
if ($userAccount[$propertyName]['scope'] !== $value) {
|
$userProperty = $userAccount->getProperty($propertyName);
|
||||||
$userAccount[$propertyName]['scope'] = $value;
|
if ($userProperty->getScope() !== $value) {
|
||||||
|
$userProperty->setScope($value);
|
||||||
try {
|
try {
|
||||||
$this->accountManager->updateUser($targetUser, $userAccount, true);
|
$this->accountManager->updateAccount($userAccount);
|
||||||
} catch (\InvalidArgumentException $e) {
|
} catch (\InvalidArgumentException $e) {
|
||||||
throw new OCSException('Invalid ' . $e->getMessage(), 102);
|
throw new OCSException('Invalid ' . $e->getMessage(), 102);
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,7 +30,6 @@
|
||||||
|
|
||||||
namespace OCA\Provisioning_API\Tests\Controller;
|
namespace OCA\Provisioning_API\Tests\Controller;
|
||||||
|
|
||||||
use OC\Accounts\AccountManager;
|
|
||||||
use OC\Group\Manager;
|
use OC\Group\Manager;
|
||||||
use OC\SubAdmin;
|
use OC\SubAdmin;
|
||||||
use OC\User\NoUserException;
|
use OC\User\NoUserException;
|
||||||
|
@ -57,7 +56,7 @@ class GroupsControllerTest extends \Test\TestCase {
|
||||||
protected $groupManager;
|
protected $groupManager;
|
||||||
/** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
|
/** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
|
||||||
protected $userSession;
|
protected $userSession;
|
||||||
/** @var AccountManager|\PHPUnit\Framework\MockObject\MockObject */
|
/** @var IAccountManager|\PHPUnit\Framework\MockObject\MockObject */
|
||||||
protected $accountManager;
|
protected $accountManager;
|
||||||
/** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
|
/** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
|
||||||
protected $logger;
|
protected $logger;
|
||||||
|
@ -76,7 +75,7 @@ class GroupsControllerTest extends \Test\TestCase {
|
||||||
$this->config = $this->createMock(IConfig::class);
|
$this->config = $this->createMock(IConfig::class);
|
||||||
$this->groupManager = $this->createMock(Manager::class);
|
$this->groupManager = $this->createMock(Manager::class);
|
||||||
$this->userSession = $this->createMock(IUserSession::class);
|
$this->userSession = $this->createMock(IUserSession::class);
|
||||||
$this->accountManager = $this->createMock(AccountManager::class);
|
$this->accountManager = $this->createMock(IAccountManager::class);
|
||||||
$this->l10nFactory = $this->createMock(IFactory::class);
|
$this->l10nFactory = $this->createMock(IFactory::class);
|
||||||
$this->logger = $this->createMock(LoggerInterface::class);
|
$this->logger = $this->createMock(LoggerInterface::class);
|
||||||
|
|
||||||
|
@ -181,19 +180,6 @@ class GroupsControllerTest extends \Test\TestCase {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private function useAccountManager() {
|
|
||||||
$this->accountManager->expects($this->any())
|
|
||||||
->method('getUser')
|
|
||||||
->willReturnCallback(function (IUser $user) {
|
|
||||||
return [
|
|
||||||
IAccountManager::PROPERTY_PHONE => ['value' => '0800-call-' . $user->getUID()],
|
|
||||||
IAccountManager::PROPERTY_ADDRESS => ['value' => 'Holzweg 99, 0601 Herrera, Panama'],
|
|
||||||
IAccountManager::PROPERTY_WEBSITE => ['value' => 'https://' . $user->getUid() . '.pa'],
|
|
||||||
IAccountManager::PROPERTY_TWITTER => ['value' => '@' . $user->getUID()],
|
|
||||||
];
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public function dataGetGroups() {
|
public function dataGetGroups() {
|
||||||
return [
|
return [
|
||||||
[null, 0, 0],
|
[null, 0, 0],
|
||||||
|
@ -505,7 +491,6 @@ class GroupsControllerTest extends \Test\TestCase {
|
||||||
$gid = 'ncg1';
|
$gid = 'ncg1';
|
||||||
|
|
||||||
$this->asAdmin();
|
$this->asAdmin();
|
||||||
$this->useAccountManager();
|
|
||||||
|
|
||||||
$users = [
|
$users = [
|
||||||
'ncu1' => $this->createUser('ncu1'), # regular
|
'ncu1' => $this->createUser('ncu1'), # regular
|
||||||
|
@ -551,7 +536,6 @@ class GroupsControllerTest extends \Test\TestCase {
|
||||||
$gid = 'Department A/B C/D';
|
$gid = 'Department A/B C/D';
|
||||||
|
|
||||||
$this->asAdmin();
|
$this->asAdmin();
|
||||||
$this->useAccountManager();
|
|
||||||
|
|
||||||
$users = [
|
$users = [
|
||||||
'ncu1' => $this->createUser('ncu1'), # regular
|
'ncu1' => $this->createUser('ncu1'), # regular
|
||||||
|
|
|
@ -41,7 +41,6 @@
|
||||||
namespace OCA\Provisioning_API\Tests\Controller;
|
namespace OCA\Provisioning_API\Tests\Controller;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use OC\Accounts\AccountManager;
|
|
||||||
use OC\Authentication\Token\RemoteWipe;
|
use OC\Authentication\Token\RemoteWipe;
|
||||||
use OC\Group\Manager;
|
use OC\Group\Manager;
|
||||||
use OC\KnownUser\KnownUserService;
|
use OC\KnownUser\KnownUserService;
|
||||||
|
@ -88,7 +87,7 @@ class UsersControllerTest extends TestCase {
|
||||||
protected $logger;
|
protected $logger;
|
||||||
/** @var UsersController|MockObject */
|
/** @var UsersController|MockObject */
|
||||||
protected $api;
|
protected $api;
|
||||||
/** @var AccountManager|MockObject */
|
/** @var IAccountManager|MockObject */
|
||||||
protected $accountManager;
|
protected $accountManager;
|
||||||
/** @var IURLGenerator|MockObject */
|
/** @var IURLGenerator|MockObject */
|
||||||
protected $urlGenerator;
|
protected $urlGenerator;
|
||||||
|
@ -117,7 +116,7 @@ class UsersControllerTest extends TestCase {
|
||||||
$this->userSession = $this->createMock(IUserSession::class);
|
$this->userSession = $this->createMock(IUserSession::class);
|
||||||
$this->logger = $this->createMock(LoggerInterface::class);
|
$this->logger = $this->createMock(LoggerInterface::class);
|
||||||
$this->request = $this->createMock(IRequest::class);
|
$this->request = $this->createMock(IRequest::class);
|
||||||
$this->accountManager = $this->createMock(AccountManager::class);
|
$this->accountManager = $this->createMock(IAccountManager::class);
|
||||||
$this->urlGenerator = $this->createMock(IURLGenerator::class);
|
$this->urlGenerator = $this->createMock(IURLGenerator::class);
|
||||||
$this->l10nFactory = $this->createMock(IFactory::class);
|
$this->l10nFactory = $this->createMock(IFactory::class);
|
||||||
$this->newUserMailHelper = $this->createMock(NewUserMailHelper::class);
|
$this->newUserMailHelper = $this->createMock(NewUserMailHelper::class);
|
||||||
|
@ -1574,13 +1573,34 @@ class UsersControllerTest extends TestCase {
|
||||||
->method('getBackend')
|
->method('getBackend')
|
||||||
->willReturn($backend);
|
->willReturn($backend);
|
||||||
|
|
||||||
$this->accountManager->expects($this->once())
|
$propertyMock = $this->createMock(IAccountProperty::class);
|
||||||
->method('getUser')
|
$propertyMock->expects($this->any())
|
||||||
|
->method('getName')
|
||||||
|
->willReturn($propertyName);
|
||||||
|
$propertyMock->expects($this->any())
|
||||||
|
->method('getValue')
|
||||||
|
->willReturn($oldValue);
|
||||||
|
$propertyMock->expects($this->once())
|
||||||
|
->method('setValue')
|
||||||
|
->with($newValue)
|
||||||
|
->willReturnSelf();
|
||||||
|
$propertyMock->expects($this->any())
|
||||||
|
->method('getScope')
|
||||||
|
->willReturn(IAccountManager::SCOPE_LOCAL);
|
||||||
|
|
||||||
|
$accountMock = $this->createMock(IAccount::class);
|
||||||
|
$accountMock->expects($this->any())
|
||||||
|
->method('getProperty')
|
||||||
|
->with($propertyName)
|
||||||
|
->willReturn($propertyMock);
|
||||||
|
|
||||||
|
$this->accountManager->expects($this->atLeastOnce())
|
||||||
|
->method('getAccount')
|
||||||
->with($loggedInUser)
|
->with($loggedInUser)
|
||||||
->willReturn([$propertyName => ['value' => $oldValue, 'scope' => IAccountManager::SCOPE_LOCAL]]);
|
->willReturn($accountMock);
|
||||||
$this->accountManager->expects($this->once())
|
$this->accountManager->expects($this->once())
|
||||||
->method('updateUser')
|
->method('updateAccount')
|
||||||
->with($loggedInUser, [$propertyName => ['value' => $newValue, 'scope' => IAccountManager::SCOPE_LOCAL]], true);
|
->with($accountMock);
|
||||||
|
|
||||||
$this->assertEquals([], $this->api->editUser('UserToEdit', $propertyName, $newValue)->getData());
|
$this->assertEquals([], $this->api->editUser('UserToEdit', $propertyName, $newValue)->getData());
|
||||||
}
|
}
|
||||||
|
@ -1624,13 +1644,34 @@ class UsersControllerTest extends TestCase {
|
||||||
->method('getBackend')
|
->method('getBackend')
|
||||||
->willReturn($backend);
|
->willReturn($backend);
|
||||||
|
|
||||||
$this->accountManager->expects($this->once())
|
$propertyMock = $this->createMock(IAccountProperty::class);
|
||||||
->method('getUser')
|
$propertyMock->expects($this->any())
|
||||||
|
->method('getName')
|
||||||
|
->willReturn($propertyName);
|
||||||
|
$propertyMock->expects($this->any())
|
||||||
|
->method('getValue')
|
||||||
|
->willReturn('somevalue');
|
||||||
|
$propertyMock->expects($this->any())
|
||||||
|
->method('getScope')
|
||||||
|
->willReturn($oldScope);
|
||||||
|
$propertyMock->expects($this->atLeastOnce())
|
||||||
|
->method('setScope')
|
||||||
|
->with($newScope)
|
||||||
|
->willReturnSelf();
|
||||||
|
|
||||||
|
$accountMock = $this->createMock(IAccount::class);
|
||||||
|
$accountMock->expects($this->any())
|
||||||
|
->method('getProperty')
|
||||||
|
->with($propertyName)
|
||||||
|
->willReturn($propertyMock);
|
||||||
|
|
||||||
|
$this->accountManager->expects($this->atLeastOnce())
|
||||||
|
->method('getAccount')
|
||||||
->with($loggedInUser)
|
->with($loggedInUser)
|
||||||
->willReturn([$propertyName => ['value' => 'somevalue', 'scope' => $oldScope]]);
|
->willReturn($accountMock);
|
||||||
$this->accountManager->expects($this->once())
|
$this->accountManager->expects($this->once())
|
||||||
->method('updateUser')
|
->method('updateAccount')
|
||||||
->with($loggedInUser, [$propertyName => ['value' => 'somevalue', 'scope' => $newScope]], true);
|
->with($accountMock);
|
||||||
|
|
||||||
$this->assertEquals([], $this->api->editUser('UserToEdit', $propertyName . 'Scope', $newScope)->getData());
|
$this->assertEquals([], $this->api->editUser('UserToEdit', $propertyName . 'Scope', $newScope)->getData());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue