Skip to content

Commit

Permalink
get userId from IUserSession, renaming of variables
Browse files Browse the repository at this point in the history
Signed-off-by: call-me-matt <nextcloud@matthiasheinisch.de>
  • Loading branch information
call-me-matt committed Jul 31, 2020
1 parent 64187bd commit d64317c
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 37 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Nextcloud Contacts
![Downloads](https://img.shields.io/github/downloads/nextcloud/contacts/total?style=flat-square)
![Downloads](https://img.shields.io/github/downloads/nextcloud/contacts/total.svg?style=flat-square)
[![Codacy Badge](https://img.shields.io/codacy/grade/ea24ea9fccb942419d73ec05105938aa.svg?style=flat-square)](https://app.codacy.com/app/skjnldsv/contacts)
[![Code coverage](https://img.shields.io/codecov/c/github/nextcloud/contacts.svg?style=flat-square)](https://codecov.io/gh/nextcloud/contacts/)
[![Dependabot status](https://img.shields.io/badge/Dependabot-enabled-brightgreen.svg?longCache=true&style=flat-square&logo=dependabot)](https://dependabot.com)
Expand Down
16 changes: 11 additions & 5 deletions lib/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

use OCP\IConfig;
use OCP\IInitialStateService;
use OCP\IUserSession;
use OCP\IRequest;
use OCP\L10N\IFactory;
use OCP\Util;
Expand All @@ -46,6 +47,9 @@ class PageController extends Controller {
/** @var IFactory */
private $languageFactory;

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

/** @var SocialApiService */
private $socialApiService;

Expand All @@ -54,13 +58,15 @@ public function __construct(string $appName,
IConfig $config,
IInitialStateService $initialStateService,
IFactory $languageFactory,
IUserSession $userSession,
SocialApiService $socialApiService) {
parent::__construct($appName, $request);

$this->appName = $appName;
$this->config = $config;
$this->initialStateService = $initialStateService;
$this->languageFactory = $languageFactory;
$this->userSession = $userSession;
$this->socialApiService = $socialApiService;
}

Expand All @@ -71,19 +77,19 @@ public function __construct(string $appName,
* Default routing
*/
public function index(): TemplateResponse {
$userId = \OC_User::getUser();
$userId = $this->userSession->getUser()->getUid();

$locales = $this->languageFactory->findAvailableLocales();
$defaultProfile = $this->config->getAppValue($this->appName, 'defaultProfile', 'HOME');
$supportedNetworks = $this->socialApiService->getSupportedNetworks();
$allowSocialSync = $this->config->getAppValue($this->appName, 'allowSocialSync', 'yes'); // allow users to retrieve avatars from social networks (default: yes)
$enableSocialSync = $this->config->getUserValue($userId, $this->appName, 'enableSocialSync', 'no'); // automated background syncs for social avatars (default: no)
$isSocialEnabledByAdmin = $this->config->getAppValue($this->appName, 'allowSocialSync', 'yes'); // allow users to retrieve avatars from social networks (default: yes)
$isSocialEnabledByUser = $this->config->getUserValue($userId, $this->appName, 'enableSocialSync', 'no'); // automated background syncs for social avatars (default: no)

$this->initialStateService->provideInitialState($this->appName, 'locales', $locales);
$this->initialStateService->provideInitialState($this->appName, 'defaultProfile', $defaultProfile);
$this->initialStateService->provideInitialState($this->appName, 'supportedNetworks', $supportedNetworks);
$this->initialStateService->provideInitialState($this->appName, 'allowSocialSync', $allowSocialSync);
$this->initialStateService->provideInitialState($this->appName, 'enableSocialSync', $enableSocialSync);
$this->initialStateService->provideInitialState($this->appName, 'allowSocialSync', $isSocialEnabledByAdmin);
$this->initialStateService->provideInitialState($this->appName, 'enableSocialSync', $isSocialEnabledByUser);

Util::addScript($this->appName, 'contacts');
Util::addStyle($this->appName, 'contacts');
Expand Down
14 changes: 10 additions & 4 deletions lib/Controller/SocialApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,27 @@
use OCP\AppFramework\Http\JSONResponse;
use OCP\IConfig;
use OCP\IRequest;
use OCP\IUserSession;

class SocialApiController extends ApiController {

/** @var IConfig */
private $config;

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

/** @var SocialApiService */
private $socialApiService;

public function __construct(IRequest $request,
IConfig $config,
IUserSession $userSession,
SocialApiService $socialApiService) {
parent::__construct(Application::APP_ID, $request);

$this->config = $config;
$this->userSession = $userSession;
$this->socialApiService = $socialApiService;
}

Expand Down Expand Up @@ -77,8 +83,8 @@ public function setAppConfig($key, $allow) {
* @returns {JSONResponse} an empty JSONResponse with respective http status code
*/
public function setUserConfig($key, $allow) {
$user = \OC_User::getUser();
$this->config->setUserValue($user, $this->appName, $key, $allow);
$userId = $this->userSession->getUser()->getUid();
$this->config->setUserValue($userId, $this->appName, $key, $allow);
return new JSONResponse([], Http::STATUS_OK);
}

Expand All @@ -93,8 +99,8 @@ public function setUserConfig($key, $allow) {
* @returns {string} the desired value or null if not existing
*/
public function getUserConfig($key) {
$user = \OC_User::getUser();
return $this->config->getUserValue($user, $this->appName, $key, 'null');
$userId = $this->userSession->getUser()->getUid();
return $this->config->getUserValue($userId, $this->appName, $key, 'null');
}


Expand Down
8 changes: 4 additions & 4 deletions lib/Cron/SocialUpdateRegistration.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,16 @@ public function __construct(string $AppName,
protected function run($arguments) {

// check if admin allows for social updates:
$isAdminEnabled = $this->config->getAppValue($this->appName, 'allowSocialSync', 'yes');
if (!($isAdminEnabled === 'yes')) {
$isSocialEnabledByAdmin = $this->config->getAppValue($this->appName, 'allowSocialSync', 'yes');
if (!($isSocialEnabledByAdmin === 'yes')) {
return;
}

$this->userManager->callForSeenUsers(function (IUser $user) {

// check that user opted-in:
$isUserEnabled = $this->config->getUserValue($user->getUID(), $this->appName, 'enableSocialSync', 'no');
if ($isUserEnabled === 'yes') {
$isSocialEnabledByUser = $this->config->getUserValue($user->getUID(), $this->appName, 'enableSocialSync', 'no');
if ($isSocialEnabledByUser === 'yes') {
$this->jobList->add(SocialUpdate::class, [
'userId' => $user->getUID()
]);
Expand Down
46 changes: 23 additions & 23 deletions lib/Service/SocialApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ public function __construct(
* @returns {array} array of the supported social networks
*/
public function getSupportedNetworks() : array {
$isAdminEnabled = $this->config->getAppValue(Application::APP_ID, 'allowSocialSync', 'yes');
if ($isAdminEnabled !== 'yes') {
$isSocialEnabledByAdmin = $this->config->getAppValue(Application::APP_ID, 'allowSocialSync', 'yes');
if ($isSocialEnabledByAdmin !== 'yes') {
return [];
}
return $this->socialProvider->getSupportedNetworks();
Expand Down Expand Up @@ -146,12 +146,12 @@ protected function getAddressBook(string $addressbookId) : ?IAddressBook {
*
* Retrieves and initiates all addressbooks from a user
*
* @param {string} user the user to query
* @param {string} userId the user to query
* @param {IManager} the contact manager to load
*/
protected function registerAddressbooks($user, IManager $manager) {
protected function registerAddressbooks($userId, IManager $manager) {
$cm = new ContactsManager($this->davBackend, $this->l10n);
$cm->setupContactsProvider($manager, $user, $this->urlGen);
$cm->setupContactsProvider($manager, $userId, $this->urlGen);
//FIXME: better would be: davBackend->getUsersOwnAddressBooks($principal); (no shared or system address books)
// ... or the promising IAddressBookProvider, which I cant get running
$this->manager = $manager;
Expand Down Expand Up @@ -224,19 +224,16 @@ public function updateContact(string $addressbookId, string $contactId, string $
* // TODO: how to exclude certain contacts?
*
* @param {String} network the social network to use (fallback: take first match)
* @param {String} user the address book owner
* @param {String} userId the address book owner
*
* @returns {JSONResponse} JSONResponse with the list of changed and failed contacts
*/
public function updateAddressbooks(string $network, string $user) : JSONResponse {
public function updateAddressbooks(string $network, string $userId) : JSONResponse {

// double check!
$isAdminAllowed = $this->config->getAppValue(Application::APP_ID, 'allowSocialSync', 'yes');
if (!($isAdminAllowed === 'yes')) {
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}
$isUserEnabled = $this->config->getUserValue($user, Application::APP_ID, 'enableSocialSync', 'no');
if ($isUserEnabled !== 'yes') {
$isSocialEnabledByAdmin = $this->config->getAppValue(Application::APP_ID, 'allowSocialSync', 'yes');
$isSocialEnabledByUser = $this->config->getUserValue($userId, Application::APP_ID, 'enableSocialSync', 'no');
if (($isSocialEnabledByAdmin !== 'yes') || ($isSocialEnabledByUser !== 'yes')) {
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}

Expand All @@ -248,7 +245,7 @@ public function updateAddressbooks(string $network, string $user) : JSONResponse
];

// get corresponding addressbook
$this->registerAddressbooks($user, $this->manager);
$this->registerAddressbooks($userId, $this->manager);

$addressBooks = $this->manager->getUserAddressBooks();
// TODO: filter out system addr books
Expand Down Expand Up @@ -276,15 +273,18 @@ public function updateAddressbooks(string $network, string $user) : JSONResponse
try {
$r = $this->updateContact($addressBook->getURI(), $contact['UID'], $network);

if ($r->getStatus() === Http::STATUS_OK) {
array_push($response['updated'], $contact['FN']);
} elseif ($r->getStatus() === Http::STATUS_NOT_MODIFIED) {
array_push($response['checked'], $contact['FN']);
} else {
if (!isset($response['failed'][$r->getStatus()])) {
$response['failed'][$r->getStatus()] = [];
}
array_push($response['failed'][$r->getStatus()], $contact['FN']);
switch ($r->getStatus()) {
case Http::STATUS_OK:
array_push($response['updated'], $contact['FN']);
break;
case Http::STATUS_NOT_MODIFIED:
array_push($response['checked'], $contact['FN']);
break;
default:
if (!isset($response['failed'][$r->getStatus()])) {
$response['failed'][$r->getStatus()] = [];
}
array_push($response['failed'][$r->getStatus()], $contact['FN']);
}
} catch (Exception $e) {
array_push($response['failed'], $contact['FN']);
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/Controller/PageControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* @copyright Copyright (c) 2019 John Molakvoæ <skjnldsv@protonmail.com>
*
* @author John Molakvoæ <skjnldsv@protonmail.com>
* @author Matthias Heinisch <nextcloud@matthiasheinisch.de>
*
* @license GNU AGPL version 3 or any later version
*
Expand All @@ -27,6 +28,8 @@
use OCP\IConfig;
use PHPUnit\Framework\MockObject\MockObject;
use OCP\IInitialStateService;
use OCP\IUser;
use OCP\IUserSession;
use OCP\IRequest;
use OCP\L10N\IFactory;
use OCA\Contacts\Service\SocialApiService;
Expand All @@ -47,6 +50,9 @@ class PageControllerTest extends TestCase {
/** @var IFactory|MockObject */
private $languageFactory;

/** @var IUserSession|MockObject */
private $userSession;

/** @var SocialApiService|MockObject*/
private $socialApi;

Expand All @@ -57,6 +63,7 @@ public function setUp() {
$this->config = $this->createMock(IConfig::class);
$this->initialStateService = $this->createMock(IInitialStateService::class);
$this->languageFactory = $this->createMock(IFactory::class);
$this->userSession = $this->createMock(IUserSession::class);
$this->socialApi = $this->createMock(SocialApiService::class);

$this->controller = new PageController(
Expand All @@ -65,12 +72,17 @@ public function setUp() {
$this->config,
$this->initialStateService,
$this->languageFactory,
$this->userSession,
$this->socialApi
);
}


public function testIndex() {
$user = $this->createMock(IUser::class);
$user->method('getUid')->willReturn('mrstest');
$this->userSession->method('getUser')->willReturn($user);

$result = $this->controller->index();

$this->assertEquals('main', $result->getTemplateName());
Expand Down

0 comments on commit d64317c

Please sign in to comment.