Skip to content

Commit

Permalink
Implement migrations within setup, reduce redundancy, 2016
Browse files Browse the repository at this point in the history
  • Loading branch information
cydrobolt committed Jan 18, 2016
1 parent e73cede commit fc0ed95
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 29 deletions.
2 changes: 0 additions & 2 deletions .env-temporary
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ APP_KEY=F8Lj#2v%!@$ku6FXrTBscBSs^O$VOvus
APP_LOCALE=en
APP_FALLBACK_LOCALE=en

POLR_SETUP_RAN=false

# DB_CONNECTION=mysql
# DB_HOST=localhost
# DB_PORT=3306
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Polr 2.0 moves away from `mysqli`, rather taking on `PDO` with `Eloquent`. Routi
####License


Copyright (C) 2013-2015 Chaoyi Zha
Copyright (C) 2013-2016 Chaoyi Zha

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
Expand Down
24 changes: 24 additions & 0 deletions app/Factories/UserFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
namespace App\Factories;

use Hash;
use App\Models\User;
use App\Helpers\CryptoHelper;
class UserFactory {
public static function createUser($username, $email, $password, $active=0, $ip='127.0.0.1') {
$hashed_password = Hash::make($password);

$recovery_key = CryptoHelper::generateRandomHex(50);
$user = new User;
$user->username = $username;
$user->password = $hashed_password;
$user->email = $email;
$user->recovery_key = $recovery_key;
$user->active = $active;
$user->ip = $ip;
$user->save();

return $user;
}

}
2 changes: 1 addition & 1 deletion app/Http/Controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function changePassword(Request $request) {
$user->save();

$request->session()->flash('success', "Password changed successfully.");
return redirect()->route('admin');
return redirect(route('admin'));
}
}
}
4 changes: 3 additions & 1 deletion app/Http/Controllers/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ class IndexController extends Controller {
* @return Response
*/
public function showIndexPage(Request $request) {
$random_key = CryptoHelper::generateRandomHex(50);
if (env('POLR_SETUP_RAN') != true) {
return redirect(route('setup'));
}
return view('index', ['large' => true]);
}
}
52 changes: 41 additions & 11 deletions app/Http/Controllers/SetupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Redirect;
use Illuminate\Console\Application\Artisan;
use Illuminate\Support\Facades\Artisan;

use App\Helpers\CryptoHelper;
use App\Models\User;
use App\Factories\UserFactory;
use Cache;

class SetupController extends Controller {
protected function parseExitCode($exitCode) {
protected static function parseExitCode($exitCode) {
if ($exitCode == 0) {
return true;
}
Expand All @@ -16,7 +19,7 @@ protected function parseExitCode($exitCode) {
}
}

private function setupAlreadyRan() {
private static function setupAlreadyRan() {
return view('error', [
'message' => 'Sorry, but you have already ran the setup script previously.'
]);
Expand All @@ -26,28 +29,28 @@ private function resetDatabase() {
$exitCode = Artisan::call('migrate:refresh', [
'--force' => true,
]);
return $this->parseExitCode($exitCode);
return self::parseExitCode($exitCode);
}

private function createDatabase() {
private static function createDatabase() {
$exitCode = Artisan::call('migrate');
return $this->parseExitCode($exitCode);
return self::parseExitCode($exitCode);
}

public static function displaySetupPage(Request $request) {
if (env('POLR_SETUP_RAN')) {
return $this->setupAlreadyRan();
return self::setupAlreadyRan();
}

return view('setup');
}

public static function performSetup(Request $request) {
if (env('POLR_SETUP_RAN')) {
return $this->setupAlreadyRan();
return self::setupAlreadyRan();
}

$app_key = CryptoHelper::generateRandomHex(32);
$app_key = CryptoHelper::generateRandomHex(16);
$app_name = $request->input('app:name');
$app_protocol = $request->input('app:protocol');

Expand Down Expand Up @@ -87,6 +90,11 @@ public static function performSetup(Request $request) {
]);
}

$acct_username = $request->input('acct:username');
$acct_email = $request->input('acct:email');
$acct_password = $request->input('acct:password');
$acct_group = "admin";

// if true, only logged in users can shorten
$st_shorten_permission = $request->input('setting:shorten_permission');
$st_index_redirect = $request->input('setting:index_redirect');
Expand Down Expand Up @@ -142,12 +150,34 @@ public static function performSetup(Request $request) {

$handle = fopen('../.env', 'w');
if (fwrite($handle, $compiled_configuration) === FALSE) {
return view('error', [
$response = view('error', [
'message' => 'Could not write configuration to disk.'
]);
} else {
return redirect(route('index'))->with('success', 'Set up completed! Thanks for using Polr!');

$response = redirect(route('setup_finish'))->with(
'acct_username', $acct_username)->with(
'acct_email', $acct_email)->with(
'acct_password', $acct_password);


}
fclose($handle);

return $response;

}
public static function finishSetup(Request $request) {
$database_created = self::createDatabase();
if (!$database_created) {
return redirect(route('setup'))->with('error', 'Could not create database. Perhaps some credentials were incorrect?');
}

$user = UserFactory::createUser(session('acct_username'), session('acct_email'), session('acct_password'), 1, $request->ip());
$user->role = 'admin';
$user->save();
Cache::flush();

return view('setup_thanks')->with('success', 'Set up completed! Thanks for using Polr!');
}
}
14 changes: 3 additions & 11 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<?php
namespace App\Http\Controllers;
use Hash;
use Mail;
use App\Models\User;
use Illuminate\Http\Request;
use App\Helpers\CryptoHelper;
use App\Helpers\UserHelper;
use App\Factories\UserFactory;

class UserController extends Controller {
/**
Expand Down Expand Up @@ -71,13 +70,7 @@ public function performSignup(Request $request) {
return redirect('signup')->with('error', 'Please use a valid email to sign up.');
}

$recovery_key = CryptoHelper::generateRandomHex(50);
$user = new User;
$user->username = $username;
$user->password = $hashed_password;
$user->recovery_key = $recovery_key;
$user->active = 0;
$user->ip = $ip;
$user = UserFactory::createUser($username, $email, $password, $active, $ip);

$acct_activation_needed = env('POLR_ACCT_ACTIVATION');

Expand All @@ -89,14 +82,13 @@ public function performSignup(Request $request) {
else {
// email activation is necessary
Mail::send('emails.activation', [
'username' => $username, 'recovery_key' => $recovery_key, 'ip' => $ip
'username' => $username, 'recovery_key' => $user->recovery_key, 'ip' => $ip
], function ($m) use ($user) {
$m->to($user->email, $user->username)->subject(env('APP_NAME') . ' account activation');
});
$response = redirect('login')->with('success', 'Thanks for signing up! Please confirm your email to continue..');

}
$user->save();

return $response;
}
Expand Down
2 changes: 2 additions & 0 deletions app/Http/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

$app->get('/setup', ['as' => 'setup', 'uses' => 'SetupController@displaySetupPage']);
$app->post('/setup', ['as' => 'psetup', 'uses' => 'SetupController@performSetup']);
$app->get('/setup_finish', ['as' => 'setup_finish', 'uses' => 'SetupController@finishSetup']);


$app->get('/{short_url}', ['uses' => 'LinkController@performRedirect']);
$app->get('/{short_url}/{secret_key}', ['uses' => 'LinkController@performRedirect']);
Expand Down
2 changes: 1 addition & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;


class User extends Model {
protected $table = 'users';

Expand Down
2 changes: 1 addition & 1 deletion resources/views/about.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</div>
<a href='#' class='btn btn-success license-btn'>More Information</a>
<pre class="license" id="gpl-license">
Copyright (C) 2013-2015 Chaoyi Zha
Copyright (C) 2013-2016 Chaoyi Zha

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
Expand Down
1 change: 1 addition & 0 deletions resources/views/emails/activation.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

<p>Thanks,</p>
<p>The {{env('APP_NAME')}} team.</p>

--
You received this email because someone (hopefully you) from IP {{$ip}} signed up
for an account at {{env('APP_PROTOCOL')}}{{env('APP_ADDRESS')}}. If this was not you,
Expand Down
40 changes: 40 additions & 0 deletions resources/views/setup_thanks.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
@extends('layouts.minimal')

@section('title')
Setup Completed
@endsection

@section('css')
<link rel='stylesheet' href='/css/default-bootstrap.min.css'>
<link rel='stylesheet' href='/css/setup.css'>
@endsection

@section('content')
<div class="navbar navbar-default navbar-fixed-top">
<a class="navbar-brand" href="/">Polr</a>
</div>

<div class='row'>
<div class='col-md-3'></div>

<div class='col-md-6 setup-body well'>
<div class='setup-center'>
<img class='setup-logo' src='/img/logo.png'>
</div>
<h2>Setup Complete</h2>
<p>Your Polr setup is complete. To continue, you may <a href='{{route('login')}}'>login</a> or
access your <a href='{{route('index')}}'>home page</a>.
</p>
<p>Consider taking a look at the <a href='http://docs.polr.me/'>docs</a> or <a href='//github.com/cydrobolt/polr'>README</a>
for assistance.
</p>
<p>You may also join us on IRC at <a href='//webchat.freenode.net/?channels=#polr'><code>#polr</code></a> on freenode for assistance or questions.</p>

<p>Thanks for using Polr!</p>
</div>

<div class='col-md-3'></div>
</div>


@endsection

0 comments on commit fc0ed95

Please sign in to comment.