fix: early user detection caused errors

This commit is contained in:
korelstar 2018-09-16 17:21:43 +02:00
parent 8c2cb3dfab
commit b136e57352
4 changed files with 40 additions and 29 deletions

View File

@ -15,6 +15,7 @@ use OCP\AppFramework\ApiController;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\IRequest;
use OCP\IUserSession;
use OCA\Notes\Service\NotesService;
use OCA\Notes\Service\MetaService;
@ -33,22 +34,25 @@ class NotesApiController extends ApiController {
private $service;
/** @var MetaService */
private $metaService;
/** @var string */
private $userId;
/** @var IUserSession */
private $userSession;
/**
* @param string $AppName
* @param IRequest $request
* @param NotesService $service
* @param string $UserId
* @param IUserSession $userSession
*/
public function __construct($AppName, IRequest $request, NotesService $service, MetaService $metaService, $UserId) {
public function __construct($AppName, IRequest $request, NotesService $service, MetaService $metaService, IUserSession $userSession) {
parent::__construct($AppName, $request);
$this->service = $service;
$this->metaService = $metaService;
$this->userId = $UserId;
$this->userSession = $userSession;
}
private function getUID() {
return $this->userSession->getUser()->getUID();
}
/**
* @param Note $note
@ -79,8 +83,8 @@ class NotesApiController extends ApiController {
public function index($exclude='', $pruneBefore=0) {
$exclude = explode(',', $exclude);
$now = new \DateTime(); // this must be before loading notes if there are concurrent changes possible
$notes = $this->service->getAll($this->userId);
$metas = $this->metaService->updateAll($this->userId, $notes);
$notes = $this->service->getAll($this->getUID());
$metas = $this->metaService->updateAll($this->getUID(), $notes);
foreach ($notes as $note) {
$lastUpdate = $metas[$note->getId()]->getLastUpdate();
if($pruneBefore && $lastUpdate<$pruneBefore) {
@ -114,7 +118,7 @@ class NotesApiController extends ApiController {
$exclude = explode(',', $exclude);
return $this->respond(function () use ($id, $exclude) {
$note = $this->service->get($id, $this->userId);
$note = $this->service->get($id, $this->getUID());
$note = $this->excludeFields($note, $exclude);
return $note;
});
@ -134,7 +138,7 @@ class NotesApiController extends ApiController {
*/
public function create($content, $category=null, $modified=0, $favorite=null) {
return $this->respond(function () use ($content, $category, $modified, $favorite) {
$note = $this->service->create($this->userId);
$note = $this->service->create($this->getUID());
return $this->updateData($note->getId(), $content, $category, $modified, $favorite);
});
}
@ -168,12 +172,12 @@ class NotesApiController extends ApiController {
*/
private function updateData($id, $content, $category, $modified, $favorite) {
if($favorite!==null) {
$this->service->favorite($id, $favorite, $this->userId);
$this->service->favorite($id, $favorite, $this->getUID());
}
if($content===null) {
return $this->service->get($id, $this->userId);
return $this->service->get($id, $this->getUID());
} else {
return $this->service->update($id, $content, $this->userId, $category, $modified);
return $this->service->update($id, $content, $this->getUID(), $category, $modified);
}
}
@ -187,7 +191,7 @@ class NotesApiController extends ApiController {
*/
public function destroy($id) {
return $this->respond(function () use ($id) {
$this->service->delete($id, $this->userId);
$this->service->delete($id, $this->getUID());
return [];
});
}

View File

@ -13,14 +13,21 @@ use OCA\Notes\Service\SettingsService;
class SettingsController extends Controller
{
private $service;
private $userSession;
public function __construct(
$appName,
IRequest $request,
SettingsService $service
SettingsService $service,
IUserSession $userSession
) {
parent::__construct($appName, $request);
$this->service = $service;
$this->userSession = $userSession;
}
private function getUID() {
return $this->userSession->getUser()->getUID();
}
/**
@ -28,7 +35,10 @@ class SettingsController extends Controller
* @throws \OCP\PreConditionNotMetException
*/
public function set() {
$this->service->set($this->request->getParams());
$this->service->set(
$this->getUID(),
$this->request->getParams()
);
return $this->get();
}
@ -36,6 +46,6 @@ class SettingsController extends Controller
* @NoAdminRequired
*/
public function get() {
return new JSONResponse($this->service->getAll());
return new JSONResponse($this->service->getAll($this->getUID()));
}
}

View File

@ -133,7 +133,7 @@ class NotesService {
// check new note exists already and we need to number it
// pass -1 because no file has id -1 and that will ensure
// to only return filenames that dont yet exist
$path = $this->generateFileName($folder, $title, $this->settings->get('fileSuffix'), -1);
$path = $this->generateFileName($folder, $title, $this->settings->get($userId, 'fileSuffix'), -1);
$file = $folder->newFile($path);
// If server-side encryption is activated, the server creates an empty file without signature
@ -319,7 +319,7 @@ class NotesService {
* @return Folder
*/
private function getFolderForUser ($userId) {
$path = '/' . $userId . '/files/' . $this->settings->get('notesPath');
$path = '/' . $userId . '/files/' . $this->settings->get($userId, 'notesPath');
try {
$folder = $this->getOrCreateFolder($path);
} catch(\Exception $e) {

View File

@ -13,7 +13,6 @@ use OCP\AppFramework\Http\JSONResponse;
class SettingsService
{
private $config;
private $uid;
private $root;
/* Default values */
@ -23,17 +22,15 @@ class SettingsService
];
public function __construct(
IConfig $config,
IUserSession $userSession
IConfig $config
) {
$this->config = $config;
$this->uid = $userSession->getUser()->getUID();
}
/**
* @throws \OCP\PreConditionNotMetException
*/
public function set($settings) {
public function set($uid, $settings) {
// remove illegal, empty and default settings
foreach($settings as $name => $value) {
if(!array_key_exists($name, $this->defaults)
@ -43,11 +40,11 @@ class SettingsService
unset($settings[$name]);
}
}
$this->config->setUserValue($this->uid, 'notes', 'settings', json_encode($settings));
$this->config->setUserValue($uid, 'notes', 'settings', json_encode($settings));
}
public function getAll() {
$settings = json_decode($this->config->getUserValue($this->uid, 'notes', 'settings'));
public function getAll($uid) {
$settings = json_decode($this->config->getUserValue($uid, 'notes', 'settings'));
if(is_object($settings)) {
// use default for empty settings
foreach($this->defaults as $name => $defaultValue) {
@ -64,12 +61,12 @@ class SettingsService
/**
* @throws \OCP\PreConditionNotMetException
*/
public function get($name) {
$settings = $this->getAll();
public function get($uid, $name) {
$settings = $this->getAll($uid);
if(property_exists($settings, $name)) {
return $settings->{$name};
} else {
throw new \OCP\PreConditionNotMetException('Setting '.$name.' not found.');
throw new \OCP\PreConditionNotMetException('Setting '.$name.' not found for user '.$uid.'.');
}
}
}