do not use private AccountManager in UsersController
- extends IAccountProperty for verificationData getters and setters - implementation thereof ^ - and of course adaption of UsersController Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
This commit is contained in:
parent
3b230f9928
commit
847ed37afc
|
@ -37,7 +37,7 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace OCA\Settings\Controller;
|
namespace OCA\Settings\Controller;
|
||||||
|
|
||||||
use OC\Accounts\AccountManager;
|
use InvalidArgumentException;
|
||||||
use OC\AppFramework\Http;
|
use OC\AppFramework\Http;
|
||||||
use OC\Encryption\Exceptions\ModuleDoesNotExistsException;
|
use OC\Encryption\Exceptions\ModuleDoesNotExistsException;
|
||||||
use OC\ForbiddenException;
|
use OC\ForbiddenException;
|
||||||
|
@ -49,7 +49,9 @@ use OC\User\Manager as UserManager;
|
||||||
use OCA\Settings\BackgroundJobs\VerifyUserData;
|
use OCA\Settings\BackgroundJobs\VerifyUserData;
|
||||||
use OCA\Settings\Events\BeforeTemplateRenderedEvent;
|
use OCA\Settings\Events\BeforeTemplateRenderedEvent;
|
||||||
use OCA\User_LDAP\User_Proxy;
|
use OCA\User_LDAP\User_Proxy;
|
||||||
|
use OCP\Accounts\IAccount;
|
||||||
use OCP\Accounts\IAccountManager;
|
use OCP\Accounts\IAccountManager;
|
||||||
|
use OCP\Accounts\PropertyDoesNotExistException;
|
||||||
use OCP\App\IAppManager;
|
use OCP\App\IAppManager;
|
||||||
use OCP\AppFramework\Controller;
|
use OCP\AppFramework\Controller;
|
||||||
use OCP\AppFramework\Http\DataResponse;
|
use OCP\AppFramework\Http\DataResponse;
|
||||||
|
@ -88,7 +90,7 @@ class UsersController extends Controller {
|
||||||
private $l10nFactory;
|
private $l10nFactory;
|
||||||
/** @var IAppManager */
|
/** @var IAppManager */
|
||||||
private $appManager;
|
private $appManager;
|
||||||
/** @var AccountManager */
|
/** @var IAccountManager */
|
||||||
private $accountManager;
|
private $accountManager;
|
||||||
/** @var Manager */
|
/** @var Manager */
|
||||||
private $keyManager;
|
private $keyManager;
|
||||||
|
@ -114,7 +116,7 @@ class UsersController extends Controller {
|
||||||
IMailer $mailer,
|
IMailer $mailer,
|
||||||
IFactory $l10nFactory,
|
IFactory $l10nFactory,
|
||||||
IAppManager $appManager,
|
IAppManager $appManager,
|
||||||
AccountManager $accountManager,
|
IAccountManager $accountManager,
|
||||||
Manager $keyManager,
|
Manager $keyManager,
|
||||||
IJobList $jobList,
|
IJobList $jobList,
|
||||||
IManager $encryptionManager,
|
IManager $encryptionManager,
|
||||||
|
@ -393,53 +395,36 @@ class UsersController extends Controller {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
$data = $this->accountManager->getUser($user);
|
$userAccount = $this->accountManager->getAccount($user);
|
||||||
$beforeData = $data;
|
$oldPhoneValue = $userAccount->getProperty(IAccountManager::PROPERTY_PHONE)->getValue();
|
||||||
if (!is_null($avatarScope)) {
|
|
||||||
$data[IAccountManager::PROPERTY_AVATAR]['scope'] = $avatarScope;
|
$updatable = [
|
||||||
}
|
IAccountManager::PROPERTY_AVATAR => ['value' => null, 'scope' => $avatarScope],
|
||||||
if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) {
|
IAccountManager::PROPERTY_DISPLAYNAME => ['value' => $displayname, 'scope' => $displaynameScope],
|
||||||
if (!is_null($displayname)) {
|
IAccountManager::PROPERTY_EMAIL => ['value' => $email, 'scope' => $emailScope],
|
||||||
$data[IAccountManager::PROPERTY_DISPLAYNAME]['value'] = $displayname;
|
IAccountManager::PROPERTY_WEBSITE => ['value' => $website, 'scope' => $websiteScope],
|
||||||
|
IAccountManager::PROPERTY_ADDRESS => ['value' => $address, 'scope' => $addressScope],
|
||||||
|
IAccountManager::PROPERTY_PHONE => ['value' => $phone, 'scope' => $phoneScope],
|
||||||
|
IAccountManager::PROPERTY_TWITTER => ['value' => $twitter, 'scope' => $twitterScope],
|
||||||
|
];
|
||||||
|
$allowUserToChangeDisplayName = $this->config->getSystemValueBool('allow_user_to_change_display_name', true);
|
||||||
|
foreach ($updatable as $property => $data) {
|
||||||
|
if ($allowUserToChangeDisplayName === false
|
||||||
|
&& in_array($property, [IAccountManager::PROPERTY_DISPLAYNAME, IAccountManager::PROPERTY_EMAIL], true)) {
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
if (!is_null($displaynameScope)) {
|
$property = $userAccount->getProperty($property);
|
||||||
$data[IAccountManager::PROPERTY_DISPLAYNAME]['scope'] = $displaynameScope;
|
if (null !== $data['value']) {
|
||||||
|
$property->setValue($data['value']);
|
||||||
}
|
}
|
||||||
if (!is_null($email)) {
|
if (null !== $data['scope']) {
|
||||||
$data[IAccountManager::PROPERTY_EMAIL]['value'] = $email;
|
$property->setScope($data['scope']);
|
||||||
}
|
}
|
||||||
if (!is_null($emailScope)) {
|
|
||||||
$data[IAccountManager::PROPERTY_EMAIL]['scope'] = $emailScope;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!is_null($website)) {
|
|
||||||
$data[IAccountManager::PROPERTY_WEBSITE]['value'] = $website;
|
|
||||||
}
|
|
||||||
if (!is_null($websiteScope)) {
|
|
||||||
$data[IAccountManager::PROPERTY_WEBSITE]['scope'] = $websiteScope;
|
|
||||||
}
|
|
||||||
if (!is_null($address)) {
|
|
||||||
$data[IAccountManager::PROPERTY_ADDRESS]['value'] = $address;
|
|
||||||
}
|
|
||||||
if (!is_null($addressScope)) {
|
|
||||||
$data[IAccountManager::PROPERTY_ADDRESS]['scope'] = $addressScope;
|
|
||||||
}
|
|
||||||
if (!is_null($phone)) {
|
|
||||||
$data[IAccountManager::PROPERTY_PHONE]['value'] = $phone;
|
|
||||||
}
|
|
||||||
if (!is_null($phoneScope)) {
|
|
||||||
$data[IAccountManager::PROPERTY_PHONE]['scope'] = $phoneScope;
|
|
||||||
}
|
|
||||||
if (!is_null($twitter)) {
|
|
||||||
$data[IAccountManager::PROPERTY_TWITTER]['value'] = $twitter;
|
|
||||||
}
|
|
||||||
if (!is_null($twitterScope)) {
|
|
||||||
$data[IAccountManager::PROPERTY_TWITTER]['scope'] = $twitterScope;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$data = $this->saveUserSettings($user, $data);
|
$this->saveUserSettings($userAccount);
|
||||||
if ($beforeData[IAccountManager::PROPERTY_PHONE]['value'] !== $data[IAccountManager::PROPERTY_PHONE]['value']) {
|
if ($oldPhoneValue !== $userAccount->getProperty(IAccountManager::PROPERTY_PHONE)->getValue()) {
|
||||||
$this->knownUserService->deleteByContactUserId($user->getUID());
|
$this->knownUserService->deleteByContactUserId($user->getUID());
|
||||||
}
|
}
|
||||||
return new DataResponse(
|
return new DataResponse(
|
||||||
|
@ -447,32 +432,25 @@ class UsersController extends Controller {
|
||||||
'status' => 'success',
|
'status' => 'success',
|
||||||
'data' => [
|
'data' => [
|
||||||
'userId' => $user->getUID(),
|
'userId' => $user->getUID(),
|
||||||
'avatarScope' => $data[IAccountManager::PROPERTY_AVATAR]['scope'],
|
'avatarScope' => $userAccount->getProperty(IAccountManager::PROPERTY_AVATAR)->getScope(),
|
||||||
'displayname' => $data[IAccountManager::PROPERTY_DISPLAYNAME]['value'],
|
'displayname' => $userAccount->getProperty(IAccountManager::PROPERTY_DISPLAYNAME)->getValue(),
|
||||||
'displaynameScope' => $data[IAccountManager::PROPERTY_DISPLAYNAME]['scope'],
|
'displaynameScope' => $userAccount->getProperty(IAccountManager::PROPERTY_DISPLAYNAME)->getScope(),
|
||||||
'phone' => $data[IAccountManager::PROPERTY_PHONE]['value'],
|
'phone' => $userAccount->getProperty(IAccountManager::PROPERTY_PHONE)->getValue(),
|
||||||
'phoneScope' => $data[IAccountManager::PROPERTY_PHONE]['scope'],
|
'phoneScope' => $userAccount->getProperty(IAccountManager::PROPERTY_PHONE)->getScope(),
|
||||||
'email' => $data[IAccountManager::PROPERTY_EMAIL]['value'],
|
'email' => $userAccount->getProperty(IAccountManager::PROPERTY_EMAIL)->getValue(),
|
||||||
'emailScope' => $data[IAccountManager::PROPERTY_EMAIL]['scope'],
|
'emailScope' => $userAccount->getProperty(IAccountManager::PROPERTY_EMAIL)->getScope(),
|
||||||
'website' => $data[IAccountManager::PROPERTY_WEBSITE]['value'],
|
'website' => $userAccount->getProperty(IAccountManager::PROPERTY_WEBSITE)->getValue(),
|
||||||
'websiteScope' => $data[IAccountManager::PROPERTY_WEBSITE]['scope'],
|
'websiteScope' => $userAccount->getProperty(IAccountManager::PROPERTY_WEBSITE)->getScope(),
|
||||||
'address' => $data[IAccountManager::PROPERTY_ADDRESS]['value'],
|
'address' => $userAccount->getProperty(IAccountManager::PROPERTY_ADDRESS)->getValue(),
|
||||||
'addressScope' => $data[IAccountManager::PROPERTY_ADDRESS]['scope'],
|
'addressScope' => $userAccount->getProperty(IAccountManager::PROPERTY_ADDRESS)->getScope(),
|
||||||
'twitter' => $data[IAccountManager::PROPERTY_TWITTER]['value'],
|
'twitter' => $userAccount->getProperty(IAccountManager::PROPERTY_TWITTER)->getValue(),
|
||||||
'twitterScope' => $data[IAccountManager::PROPERTY_TWITTER]['scope'],
|
'twitterScope' => $userAccount->getProperty(IAccountManager::PROPERTY_TWITTER)->getScope(),
|
||||||
'message' => $this->l10n->t('Settings saved')
|
'message' => $this->l10n->t('Settings saved'),
|
||||||
]
|
],
|
||||||
],
|
],
|
||||||
Http::STATUS_OK
|
Http::STATUS_OK
|
||||||
);
|
);
|
||||||
} catch (ForbiddenException $e) {
|
} catch (ForbiddenException | InvalidArgumentException | PropertyDoesNotExistException $e) {
|
||||||
return new DataResponse([
|
|
||||||
'status' => 'error',
|
|
||||||
'data' => [
|
|
||||||
'message' => $e->getMessage()
|
|
||||||
],
|
|
||||||
]);
|
|
||||||
} catch (\InvalidArgumentException $e) {
|
|
||||||
return new DataResponse([
|
return new DataResponse([
|
||||||
'status' => 'error',
|
'status' => 'error',
|
||||||
'data' => [
|
'data' => [
|
||||||
|
@ -484,49 +462,41 @@ class UsersController extends Controller {
|
||||||
/**
|
/**
|
||||||
* update account manager with new user data
|
* update account manager with new user data
|
||||||
*
|
*
|
||||||
* @param IUser $user
|
|
||||||
* @param array $data
|
|
||||||
* @return array
|
|
||||||
* @throws ForbiddenException
|
* @throws ForbiddenException
|
||||||
* @throws \InvalidArgumentException
|
* @throws InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
protected function saveUserSettings(IUser $user, array $data): array {
|
protected function saveUserSettings(IAccount $userAccount): void {
|
||||||
// keep the user back-end up-to-date with the latest display name and email
|
// keep the user back-end up-to-date with the latest display name and email
|
||||||
// address
|
// address
|
||||||
$oldDisplayName = $user->getDisplayName();
|
$oldDisplayName = $userAccount->getUser()->getDisplayName();
|
||||||
$oldDisplayName = is_null($oldDisplayName) ? '' : $oldDisplayName;
|
if ($oldDisplayName !== $userAccount->getProperty(IAccountManager::PROPERTY_DISPLAYNAME)->getValue()) {
|
||||||
if (isset($data[IAccountManager::PROPERTY_DISPLAYNAME]['value'])
|
$result = $userAccount->getUser()->setDisplayName($userAccount->getProperty(IAccountManager::PROPERTY_DISPLAYNAME)->getValue());
|
||||||
&& $oldDisplayName !== $data[IAccountManager::PROPERTY_DISPLAYNAME]['value']
|
|
||||||
) {
|
|
||||||
$result = $user->setDisplayName($data[IAccountManager::PROPERTY_DISPLAYNAME]['value']);
|
|
||||||
if ($result === false) {
|
if ($result === false) {
|
||||||
throw new ForbiddenException($this->l10n->t('Unable to change full name'));
|
throw new ForbiddenException($this->l10n->t('Unable to change full name'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$oldEmailAddress = $user->getEMailAddress();
|
$oldEmailAddress = $userAccount->getUser()->getEMailAddress();
|
||||||
$oldEmailAddress = is_null($oldEmailAddress) ? '' : strtolower($oldEmailAddress);
|
$oldEmailAddress = strtolower((string)$oldEmailAddress);
|
||||||
if (isset($data[IAccountManager::PROPERTY_EMAIL]['value'])
|
if ($oldEmailAddress !== $userAccount->getProperty(IAccountManager::PROPERTY_EMAIL)->getValue()) {
|
||||||
&& $oldEmailAddress !== $data[IAccountManager::PROPERTY_EMAIL]['value']
|
|
||||||
) {
|
|
||||||
// this is the only permission a backend provides and is also used
|
// this is the only permission a backend provides and is also used
|
||||||
// for the permission of setting a email address
|
// for the permission of setting a email address
|
||||||
if (!$user->canChangeDisplayName()) {
|
if (!$userAccount->getUser()->canChangeDisplayName()) {
|
||||||
throw new ForbiddenException($this->l10n->t('Unable to change email address'));
|
throw new ForbiddenException($this->l10n->t('Unable to change email address'));
|
||||||
}
|
}
|
||||||
$user->setEMailAddress($data[IAccountManager::PROPERTY_EMAIL]['value']);
|
$userAccount->getUser()->setEMailAddress($userAccount->getProperty(IAccountManager::PROPERTY_EMAIL)->getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return $this->accountManager->updateUser($user, $data, true);
|
$this->accountManager->updateAccount($userAccount);
|
||||||
} catch (\InvalidArgumentException $e) {
|
} catch (InvalidArgumentException $e) {
|
||||||
if ($e->getMessage() === IAccountManager::PROPERTY_PHONE) {
|
if ($e->getMessage() === IAccountManager::PROPERTY_PHONE) {
|
||||||
throw new \InvalidArgumentException($this->l10n->t('Unable to set invalid phone number'));
|
throw new InvalidArgumentException($this->l10n->t('Unable to set invalid phone number'));
|
||||||
}
|
}
|
||||||
if ($e->getMessage() === IAccountManager::PROPERTY_WEBSITE) {
|
if ($e->getMessage() === IAccountManager::PROPERTY_WEBSITE) {
|
||||||
throw new \InvalidArgumentException($this->l10n->t('Unable to set invalid website'));
|
throw new InvalidArgumentException($this->l10n->t('Unable to set invalid website'));
|
||||||
}
|
}
|
||||||
throw new \InvalidArgumentException($this->l10n->t('Some account data was invalid'));
|
throw new InvalidArgumentException($this->l10n->t('Some account data was invalid'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -548,7 +518,7 @@ class UsersController extends Controller {
|
||||||
return new DataResponse([], Http::STATUS_BAD_REQUEST);
|
return new DataResponse([], Http::STATUS_BAD_REQUEST);
|
||||||
}
|
}
|
||||||
|
|
||||||
$accountData = $this->accountManager->getUser($user);
|
$userAccount = $this->accountManager->getAccount($user);
|
||||||
$cloudId = $user->getCloudId();
|
$cloudId = $user->getCloudId();
|
||||||
$message = 'Use my Federated Cloud ID to share with me: ' . $cloudId;
|
$message = 'Use my Federated Cloud ID to share with me: ' . $cloudId;
|
||||||
$signature = $this->signMessage($user, $message);
|
$signature = $this->signMessage($user, $message);
|
||||||
|
@ -558,30 +528,30 @@ class UsersController extends Controller {
|
||||||
|
|
||||||
switch ($account) {
|
switch ($account) {
|
||||||
case 'verify-twitter':
|
case 'verify-twitter':
|
||||||
$accountData[IAccountManager::PROPERTY_TWITTER]['verified'] = IAccountManager::VERIFICATION_IN_PROGRESS;
|
|
||||||
$msg = $this->l10n->t('In order to verify your Twitter account, post the following tweet on Twitter (please make sure to post it without any line breaks):');
|
$msg = $this->l10n->t('In order to verify your Twitter account, post the following tweet on Twitter (please make sure to post it without any line breaks):');
|
||||||
$code = $codeMd5;
|
$code = $codeMd5;
|
||||||
$type = IAccountManager::PROPERTY_TWITTER;
|
$type = IAccountManager::PROPERTY_TWITTER;
|
||||||
$accountData[IAccountManager::PROPERTY_TWITTER]['signature'] = $signature;
|
|
||||||
break;
|
break;
|
||||||
case 'verify-website':
|
case 'verify-website':
|
||||||
$accountData[IAccountManager::PROPERTY_WEBSITE]['verified'] = IAccountManager::VERIFICATION_IN_PROGRESS;
|
|
||||||
$msg = $this->l10n->t('In order to verify your Website, store the following content in your web-root at \'.well-known/CloudIdVerificationCode.txt\' (please make sure that the complete text is in one line):');
|
$msg = $this->l10n->t('In order to verify your Website, store the following content in your web-root at \'.well-known/CloudIdVerificationCode.txt\' (please make sure that the complete text is in one line):');
|
||||||
$type = IAccountManager::PROPERTY_WEBSITE;
|
$type = IAccountManager::PROPERTY_WEBSITE;
|
||||||
$accountData[IAccountManager::PROPERTY_WEBSITE]['signature'] = $signature;
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return new DataResponse([], Http::STATUS_BAD_REQUEST);
|
return new DataResponse([], Http::STATUS_BAD_REQUEST);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$userProperty = $userAccount->getProperty($type);
|
||||||
|
$userProperty
|
||||||
|
->setVerified(IAccountManager::VERIFICATION_IN_PROGRESS)
|
||||||
|
->setVerificationData($signature);
|
||||||
|
|
||||||
if ($onlyVerificationCode === false) {
|
if ($onlyVerificationCode === false) {
|
||||||
$accountData = $this->accountManager->updateUser($user, $accountData);
|
$this->accountManager->updateAccount($userAccount);
|
||||||
$data = $accountData[$type]['value'];
|
|
||||||
|
|
||||||
$this->jobList->add(VerifyUserData::class,
|
$this->jobList->add(VerifyUserData::class,
|
||||||
[
|
[
|
||||||
'verificationCode' => $code,
|
'verificationCode' => $code,
|
||||||
'data' => $data,
|
'data' => $userProperty->getValue(),
|
||||||
'type' => $type,
|
'type' => $type,
|
||||||
'uid' => $user->getUID(),
|
'uid' => $user->getUID(),
|
||||||
'try' => 0,
|
'try' => 0,
|
||||||
|
|
|
@ -31,10 +31,14 @@ namespace OCA\Settings\Tests\Controller;
|
||||||
|
|
||||||
use OC\Accounts\AccountManager;
|
use OC\Accounts\AccountManager;
|
||||||
use OC\Encryption\Exceptions\ModuleDoesNotExistsException;
|
use OC\Encryption\Exceptions\ModuleDoesNotExistsException;
|
||||||
|
use OC\ForbiddenException;
|
||||||
use OC\Group\Manager;
|
use OC\Group\Manager;
|
||||||
use OC\KnownUser\KnownUserService;
|
use OC\KnownUser\KnownUserService;
|
||||||
use OCA\Settings\Controller\UsersController;
|
use OCA\Settings\Controller\UsersController;
|
||||||
|
use OCP\Accounts\IAccount;
|
||||||
use OCP\Accounts\IAccountManager;
|
use OCP\Accounts\IAccountManager;
|
||||||
|
use OCP\Accounts\IAccountProperty;
|
||||||
|
use OCP\Accounts\PropertyDoesNotExistException;
|
||||||
use OCP\App\IAppManager;
|
use OCP\App\IAppManager;
|
||||||
use OCP\AppFramework\Http;
|
use OCP\AppFramework\Http;
|
||||||
use OCP\BackgroundJob\IJobList;
|
use OCP\BackgroundJob\IJobList;
|
||||||
|
@ -53,6 +57,7 @@ use OCP\IUserSession;
|
||||||
use OCP\L10N\IFactory;
|
use OCP\L10N\IFactory;
|
||||||
use OCP\Mail\IMailer;
|
use OCP\Mail\IMailer;
|
||||||
use OCP\Security\ISecureRandom;
|
use OCP\Security\ISecureRandom;
|
||||||
|
use PHPUnit\Framework\MockObject\MockObject;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @group DB
|
* @group DB
|
||||||
|
@ -180,49 +185,77 @@ class UsersControllerTest extends \Test\TestCase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getDefaultAccountManagerUserData() {
|
protected function buildPropertyMock(string $name, string $value, string $scope, string $verified = IAccountManager::VERIFIED): MockObject {
|
||||||
return [
|
$property = $this->createMock(IAccountProperty::class);
|
||||||
IAccountManager::PROPERTY_DISPLAYNAME =>
|
$property->expects($this->any())
|
||||||
[
|
->method('getName')
|
||||||
'value' => 'Default display name',
|
->willReturn($name);
|
||||||
'scope' => IAccountManager::SCOPE_FEDERATED,
|
$property->expects($this->any())
|
||||||
'verified' => IAccountManager::VERIFIED,
|
->method('getValue')
|
||||||
],
|
->willReturn($value);
|
||||||
IAccountManager::PROPERTY_ADDRESS =>
|
$property->expects($this->any())
|
||||||
[
|
->method('getScope')
|
||||||
'value' => 'Default address',
|
->willReturn($scope);
|
||||||
'scope' => IAccountManager::SCOPE_LOCAL,
|
$property->expects($this->any())
|
||||||
'verified' => IAccountManager::VERIFIED,
|
->method('getVerified')
|
||||||
],
|
->willReturn($verified);
|
||||||
IAccountManager::PROPERTY_WEBSITE =>
|
|
||||||
[
|
return $property;
|
||||||
'value' => 'Default website',
|
}
|
||||||
'scope' => IAccountManager::SCOPE_LOCAL,
|
|
||||||
'verified' => IAccountManager::VERIFIED,
|
protected function getDefaultAccountMock(bool $useDefaultValues = true): MockObject {
|
||||||
],
|
$propertyMocks = [
|
||||||
IAccountManager::PROPERTY_EMAIL =>
|
IAccountManager::PROPERTY_DISPLAYNAME => $this->buildPropertyMock(
|
||||||
[
|
IAccountManager::PROPERTY_DISPLAYNAME,
|
||||||
'value' => 'Default email',
|
'Default display name',
|
||||||
'scope' => IAccountManager::SCOPE_FEDERATED,
|
IAccountManager::SCOPE_FEDERATED,
|
||||||
'verified' => IAccountManager::VERIFIED,
|
),
|
||||||
],
|
IAccountManager::PROPERTY_ADDRESS => $this->buildPropertyMock(
|
||||||
IAccountManager::PROPERTY_AVATAR =>
|
IAccountManager::PROPERTY_ADDRESS,
|
||||||
[
|
'Default address',
|
||||||
'scope' => IAccountManager::SCOPE_FEDERATED
|
IAccountManager::SCOPE_LOCAL,
|
||||||
],
|
),
|
||||||
IAccountManager::PROPERTY_PHONE =>
|
IAccountManager::PROPERTY_WEBSITE => $this->buildPropertyMock(
|
||||||
[
|
IAccountManager::PROPERTY_WEBSITE,
|
||||||
'value' => 'Default phone',
|
'Default website',
|
||||||
'scope' => IAccountManager::SCOPE_LOCAL,
|
IAccountManager::SCOPE_LOCAL,
|
||||||
'verified' => IAccountManager::VERIFIED,
|
),
|
||||||
],
|
IAccountManager::PROPERTY_EMAIL => $this->buildPropertyMock(
|
||||||
IAccountManager::PROPERTY_TWITTER =>
|
IAccountManager::PROPERTY_EMAIL,
|
||||||
[
|
'Default email',
|
||||||
'value' => 'Default twitter',
|
IAccountManager::SCOPE_FEDERATED,
|
||||||
'scope' => IAccountManager::SCOPE_LOCAL,
|
),
|
||||||
'verified' => IAccountManager::VERIFIED,
|
IAccountManager::PROPERTY_AVATAR => $this->buildPropertyMock(
|
||||||
],
|
IAccountManager::PROPERTY_AVATAR,
|
||||||
|
'',
|
||||||
|
IAccountManager::SCOPE_FEDERATED,
|
||||||
|
),
|
||||||
|
IAccountManager::PROPERTY_PHONE => $this->buildPropertyMock(
|
||||||
|
IAccountManager::PROPERTY_PHONE,
|
||||||
|
'Default phone',
|
||||||
|
IAccountManager::SCOPE_LOCAL,
|
||||||
|
),
|
||||||
|
IAccountManager::PROPERTY_TWITTER => $this->buildPropertyMock(
|
||||||
|
IAccountManager::PROPERTY_TWITTER,
|
||||||
|
'Default twitter',
|
||||||
|
IAccountManager::SCOPE_LOCAL,
|
||||||
|
),
|
||||||
];
|
];
|
||||||
|
|
||||||
|
$account = $this->createMock(IAccount::class);
|
||||||
|
$account->expects($this->any())
|
||||||
|
->method('getProperty')
|
||||||
|
->willReturnCallback(function (string $propertyName) use ($propertyMocks) {
|
||||||
|
if (isset($propertyMocks[$propertyName])) {
|
||||||
|
return $propertyMocks[$propertyName];
|
||||||
|
}
|
||||||
|
throw new PropertyDoesNotExistException($propertyName);
|
||||||
|
});
|
||||||
|
$account->expects($this->any())
|
||||||
|
->method('getProperties')
|
||||||
|
->willReturn($propertyMocks);
|
||||||
|
|
||||||
|
return $account;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -248,13 +281,12 @@ class UsersControllerTest extends \Test\TestCase {
|
||||||
|
|
||||||
if ($saveData) {
|
if ($saveData) {
|
||||||
$this->accountManager->expects($this->once())
|
$this->accountManager->expects($this->once())
|
||||||
->method('getUser')
|
->method('getAccount')
|
||||||
->with($user)
|
->with($user)
|
||||||
->willReturn($this->getDefaultAccountManagerUserData());
|
->willReturn($this->getDefaultAccountMock());
|
||||||
|
|
||||||
$controller->expects($this->once())
|
$controller->expects($this->once())
|
||||||
->method('saveUserSettings')
|
->method('saveUserSettings');
|
||||||
->willReturnArgument(1);
|
|
||||||
} else {
|
} else {
|
||||||
$controller->expects($this->never())->method('saveUserSettings');
|
$controller->expects($this->never())->method('saveUserSettings');
|
||||||
}
|
}
|
||||||
|
@ -289,27 +321,6 @@ class UsersControllerTest extends \Test\TestCase {
|
||||||
|
|
||||||
public function testSetUserSettingsWhenUserDisplayNameChangeNotAllowed() {
|
public function testSetUserSettingsWhenUserDisplayNameChangeNotAllowed() {
|
||||||
$controller = $this->getController(false, ['saveUserSettings']);
|
$controller = $this->getController(false, ['saveUserSettings']);
|
||||||
$user = $this->createMock(IUser::class);
|
|
||||||
$user->method('getUID')->willReturn('johndoe');
|
|
||||||
|
|
||||||
$this->userSession->method('getUser')->willReturn($user);
|
|
||||||
|
|
||||||
$defaultProperties = $this->getDefaultAccountManagerUserData();
|
|
||||||
|
|
||||||
$this->accountManager->expects($this->once())
|
|
||||||
->method('getUser')
|
|
||||||
->with($user)
|
|
||||||
->willReturn($defaultProperties);
|
|
||||||
|
|
||||||
$this->config->expects($this->once())
|
|
||||||
->method('getSystemValue')
|
|
||||||
->with('allow_user_to_change_display_name')
|
|
||||||
->willReturn(false);
|
|
||||||
|
|
||||||
$this->appManager->expects($this->any())
|
|
||||||
->method('isEnabledForUser')
|
|
||||||
->with('federatedfilesharing')
|
|
||||||
->willReturn(true);
|
|
||||||
|
|
||||||
$avatarScope = IAccountManager::SCOPE_PUBLISHED;
|
$avatarScope = IAccountManager::SCOPE_PUBLISHED;
|
||||||
$displayName = 'Display name';
|
$displayName = 'Display name';
|
||||||
|
@ -325,27 +336,67 @@ class UsersControllerTest extends \Test\TestCase {
|
||||||
$twitter = '@nextclouders';
|
$twitter = '@nextclouders';
|
||||||
$twitterScope = IAccountManager::SCOPE_PUBLISHED;
|
$twitterScope = IAccountManager::SCOPE_PUBLISHED;
|
||||||
|
|
||||||
// Display name and email are not changed.
|
$user = $this->createMock(IUser::class);
|
||||||
$expectedProperties = $defaultProperties;
|
$user->method('getUID')->willReturn('johndoe');
|
||||||
$expectedProperties[IAccountManager::PROPERTY_AVATAR]['scope'] = $avatarScope;
|
|
||||||
$expectedProperties[IAccountManager::PROPERTY_PHONE]['value'] = $phone;
|
$this->userSession->method('getUser')->willReturn($user);
|
||||||
$expectedProperties[IAccountManager::PROPERTY_PHONE]['scope'] = $phoneScope;
|
|
||||||
$expectedProperties[IAccountManager::PROPERTY_WEBSITE]['value'] = $website;
|
/** @var MockObject|IAccount $userAccount */
|
||||||
$expectedProperties[IAccountManager::PROPERTY_WEBSITE]['scope'] = $websiteScope;
|
$userAccount = $this->getDefaultAccountMock();
|
||||||
$expectedProperties[IAccountManager::PROPERTY_ADDRESS]['value'] = $address;
|
$this->accountManager->expects($this->once())
|
||||||
$expectedProperties[IAccountManager::PROPERTY_ADDRESS]['scope'] = $addressScope;
|
->method('getAccount')
|
||||||
$expectedProperties[IAccountManager::PROPERTY_TWITTER]['value'] = $twitter;
|
->with($user)
|
||||||
$expectedProperties[IAccountManager::PROPERTY_TWITTER]['scope'] = $twitterScope;
|
->willReturn($userAccount);
|
||||||
|
|
||||||
|
/** @var MockObject|IAccountProperty $avatarProperty */
|
||||||
|
$avatarProperty = $userAccount->getProperty(IAccountManager::PROPERTY_AVATAR);
|
||||||
|
$avatarProperty->expects($this->atLeastOnce())
|
||||||
|
->method('setScope')
|
||||||
|
->with($avatarScope)
|
||||||
|
->willReturnSelf();
|
||||||
|
|
||||||
|
/** @var MockObject|IAccountProperty $avatarProperty */
|
||||||
|
$avatarProperty = $userAccount->getProperty(IAccountManager::PROPERTY_ADDRESS);
|
||||||
|
$avatarProperty->expects($this->atLeastOnce())
|
||||||
|
->method('setScope')
|
||||||
|
->with($addressScope)
|
||||||
|
->willReturnSelf();
|
||||||
|
$avatarProperty->expects($this->atLeastOnce())
|
||||||
|
->method('setValue')
|
||||||
|
->with($address)
|
||||||
|
->willReturnSelf();
|
||||||
|
|
||||||
|
/** @var MockObject|IAccountProperty $emailProperty */
|
||||||
|
$emailProperty = $userAccount->getProperty(IAccountManager::PROPERTY_EMAIL);
|
||||||
|
$emailProperty->expects($this->never())
|
||||||
|
->method('setValue');
|
||||||
|
$emailProperty->expects($this->never())
|
||||||
|
->method('setScope');
|
||||||
|
|
||||||
|
/** @var MockObject|IAccountProperty $emailProperty */
|
||||||
|
$emailProperty = $userAccount->getProperty(IAccountManager::PROPERTY_DISPLAYNAME);
|
||||||
|
$emailProperty->expects($this->never())
|
||||||
|
->method('setValue');
|
||||||
|
$emailProperty->expects($this->never())
|
||||||
|
->method('setScope');
|
||||||
|
|
||||||
|
$this->config->expects($this->once())
|
||||||
|
->method('getSystemValueBool')
|
||||||
|
->with('allow_user_to_change_display_name')
|
||||||
|
->willReturn(false);
|
||||||
|
|
||||||
|
$this->appManager->expects($this->any())
|
||||||
|
->method('isEnabledForUser')
|
||||||
|
->with('federatedfilesharing')
|
||||||
|
->willReturn(true);
|
||||||
|
|
||||||
$this->mailer->expects($this->once())->method('validateMailAddress')
|
$this->mailer->expects($this->once())->method('validateMailAddress')
|
||||||
->willReturn(true);
|
->willReturn(true);
|
||||||
|
|
||||||
$controller->expects($this->once())
|
$controller->expects($this->once())
|
||||||
->method('saveUserSettings')
|
->method('saveUserSettings');
|
||||||
->with($user, $expectedProperties)
|
|
||||||
->willReturnArgument(1);
|
|
||||||
|
|
||||||
$result = $controller->setUserSettings(
|
$controller->setUserSettings(
|
||||||
$avatarScope,
|
$avatarScope,
|
||||||
$displayName,
|
$displayName,
|
||||||
$displayNameScope,
|
$displayNameScope,
|
||||||
|
@ -369,12 +420,13 @@ class UsersControllerTest extends \Test\TestCase {
|
||||||
|
|
||||||
$this->userSession->method('getUser')->willReturn($user);
|
$this->userSession->method('getUser')->willReturn($user);
|
||||||
|
|
||||||
$defaultProperties = $this->getDefaultAccountManagerUserData();
|
$defaultProperties = []; //$this->getDefaultAccountMock();
|
||||||
|
|
||||||
|
$userAccount = $this->getDefaultAccountMock();
|
||||||
$this->accountManager->expects($this->once())
|
$this->accountManager->expects($this->once())
|
||||||
->method('getUser')
|
->method('getAccount')
|
||||||
->with($user)
|
->with($user)
|
||||||
->willReturn($defaultProperties);
|
->willReturn($userAccount);
|
||||||
|
|
||||||
$this->appManager->expects($this->any())
|
$this->appManager->expects($this->any())
|
||||||
->method('isEnabledForUser')
|
->method('isEnabledForUser')
|
||||||
|
@ -417,10 +469,9 @@ class UsersControllerTest extends \Test\TestCase {
|
||||||
|
|
||||||
$controller->expects($this->once())
|
$controller->expects($this->once())
|
||||||
->method('saveUserSettings')
|
->method('saveUserSettings')
|
||||||
->with($user, $expectedProperties)
|
->with($userAccount);
|
||||||
->willReturnArgument(1);
|
|
||||||
|
|
||||||
$result = $controller->setUserSettings(
|
$controller->setUserSettings(
|
||||||
$avatarScope,
|
$avatarScope,
|
||||||
$displayName,
|
$displayName,
|
||||||
$displayNameScope,
|
$displayNameScope,
|
||||||
|
@ -450,12 +501,13 @@ class UsersControllerTest extends \Test\TestCase {
|
||||||
|
|
||||||
$this->userSession->method('getUser')->willReturn($user);
|
$this->userSession->method('getUser')->willReturn($user);
|
||||||
|
|
||||||
$defaultProperties = $this->getDefaultAccountManagerUserData();
|
/** @var IAccount|MockObject $userAccount */
|
||||||
|
$userAccount = $this->getDefaultAccountMock();
|
||||||
|
|
||||||
$this->accountManager->expects($this->once())
|
$this->accountManager->expects($this->once())
|
||||||
->method('getUser')
|
->method('getAccount')
|
||||||
->with($user)
|
->with($user)
|
||||||
->willReturn($defaultProperties);
|
->willReturn($userAccount);
|
||||||
|
|
||||||
$avatarScope = ($property === 'avatarScope') ? $propertyValue : null;
|
$avatarScope = ($property === 'avatarScope') ? $propertyValue : null;
|
||||||
$displayName = ($property === 'displayName') ? $propertyValue : null;
|
$displayName = ($property === 'displayName') ? $propertyValue : null;
|
||||||
|
@ -471,46 +523,43 @@ class UsersControllerTest extends \Test\TestCase {
|
||||||
$twitter = ($property === 'twitter') ? $propertyValue : null;
|
$twitter = ($property === 'twitter') ? $propertyValue : null;
|
||||||
$twitterScope = ($property === 'twitterScope') ? $propertyValue : null;
|
$twitterScope = ($property === 'twitterScope') ? $propertyValue : null;
|
||||||
|
|
||||||
$expectedProperties = $defaultProperties;
|
/** @var IAccountProperty[]|MockObject[] $expectedProperties */
|
||||||
if ($property === 'avatarScope') {
|
$expectedProperties = $userAccount->getProperties();
|
||||||
$expectedProperties[IAccountManager::PROPERTY_AVATAR]['scope'] = $propertyValue;
|
$isScope = strrpos($property, 'Scope') === strlen($property) - strlen(5);
|
||||||
}
|
switch ($property) {
|
||||||
if ($property === 'displayName') {
|
case 'avatarScope':
|
||||||
$expectedProperties[IAccountManager::PROPERTY_DISPLAYNAME]['value'] = $propertyValue;
|
$propertyId = IAccountManager::PROPERTY_AVATAR;
|
||||||
}
|
break;
|
||||||
if ($property === 'displayNameScope') {
|
case 'displayName':
|
||||||
$expectedProperties[IAccountManager::PROPERTY_DISPLAYNAME]['scope'] = $propertyValue;
|
case 'displayNameScope':
|
||||||
}
|
$propertyId = IAccountManager::PROPERTY_DISPLAYNAME;
|
||||||
if ($property === 'phone') {
|
break;
|
||||||
$expectedProperties[IAccountManager::PROPERTY_PHONE]['value'] = $propertyValue;
|
case 'phone':
|
||||||
}
|
case 'phoneScope':
|
||||||
if ($property === 'phoneScope') {
|
$propertyId = IAccountManager::PROPERTY_PHONE;
|
||||||
$expectedProperties[IAccountManager::PROPERTY_PHONE]['scope'] = $propertyValue;
|
break;
|
||||||
}
|
case 'email':
|
||||||
if ($property === 'email') {
|
case 'emailScope':
|
||||||
$expectedProperties[IAccountManager::PROPERTY_EMAIL]['value'] = $propertyValue;
|
$propertyId = IAccountManager::PROPERTY_EMAIL;
|
||||||
}
|
break;
|
||||||
if ($property === 'emailScope') {
|
case 'website':
|
||||||
$expectedProperties[IAccountManager::PROPERTY_EMAIL]['scope'] = $propertyValue;
|
case 'websiteScope':
|
||||||
}
|
$propertyId = IAccountManager::PROPERTY_WEBSITE;
|
||||||
if ($property === 'website') {
|
break;
|
||||||
$expectedProperties[IAccountManager::PROPERTY_WEBSITE]['value'] = $propertyValue;
|
case 'address':
|
||||||
}
|
case 'addressScope':
|
||||||
if ($property === 'websiteScope') {
|
$propertyId = IAccountManager::PROPERTY_ADDRESS;
|
||||||
$expectedProperties[IAccountManager::PROPERTY_WEBSITE]['scope'] = $propertyValue;
|
break;
|
||||||
}
|
case 'twitter':
|
||||||
if ($property === 'address') {
|
case 'twitterScope':
|
||||||
$expectedProperties[IAccountManager::PROPERTY_ADDRESS]['value'] = $propertyValue;
|
$propertyId = IAccountManager::PROPERTY_TWITTER;
|
||||||
}
|
break;
|
||||||
if ($property === 'addressScope') {
|
default:
|
||||||
$expectedProperties[IAccountManager::PROPERTY_ADDRESS]['scope'] = $propertyValue;
|
$propertyId = '404';
|
||||||
}
|
|
||||||
if ($property === 'twitter') {
|
|
||||||
$expectedProperties[IAccountManager::PROPERTY_TWITTER]['value'] = $propertyValue;
|
|
||||||
}
|
|
||||||
if ($property === 'twitterScope') {
|
|
||||||
$expectedProperties[IAccountManager::PROPERTY_TWITTER]['scope'] = $propertyValue;
|
|
||||||
}
|
}
|
||||||
|
$expectedProperties[$propertyId]->expects($this->any())
|
||||||
|
->method($isScope ? 'getScope' : 'getValue')
|
||||||
|
->willReturn($propertyValue);
|
||||||
|
|
||||||
if (!empty($email)) {
|
if (!empty($email)) {
|
||||||
$this->mailer->expects($this->once())->method('validateMailAddress')
|
$this->mailer->expects($this->once())->method('validateMailAddress')
|
||||||
|
@ -519,10 +568,9 @@ class UsersControllerTest extends \Test\TestCase {
|
||||||
|
|
||||||
$controller->expects($this->once())
|
$controller->expects($this->once())
|
||||||
->method('saveUserSettings')
|
->method('saveUserSettings')
|
||||||
->with($user, $expectedProperties)
|
->with($userAccount);
|
||||||
->willReturnArgument(1);
|
|
||||||
|
|
||||||
$result = $controller->setUserSettings(
|
$controller->setUserSettings(
|
||||||
$avatarScope,
|
$avatarScope,
|
||||||
$displayName,
|
$displayName,
|
||||||
$displayNameScope,
|
$displayNameScope,
|
||||||
|
@ -593,10 +641,33 @@ class UsersControllerTest extends \Test\TestCase {
|
||||||
->willReturn(true);
|
->willReturn(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->accountManager->expects($this->once())->method('updateUser')
|
$properties = [];
|
||||||
->with($user, $data);
|
foreach ($data as $propertyName => $propertyData) {
|
||||||
|
$properties[$propertyName] = $this->createMock(IAccountProperty::class);
|
||||||
|
$properties[$propertyName]->expects($this->any())
|
||||||
|
->method('getValue')
|
||||||
|
->willReturn($propertyData['value']);
|
||||||
|
}
|
||||||
|
|
||||||
$this->invokePrivate($controller, 'saveUserSettings', [$user, $data]);
|
$account = $this->createMock(IAccount::class);
|
||||||
|
$account->expects($this->any())
|
||||||
|
->method('getUser')
|
||||||
|
->willReturn($user);
|
||||||
|
$account->expects($this->any())
|
||||||
|
->method('getProperty')
|
||||||
|
->willReturnCallback(function (string $propertyName) use ($properties) {
|
||||||
|
return $properties[$propertyName];
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->accountManager->expects($this->any())
|
||||||
|
->method('getAccount')
|
||||||
|
->willReturn($account);
|
||||||
|
|
||||||
|
$this->accountManager->expects($this->once())
|
||||||
|
->method('updateAccount')
|
||||||
|
->with($account);
|
||||||
|
|
||||||
|
$this->invokePrivate($controller, 'saveUserSettings', [$account]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function dataTestSaveUserSettings() {
|
public function dataTestSaveUserSettings() {
|
||||||
|
@ -655,21 +726,15 @@ class UsersControllerTest extends \Test\TestCase {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dataProvider dataTestSaveUserSettingsException
|
* @dataProvider dataTestSaveUserSettingsException
|
||||||
*
|
|
||||||
* @param array $data
|
|
||||||
* @param string $oldEmailAddress
|
|
||||||
* @param string $oldDisplayName
|
|
||||||
* @param bool $setDisplayNameResult
|
|
||||||
* @param bool $canChangeEmail
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public function testSaveUserSettingsException($data,
|
public function testSaveUserSettingsException(
|
||||||
$oldEmailAddress,
|
array $data,
|
||||||
$oldDisplayName,
|
string $oldEmailAddress,
|
||||||
$setDisplayNameResult,
|
string $oldDisplayName,
|
||||||
$canChangeEmail
|
bool $setDisplayNameResult,
|
||||||
|
bool $canChangeEmail
|
||||||
) {
|
) {
|
||||||
$this->expectException(\OC\ForbiddenException::class);
|
$this->expectException(ForbiddenException::class);
|
||||||
|
|
||||||
$controller = $this->getController();
|
$controller = $this->getController();
|
||||||
$user = $this->createMock(IUser::class);
|
$user = $this->createMock(IUser::class);
|
||||||
|
@ -677,6 +742,22 @@ class UsersControllerTest extends \Test\TestCase {
|
||||||
$user->method('getDisplayName')->willReturn($oldDisplayName);
|
$user->method('getDisplayName')->willReturn($oldDisplayName);
|
||||||
$user->method('getEMailAddress')->willReturn($oldEmailAddress);
|
$user->method('getEMailAddress')->willReturn($oldEmailAddress);
|
||||||
|
|
||||||
|
/** @var MockObject|IAccount $userAccount */
|
||||||
|
$userAccount = $this->createMock(IAccount::class);
|
||||||
|
$userAccount->expects($this->any())
|
||||||
|
->method('getUser')
|
||||||
|
->willReturn($user);
|
||||||
|
$propertyMocks = [];
|
||||||
|
foreach ($data as $propertyName => $propertyData) {
|
||||||
|
/** @var MockObject|IAccountProperty $property */
|
||||||
|
$propertyMocks[$propertyName] = $this->buildPropertyMock($propertyName, $propertyData['value'], '');
|
||||||
|
}
|
||||||
|
$userAccount->expects($this->any())
|
||||||
|
->method('getProperty')
|
||||||
|
->willReturnCallback(function (string $propertyName) use ($propertyMocks) {
|
||||||
|
return $propertyMocks[$propertyName];
|
||||||
|
});
|
||||||
|
|
||||||
if ($data[IAccountManager::PROPERTY_EMAIL]['value'] !== $oldEmailAddress) {
|
if ($data[IAccountManager::PROPERTY_EMAIL]['value'] !== $oldEmailAddress) {
|
||||||
$user->method('canChangeDisplayName')
|
$user->method('canChangeDisplayName')
|
||||||
->willReturn($canChangeEmail);
|
->willReturn($canChangeEmail);
|
||||||
|
@ -688,7 +769,7 @@ class UsersControllerTest extends \Test\TestCase {
|
||||||
->willReturn($setDisplayNameResult);
|
->willReturn($setDisplayNameResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->invokePrivate($controller, 'saveUserSettings', [$user, $data]);
|
$this->invokePrivate($controller, 'saveUserSettings', [$userAccount]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -748,15 +829,34 @@ class UsersControllerTest extends \Test\TestCase {
|
||||||
$controller = $this->getController(false, ['signMessage', 'getCurrentTime']);
|
$controller = $this->getController(false, ['signMessage', 'getCurrentTime']);
|
||||||
|
|
||||||
$user = $this->createMock(IUser::class);
|
$user = $this->createMock(IUser::class);
|
||||||
|
|
||||||
|
$property = $this->buildPropertyMock($type, $dataBefore[$type]['value'], '', IAccountManager::NOT_VERIFIED);
|
||||||
|
$property->expects($this->atLeastOnce())
|
||||||
|
->method('setVerified')
|
||||||
|
->with(IAccountManager::VERIFICATION_IN_PROGRESS)
|
||||||
|
->willReturnSelf();
|
||||||
|
$property->expects($this->atLeastOnce())
|
||||||
|
->method('setVerificationData')
|
||||||
|
->with($signature)
|
||||||
|
->willReturnSelf();
|
||||||
|
|
||||||
|
$userAccount = $this->createMock(IAccount::class);
|
||||||
|
$userAccount->expects($this->any())
|
||||||
|
->method('getUser')
|
||||||
|
->willReturn($user);
|
||||||
|
$userAccount->expects($this->any())
|
||||||
|
->method('getProperty')
|
||||||
|
->willReturn($property);
|
||||||
|
|
||||||
$this->userSession->expects($this->once())->method('getUser')->willReturn($user);
|
$this->userSession->expects($this->once())->method('getUser')->willReturn($user);
|
||||||
$this->accountManager->expects($this->once())->method('getUser')->with($user)->willReturn($dataBefore);
|
$this->accountManager->expects($this->once())->method('getAccount')->with($user)->willReturn($userAccount);
|
||||||
$user->expects($this->any())->method('getCloudId')->willReturn('user@nextcloud.com');
|
$user->expects($this->any())->method('getCloudId')->willReturn('user@nextcloud.com');
|
||||||
$user->expects($this->any())->method('getUID')->willReturn('uid');
|
$user->expects($this->any())->method('getUID')->willReturn('uid');
|
||||||
$controller->expects($this->once())->method('signMessage')->with($user, $message)->willReturn($signature);
|
$controller->expects($this->once())->method('signMessage')->with($user, $message)->willReturn($signature);
|
||||||
$controller->expects($this->any())->method('getCurrentTime')->willReturn(1234567);
|
$controller->expects($this->any())->method('getCurrentTime')->willReturn(1234567);
|
||||||
|
|
||||||
if ($onlyVerificationCode === false) {
|
if ($onlyVerificationCode === false) {
|
||||||
$this->accountManager->expects($this->once())->method('updateUser')->with($user, $expectedData)->willReturnArgument(1);
|
$this->accountManager->expects($this->once())->method('updateAccount')->with($userAccount)->willReturnArgument(1);
|
||||||
$this->jobList->expects($this->once())->method('add')
|
$this->jobList->expects($this->once())->method('add')
|
||||||
->with('OCA\Settings\BackgroundJobs\VerifyUserData',
|
->with('OCA\Settings\BackgroundJobs\VerifyUserData',
|
||||||
[
|
[
|
||||||
|
|
|
@ -44,8 +44,8 @@ class Account implements IAccount {
|
||||||
$this->user = $user;
|
$this->user = $user;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setProperty(string $property, string $value, string $scope, string $verified): IAccount {
|
public function setProperty(string $property, string $value, string $scope, string $verified, string $verificationData = ''): IAccount {
|
||||||
$this->properties[$property] = new AccountProperty($property, $value, $scope, $verified);
|
$this->properties[$property] = new AccountProperty($property, $value, $scope, $verified, $verificationData);
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -39,12 +39,15 @@ class AccountProperty implements IAccountProperty {
|
||||||
private $scope;
|
private $scope;
|
||||||
/** @var string */
|
/** @var string */
|
||||||
private $verified;
|
private $verified;
|
||||||
|
/** @var string */
|
||||||
|
private $verificationData;
|
||||||
|
|
||||||
public function __construct(string $name, string $value, string $scope, string $verified) {
|
public function __construct(string $name, string $value, string $scope, string $verified, string $verificationData) {
|
||||||
$this->name = $name;
|
$this->name = $name;
|
||||||
$this->value = $value;
|
$this->value = $value;
|
||||||
$this->setScope($scope);
|
$this->setScope($scope);
|
||||||
$this->verified = $verified;
|
$this->verified = $verified;
|
||||||
|
$this->verificationData = $verificationData;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function jsonSerialize() {
|
public function jsonSerialize() {
|
||||||
|
@ -52,7 +55,8 @@ class AccountProperty implements IAccountProperty {
|
||||||
'name' => $this->getName(),
|
'name' => $this->getName(),
|
||||||
'value' => $this->getValue(),
|
'value' => $this->getValue(),
|
||||||
'scope' => $this->getScope(),
|
'scope' => $this->getScope(),
|
||||||
'verified' => $this->getVerified()
|
'verified' => $this->getVerified(),
|
||||||
|
'verificationData' => $this->getVerificationData(),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -164,4 +168,13 @@ class AccountProperty implements IAccountProperty {
|
||||||
public function getVerified(): string {
|
public function getVerified(): string {
|
||||||
return $this->verified;
|
return $this->verified;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function setVerificationData(string $verificationData): IAccountProperty {
|
||||||
|
$this->verificationData = $verificationData;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getVerificationData(): string {
|
||||||
|
return $this->verificationData;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,9 +44,10 @@ interface IAccount extends \JsonSerializable {
|
||||||
* @param string $value
|
* @param string $value
|
||||||
* @param string $scope Must be one of the VISIBILITY_ prefixed constants of \OCP\Accounts\IAccountManager
|
* @param string $scope Must be one of the VISIBILITY_ prefixed constants of \OCP\Accounts\IAccountManager
|
||||||
* @param string $verified \OCP\Accounts\IAccountManager::NOT_VERIFIED | \OCP\Accounts\IAccountManager::VERIFICATION_IN_PROGRESS | \OCP\Accounts\IAccountManager::VERIFIED
|
* @param string $verified \OCP\Accounts\IAccountManager::NOT_VERIFIED | \OCP\Accounts\IAccountManager::VERIFICATION_IN_PROGRESS | \OCP\Accounts\IAccountManager::VERIFIED
|
||||||
|
* @param string $verificationData Optional, defaults to empty string. Since @22.0.0.
|
||||||
* @return IAccount
|
* @return IAccount
|
||||||
*/
|
*/
|
||||||
public function setProperty(string $property, string $value, string $scope, string $verified): IAccount;
|
public function setProperty(string $property, string $value, string $scope, string $verified, string $verificationData = ''): IAccount;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a property by its key
|
* Get a property by its key
|
||||||
|
@ -60,7 +61,7 @@ interface IAccount extends \JsonSerializable {
|
||||||
public function getProperty(string $property): IAccountProperty;
|
public function getProperty(string $property): IAccountProperty;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get all properties of an account
|
* Get all properties of an account. Array indices are property names.
|
||||||
*
|
*
|
||||||
* @since 15.0.0
|
* @since 15.0.0
|
||||||
*
|
*
|
||||||
|
|
|
@ -101,4 +101,18 @@ interface IAccountProperty extends \JsonSerializable {
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getVerified(): string;
|
public function getVerified(): string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets data for verification purposes.
|
||||||
|
*
|
||||||
|
* @since 22.0.0
|
||||||
|
*/
|
||||||
|
public function setVerificationData(string $verificationData): IAccountProperty;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves data for verification purposes.
|
||||||
|
*
|
||||||
|
* @since 22.0.0
|
||||||
|
*/
|
||||||
|
public function getVerificationData(): string;
|
||||||
}
|
}
|
||||||
|
|
|
@ -106,6 +106,7 @@ class AccountManagerTest extends TestCase {
|
||||||
/** @var IUser $user */
|
/** @var IUser $user */
|
||||||
$user = $this->createMock(IUser::class);
|
$user = $this->createMock(IUser::class);
|
||||||
|
|
||||||
|
// FIXME: should be an integration test instead of this abomination
|
||||||
$accountManager->expects($this->once())->method('getUser')->with($user)->willReturn($oldData);
|
$accountManager->expects($this->once())->method('getUser')->with($user)->willReturn($oldData);
|
||||||
|
|
||||||
if ($updateExisting) {
|
if ($updateExisting) {
|
||||||
|
@ -147,9 +148,9 @@ class AccountManagerTest extends TestCase {
|
||||||
|
|
||||||
public function dataTrueFalse() {
|
public function dataTrueFalse() {
|
||||||
return [
|
return [
|
||||||
[['newData'], ['oldData'], false, true],
|
[['myProperty' => ['value' => 'newData']], ['myProperty' => ['value' => 'oldData']], false, true],
|
||||||
[['newData'], [], true, false],
|
[['myProperty' => ['value' => 'newData']], [], true, false],
|
||||||
[['oldData'], ['oldData'], false, false]
|
[['myProperty' => ['value' => 'oldData']], ['myProperty' => ['value' => 'oldData']], false, false]
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -38,7 +38,8 @@ class AccountPropertyTest extends TestCase {
|
||||||
IAccountManager::PROPERTY_WEBSITE,
|
IAccountManager::PROPERTY_WEBSITE,
|
||||||
'https://example.com',
|
'https://example.com',
|
||||||
IAccountManager::SCOPE_PUBLISHED,
|
IAccountManager::SCOPE_PUBLISHED,
|
||||||
IAccountManager::VERIFIED
|
IAccountManager::VERIFIED,
|
||||||
|
''
|
||||||
);
|
);
|
||||||
$this->assertEquals(IAccountManager::PROPERTY_WEBSITE, $accountProperty->getName());
|
$this->assertEquals(IAccountManager::PROPERTY_WEBSITE, $accountProperty->getName());
|
||||||
$this->assertEquals('https://example.com', $accountProperty->getValue());
|
$this->assertEquals('https://example.com', $accountProperty->getValue());
|
||||||
|
@ -51,7 +52,8 @@ class AccountPropertyTest extends TestCase {
|
||||||
IAccountManager::PROPERTY_WEBSITE,
|
IAccountManager::PROPERTY_WEBSITE,
|
||||||
'https://example.com',
|
'https://example.com',
|
||||||
IAccountManager::SCOPE_PUBLISHED,
|
IAccountManager::SCOPE_PUBLISHED,
|
||||||
IAccountManager::VERIFIED
|
IAccountManager::VERIFIED,
|
||||||
|
''
|
||||||
);
|
);
|
||||||
$actualReturn = $accountProperty->setValue('https://example.org');
|
$actualReturn = $accountProperty->setValue('https://example.org');
|
||||||
$this->assertEquals('https://example.org', $accountProperty->getValue());
|
$this->assertEquals('https://example.org', $accountProperty->getValue());
|
||||||
|
@ -63,7 +65,8 @@ class AccountPropertyTest extends TestCase {
|
||||||
IAccountManager::PROPERTY_WEBSITE,
|
IAccountManager::PROPERTY_WEBSITE,
|
||||||
'https://example.com',
|
'https://example.com',
|
||||||
IAccountManager::SCOPE_PUBLISHED,
|
IAccountManager::SCOPE_PUBLISHED,
|
||||||
IAccountManager::VERIFIED
|
IAccountManager::VERIFIED,
|
||||||
|
''
|
||||||
);
|
);
|
||||||
$actualReturn = $accountProperty->setScope(IAccountManager::SCOPE_LOCAL);
|
$actualReturn = $accountProperty->setScope(IAccountManager::SCOPE_LOCAL);
|
||||||
$this->assertEquals(IAccountManager::SCOPE_LOCAL, $accountProperty->getScope());
|
$this->assertEquals(IAccountManager::SCOPE_LOCAL, $accountProperty->getScope());
|
||||||
|
@ -98,7 +101,8 @@ class AccountPropertyTest extends TestCase {
|
||||||
IAccountManager::PROPERTY_WEBSITE,
|
IAccountManager::PROPERTY_WEBSITE,
|
||||||
'https://example.com',
|
'https://example.com',
|
||||||
$storedScope,
|
$storedScope,
|
||||||
IAccountManager::VERIFIED
|
IAccountManager::VERIFIED,
|
||||||
|
''
|
||||||
);
|
);
|
||||||
$this->assertEquals($returnedScope, $accountProperty->getScope());
|
$this->assertEquals($returnedScope, $accountProperty->getScope());
|
||||||
}
|
}
|
||||||
|
@ -108,25 +112,42 @@ class AccountPropertyTest extends TestCase {
|
||||||
IAccountManager::PROPERTY_WEBSITE,
|
IAccountManager::PROPERTY_WEBSITE,
|
||||||
'https://example.com',
|
'https://example.com',
|
||||||
IAccountManager::SCOPE_PUBLISHED,
|
IAccountManager::SCOPE_PUBLISHED,
|
||||||
IAccountManager::VERIFIED
|
IAccountManager::VERIFIED,
|
||||||
|
''
|
||||||
);
|
);
|
||||||
$actualReturn = $accountProperty->setVerified(IAccountManager::NOT_VERIFIED);
|
$actualReturn = $accountProperty->setVerified(IAccountManager::NOT_VERIFIED);
|
||||||
$this->assertEquals(IAccountManager::NOT_VERIFIED, $accountProperty->getVerified());
|
$this->assertEquals(IAccountManager::NOT_VERIFIED, $accountProperty->getVerified());
|
||||||
$this->assertEquals(IAccountManager::NOT_VERIFIED, $actualReturn->getVerified());
|
$this->assertEquals(IAccountManager::NOT_VERIFIED, $actualReturn->getVerified());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSetVerificationData() {
|
||||||
|
$accountProperty = new AccountProperty(
|
||||||
|
IAccountManager::PROPERTY_WEBSITE,
|
||||||
|
'https://example.com',
|
||||||
|
IAccountManager::SCOPE_PUBLISHED,
|
||||||
|
IAccountManager::VERIFIED,
|
||||||
|
''
|
||||||
|
);
|
||||||
|
$token = uniqid();
|
||||||
|
$actualReturn = $accountProperty->setVerificationData($token);
|
||||||
|
$this->assertEquals($token, $accountProperty->getVerificationData());
|
||||||
|
$this->assertEquals($token, $actualReturn->getVerificationData());
|
||||||
|
}
|
||||||
|
|
||||||
public function testJsonSerialize() {
|
public function testJsonSerialize() {
|
||||||
$accountProperty = new AccountProperty(
|
$accountProperty = new AccountProperty(
|
||||||
IAccountManager::PROPERTY_WEBSITE,
|
IAccountManager::PROPERTY_WEBSITE,
|
||||||
'https://example.com',
|
'https://example.com',
|
||||||
IAccountManager::SCOPE_PUBLISHED,
|
IAccountManager::SCOPE_PUBLISHED,
|
||||||
IAccountManager::VERIFIED
|
IAccountManager::VERIFIED,
|
||||||
|
'60a7a633b74af',
|
||||||
);
|
);
|
||||||
$this->assertEquals([
|
$this->assertEquals([
|
||||||
'name' => IAccountManager::PROPERTY_WEBSITE,
|
'name' => IAccountManager::PROPERTY_WEBSITE,
|
||||||
'value' => 'https://example.com',
|
'value' => 'https://example.com',
|
||||||
'scope' => IAccountManager::SCOPE_PUBLISHED,
|
'scope' => IAccountManager::SCOPE_PUBLISHED,
|
||||||
'verified' => IAccountManager::VERIFIED
|
'verified' => IAccountManager::VERIFIED,
|
||||||
|
'verificationData' => '60a7a633b74af'
|
||||||
], $accountProperty->jsonSerialize());
|
], $accountProperty->jsonSerialize());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,7 +43,7 @@ class AccountTest extends TestCase {
|
||||||
|
|
||||||
public function testSetProperty() {
|
public function testSetProperty() {
|
||||||
$user = $this->createMock(IUser::class);
|
$user = $this->createMock(IUser::class);
|
||||||
$property = new AccountProperty(IAccountManager::PROPERTY_WEBSITE, 'https://example.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED);
|
$property = new AccountProperty(IAccountManager::PROPERTY_WEBSITE, 'https://example.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED, '');
|
||||||
$account = new Account($user);
|
$account = new Account($user);
|
||||||
$account->setProperty(IAccountManager::PROPERTY_WEBSITE, 'https://example.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED);
|
$account->setProperty(IAccountManager::PROPERTY_WEBSITE, 'https://example.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED);
|
||||||
$this->assertEquals($property, $account->getProperty(IAccountManager::PROPERTY_WEBSITE));
|
$this->assertEquals($property, $account->getProperty(IAccountManager::PROPERTY_WEBSITE));
|
||||||
|
@ -52,8 +52,8 @@ class AccountTest extends TestCase {
|
||||||
public function testGetProperties() {
|
public function testGetProperties() {
|
||||||
$user = $this->createMock(IUser::class);
|
$user = $this->createMock(IUser::class);
|
||||||
$properties = [
|
$properties = [
|
||||||
IAccountManager::PROPERTY_WEBSITE => new AccountProperty(IAccountManager::PROPERTY_WEBSITE, 'https://example.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED),
|
IAccountManager::PROPERTY_WEBSITE => new AccountProperty(IAccountManager::PROPERTY_WEBSITE, 'https://example.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED, ''),
|
||||||
IAccountManager::PROPERTY_EMAIL => new AccountProperty(IAccountManager::PROPERTY_EMAIL, 'user@example.com', IAccountManager::SCOPE_LOCAL, IAccountManager::VERIFIED)
|
IAccountManager::PROPERTY_EMAIL => new AccountProperty(IAccountManager::PROPERTY_EMAIL, 'user@example.com', IAccountManager::SCOPE_LOCAL, IAccountManager::VERIFIED, '')
|
||||||
];
|
];
|
||||||
$account = new Account($user);
|
$account = new Account($user);
|
||||||
$account->setProperty(IAccountManager::PROPERTY_WEBSITE, 'https://example.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED);
|
$account->setProperty(IAccountManager::PROPERTY_WEBSITE, 'https://example.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED);
|
||||||
|
@ -65,9 +65,9 @@ class AccountTest extends TestCase {
|
||||||
public function testGetFilteredProperties() {
|
public function testGetFilteredProperties() {
|
||||||
$user = $this->createMock(IUser::class);
|
$user = $this->createMock(IUser::class);
|
||||||
$properties = [
|
$properties = [
|
||||||
IAccountManager::PROPERTY_WEBSITE => new AccountProperty(IAccountManager::PROPERTY_WEBSITE, 'https://example.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED),
|
IAccountManager::PROPERTY_WEBSITE => new AccountProperty(IAccountManager::PROPERTY_WEBSITE, 'https://example.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED, ''),
|
||||||
IAccountManager::PROPERTY_EMAIL => new AccountProperty(IAccountManager::PROPERTY_EMAIL, 'user@example.com', IAccountManager::SCOPE_LOCAL, IAccountManager::VERIFIED),
|
IAccountManager::PROPERTY_EMAIL => new AccountProperty(IAccountManager::PROPERTY_EMAIL, 'user@example.com', IAccountManager::SCOPE_LOCAL, IAccountManager::VERIFIED, ''),
|
||||||
IAccountManager::PROPERTY_PHONE => new AccountProperty(IAccountManager::PROPERTY_PHONE, '123456', IAccountManager::SCOPE_PUBLISHED, IAccountManager::VERIFIED),
|
IAccountManager::PROPERTY_PHONE => new AccountProperty(IAccountManager::PROPERTY_PHONE, '123456', IAccountManager::SCOPE_PUBLISHED, IAccountManager::VERIFIED, ''),
|
||||||
];
|
];
|
||||||
$account = new Account($user);
|
$account = new Account($user);
|
||||||
$account->setProperty(IAccountManager::PROPERTY_WEBSITE, 'https://example.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED);
|
$account->setProperty(IAccountManager::PROPERTY_WEBSITE, 'https://example.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED);
|
||||||
|
@ -98,8 +98,8 @@ class AccountTest extends TestCase {
|
||||||
public function testJsonSerialize() {
|
public function testJsonSerialize() {
|
||||||
$user = $this->createMock(IUser::class);
|
$user = $this->createMock(IUser::class);
|
||||||
$properties = [
|
$properties = [
|
||||||
IAccountManager::PROPERTY_WEBSITE => new AccountProperty(IAccountManager::PROPERTY_WEBSITE, 'https://example.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED),
|
IAccountManager::PROPERTY_WEBSITE => new AccountProperty(IAccountManager::PROPERTY_WEBSITE, 'https://example.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED, ''),
|
||||||
IAccountManager::PROPERTY_EMAIL => new AccountProperty(IAccountManager::PROPERTY_EMAIL, 'user@example.com', IAccountManager::SCOPE_LOCAL, IAccountManager::VERIFIED)
|
IAccountManager::PROPERTY_EMAIL => new AccountProperty(IAccountManager::PROPERTY_EMAIL, 'user@example.com', IAccountManager::SCOPE_LOCAL, IAccountManager::VERIFIED, '')
|
||||||
];
|
];
|
||||||
$account = new Account($user);
|
$account = new Account($user);
|
||||||
$account->setProperty(IAccountManager::PROPERTY_WEBSITE, 'https://example.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED);
|
$account->setProperty(IAccountManager::PROPERTY_WEBSITE, 'https://example.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED);
|
||||||
|
|
Loading…
Reference in New Issue