From fc0ed95fe3616ca60d94b6f29fc88c2067580610 Mon Sep 17 00:00:00 2001
From: Chaoyi Zha
Date: Mon, 18 Jan 2016 18:15:35 -0500
Subject: [PATCH] Implement migrations within setup, reduce redundancy, 2016
---
.env-temporary | 2 -
README.md | 2 +-
app/Factories/UserFactory.php | 24 ++++++++++
app/Http/Controllers/AdminController.php | 2 +-
app/Http/Controllers/IndexController.php | 4 +-
app/Http/Controllers/SetupController.php | 52 ++++++++++++++++-----
app/Http/Controllers/UserController.php | 14 ++----
app/Http/routes.php | 2 +
app/Models/User.php | 2 +-
resources/views/about.blade.php | 2 +-
resources/views/emails/activation.blade.php | 1 +
resources/views/setup_thanks.blade.php | 40 ++++++++++++++++
12 files changed, 118 insertions(+), 29 deletions(-)
create mode 100644 app/Factories/UserFactory.php
create mode 100644 resources/views/setup_thanks.blade.php
diff --git a/.env-temporary b/.env-temporary
index 83928a0..383d1f0 100644
--- a/.env-temporary
+++ b/.env-temporary
@@ -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
diff --git a/README.md b/README.md
index ef92863..5474a4f 100644
--- a/README.md
+++ b/README.md
@@ -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
diff --git a/app/Factories/UserFactory.php b/app/Factories/UserFactory.php
new file mode 100644
index 0000000..9d13efa
--- /dev/null
+++ b/app/Factories/UserFactory.php
@@ -0,0 +1,24 @@
+username = $username;
+ $user->password = $hashed_password;
+ $user->email = $email;
+ $user->recovery_key = $recovery_key;
+ $user->active = $active;
+ $user->ip = $ip;
+ $user->save();
+
+ return $user;
+ }
+
+}
diff --git a/app/Http/Controllers/AdminController.php b/app/Http/Controllers/AdminController.php
index b3f90c3..aea1528 100644
--- a/app/Http/Controllers/AdminController.php
+++ b/app/Http/Controllers/AdminController.php
@@ -59,7 +59,7 @@ class AdminController extends Controller {
$user->save();
$request->session()->flash('success', "Password changed successfully.");
- return redirect()->route('admin');
+ return redirect(route('admin'));
}
}
}
diff --git a/app/Http/Controllers/IndexController.php b/app/Http/Controllers/IndexController.php
index a964fd0..b4008a8 100644
--- a/app/Http/Controllers/IndexController.php
+++ b/app/Http/Controllers/IndexController.php
@@ -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]);
}
}
diff --git a/app/Http/Controllers/SetupController.php b/app/Http/Controllers/SetupController.php
index 5632d49..3507ede 100644
--- a/app/Http/Controllers/SetupController.php
+++ b/app/Http/Controllers/SetupController.php
@@ -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;
}
@@ -16,7 +19,7 @@ class SetupController extends Controller {
}
}
- private function setupAlreadyRan() {
+ private static function setupAlreadyRan() {
return view('error', [
'message' => 'Sorry, but you have already ran the setup script previously.'
]);
@@ -26,17 +29,17 @@ class SetupController extends Controller {
$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');
@@ -44,10 +47,10 @@ class SetupController extends Controller {
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');
@@ -87,6 +90,11 @@ class SetupController extends Controller {
]);
}
+ $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');
@@ -142,12 +150,34 @@ class SetupController extends Controller {
$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!');
}
}
diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php
index 45e0c35..52acb39 100644
--- a/app/Http/Controllers/UserController.php
+++ b/app/Http/Controllers/UserController.php
@@ -1,11 +1,10 @@
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');
@@ -89,14 +82,13 @@ class UserController extends Controller {
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;
}
diff --git a/app/Http/routes.php b/app/Http/routes.php
index 3a1a8c6..6bade41 100644
--- a/app/Http/routes.php
+++ b/app/Http/routes.php
@@ -23,6 +23,8 @@ $app->get('/admin', ['as' => 'admin', 'uses' => 'AdminController@displayAdminPag
$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']);
diff --git a/app/Models/User.php b/app/Models/User.php
index c6424cd..fa63ab9 100644
--- a/app/Models/User.php
+++ b/app/Models/User.php
@@ -1,8 +1,8 @@
More Information
-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
diff --git a/resources/views/emails/activation.blade.php b/resources/views/emails/activation.blade.php
index 09e793e..f497171 100644
--- a/resources/views/emails/activation.blade.php
+++ b/resources/views/emails/activation.blade.php
@@ -13,6 +13,7 @@ you will need to activate it by clicking the following link:
Thanks,
The {{env('APP_NAME')}} team.
+
--
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,
diff --git a/resources/views/setup_thanks.blade.php b/resources/views/setup_thanks.blade.php
new file mode 100644
index 0000000..24f5451
--- /dev/null
+++ b/resources/views/setup_thanks.blade.php
@@ -0,0 +1,40 @@
+@extends('layouts.minimal')
+
+@section('title')
+Setup Completed
+@endsection
+
+@section('css')
+
+
+@endsection
+
+@section('content')
+