Skip to content
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

Part a #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
19 changes: 18 additions & 1 deletion app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,27 @@

namespace App\Exceptions;

use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Support\Facades\Auth;

class Handler extends ExceptionHandler
{
protected function unauthenticated($request, AuthenticationException $exception)
{
if ($request->expectsJson()) {
return response()->json(['error' => 'Unauthenticated.'], 401);
}
if ($request->is('admin') || $request->is('admin/*')) {
return redirect()->guest('/login/admin');
}
if ($request->is('manager') || $request->is('manager/*')) {
return redirect()->guest('/login/manager');
}
return redirect()->guest(route('login'));
}

/**
* A list of the exception types that are not reported.
*
Expand Down Expand Up @@ -34,4 +51,4 @@ public function register()
{
//
}
}
}
70 changes: 70 additions & 0 deletions app/Http/Controllers/Auth/LoginController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace App\Http\Controllers\Auth;

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

class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
$this->middleware('guest:admin')->except('logout');
$this->middleware('guest:manager')->except('logout');
}
public function showAdminLoginForm()
{
return view('auth.login', ['url' => 'admin']);
}
public function adminLogin(Request $request)
{
$this->validate($request, [
'email' => 'required|email',
'password' => 'required|min:6'
]);
if (Auth::guard('admin')->attempt(['email' => $request->email, 'password' => $request->password], $request->get('remember'))) {
return redirect()->intended('/admin'); //not implemented
}
return back()->withInput($request->only('email', 'remember'));
}
public function showManagerLoginForm()
{
return view('auth.login', ['url' => 'manager']);
}
public function managerLogin(Request $request)
{
$this->validate($request, [
'email' => 'required|email',
'password' => 'required|min:6'
]);
if (Auth::guard('manager')->attempt(['email' => $request->email, 'password' => $request->password], $request->get('remember'))) {
return redirect()->intended('/manager'); //not implemented
}
return back()->withInput($request->only('email', 'remember'));
}
}
119 changes: 119 additions & 0 deletions app/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

namespace App\Http\Controllers\Auth;

use App\Models\Manager;
use App\Models\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Http\Request;

class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
$this->middleware('guest:admin');
$this->middleware('guest:manager');
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:6', 'confirmed'],
]);
}
/**
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function showAdminRegisterForm()
{
return view('auth.register', ['url' => 'admin']);
}
/**
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function showManagerRegisterForm()
{
return view('auth.register', ['url' => 'manager']);
}
/**
* @param array $data
*
* @return mixed
*/

protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}

/**
* @param Request $request
*
* @return \Illuminate\Http\RedirectResponse
*/

protected function createAdmin(Request $request)
{
$this->validator($request->all())->validate();
Admin::create([
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password),
]);
return redirect()->intended('login/admin');
}

/**
* @param Request $request
*
* @return \Illuminate\Http\RedirectResponse
*/

protected function createManager(Request $request)
{
$this->validator($request->all())->validate();
Manager::create([
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password),
]);
return redirect()->intended('login/manager');
}
}
103 changes: 103 additions & 0 deletions app/Http/Controllers/CommentController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace App\Http\Controllers;

use App\Models\Comment;
use App\Models\Project;
use Illuminate\Http\Request;

class CommentController extends Controller
{
public function view($project)
{
$data = Project::find($project);
return view('comments.createComment', ['project' => $data]);
}

public function store(Request $request, $project)
{
// dd($request);
$data = Project::find($project);

$request->validate([
'content' => 'required|string|max:1000',
]);


// $user = auth()->user();

$comment = new Comment();
$comment->content = $request['content'];
$comment->user_id = $request['user_id'];
// $comment->user_id = $user->id;
$comment->project_id = $request['project_id'];

$comment->save();

return view('comments.createComment', ['project' => $data]);
}

// Retrieve a specific comment
// public function show(Project $project, Comment $comment)
// {
// return view('comments.show', ['comment' => $comment]);
// }

// Update an existing comment
public function edit($project, $comment)
{
$project = Project::findOrFail($project);
$comment = Comment::findOrFail($comment);

// Ensure the authenticated user is the owner of the comment
// if ($comment->user_id !== auth()->user()->id) {
// return redirect()->route('projects.show', ['project' => $project->id])
// ->withErrors('You do not have permission to edit this comment.');
// }

return view('comments.editComment', ['project' => $project, 'comment' => $comment]);
}

public function update(Request $request, $project, $comment)
{
// echo 123;
// dd($request);

$project = Project::findOrFail($project);
$comment = Comment::findOrFail($comment);
// dd($project, $comment);
// Ensure the authenticated user is the owner of the comment
// if ($comment->user_id !== auth()->user()->id) {
// // If the user is not authorized, redirect them back
// return redirect()->route('projects.show', ['project' => $project->id])
// ->withErrors('You do not have permission to edit this comment.');
// }

// Validate the input data
$request->validate([
'content' => 'required|string|max:1000',
]);

$comment->content = $request->input('content');
$comment->save();

return view('comments.createComment', ['project' => $project]);
}

// // Delete a specific comment
public function destroy($project, $comment)
{
$project = Project::findOrFail($project);
$comment = Comment::findOrFail($comment);

// Ensure the authenticated user is the owner of the comment
// if ($comment->user_id !== auth()->user()->id) {
// return redirect()->route('projects.show', ['project' => $project])
// ->withErrors('You do not have permission to delete this comment.');
// }

$comment->delete();

return view('comments.createComment', ['project' => $project]);
}
}
17 changes: 9 additions & 8 deletions app/Http/Middleware/RedirectIfAuthenticated.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ class RedirectIfAuthenticated
* @param string[]|null ...$guards
* @return mixed
*/
public function handle($request, Closure $next, ...$guards)
public function handle($request, Closure $next, $guard = null)
{
$guards = empty($guards) ? [null] : $guards;

foreach ($guards as $guard) {
if (Auth::guard($guard)->check()) {
return redirect(RouteServiceProvider::HOME);
}
if ($guard == "admin" && Auth::guard($guard)->check()) {
return redirect('/admin');
}
if ($guard == "manager" && Auth::guard($guard)->check()) {
return redirect('/manager');
}
if (Auth::guard($guard)->check()) {
return redirect('/home');
}

return $next($request);
}
}
Loading