Commit 41cc5dfe authored by reaby's avatar reaby

addming oauth2 and finished setup wizard

parent a09a852d
......@@ -9,3 +9,4 @@ Homestead.yaml
npm-debug.log
.env
.idea/*
.setupDone
<?php
namespace App\Helpers;
use Laravel\Socialite\Two\AbstractProvider;
use Laravel\Socialite\Two\ProviderInterface;
class ManiaplanetProvider extends AbstractProvider implements ProviderInterface
{
const SCOPE_BASIC = 'basic';
const SCOPE_DEDICATED = 'dedicated';
const SCOPE_TITLES = 'titles';
const SCOPE_EVENTS = 'events';
const SCOPE_MAPS = 'maps';
/**
* {@inheritdoc}
*/
protected function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase('https://v4.live.maniaplanet.com/login/oauth2/authorize', $state);
}
protected function usesState()
{
return true;
}
protected function getCodeFields($state = null)
{
$fields = [
'client_id' => $this->clientId,
'redirect_uri' => $this->redirectUrl,
'scope' => $this->formatScopes($this->getScopes(), $this->scopeSeparator),
'response_type' => 'code',
];
if ($this->usesState()) {
$fields['state'] = $state;
}
return array_merge($fields, $this->parameters);
}
/**
* {@inheritdoc}
*/
protected function getTokenUrl()
{
return 'https://v4.live.maniaplanet.com/login/oauth2/access_token';
}
/**
* {@inheritdoc}
*/
protected function getTokenFields($code)
{
return array_add(
parent::getTokenFields($code), 'grant_type', 'authorization_code'
);
}
/**
* {@inheritdoc}
*/
protected function getUserByToken($token)
{
$response = $this->getHttpClient()->get('https://v4.live.maniaplanet.com/webservices/me', [
'headers' => [
'Authorization' => 'Bearer ' . $token,
],
]);
return json_decode($response->getBody(), true);
}
/**
* {@inheritdoc}
*/
protected function formatScopes(array $scopes, $scopeSeparator)
{
return implode(' ', $scopes);
}
/**
* {@inheritdoc}
*/
protected function mapUserToObject(array $user)
{
$uuser = new \Laravel\Socialite\Two\User();
return $uuser->setRaw($user)->map([
'name' => $user['login'],
'login' => $user['login'],
'nickname' => $user['nickname'],
'path' => $user['path']
]);
}
}
......@@ -4,6 +4,7 @@ namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
class LoginController extends Controller
{
......@@ -18,7 +19,9 @@ class LoginController extends Controller
|
*/
use AuthenticatesUsers;
use AuthenticatesUsers {
logout as performLogout;
}
/**
* Where to redirect users after login.
......@@ -36,4 +39,10 @@ class LoginController extends Controller
{
$this->middleware('guest')->except('logout');
}
public function logout(Request $request)
{
$this->performLogout($request);
return redirect("/");
}
}
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Support\Facades\Auth;
use Laravel\Socialite\Facades\Socialite;
class OauthLoginController extends Controller
{
protected $redirectTo = "/home";
/**
* Redirect the user to the GitHub authentication page.
*
* @return Response
*/
public function redirectToProvider()
{
return Socialite::driver('maniaplanet')->scopes(["basic", "dedicated"])->redirect();
}
/**
*
* @return Response
*/
public function handleProviderCallback()
{
try {
$user = Socialite::driver('maniaplanet')->user();
$authUser = $this->findOrCreateUser($user);
Auth::login($authUser, true);
return redirect($this->redirectTo);
} catch (\Exception $ex) {
dd($ex->getMessage());
}
}
public function findOrCreateUser($user)
{
$authUser = User::where('name', $user->login)->first();
if ($authUser) {
return $authUser;
}
return User::create([
'name' => $user->login,
'nickname' => $user->nickname,
'path' => $user->path
]);
}
}
<?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');
}
}
......@@ -15,7 +15,7 @@ class Setup extends Controller
public function step1(Request $request)
{
return view("setup");
return view("setup.mysql");
}
public function step2()
......@@ -23,30 +23,62 @@ class Setup extends Controller
try {
// Bootstrap artisan
\Artisan::bootstrap();
// see th
\Artisan::call('migrate:status');
$info = explode("\r\n", \Artisan::output());
switch ($info[0]) {
case "No migrations found.":
dd("success!");
break;
\Artisan::call('migrate:install');
\Artisan::call('migrate');
return redirect("/setup/3");
default:
\Artisan::call('migrate:refresh');
return redirect("/setup/3")->with("info", "success!");
}
} catch (\PDOException $PDOException) {
// in case mysql is not connected
return $PDOException->getMessage();
} catch (\Illuminate\Database\QueryException $queryException) {
// in case query exception
return $queryException->getMessage();
if ($queryException->getCode() == "42S01") {
return redirect("/setup/3")->with("info", "Already migrated");
}
//return redirect("/setup")->with("error", $queryException->getCode() . "<br/>" . $queryException->getMessage());
} catch (\PDOException $PDOException) {
// in case mysql is not connected
return redirect("/setup")->with("error", $PDOException->getMessage());
}
}
public function step3(Request $request)
{
return view("setup.maniaplanet");
}
public function postSetupMySql(StoreSetup $request)
public function step4()
{
touch(base_path(".setupDone"));
return view("setup.done");
}
public function postSetupOauth(Request $request)
{
$ini = $this->parseEnv();
$ini['DB_CONNECTION'] = $request->get('connection', "mysql");
$ini['USE_OAUTH2'] = $request->get('oauth2', true);
$ini['MANIAPLANET_CLIENT_ID'] = $request->get('client_id');
$ini['MANIAPLANET_CLIENT_SECRET'] = $request->get('client_secret');
try {
file_put_contents(base_path() . DIRECTORY_SEPARATOR . app()->environmentFile(), $this->makeEnv($ini));
} catch (\Exception $exception) {
return redirect("/setup/3")->with("error", $exception->getMessage());
}
return redirect("setup/4");
}
public function postSetupMySql(StoreSetup $request)
{
$ini = $this->parseEnv();
$ini['APP_URL'] = $request->get('appurl');
// $ini['DB_CONNECTION'] = $request->get('connection', "mysql");
$ini['DB_CONNECTION'] = "mysql";
$ini['DB_HOST'] = $request->get('host', "localhost");
$ini['DB_PORT'] = $request->get('port', "3306");
$ini['DB_DATABASE'] = $request->get('database', "adminserv");
......@@ -55,13 +87,9 @@ class Setup extends Controller
try {
file_put_contents(base_path() . DIRECTORY_SEPARATOR . app()->environmentFile(), $this->makeEnv($ini));
} catch (\Exception $exception) {
return view("/setup")->with("error", $exception->getMessage());
return redirect("/setup")->with("error", $exception->getMessage());
}
$this->step2();
// return view("/setup/2/");
return redirect("/setup/2");
}
......
......@@ -24,7 +24,7 @@ class StoreSetup extends FormRequest
public function rules()
{
return [
'connection' => 'required',
'appurl' => "required",
'port' => "required|numeric",
'database' => 'required',
'user' => 'required',
......@@ -36,11 +36,10 @@ class StoreSetup extends FormRequest
public function messages()
{
return [
// custom error messages:
// "connection.required" => "test",
];
return [
// custom error messages:
// "connection.required" => "test",
];
}
}
......@@ -2,6 +2,8 @@
namespace App\Providers;
use App\Helpers\ManiaplanetProvider;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
......@@ -13,7 +15,18 @@ class AppServiceProvider extends ServiceProvider
*/
public function boot()
{
//
Schema::defaultStringLength(191);
if (env("USE_OAUTH2")) {
$socialite = $this->app->make('Laravel\Socialite\Contracts\Factory');
$socialite->extend(
'maniaplanet',
function ($app) use ($socialite) {
$config = $app['config']['services.maniaplanet'];
return $socialite->buildProvider(ManiaplanetProvider::class, $config);
}
);
}
}
/**
......
......@@ -15,7 +15,7 @@ class User extends Authenticatable
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
'name', 'email', 'password', 'login', 'path', 'nickname'
];
/**
......
This diff is collapsed.
......@@ -180,6 +180,8 @@ return [
/* Additions... */
Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class,
Collective\Html\HtmlServiceProvider::class,
Laravel\Socialite\SocialiteServiceProvider::class,
],
/*
......@@ -228,8 +230,11 @@ return [
'URL' => Illuminate\Support\Facades\URL::class,
'Validator' => Illuminate\Support\Facades\Validator::class,
'View' => Illuminate\Support\Facades\View::class,
/* custom stuff starts here */
'Form' => Collective\Html\FormFacade::class,
'Html' => Collective\Html\HtmlFacade::class,
'Socialite' => Laravel\Socialite\Facades\Socialite::class,
],
];
......@@ -35,4 +35,9 @@ return [
'secret' => env('STRIPE_SECRET'),
],
"maniaplanet" => [
'client_id' => env('MANIAPLANET_CLIENT_ID'),
'client_secret' => env('MANIAPLANET_CLIENT_SECRET'),
'redirect' => env('APP_URL') . '/oauthlogin'
],
];
......@@ -16,8 +16,11 @@ class CreateUsersTable extends Migration
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->string('login', 100)->unique();
$table->string('path')->nullable();
$table->string('nickname')->nullable();
$table->string('email')->nullable();
$table->string('password')->nullable();
$table->rememberToken();
$table->timestamps();
});
......
This diff is collapsed.
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
color: #fff;
color: #122;
}
.is-dark {
......@@ -38,3 +38,16 @@ body {
border-radius: 10px;
border: 10px solid #fff;
}
.maniaconnect {
display: block;
width: 161px; /* 202px */
height: 40px; /* 50px*/
background: url('/images/maniaconnect.png') top;
background-size: cover;
text-indent: -99999px;
}
.maniaconnect:hover {
background-position: bottom;
}
@extends('layouts.app')
@section('content')
<section>
<form class="form-horizontal" role="form" method="POST" action="{{ route('login') }}">
{{ csrf_field() }}
<div class="field">
<label class="label">Login</label>
<p class="control has-icons-left has-icons-right">
<input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required
autofocus>
<span class="icon is-small is-left">
<i class="fa fa-envelope"></i>
</span>
</p>
</div>
<div class="field">
<label class="label">Password</label>
<p class="control has-icons-left has-icons-right">
<input id="password" type="password" class="form-control" name="password" required>
<span class="icon is-small is-left">
<i class="fa fa-password"></i>
</span>
</p>
</div>
<div class="field">
<p class="control">
<label class="checkbox">
<input type="checkbox" name="remember" {{ old('remember') ? 'checked' : '' }}> Remember Me
</label>
</p>
</div>
<div class="field is-grouped">
<p class="control">
<button class="button is-primary">Submit</button>
</p>
<p class="control">
<a class="maniaconnect" href="/login/maniaplanet">Mania Connect</a>
</p>
<p class="control">
<a class="button is-link" href="{{ route('password.request') }}">
Forgot Your Password?
</a>
</p>
</div>
</form>
</section>
@endsection
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Reset Password</div>
<div class="panel-body">
@if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
@endif
<form class="form-horizontal" role="form" method="POST" action="{{ route('password.email') }}">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="email" class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required>
@if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Send Password Reset Link
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Reset Password</div>
<div class="panel-body">
@if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
@endif
<form class="form-horizontal" role="form" method="POST" action="{{ route('password.request') }}">
{{ csrf_field() }}
<input type="hidden" name="token" value="{{ $token }}">
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="email" class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control" name="email" value="{{ $email or old('email') }}" required autofocus>
@if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control" name="password" required>
@if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('password_confirmation') ? ' has-error' : '' }}">
<label for="password-confirm" class="col-md-4 control-label">Confirm Password</label>
<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" required>
@if ($errors->has('password_confirmation'))
<span class="help-block">
<strong>{{ $errors->first('password_confirmation') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Reset Password
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Register</div>
<div class="panel-body">
<form class="form-horizontal" role="form" method="POST" action="{{ route('register') }}">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<label for="name" class="col-md-4 control-label">Name</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control" name="name" value="{{ old('name') }}" required autofocus>
@if ($errors->has('name'))
<span class="help-block">
<strong>{{ $errors->first('name') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="email" class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required>
@if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control" name="password" required>
@if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group">
<label for="password-confirm" class="col-md-4 control-label">Confirm Password</label>
<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" required>
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Register