Skip to content

Allow access of guest accounts #506

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
[#565](https://github.com./nextcloud/cookbook/pull/565/)
- Enhanced testing interface
[#564](https://github.com./nextcloud/cookbook/pull/564) @christianlupus
- Allow guest users to use the cookbook and avoid nextcloud exception handling
[#506](https://github.com./nextcloud/cookbook/pull/506) @christianlupus

### Fixed
- Added some documentation how to install GH action generated builds
Expand Down
4 changes: 2 additions & 2 deletions lib/Controller/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ public function list() {
* @NoCSRFRequired
*/
public function config() {
$this->dbCacheService->triggerCheck();

$data = $this->restParser->getParameters();

if (isset($data['folder'])) {
Expand All @@ -77,6 +75,8 @@ public function config() {
$this->service->setPrintImage((bool)$data['print_image']);
}

$this->dbCacheService->triggerCheck();

return new DataResponse('OK', Http::STATUS_OK);
}

Expand Down
8 changes: 8 additions & 0 deletions lib/Controller/MainController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use OCA\Cookbook\Service\RecipeService;
use OCA\Cookbook\Service\DbCacheService;
use OCA\Cookbook\Helper\RestParameterParser;
use OCA\Cookbook\Exception\UserFolderNotWritableException;

class MainController extends Controller {
protected $appName;
Expand Down Expand Up @@ -50,6 +51,13 @@ public function __construct(string $AppName, IRequest $request, RecipeService $r
* @NoCSRFRequired
*/
public function index(): TemplateResponse {
try {
// Check if the user folder can be accessed
$this->service->getFolderForUser();
} catch (UserFolderNotWritableException $ex) {
return new TemplateResponse($this->appName, 'invalid_guest');
}

$this->dbCacheService->triggerCheck();

return new TemplateResponse($this->appName, 'index'); // templates/index.php
Expand Down
9 changes: 9 additions & 0 deletions lib/Exception/UserFolderNotWritableException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace OCA\Cookbook\Exception;

class UserFolderNotWritableException extends \Exception {
public function __construct($message = null, $code = null, $previous = null) {
parent::__construct($message, $code, $previous);
}
}
15 changes: 13 additions & 2 deletions lib/Service/RecipeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use OCA\Cookbook\Db\RecipeDb;
use OCP\PreConditionNotMetException;
use Psr\Log\LoggerInterface;
use OCA\Cookbook\Exception\UserFolderNotWritableException;

/**
* Main service class for the cookbook app.
Expand Down Expand Up @@ -862,7 +863,13 @@ public function getRecipeFiles() {
* @deprecated
*/
public function updateSearchIndex() {
$this->migrateFolderStructure();
try {
$this->migrateFolderStructure();
} catch (UserFolderNotWritableException $ex) {
// Ignore migration if not permitted.
$this->logger->warning("Cannot migrate cookbook file structure as not permitted.");
throw $ex;
}
}

private function migrateFolderStructure() {
Expand Down Expand Up @@ -1030,7 +1037,11 @@ private function getOrCreateFolder($path) {
if ($this->root->nodeExists($path)) {
$folder = $this->root->get($path);
} else {
$folder = $this->root->newFolder($path);
try {
$folder = $this->root->newFolder($path);
} catch (NotPermittedException $ex) {
throw new UserFolderNotWritableException($this->il10n->t('User cannot create recipe folder'), null, $ex);
}
}
return $folder;
}
Expand Down
89 changes: 89 additions & 0 deletions src/components/AppInvalidGuest.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<template>
<Content app-name="cookbook">
<AppContent>
<div class="main">
<div class="dialog">
<div class="message">
{{ t('cookbook', 'Cannot access recipe folder.') }}
</div>
<div>
{{
t('cookbook', 'You are logged in with a guest account. Therefore, you are not allowed to generate arbitrary files and folders on this Nextcloud instance. To be able to use the Cookbook app as a guest, you need to specify a folder where all recipes are stored. You will need write permission to this folder.')
}}
</div>
<div>
<button
@click.prevent="selectFolder"
>
{{ t('cookbook', 'Select recipe folder') }}
</button>
</div>

</div>
</div>
</AppContent>
</Content>
</template>

<script>

import Content from '@nextcloud/vue/dist/Components/Content'
import AppContent from '@nextcloud/vue/dist/Components/AppContent'
import axios from '@nextcloud/axios'

export default {
name: "InvalidGuest",
components: {
Content,
AppContent
},
methods: {
selectFolder: function (e){
OC.dialogs.filepicker(
t('cookbook', 'Path to your recipe collection'),
(path) => {
this.$store
.dispatch('updateRecipeDirectory', {dir: path})
.then(function(){
location.reload()
})
},
false, // Single result
'httpd/unix-directory', // Desired MIME type
true // Make modal dialog
)
},
},
}
</script>

<style lang="scss" scoped>
div.main {
height: 100%;
display: flex;
flex-direction: colummn;
justify-content: center;
align-items: center;

div.dialog {
width: 50%;

@media screen and (max-width: 800px) {
width: 90%;
}

padding: 20px;
border: 1px solid;
border-radius: 15px;

div {
margin: 20px 5px;
}
div.message {
font-weight: bold;
font-size: x-large;
}

}
}
</style>
37 changes: 37 additions & 0 deletions src/guest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Nextcloud Cookbook app
* Vue frontend entry file
* ---------------------------
* @license AGPL3 or later
*/

import Vue from 'vue'
import store from './store'

import AppInvalidGuest from './components/AppInvalidGuest'

(function (OC, window) {
'use strict'

// Fetch Nextcloud nonce identifier for dynamic script loading
__webpack_nonce__ = btoa(OC.requestToken)

window.baseUrl = OC.generateUrl('apps/cookbook')


// Also make the injections available in Vue components
Vue.prototype.$window = window
Vue.prototype.OC = OC

// Pass translation engine to Vue
Vue.prototype.t = window.t

// Start the app once document is done loading
document.addEventListener("DOMContentLoaded", function(event) {
const App = Vue.extend(AppInvalidGuest)
new App({
store,
// router,
}).$mount("#content")
})
})(OC, window)
6 changes: 6 additions & 0 deletions templates/invalid_guest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
script('cookbook', 'guest');
?>

<div id="app">
</div>
1 change: 1 addition & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = {

entry:{
vue: path.join(__dirname, 'src', 'main.js'),
guest: path.join(__dirname, 'src', 'guest.js'),
},
output: {
path: path.resolve(__dirname, './js'),
Expand Down