Last active
August 5, 2019 14:04
-
-
Save fhulufhelo/5f4b48fa0844c8db1cf4c4e2df1b95a8 to your computer and use it in GitHub Desktop.
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
<?php | |
namespace App\Http\Controllers; | |
use Illuminate\Http\Request; | |
use App\User; | |
use Illuminate\Support\Facades\Auth; | |
use Illuminate\Support\Facades\Mail; | |
use App\Mail\WelcomeSeller; | |
use App\Mail\WelcomeBuyer; | |
use App\Mail\WelcomeNewAdmin; | |
use Illuminate\Auth\Access\AuthorizationException; | |
class UsersController extends Controller | |
{ | |
/** | |
* handles the registration of new users | |
* | |
* @param \Illuminate\Http\Request $request | |
* @return \Illuminate\Http\Response | |
*/ | |
public function store(Request $request) | |
{ | |
#1 Register seller | |
if ($request->role == 'seller') { | |
$user = $this->create($request->all()); | |
// Send welcome email to newly created seller user | |
Mail::to($user)->send(new WelcomeSeller($user)); | |
// Login user | |
$credentials = $request->only('email', 'password'); | |
Auth::attempt($credentials); | |
// rediect user to dashbord | |
return redirect()->intended('dashboard.seller'); | |
} | |
#2 Register buyer | |
if ($request->role == 'buyer') { | |
$user = $this->create($request->all()); | |
// Send welcome email to newly created buyer user | |
Mail::to($user)->send(new WelcomeBuyer($user)); | |
// Login user | |
$credentials = $request->only('email', 'password'); | |
Auth::attempt($credentials); | |
// rediect user to dashbord | |
return redirect()->intended('dashboard.seller'); | |
} | |
#3 Register admin | |
if ($request->role == 'admin') { | |
$user = $this->create($request->all()); | |
// The current user can add admin user? | |
if (Auth::check() && Auth::user()->superAdmin()) { | |
$user = $this->create($request); | |
// Send welcome email to newly created buyer user | |
Mail::to($user)->send(new WelcomeNewAdmin($user)); | |
// rediect user to dashbord | |
} else { | |
throw new ValidationException( 'This action is unauthorized.' ); | |
} | |
} | |
} | |
/** | |
* Store a newly created user in database. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @return \App\User | |
*/ | |
function create(array $data) | |
{ | |
return User::create($data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment