Skip to content

Commit 56ae866

Browse files
committed
Merge branch 'master' into release
2 parents 1d2b6fd + 080acf0 commit 56ae866

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+2365
-686
lines changed

.env.example

+15-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ APP_KEY=SomeRandomString
77
DB_HOST=localhost
88
DB_DATABASE=database_database
99
DB_USERNAME=database_username
10-
DB_PASSWORD=database__user_password
10+
DB_PASSWORD=database_user_password
1111

1212
# Cache and session
1313
CACHE_DRIVER=file
@@ -25,6 +25,9 @@ STORAGE_S3_BUCKET=false
2525
# Used to prefix image urls for when using custom domains/cdns
2626
STORAGE_URL=false
2727

28+
# General auth
29+
AUTH_METHOD=standard
30+
2831
# Social Authentication information. Defaults as off.
2932
GITHUB_APP_ID=false
3033
GITHUB_APP_SECRET=false
@@ -33,13 +36,21 @@ GOOGLE_APP_SECRET=false
3336
# URL used for social login redirects, NO TRAILING SLASH
3437
APP_URL=http://bookstack.dev
3538

36-
# External services
37-
USE_GRAVATAR=true
39+
# External services such as Gravatar
40+
DISABLE_EXTERNAL_SERVICES=false
41+
42+
# LDAP Settings
43+
LDAP_SERVER=false
44+
LDAP_BASE_DN=false
45+
LDAP_DN=false
46+
LDAP_PASS=false
47+
LDAP_USER_FILTER=false
48+
LDAP_VERSION=false
3849

3950
# Mail settings
4051
MAIL_DRIVER=smtp
4152
MAIL_HOST=localhost
4253
MAIL_PORT=1025
4354
MAIL_USERNAME=null
4455
MAIL_PASSWORD=null
45-
MAIL_ENCRYPTION=null
56+
MAIL_ENCRYPTION=null

app/Exceptions/Handler.php

+6
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
namespace BookStack\Exceptions;
44

55
use Exception;
6+
use Illuminate\Contracts\Validation\ValidationException;
7+
use Illuminate\Database\Eloquent\ModelNotFoundException;
68
use Symfony\Component\HttpKernel\Exception\HttpException;
79
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
10+
use Illuminate\Auth\Access\AuthorizationException;
811

912
class Handler extends ExceptionHandler
1013
{
@@ -14,7 +17,10 @@ class Handler extends ExceptionHandler
1417
* @var array
1518
*/
1619
protected $dontReport = [
20+
AuthorizationException::class,
1721
HttpException::class,
22+
ModelNotFoundException::class,
23+
ValidationException::class,
1824
];
1925

2026
/**
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php namespace BookStack\Exceptions;
2+
3+
4+
use Exception;
5+
6+
class ImageUploadException extends Exception {}

app/Exceptions/LdapException.php

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php namespace BookStack\Exceptions;
2+
3+
4+
use Exception;
5+
6+
class LdapException extends Exception
7+
{
8+
9+
}

app/Http/Controllers/Auth/AuthController.php

+42-10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace BookStack\Http\Controllers\Auth;
44

5+
use Illuminate\Contracts\Auth\Authenticatable;
56
use Illuminate\Http\Request;
67
use BookStack\Exceptions\SocialSignInException;
78
use BookStack\Exceptions\UserRegistrationException;
@@ -29,9 +30,10 @@ class AuthController extends Controller
2930

3031
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
3132

32-
protected $loginPath = '/login';
3333
protected $redirectPath = '/';
3434
protected $redirectAfterLogout = '/login';
35+
protected $username = 'email';
36+
3537

3638
protected $socialAuthService;
3739
protected $emailConfirmationService;
@@ -49,6 +51,7 @@ public function __construct(SocialAuthService $socialAuthService, EmailConfirmat
4951
$this->socialAuthService = $socialAuthService;
5052
$this->emailConfirmationService = $emailConfirmationService;
5153
$this->userRepo = $userRepo;
54+
$this->username = config('auth.method') === 'standard' ? 'email' : 'username';
5255
parent::__construct();
5356
}
5457

@@ -105,6 +108,38 @@ public function postRegister(Request $request)
105108
return $this->registerUser($userData);
106109
}
107110

111+
112+
/**
113+
* Overrides the action when a user is authenticated.
114+
* If the user authenticated but does not exist in the user table we create them.
115+
* @param Request $request
116+
* @param Authenticatable $user
117+
* @return \Illuminate\Http\RedirectResponse
118+
*/
119+
protected function authenticated(Request $request, Authenticatable $user)
120+
{
121+
// Explicitly log them out for now if they do no exist.
122+
if (!$user->exists) auth()->logout($user);
123+
124+
if (!$user->exists && $user->email === null && !$request->has('email')) {
125+
$request->flash();
126+
session()->flash('request-email', true);
127+
return redirect('/login');
128+
}
129+
130+
if (!$user->exists && $user->email === null && $request->has('email')) {
131+
$user->email = $request->get('email');
132+
}
133+
134+
if (!$user->exists) {
135+
$user->save();
136+
$this->userRepo->attachDefaultRole($user);
137+
auth()->login($user);
138+
}
139+
140+
return redirect()->intended($this->redirectPath());
141+
}
142+
108143
/**
109144
* Register a new user after a registration callback.
110145
* @param $socialDriver
@@ -156,13 +191,14 @@ protected function registerUser(array $userData, $socialAccount = false)
156191
}
157192

158193
$newUser->email_confirmed = true;
194+
159195
auth()->login($newUser);
160196
session()->flash('success', 'Thanks for signing up! You are now registered and signed in.');
161197
return redirect($this->redirectPath());
162198
}
163199

164200
/**
165-
* Show the page to tell the user to check thier email
201+
* Show the page to tell the user to check their email
166202
* and confirm their address.
167203
*/
168204
public function getRegisterConfirmation()
@@ -222,7 +258,7 @@ public function resendConfirmation(Request $request)
222258
]);
223259
$user = $this->userRepo->getByEmail($request->get('email'));
224260
$this->emailConfirmationService->sendConfirmation($user);
225-
\Session::flash('success', 'Confirmation email resent, Please check your inbox.');
261+
session()->flash('success', 'Confirmation email resent, Please check your inbox.');
226262
return redirect('/register/confirm');
227263
}
228264

@@ -232,13 +268,9 @@ public function resendConfirmation(Request $request)
232268
*/
233269
public function getLogin()
234270
{
235-
236-
if (view()->exists('auth.authenticate')) {
237-
return view('auth.authenticate');
238-
}
239-
240271
$socialDrivers = $this->socialAuthService->getActiveDrivers();
241-
return view('auth.login', ['socialDrivers' => $socialDrivers]);
272+
$authMethod = config('auth.method');
273+
return view('auth/login', ['socialDrivers' => $socialDrivers, 'authMethod' => $authMethod]);
242274
}
243275

244276
/**
@@ -253,7 +285,7 @@ public function getSocialLogin($socialDriver)
253285
}
254286

255287
/**
256-
* Redirect to the social site for authentication initended to register.
288+
* Redirect to the social site for authentication intended to register.
257289
* @param $socialDriver
258290
* @return mixed
259291
*/

app/Http/Controllers/Controller.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function __construct()
4848
*/
4949
protected function preventAccessForDemoUsers()
5050
{
51-
if (env('APP_ENV', 'production') === 'demo') $this->showPermissionError();
51+
if (config('app.env') === 'demo') $this->showPermissionError();
5252
}
5353

5454
/**

app/Http/Controllers/ImageController.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace BookStack\Http\Controllers;
44

5+
use BookStack\Exceptions\ImageUploadException;
56
use BookStack\Repos\ImageRepo;
67
use Illuminate\Filesystem\Filesystem as File;
78
use Illuminate\Http\Request;
@@ -69,7 +70,13 @@ public function uploadByType($type, Request $request)
6970
]);
7071

7172
$imageUpload = $request->file('file');
72-
$image = $this->imageRepo->saveNew($imageUpload, $type);
73+
74+
try {
75+
$image = $this->imageRepo->saveNew($imageUpload, $type);
76+
} catch (ImageUploadException $e) {
77+
return response($e->getMessage(), 500);
78+
}
79+
7380
return response()->json($image);
7481
}
7582

app/Http/Controllers/PageController.php

+61-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace BookStack\Http\Controllers;
44

55
use Activity;
6+
use BookStack\Services\ExportService;
67
use Illuminate\Http\Request;
78

89
use Illuminate\Support\Facades\Auth;
@@ -18,18 +19,21 @@ class PageController extends Controller
1819
protected $pageRepo;
1920
protected $bookRepo;
2021
protected $chapterRepo;
22+
protected $exportService;
2123

2224
/**
2325
* PageController constructor.
24-
* @param PageRepo $pageRepo
25-
* @param BookRepo $bookRepo
26-
* @param ChapterRepo $chapterRepo
26+
* @param PageRepo $pageRepo
27+
* @param BookRepo $bookRepo
28+
* @param ChapterRepo $chapterRepo
29+
* @param ExportService $exportService
2730
*/
28-
public function __construct(PageRepo $pageRepo, BookRepo $bookRepo, ChapterRepo $chapterRepo)
31+
public function __construct(PageRepo $pageRepo, BookRepo $bookRepo, ChapterRepo $chapterRepo, ExportService $exportService)
2932
{
3033
$this->pageRepo = $pageRepo;
3134
$this->bookRepo = $bookRepo;
3235
$this->chapterRepo = $chapterRepo;
36+
$this->exportService = $exportService;
3337
parent::__construct();
3438
}
3539

@@ -221,4 +225,57 @@ public function restoreRevision($bookSlug, $pageSlug, $revisionId)
221225
Activity::add($page, 'page_restore', $book->id);
222226
return redirect($page->getUrl());
223227
}
228+
229+
/**
230+
* Exports a page to pdf format using barryvdh/laravel-dompdf wrapper.
231+
* https://github.com/barryvdh/laravel-dompdf
232+
* @param $bookSlug
233+
* @param $pageSlug
234+
* @return \Illuminate\Http\Response
235+
*/
236+
public function exportPdf($bookSlug, $pageSlug)
237+
{
238+
$book = $this->bookRepo->getBySlug($bookSlug);
239+
$page = $this->pageRepo->getBySlug($pageSlug, $book->id);
240+
$pdfContent = $this->exportService->pageToPdf($page);
241+
return response()->make($pdfContent, 200, [
242+
'Content-Type' => 'application/octet-stream',
243+
'Content-Disposition' => 'attachment; filename="'.$pageSlug.'.pdf'
244+
]);
245+
}
246+
247+
/**
248+
* Export a page to a self-contained HTML file.
249+
* @param $bookSlug
250+
* @param $pageSlug
251+
* @return \Illuminate\Http\Response
252+
*/
253+
public function exportHtml($bookSlug, $pageSlug)
254+
{
255+
$book = $this->bookRepo->getBySlug($bookSlug);
256+
$page = $this->pageRepo->getBySlug($pageSlug, $book->id);
257+
$containedHtml = $this->exportService->pageToContainedHtml($page);
258+
return response()->make($containedHtml, 200, [
259+
'Content-Type' => 'application/octet-stream',
260+
'Content-Disposition' => 'attachment; filename="'.$pageSlug.'.html'
261+
]);
262+
}
263+
264+
/**
265+
* Export a page to a simple plaintext .txt file.
266+
* @param $bookSlug
267+
* @param $pageSlug
268+
* @return \Illuminate\Http\Response
269+
*/
270+
public function exportPlainText($bookSlug, $pageSlug)
271+
{
272+
$book = $this->bookRepo->getBySlug($bookSlug);
273+
$page = $this->pageRepo->getBySlug($pageSlug, $book->id);
274+
$containedHtml = $this->exportService->pageToPlainText($page);
275+
return response()->make($containedHtml, 200, [
276+
'Content-Type' => 'application/octet-stream',
277+
'Content-Disposition' => 'attachment; filename="'.$pageSlug.'.txt'
278+
]);
279+
}
280+
224281
}

0 commit comments

Comments
 (0)