diff --git a/app/Entity.php b/app/Entity.php
index 3d1c4ad58..705444959 100644
--- a/app/Entity.php
+++ b/app/Entity.php
@@ -31,11 +31,7 @@ abstract class Entity extends Model
if ($matches) return true;
- if ($entity->isA('chapter') && $this->isA('book')) {
- return $entity->book_id === $this->id;
- }
-
- if ($entity->isA('page') && $this->isA('book')) {
+ if (($entity->isA('chapter') || $entity->isA('page')) && $this->isA('book')) {
return $entity->book_id === $this->id;
}
@@ -64,15 +60,6 @@ abstract class Entity extends Model
return $this->morphMany('BookStack\View', 'viewable');
}
- /**
- * Get just the views for the current user.
- * @return mixed
- */
- public function userViews()
- {
- return $this->views()->where('user_id', '=', auth()->user()->id);
- }
-
/**
* Allows checking of the exact class, Used to check entity type.
* Cleaner method for is_a.
diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.php
index 5dc79eb02..ca022f7ca 100644
--- a/app/Http/Controllers/Controller.php
+++ b/app/Http/Controllers/Controller.php
@@ -42,6 +42,15 @@ abstract class Controller extends BaseController
$this->signedIn = auth()->check();
}
+ /**
+ * Stops the application and shows a permission error if
+ * the application is in demo mode.
+ */
+ protected function preventAccessForDemoUsers()
+ {
+ if (env('APP_ENV', 'production') === 'demo') $this->showPermissionError();
+ }
+
/**
* Adds the page title into the view.
* @param $title
@@ -51,6 +60,18 @@ abstract class Controller extends BaseController
view()->share('pageTitle', $title);
}
+ /**
+ * On a permission error redirect to home and display
+ * the error as a notification.
+ */
+ protected function showPermissionError()
+ {
+ Session::flash('error', trans('errors.permission'));
+ throw new HttpResponseException(
+ redirect('/')
+ );
+ }
+
/**
* Checks for a permission.
*
@@ -60,15 +81,18 @@ abstract class Controller extends BaseController
protected function checkPermission($permissionName)
{
if (!$this->currentUser || !$this->currentUser->can($permissionName)) {
- Session::flash('error', trans('errors.permission'));
- throw new HttpResponseException(
- redirect('/')
- );
+ $this->showPermissionError();
}
return true;
}
+ /**
+ * Check if a user has a permission or bypass if the callback is true.
+ * @param $permissionName
+ * @param $callback
+ * @return bool
+ */
protected function checkPermissionOr($permissionName, $callback)
{
$callbackResult = $callback();
diff --git a/app/Http/Controllers/SearchController.php b/app/Http/Controllers/SearchController.php
index c9ca1f09f..035de9fe6 100644
--- a/app/Http/Controllers/SearchController.php
+++ b/app/Http/Controllers/SearchController.php
@@ -62,9 +62,9 @@ class SearchController extends Controller
return redirect()->back();
}
$searchTerm = $request->get('term');
- $whereTerm = [['book_id', '=', $bookId]];
- $pages = $this->pageRepo->getBySearch($searchTerm, $whereTerm);
- $chapters = $this->chapterRepo->getBySearch($searchTerm, $whereTerm);
+ $searchWhereTerms = [['book_id', '=', $bookId]];
+ $pages = $this->pageRepo->getBySearch($searchTerm, $searchWhereTerms);
+ $chapters = $this->chapterRepo->getBySearch($searchTerm, $searchWhereTerms);
return view('search/book', ['pages' => $pages, 'chapters' => $chapters, 'searchTerm' => $searchTerm]);
}
diff --git a/app/Http/Controllers/SettingController.php b/app/Http/Controllers/SettingController.php
index bca48807f..1739e0b53 100644
--- a/app/Http/Controllers/SettingController.php
+++ b/app/Http/Controllers/SettingController.php
@@ -31,13 +31,16 @@ class SettingController extends Controller
*/
public function update(Request $request)
{
+ $this->preventAccessForDemoUsers();
$this->checkPermission('settings-update');
+
// Cycles through posted settings and update them
foreach($request->all() as $name => $value) {
if(strpos($name, 'setting-') !== 0) continue;
$key = str_replace('setting-', '', trim($name));
Setting::put($key, $value);
}
+
session()->flash('success', 'Settings Saved');
return redirect('/settings');
}
diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php
index 3f41b2d0e..b81be16f6 100644
--- a/app/Http/Controllers/UserController.php
+++ b/app/Http/Controllers/UserController.php
@@ -108,15 +108,19 @@ class UserController extends Controller
*/
public function update(Request $request, $id)
{
+ $this->preventAccessForDemoUsers();
$this->checkPermissionOr('user-update', function () use ($id) {
return $this->currentUser->id == $id;
});
+
$this->validate($request, [
'name' => 'required',
'email' => 'required|email|unique:users,email,' . $id,
- 'password' => 'min:5',
- 'password-confirm' => 'same:password',
+ 'password' => 'min:5|required_with:password_confirm',
+ 'password-confirm' => 'same:password|required_with:password',
'role' => 'exists:roles,id'
+ ], [
+ 'password-confirm.required_with' => 'Password confirmation required'
]);
$user = $this->user->findOrFail($id);
@@ -130,6 +134,7 @@ class UserController extends Controller
$password = $request->get('password');
$user->password = bcrypt($password);
}
+
$user->save();
return redirect('/users');
}
@@ -144,6 +149,7 @@ class UserController extends Controller
$this->checkPermissionOr('user-delete', function () use ($id) {
return $this->currentUser->id == $id;
});
+
$user = $this->user->findOrFail($id);
$this->setPageTitle('Delete User ' . $user->name);
return view('users/delete', ['user' => $user]);
@@ -156,6 +162,7 @@ class UserController extends Controller
*/
public function destroy($id)
{
+ $this->preventAccessForDemoUsers();
$this->checkPermissionOr('user-delete', function () use ($id) {
return $this->currentUser->id == $id;
});
diff --git a/app/Role.php b/app/Role.php
index 3e58164c5..c698a1cf6 100644
--- a/app/Role.php
+++ b/app/Role.php
@@ -43,6 +43,16 @@ class Role extends Model
*/
public static function getDefault()
{
- return static::where('name', '=', static::$default)->first();
+ return static::getRole(static::$default);
+ }
+
+ /**
+ * Get the role object for the specified role.
+ * @param $roleName
+ * @return mixed
+ */
+ public static function getRole($roleName)
+ {
+ return static::where('name', '=', $roleName)->first();
}
}
diff --git a/app/Services/ActivityService.php b/app/Services/ActivityService.php
index 4da928fad..2ef5f9cfe 100644
--- a/app/Services/ActivityService.php
+++ b/app/Services/ActivityService.php
@@ -107,7 +107,7 @@ class ActivityService
}
/**
- * Filters out similar acitivity.
+ * Filters out similar activity.
* @param Activity[] $activity
* @return array
*/
diff --git a/database/seeds/DummyContentSeeder.php b/database/seeds/DummyContentSeeder.php
index d2ccc7960..aa70eaa0a 100644
--- a/database/seeds/DummyContentSeeder.php
+++ b/database/seeds/DummyContentSeeder.php
@@ -12,7 +12,7 @@ class DummyContentSeeder extends Seeder
public function run()
{
$user = factory(BookStack\User::class, 1)->create();
- $role = \BookStack\Role::where('name', '=', 'admin')->first();
+ $role = \BookStack\Role::getDefault();
$user->attachRole($role);
diff --git a/phpunit.xml b/phpunit.xml
index d86aacd00..1704159e2 100644
--- a/phpunit.xml
+++ b/phpunit.xml
@@ -26,6 +26,6 @@
The page you were looking for could not be found.
+Sorry, The page you were looking for could not be found.
+ Return To Home