-
Notifications
You must be signed in to change notification settings - Fork 3
10 authentication #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ghost
wants to merge
5
commits into
develop
Choose a base branch
from
10-Authentication
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8eb5d45
Authentication with Role & Permission
1017d55
Add verify mail for registration
f226368
Ajout d'un front-end Admin pour tester les permissions/roles
dda79a4
10-Authentification : End
48a5468
Delete Entrust for native policies
Raspegaous File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -10,3 +10,4 @@ Homestead.yaml | |
| npm-debug.log | ||
| yarn-error.log | ||
| .env | ||
| /360-dev.iml | ||
This file contains hidden or 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,117 @@ | ||
| <?php | ||
|
|
||
| namespace App\Concern\Admin; | ||
|
|
||
| use Illuminate\Http\Request; | ||
| use Illuminate\Support\Facades\Validator; | ||
|
|
||
| /** | ||
| * Trait TraitAdminController | ||
| * | ||
| * Basic CRUD controller | ||
| * | ||
| * ## L'idée de base étant d'avoir un Trait qui reprend le CRUD. | ||
| * ## Ainsi il suffira soit de ne pas toucher soit de réécrire les fonctions pour les controllers particuliés. | ||
| * | ||
| * @package App\Concern\Admin | ||
| */ | ||
| trait TraitAdminController | ||
| { | ||
|
|
||
| private $model; | ||
|
|
||
| public function __construct() | ||
| { | ||
| $this->model = self::__MODEL; | ||
| } | ||
|
|
||
| /** | ||
| * Display a listing of the resource. | ||
| * | ||
| * @return \Illuminate\Http\Response | ||
| */ | ||
| public function index() | ||
| { | ||
| $items = $this->model::all(); | ||
| return view('admin.' . $this->view . '.index', compact('items')); | ||
| } | ||
|
|
||
| /** | ||
| * Show the form for creating a new resource. | ||
| * | ||
| * @return \Illuminate\Http\Response | ||
| */ | ||
| public function create() | ||
| { | ||
| $this->authorize('create', $this->model); | ||
| $item = new $this->model; | ||
| return view('admin.' . $this->view . '.form', compact('item')); | ||
| } | ||
|
|
||
| /** | ||
| * Store a newly created resource in storage. | ||
| * | ||
| * @param \Illuminate\Http\Request $request | ||
| * @return \Illuminate\Http\Response | ||
| */ | ||
| public function store(Request $request) | ||
| { | ||
| $this->authorize('create', $this->model); | ||
| Validator::make($request->all(), $this->validator)->validate(); | ||
| $this->model::create($request->all()); | ||
| return $this->index()->with('success', 'Nouvel entité créé'); | ||
| } | ||
|
|
||
| /** | ||
| * Display the specified resource. | ||
| * | ||
| * @param int $id | ||
| * @return \Illuminate\Http\Response | ||
| */ | ||
| public function show(int $id) | ||
| { | ||
| $this->authorize('view', $this->model); | ||
| $item = $this->model::where('id', $id)->first(); | ||
| return view('admin.' . $this->view . '.show', compact('item')); | ||
| } | ||
|
|
||
| /** | ||
| * Show the form for editing the specified resource. | ||
| * | ||
| * @param int $id | ||
| * @return \Illuminate\Http\Response | ||
| */ | ||
| public function edit(int $id) | ||
| { | ||
| $this->authorize('update', $this->model); | ||
| $item = $this->model::where('id', $id)->first(); | ||
| return view('admin.' . $this->view . '.form', compact('item')); | ||
| } | ||
|
|
||
| /** | ||
| * Update the specified resource in storage. | ||
| * | ||
| * @param \Illuminate\Http\Request $request | ||
| * @param int $id | ||
| * @return \Illuminate\Http\Response | ||
| */ | ||
| public function update(Request $request, int $id) | ||
| { | ||
| $this->authorize('update', $this->model); | ||
| $item = $this->model::where('id', $id)->first(); | ||
| $item->update($request->all()); | ||
| return $this->index()->with('success', 'Entité modifié avec succès'); | ||
| } | ||
|
|
||
| /** | ||
| * @param int $id | ||
| * @return mixed | ||
| */ | ||
| public function destroy(int $id) | ||
| { | ||
| $this->authorize('delete', $this->model); | ||
| $item = $this->model::where('id', $id)->first(); | ||
| $item->delete(); | ||
| return $this->index()->with('success', 'Entité supprimé'); | ||
| } | ||
| } |
This file contains hidden or 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,17 @@ | ||
| <?php | ||
|
|
||
| namespace App\Http\Controllers\Admin; | ||
|
|
||
| use App\Concern\Admin\TraitAdminController; | ||
| use App\Http\Controllers\Controller; | ||
|
|
||
| class PostController extends Controller | ||
| { | ||
| use TraitAdminController; | ||
|
|
||
| const __MODEL = 'App\\Model\\Post'; | ||
|
|
||
| protected $view = 'post'; | ||
|
|
||
| protected $validator = []; | ||
| } | ||
This file contains hidden or 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,17 @@ | ||
| <?php | ||
|
|
||
| namespace App\Http\Controllers\Admin; | ||
|
|
||
| use App\Concern\Admin\TraitAdminController; | ||
| use App\Http\Controllers\Controller; | ||
|
|
||
| class RoleController extends Controller | ||
| { | ||
| use TraitAdminController; | ||
|
|
||
| const __MODEL = 'App\\Model\\Role'; | ||
|
|
||
| protected $view = 'role'; | ||
|
|
||
| protected $validator = []; | ||
| } |
This file contains hidden or 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,39 @@ | ||
| <?php | ||
|
|
||
| namespace App\Http\Controllers\Admin; | ||
|
|
||
| use App\Concern\Admin\TraitAdminController; | ||
| use App\Http\Controllers\Controller; | ||
| use App\Model\User; | ||
| use Illuminate\Support\Facades\Auth; | ||
|
|
||
| class UserController extends Controller | ||
| { | ||
| use TraitAdminController; | ||
|
|
||
| const __MODEL = 'App\\Model\\User'; | ||
|
|
||
| protected $view = 'user'; | ||
|
|
||
| protected $validator = []; | ||
|
|
||
| /** | ||
| * Display a listing of the resource. | ||
| * | ||
| * @return \Illuminate\Http\Response | ||
| */ | ||
| public function index() | ||
| { | ||
| $this->authorize('view', self::__MODEL); | ||
| $items = User::with('roles')->get(); | ||
| return view('admin.' . $this->view . '.index', compact('items')); | ||
| } | ||
|
|
||
| public function destroy(User $user) | ||
| { | ||
| $this->authorize('delete', $user); | ||
| $user->roles()->detach(); | ||
| $user->delete(); | ||
| return $this->index()->with('success', 'Utilisateur supprimé'); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -2,8 +2,13 @@ | |
|
|
||
| namespace App\Http\Controllers\Auth; | ||
|
|
||
| use App\User; | ||
| use App\Model\Role; | ||
| use App\Model\User; | ||
| use App\Http\Controllers\Controller; | ||
| use App\Notifications\RegisteredUser; | ||
| use Illuminate\Auth\Events\Registered; | ||
| use Illuminate\Http\Request; | ||
| use Illuminate\Routing\Redirector; | ||
| use Illuminate\Support\Facades\Validator; | ||
| use Illuminate\Foundation\Auth\RegistersUsers; | ||
|
|
||
|
|
@@ -32,13 +37,48 @@ class RegisterController extends Controller | |
| /** | ||
| * Create a new controller instance. | ||
| * | ||
| * @return void | ||
| */ | ||
| public function __construct() | ||
| { | ||
| $this->middleware('guest'); | ||
| } | ||
|
|
||
| /** | ||
| * Handle a registration request for the application. | ||
| * | ||
| * @param Request $request | ||
| * @return \Illuminate\Http\Response | ||
| */ | ||
| public function register(Request $request) | ||
| { | ||
| $this->validator($request->all())->validate(); | ||
| event(new Registered($user = $this->create($request->all()))); | ||
| $user->notify(new RegisteredUser()); | ||
| return $this->registered($request, $user) | ||
| ?: redirect($this->redirectPath()); | ||
| } | ||
|
|
||
| /** | ||
| * Verify the email adress with token | ||
| * | ||
| * @param int $id | ||
| * @param string $token | ||
| * @return Redirector | ||
| */ | ||
| public function confirm(int $id, string $token) | ||
| { | ||
| $user = User::where([['id', $id], ['verify_token', $token]])->first(); | ||
| if ($user) { | ||
| $role = Role::where('name', 'user')->first(); | ||
| $user->roles()->attach($role); | ||
| $user->update(['veriffy_token' => null]); | ||
| $this->guard()->login($user); | ||
| return redirect($this->redirectPath()); | ||
| } else { | ||
| return redirect('/login')->with('error', 'Ce lien n\'est pas valide'); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Get a validator for an incoming registration request. | ||
| * | ||
|
|
@@ -58,14 +98,25 @@ protected function validator(array $data) | |
| * Create a new user instance after a valid registration. | ||
| * | ||
| * @param array $data | ||
| * @return \App\User | ||
| * @return \App\Model\User | ||
| */ | ||
| protected function create(array $data) | ||
| { | ||
| $default = null; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. peut être séparer la création d'avatar dans une méthode ? |
||
| $size = 40; | ||
| $avatar = "https://www.gravatar.com/avatar/"; | ||
| $avatar .= md5(strtolower(trim($data['email']))); | ||
| $avatar .= "?d="; | ||
| $avatar .= urlencode($default); | ||
| $avatar .= "&s="; | ||
| $avatar .= $size; | ||
|
|
||
| return User::create([ | ||
| 'name' => $data['name'], | ||
| 'email' => $data['email'], | ||
| 'password' => bcrypt($data['password']), | ||
| 'verify_token' => str_replace('/', '', bcrypt(str_random(16))), | ||
| 'avatar' => $avatar | ||
| ]); | ||
| } | ||
| } | ||
This file contains hidden or 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,28 @@ | ||
| <?php | ||
|
|
||
| namespace App\Http\Controllers; | ||
|
|
||
| use Illuminate\Http\Request; | ||
|
|
||
| class HomeController extends Controller | ||
| { | ||
| /** | ||
| * Create a new controller instance. | ||
| * | ||
| * @return void | ||
| */ | ||
| public function __construct() | ||
| { | ||
| $this->middleware('auth'); | ||
| } | ||
|
|
||
| /** | ||
| * Show the application dashboard. | ||
| * | ||
| * @return \Illuminate\Http\Response | ||
| */ | ||
| public function index() | ||
| { | ||
| return view('home'); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On ne peut pas utiliser la version en PHP : Post::class ?