Skip to content

Commit 00a6899

Browse files
committed
Home-made facade for getCurrentUser().
1 parent 251b493 commit 00a6899

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

app/Http/Controllers/EditPasteController.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public function index($link, Request $request){
1919
$paste = Paste::where('link', $link)->firstOrFail();
2020

2121
// Est-ce que l'utilisateur connecté est celui qui a écrit la paste ?
22-
if (Auth::user() != $paste->user || $paste->userId == 0) {
23-
return abort('404');
22+
if (!User::is_owner($paste) || $paste->userId == 0) {
23+
return abort('403');
2424
}
2525

2626
// Renvoi de la view
@@ -41,8 +41,8 @@ public function edit($link, Requests\EditPaste $request){
4141
$paste = Paste::where('link', $link)->firstOrFail();
4242

4343
// Est-ce que l'utilisateur connecté est celui qui a écrit la paste ?
44-
if (Auth::user() != $paste->user || $paste->userId == 0) {
45-
return abort('404');
44+
if (!User::is_owner($paste) || $paste->userId == 0) {
45+
return abort('403');
4646
}
4747

4848
$title = (empty(trim(Input::get('pasteTitle')))) ? 'Untitled' : Input::get('pasteTitle');

app/Http/Controllers/PasteController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ public function raw($link){
318318
// On crée la var envoyée à la view disant si l'user créateur est le viewer
319319
$sameUser = false;
320320
if(cas()->isAuthenticated()) {
321-
if ($paste->userId == Auth::user()->id) {
321+
if ($paste->userId == User::getCurrentUser()->id) {
322322
}
323323
}
324324
return response($paste->content, 200)->header('Content-Type', 'text/plain');

app/User.php

+19
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,23 @@ public static function create_if_absent($username) {
4646
}
4747
return $user;
4848
}
49+
50+
51+
/**
52+
* This stupid function exists because I couldn't find how to
53+
* properly implement CAS authentication as a Facade for Auth.
54+
* When I have time I'll try to read all Laravel doc and find out.
55+
*/
56+
public static function getCurrentUser() {
57+
cas()->isAuthenticated(); // XXX workaround CAS_OutOfSequenceBeforeAuthenticationCallException (because I don't know how to use Laravel properly)
58+
$username = cas()->getCurrentUser();
59+
$user = User::where('name', $username)->first();
60+
return $user;
61+
}
62+
63+
public static function is_owner($paste) {
64+
$user = User::getCurrentUser();
65+
return (($user->id == $paste->userId && $paste->userId != 0)) ? true : false;
66+
}
67+
4968
}

0 commit comments

Comments
 (0)