Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ Homestead.yaml
npm-debug.log
yarn-error.log
.env
/360-dev.iml
117 changes: 117 additions & 0 deletions app/Concern/Admin/TraitAdminController.php
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é');
}
}
17 changes: 17 additions & 0 deletions app/Http/Controllers/Admin/PostController.php
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';
Copy link
Collaborator

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 ?


protected $view = 'post';

protected $validator = [];
}
17 changes: 17 additions & 0 deletions app/Http/Controllers/Admin/RoleController.php
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 = [];
}
39 changes: 39 additions & 0 deletions app/Http/Controllers/Admin/UserController.php
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é');
}
}
30 changes: 29 additions & 1 deletion app/Http/Controllers/Auth/ForgotPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Password;

class ForgotPasswordController extends Controller
{
Expand All @@ -23,10 +27,34 @@ class ForgotPasswordController extends Controller
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}

/**
* Send a reset link to the given user.
*
* @param Request $request
* @return RedirectResponse|JsonResponse
*/
public function sendResetLinkEmail(Request $request)
{
$this->validateEmail($request);

// We will send the password reset link to this user. Once we have attempted
// to send the link, we will examine the response then see the message we
// need to show to the user. Finally, we'll send out a proper response.
$response = $this->broker()->sendResetLink(
array_merge(
$request->only('email'),
['verify_token' => null]
)
);

return $response == Password::RESET_LINK_SENT
? $this->sendResetLinkResponse($response)
: $this->sendResetLinkFailedResponse($request, $response);
}
}
16 changes: 15 additions & 1 deletion app/Http/Controllers/Auth/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;

class LoginController extends Controller
{
Expand All @@ -30,10 +31,23 @@ class LoginController extends Controller
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}

/**
* Get the needed authorization credentials from the request.
*
* @param Request $request
* @return array
*/
protected function credentials(Request $request)
{
return array_merge(
$request->only($this->username(), 'password'),
['verify_token' => null]
);
}
}
57 changes: 54 additions & 3 deletions app/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
*
Expand All @@ -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;
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
]);
}
}
28 changes: 28 additions & 0 deletions app/Http/Controllers/HomeController.php
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');
}
}
Loading