Browse Source

Add application installation feature (replaces user regsiter feature)

pull/1/head
Nafies Luthfi 8 years ago
parent
commit
844e1746a7
  1. 39
      app/Http/Controllers/AuthController.php
  2. 52
      app/Http/Controllers/InstallationController.php
  3. 2
      app/Http/Requests/Accounts/RegisterRequest.php
  4. 12
      resources/views/auth/login.blade.php
  5. 48
      resources/views/auth/register.blade.php
  6. 8
      routes/web/account.php
  7. 74
      tests/Feature/Auth/InstallationTest.php
  8. 14
      tests/Feature/Auth/LoginTest.php
  9. 62
      tests/Feature/Auth/RegistrationTest.php

39
app/Http/Controllers/AuthController.php

@ -1,39 +0,0 @@
<?php
namespace App\Http\Controllers;
use App\Entities\Users\User;
use App\Http\Requests\Accounts\RegisterRequest;
use Auth;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\PasswordBroker;
class AuthController extends Controller
{
public function __construct(Guard $auth, PasswordBroker $passwords)
{
$this->auth = $auth;
$this->passwords = $passwords;
$this->middleware('guest');
}
public function getRegister()
{
return view('auth.register');
}
public function postRegister(RegisterRequest $request)
{
$registerData = $request->only('name', 'email', 'password');
$registerData['api_token'] = str_random(32);
$user = User::create($registerData);
Auth::login($user);
flash()->success(trans('auth.welcome', ['name' => $user->name]));
return redirect()->route('home');
}
}

52
app/Http/Controllers/InstallationController.php

@ -0,0 +1,52 @@
<?php
namespace App\Http\Controllers;
use App\Entities\Users\User;
use App\Http\Requests\Accounts\RegisterRequest;
use Auth;
/**
* Installation Controller
*
* @author Nafies Luthfi <nafiesl@gmail.com>
*/
class InstallationController extends Controller
{
public function __construct()
{
$this->middleware('guest');
}
public function getRegister()
{
if (User::count()) {
return redirect()->route('auth.login');
}
return view('auth.register');
}
public function postRegister(RegisterRequest $request)
{
$adminData = $request->only('name', 'email', 'password');
$adminData['api_token'] = str_random(32);
$admin = User::create($adminData);
Auth::login($admin);
$agencyData = collect($request->only('agency_name', 'agency_website', 'email'))
->map(function ($value, $key) {
return [
'key' => $key,
'value' => $value,
];
})->toArray();
\DB::table('site_options')->insert($agencyData);
flash()->success(trans('auth.welcome', ['name' => $admin->name]));
return redirect()->route('home');
}
}

2
app/Http/Requests/Accounts/RegisterRequest.php

@ -24,6 +24,8 @@ class RegisterRequest extends Request
public function rules()
{
return [
'agency_name' => 'required|max:255',
'agency_website' => 'nullable|url|max:255',
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users,email',
'password' => 'required|between:6,15|confirmed',

12
resources/views/auth/login.blade.php

@ -5,23 +5,15 @@
@section('content')
@include('flash::message')
<div class="login-panel col-md-4 col-md-offset-4 text-center">
{{ Html::image(url('assets/imgs/logo.png'), 'Logo '.Option::get('agency_name','Aplikasi Laravel')) }}
{!! appLogoImage() !!}
<h3>{{ Option::get('app_name','Aplikasi Laravel') }}</h3>
<div class="panel panel-default">
<div class="panel-body">
@include('auth.partials._notifications')
{{ Form::open(['route'=>'auth.login']) }}
{!! FormField::email('email', ['label' => false, 'placeholder'=> trans('auth.email')]) !!}
{!! FormField::password('password', ['label' => false, 'placeholder'=> trans('auth.password')]) !!}
{{ Form::submit(trans('auth.login'), ['class'=>'btn btn-success btn-block']) }}
<div class="row">
<div class="col-md-6">
{{ link_to_route('auth.register', trans('auth.need_account'),[],['class'=>'btn btn-link']) }}
</div>
<div class="col-md-6">
{{ link_to_route('auth.reset-request', trans('auth.forgot_password'), [], ['class'=>'btn btn-link']) }}
</div>
</div>
{{ link_to_route('auth.reset-request', trans('auth.forgot_password'), [], ['class'=>'btn btn-link']) }}
{{ Form::close() }}
</div>
</div>

48
resources/views/auth/register.blade.php

@ -5,39 +5,41 @@
@section('content')
<div class="col-md-6 col-md-offset-3">
<div class="login-panel panel panel-default">
<div class="panel-heading"><h3 class="panel-title">{{ trans('auth.register') }}</h3></div>
<div class="panel-body">
@include('auth.partials._notifications')
{!! Form::open(['route'=>'auth.register','class'=>'form-horizontal']) !!}
<div class="form-group {!! $errors->has('name') ? 'has-error' : ''; !!}">
{!! Form::label('name', trans('app.name'), ['class'=>'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::text('name', null, ['class'=>'form-control','placeholder' => trans('app.name')]) !!}
</div>
<div class="text-center">
{!! appLogoImage(['style' => 'width:150px']) !!}
<h3>{{ Option::get('app_name','Aplikasi Laravel') }}</h3>
</div>
<div class="form-group {!! $errors->has('email') ? 'has-error' : ''; !!}">
{!! Form::label('email', trans('user.email'), ['class'=>'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::text('email', null, ['class'=>'form-control','placeholder'=>trans('user.email')]) !!}
</div>
<hr>
{!! Form::open(['route' => 'app.install', 'class' => '']) !!}
<p>Silakan isi formulir di bawah ini untuk membuat akun Administrator dan Agensi.</p>
<div class="row">
<div class="col-md-6">
{!! FormField::text('agency_name', ['required' => true, 'label' => trans('agency.name')]) !!}
</div>
<div class="col-md-6">
{!! FormField::text('agency_website', ['required' => true, 'label' => trans('agency.website')]) !!}
</div>
</div>
<div class="form-group {!! $errors->has('password') ? 'has-error' : ''; !!}">
{!! Form::label('password', trans('auth.password'), ['class'=>'col-md-4 control-label']) !!}
<div class="row">
<div class="col-md-6">
{!! Form::password('password', ['class'=>'form-control','placeholder'=>trans('auth.password')]) !!}
{!! FormField::text('name', ['required' => true, 'label' => trans('user.name')]) !!}
</div>
<div class="col-md-6">
{!! FormField::email('email', ['required' => true, 'label' => trans('auth.email')]) !!}
</div>
</div>
<div class="form-group {!! $errors->has('password_confirmation') ? 'has-error' : ''; !!}">
{!! Form::label('password_confirmation', trans('auth.password_confirmation'), ['class'=>'col-md-4 control-label']) !!}
<div class="row">
<div class="col-md-6">
{!! FormField::password('password', ['required' => true, 'label' => trans('auth.password')]) !!}
</div>
<div class="col-md-6">
{!! Form::password('password_confirmation', ['class'=>'form-control','placeholder'=>trans('auth.password_confirmation')]) !!}
{!! FormField::password('password_confirmation', ['required' => true, 'label' => trans('auth.password_confirmation')]) !!}
</div>
</div>
<div class="form-group">
<div class="col-md-8 col-md-offset-4">
{!! Form::submit(trans('auth.register'), ['class'=>'btn btn-info']) !!}
{!! link_to_route('auth.login',trans('auth.have_an_account'),[],['class'=>'btn btn-success']) !!}
</div>
{!! Form::submit(trans('auth.register'), ['class' => 'btn btn-success']) !!}
{!! link_to_route('auth.login', trans('auth.have_an_account'), [], ['class' => 'btn btn-link pull-right']) !!}
</div>
{!! Form::close() !!}
</div>

8
routes/web/account.php

@ -1,10 +1,10 @@
<?php
/**
* Account Routes
* Installation Routes
*/
Route::group(['middleware' => 'web', 'as' => 'auth.'], function () {
Route::get('register', ['as' => 'register', 'uses' => 'AuthController@getRegister']);
Route::post('register', ['as' => 'register', 'uses' => 'AuthController@postRegister']);
Route::group(['middleware' => 'web'], function () {
Route::get('app-install', ['as' => 'app.install', 'uses' => 'InstallationController@getRegister']);
Route::post('app-install', ['as' => 'app.install', 'uses' => 'InstallationController@postRegister']);
});
// Authentication Routes...

74
tests/Feature/Auth/InstallationTest.php

@ -0,0 +1,74 @@
<?php
namespace Tests\Feature\Auth;
use App\Entities\Users\User;
use Tests\TestCase;
/**
* Installation Feature Test
*
* @author Nafies Luthfi <nafiesl@gmail.com>
*/
class InstallationTest extends TestCase
{
/** @test */
public function user_cannot_visit_register_page_if_user_already_exists_in_database()
{
factory(User::class)->create(['email' => 'member@app.dev']);
$this->visit(route('app.install'));
$this->seePageIs(route('auth.login'));
}
/** @test */
public function registration_validation()
{
$this->visit(route('app.install'));
$this->seePageIs(route('app.install'));
$this->submitForm(trans('auth.register'), [
'name' => 'Nama Member',
'email' => 'email',
'password' => 'password',
'password_confirmation' => 'password..',
]);
$this->seePageIs(route('app.install'));
}
/** @test */
public function member_register_successfully()
{
$this->visit(route('app.install'));
$this->seePageIs(route('app.install'));
$this->submitForm(trans('auth.register'), [
'agency_name' => 'Nama Agensi',
'agency_website' => 'https://example.com',
'name' => 'Nama Admin',
'email' => 'email@mail.com',
'password' => 'password.111',
'password_confirmation' => 'password.111',
]);
$this->seePageIs(route('home'));
$this->see(trans('auth.welcome', ['name' => 'Nama Admin']));
$this->seeInDatabase('users', [
'name' => 'Nama Admin',
'email' => 'email@mail.com',
]);
$this->seeInDatabase('site_options', [
'key' => 'agency_name',
'value' => 'Nama Agensi',
]);
$this->seeInDatabase('site_options', [
'key' => 'agency_website',
'value' => 'https://example.com',
]);
}
}

14
tests/Feature/Auth/LoginTest.php

@ -19,8 +19,9 @@ class LoginTest extends TestCase
'password' => 'member',
]);
$this->seePageIs(route('home'));
$this->see(trans('auth.welcome', ['name' => $user->name]));
$this->seePageIs(route('home'));
$this->seeIsAuthenticated();
$this->click(trans('auth.logout'));
@ -32,10 +33,13 @@ class LoginTest extends TestCase
public function member_invalid_login()
{
$this->visit(route('auth.login'));
$this->type('email@mail.com', 'email');
$this->type('password.112', 'password');
$this->press(trans('auth.login'));
$this->submitForm(trans('auth.login'), [
'email' => 'email@mail.com',
'password' => 'member',
]);
$this->seePageIs(route('auth.login'));
$this->see(trans('auth.failed'));
$this->dontSeeIsAuthenticated();
}
}

62
tests/Feature/Auth/RegistrationTest.php

@ -1,62 +0,0 @@
<?php
namespace Tests\Feature\Auth;
use App\Entities\Users\User;
use Tests\TestCase;
class RegistrationTest extends TestCase
{
/** @test */
public function registration_validation()
{
factory(User::class)->create(['email' => 'member@app.dev']);
$this->visit(route('auth.register'));
$this->submitForm(trans('auth.register'), [
'name' => '',
'email' => 'member@app.dev',
'password' => '',
'password_confirmation' => '',
]);
$this->seePageIs(route('auth.register'));
$this->see('Nama harus diisi.');
$this->see('Email ini sudah terdaftar.');
$this->see('Password harus diisi.');
$this->see('Konfirmasi password harus diisi.');
$this->submitForm(trans('auth.register'), [
'name' => 'Nama Member',
'email' => 'email',
'password' => 'password',
'password_confirmation' => 'password..',
]);
$this->seePageIs(route('auth.register'));
$this->see('Email tidak valid.');
$this->see('Konfirmasi password tidak sesuai.');
}
/** @test */
public function member_register_successfully()
{
$this->visit(route('auth.register'));
$this->submitForm(trans('auth.register'), [
'name' => 'Nama Member',
'email' => 'email@mail.com',
'password' => 'password.111',
'password_confirmation' => 'password.111',
]);
$this->seePageIs(route('home'));
$this->see(trans('auth.welcome', ['name' => 'Nama Member']));
$this->seeInDatabase('users', [
'name' => 'Nama Member',
'email' => 'email@mail.com',
]);
}
}
Loading…
Cancel
Save