Skip to content

Commit 4fc75be

Browse files
committed
Merge branch 'master' into release
2 parents 3b3bc0c + d3709de commit 4fc75be

17 files changed

Lines changed: 150 additions & 44 deletions

File tree

app/Entity.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,7 @@ public function matchesOrContains(Entity $entity)
3131

3232
if ($matches) return true;
3333

34-
if ($entity->isA('chapter') && $this->isA('book')) {
35-
return $entity->book_id === $this->id;
36-
}
37-
38-
if ($entity->isA('page') && $this->isA('book')) {
34+
if (($entity->isA('chapter') || $entity->isA('page')) && $this->isA('book')) {
3935
return $entity->book_id === $this->id;
4036
}
4137

@@ -64,15 +60,6 @@ public function views()
6460
return $this->morphMany('BookStack\View', 'viewable');
6561
}
6662

67-
/**
68-
* Get just the views for the current user.
69-
* @return mixed
70-
*/
71-
public function userViews()
72-
{
73-
return $this->views()->where('user_id', '=', auth()->user()->id);
74-
}
75-
7663
/**
7764
* Allows checking of the exact class, Used to check entity type.
7865
* Cleaner method for is_a.

app/Http/Controllers/Controller.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ public function __construct()
4242
$this->signedIn = auth()->check();
4343
}
4444

45+
/**
46+
* Stops the application and shows a permission error if
47+
* the application is in demo mode.
48+
*/
49+
protected function preventAccessForDemoUsers()
50+
{
51+
if (env('APP_ENV', 'production') === 'demo') $this->showPermissionError();
52+
}
53+
4554
/**
4655
* Adds the page title into the view.
4756
* @param $title
@@ -51,6 +60,18 @@ public function setPageTitle($title)
5160
view()->share('pageTitle', $title);
5261
}
5362

63+
/**
64+
* On a permission error redirect to home and display
65+
* the error as a notification.
66+
*/
67+
protected function showPermissionError()
68+
{
69+
Session::flash('error', trans('errors.permission'));
70+
throw new HttpResponseException(
71+
redirect('/')
72+
);
73+
}
74+
5475
/**
5576
* Checks for a permission.
5677
*
@@ -60,15 +81,18 @@ public function setPageTitle($title)
6081
protected function checkPermission($permissionName)
6182
{
6283
if (!$this->currentUser || !$this->currentUser->can($permissionName)) {
63-
Session::flash('error', trans('errors.permission'));
64-
throw new HttpResponseException(
65-
redirect('/')
66-
);
84+
$this->showPermissionError();
6785
}
6886

6987
return true;
7088
}
7189

90+
/**
91+
* Check if a user has a permission or bypass if the callback is true.
92+
* @param $permissionName
93+
* @param $callback
94+
* @return bool
95+
*/
7296
protected function checkPermissionOr($permissionName, $callback)
7397
{
7498
$callbackResult = $callback();

app/Http/Controllers/SearchController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ public function searchBook(Request $request, $bookId)
6262
return redirect()->back();
6363
}
6464
$searchTerm = $request->get('term');
65-
$whereTerm = [['book_id', '=', $bookId]];
66-
$pages = $this->pageRepo->getBySearch($searchTerm, $whereTerm);
67-
$chapters = $this->chapterRepo->getBySearch($searchTerm, $whereTerm);
65+
$searchWhereTerms = [['book_id', '=', $bookId]];
66+
$pages = $this->pageRepo->getBySearch($searchTerm, $searchWhereTerms);
67+
$chapters = $this->chapterRepo->getBySearch($searchTerm, $searchWhereTerms);
6868
return view('search/book', ['pages' => $pages, 'chapters' => $chapters, 'searchTerm' => $searchTerm]);
6969
}
7070

app/Http/Controllers/SettingController.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,16 @@ public function index()
3131
*/
3232
public function update(Request $request)
3333
{
34+
$this->preventAccessForDemoUsers();
3435
$this->checkPermission('settings-update');
36+
3537
// Cycles through posted settings and update them
3638
foreach($request->all() as $name => $value) {
3739
if(strpos($name, 'setting-') !== 0) continue;
3840
$key = str_replace('setting-', '', trim($name));
3941
Setting::put($key, $value);
4042
}
43+
4144
session()->flash('success', 'Settings Saved');
4245
return redirect('/settings');
4346
}

app/Http/Controllers/UserController.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,19 @@ public function edit($id, SocialAuthService $socialAuthService)
108108
*/
109109
public function update(Request $request, $id)
110110
{
111+
$this->preventAccessForDemoUsers();
111112
$this->checkPermissionOr('user-update', function () use ($id) {
112113
return $this->currentUser->id == $id;
113114
});
115+
114116
$this->validate($request, [
115117
'name' => 'required',
116118
'email' => 'required|email|unique:users,email,' . $id,
117-
'password' => 'min:5',
118-
'password-confirm' => 'same:password',
119+
'password' => 'min:5|required_with:password_confirm',
120+
'password-confirm' => 'same:password|required_with:password',
119121
'role' => 'exists:roles,id'
122+
], [
123+
'password-confirm.required_with' => 'Password confirmation required'
120124
]);
121125

122126
$user = $this->user->findOrFail($id);
@@ -130,6 +134,7 @@ public function update(Request $request, $id)
130134
$password = $request->get('password');
131135
$user->password = bcrypt($password);
132136
}
137+
133138
$user->save();
134139
return redirect('/users');
135140
}
@@ -144,6 +149,7 @@ public function delete($id)
144149
$this->checkPermissionOr('user-delete', function () use ($id) {
145150
return $this->currentUser->id == $id;
146151
});
152+
147153
$user = $this->user->findOrFail($id);
148154
$this->setPageTitle('Delete User ' . $user->name);
149155
return view('users/delete', ['user' => $user]);
@@ -156,6 +162,7 @@ public function delete($id)
156162
*/
157163
public function destroy($id)
158164
{
165+
$this->preventAccessForDemoUsers();
159166
$this->checkPermissionOr('user-delete', function () use ($id) {
160167
return $this->currentUser->id == $id;
161168
});

app/Role.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ public function attachPermission(Permission $permission)
4343
*/
4444
public static function getDefault()
4545
{
46-
return static::where('name', '=', static::$default)->first();
46+
return static::getRole(static::$default);
47+
}
48+
49+
/**
50+
* Get the role object for the specified role.
51+
* @param $roleName
52+
* @return mixed
53+
*/
54+
public static function getRole($roleName)
55+
{
56+
return static::where('name', '=', $roleName)->first();
4757
}
4858
}

app/Services/ActivityService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ function entityActivity($entity, $count = 20, $page = 0)
107107
}
108108

109109
/**
110-
* Filters out similar acitivity.
110+
* Filters out similar activity.
111111
* @param Activity[] $activity
112112
* @return array
113113
*/

database/seeds/DummyContentSeeder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class DummyContentSeeder extends Seeder
1212
public function run()
1313
{
1414
$user = factory(BookStack\User::class, 1)->create();
15-
$role = \BookStack\Role::where('name', '=', 'admin')->first();
15+
$role = \BookStack\Role::getDefault();
1616
$user->attachRole($role);
1717

1818

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@
2626
<env name="QUEUE_DRIVER" value="sync"/>
2727
<env name="DB_CONNECTION" value="mysql_testing"/>
2828
<env name="MAIL_PRETEND" value="true"/>
29-
<env name="DISABLE_EXTERNAL_SERVICES" value="true"/>
29+
<env name="DISABLE_EXTERNAL_SERVICES" value="false"/>
3030
</php>
3131
</phpunit>

public/build/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

0 commit comments

Comments
 (0)