-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from mrefferdk/phpcs
Multi user support
- Loading branch information
Showing
28 changed files
with
297 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace App\Http\Middleware; | ||
|
||
use App\Http\Services\UserHashService; | ||
use Closure; | ||
use Illuminate\Http\Request; | ||
|
||
class ValidateUserHash | ||
{ | ||
/** | ||
* Handle an incoming request. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next | ||
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse | ||
*/ | ||
public function handle(Request $request, Closure $next) | ||
{ | ||
$userId = $request->get('userId'); | ||
if ($request->get('userIdHash') !== UserHashService::getUserHashById($userId)) { | ||
return response()->json(['error' => 'Not authorized'], 401); | ||
} | ||
return $next($request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace App\Http\Services; | ||
|
||
use App\Models\Recipe; | ||
|
||
class AccessService | ||
{ | ||
public static function hasReadAccessOrThrowException(Recipe $recipe): bool | ||
{ | ||
if (!self::hasReadAccess($recipe)) { | ||
throw new \Exception('You shall not pass', 403); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public static function hasReadAccess(Recipe $recipe): bool | ||
{ | ||
if ($recipe->user_id != null && $recipe->user_id !== auth()->user()->id) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public static function isAdmin(): bool | ||
{ | ||
return auth()->user()->admin; | ||
} | ||
|
||
public static function hasWriteAccessOrThrowException(Recipe $recipe): bool | ||
{ | ||
if (!self::hasWriteAccess($recipe)) { | ||
throw new \Exception('You shall not pass', 403); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public static function hasWriteAccess(Recipe $recipe): bool | ||
{ | ||
if ($recipe->user_id !== auth()->user()->id && !self::isAdmin()) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace App\Http\Services; | ||
|
||
class UserHashService | ||
{ | ||
/** | ||
* This method only has a User in Web routes NOT api routes. | ||
* @return string | ||
*/ | ||
public static function getUserHash(): string | ||
{ | ||
return self::getUserHashById(Auth()->user()->id); | ||
} | ||
|
||
/** | ||
* Use this in API routes where you can provide a user id from the request | ||
* @param int $userId | ||
* @return string | ||
*/ | ||
public static function getUserHashById(int $userId): string | ||
{ | ||
return sha1(config('app.key') . $userId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,11 +23,11 @@ class UserFactory extends Factory | |
public function definition() | ||
{ | ||
return [ | ||
'name' => $this->faker->name, | ||
'name' => 'effer', | ||
'email' => '[email protected]', | ||
'email_verified_at' => now(), | ||
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password | ||
'remember_token' => Str::random(10), | ||
'remember_token' => '123234', | ||
]; | ||
} | ||
|
||
|
Oops, something went wrong.