diff --git a/README.md b/README.md index 1492423..baa94ac 100644 --- a/README.md +++ b/README.md @@ -1 +1,11 @@ # Compass + + +### Credits + +Compass icon by Ryan Spiering from the Noun Project. + + +### License + +The Lumen framework is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT) diff --git a/compass/app/Http/Controllers/Controller.php b/compass/app/Http/Controllers/Controller.php index 0ccb918..b2549ce 100644 --- a/compass/app/Http/Controllers/Controller.php +++ b/compass/app/Http/Controllers/Controller.php @@ -3,8 +3,66 @@ namespace App\Http\Controllers; use Laravel\Lumen\Routing\Controller as BaseController; +use Illuminate\Http\Request; +use DB; class Controller extends BaseController { - // + public function index(Request $request) { + if(session('user_id')) { + + $databases = DB::select('SELECT d.* + FROM `databases` d + JOIN database_users u ON d.id = u.database_id + WHERE u.user_id = ?', [session('user_id')]); + + return view('dashboard', [ + 'displayURL' => preg_replace('/(^https?:\/\/|\/$)/', '', session('me')), + 'databases' => $databases + ]); + } else { + return view('index'); + } + } + + public function createDatabase(Request $request) { + if(session('user_id')) { + + if($request->input('name') == '') { + return redirect('/'); + } + + // Only alphanumeric chars are allowed + if(preg_replace('/[^a-zA-Z0-9]/', '', $request->input('name')) != $request->input('name')) { + $request->session()->flash('error', 'Only alphanumeric characters are allowed.'); + $request->session()->flash('database-name', preg_replace('/[^a-zA-Z0-9]/','',$request->input('name'))); + return redirect('/'); + } + + // Check for conflicts + $db = DB::select('SELECT * FROM `databases` WHERE name = ?', [$request->input('name')]); + if(count($db) == 0) { + + // Create the database records + $id = DB::table('databases')->insertGetId([ + 'name' => $request->input('name'), + 'created_by' => session('user_id'), + 'created_at' => date('Y-m-d H:i:s') + ]); + DB::table('database_users')->insert([ + 'database_id' => $id, + 'user_id' => session('user_id'), + 'created_at' => date('Y-m-d H:i:s') + ]); + + } else { + $request->session()->flash('error', 'That database name is already in use.'); + $request->session()->flash('database-name', $request->input('name')); + return redirect('/'); + } + + } else { + return redirect('/'); + } + } } diff --git a/compass/app/Http/Controllers/IndieAuth.php b/compass/app/Http/Controllers/IndieAuth.php index 5ebbb13..0c6f7fc 100644 --- a/compass/app/Http/Controllers/IndieAuth.php +++ b/compass/app/Http/Controllers/IndieAuth.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers; use Laravel\Lumen\Routing\Controller as BaseController; use Illuminate\Http\Request; use GuzzleHttp; +use DB; class IndieAuth extends BaseController { @@ -82,6 +83,7 @@ class IndieAuth extends BaseController if($token && array_key_exists('me', $token)) { session()->flush(); session(['me' => $token['me']]); + $this->_userLoggedIn($token['me']); } return redirect('/'); @@ -135,7 +137,9 @@ class IndieAuth extends BaseController $data = json_decode($res->getBody()); if(property_exists($data, 'login')) { session()->flush(); - session(['me' => 'https://github.com/' . $data->login]); + $me = 'https://github.com/' . $data->login; + session(['me' => $me]); + $this->_userLoggedIn($me); return redirect('/'); } else { return view('auth/error', ['error' => 'Login failed']); @@ -158,6 +162,23 @@ class IndieAuth extends BaseController } } + private function _userLoggedIn($url) { + // Create the user record if it doesn't exist yet + $user = DB::select('SELECT * + FROM users + WHERE url = ?', [$url]); + if(count($user)) { + DB::update('UPDATE users SET last_login = ?', [date('Y-m-d H:i:s')]); + session(['user_id' => $user[0]->id]); + } else { + DB::insert('INSERT INTO users (url, created_at, last_login) VALUES(?,?,?)', [$url, date('Y-m-d H:i:s'), date('Y-m-d H:i:s')]); + $user = DB::select('SELECT * + FROM users + WHERE url = ?', [$url]); + session(['user_id' => $user[0]->id]); + } + } + public function logout(Request $request) { session()->flush(); return redirect('/'); diff --git a/compass/app/Http/routes.php b/compass/app/Http/routes.php index c247693..a7bda2b 100644 --- a/compass/app/Http/routes.php +++ b/compass/app/Http/routes.php @@ -11,11 +11,11 @@ | */ -$app->get('/', function () use ($app) { - return view('index'); -}); +$app->get('/', 'Controller@index'); $app->post('/auth/start', 'IndieAuth@start'); $app->get('/auth/callback', 'IndieAuth@callback'); $app->get('/auth/github', 'IndieAuth@github'); $app->get('/auth/logout', 'IndieAuth@logout'); + +$app->post('/database/create', 'Controller@createDatabase'); diff --git a/compass/bootstrap/app.php b/compass/bootstrap/app.php index 0ceeae8..5d9ac50 100644 --- a/compass/bootstrap/app.php +++ b/compass/bootstrap/app.php @@ -19,7 +19,7 @@ $app = new Laravel\Lumen\Application( realpath(__DIR__.'/../') ); -// $app->withFacades(); +$app->withFacades(); // $app->withEloquent(); diff --git a/compass/public/assets/compass.svg b/compass/public/assets/compass.svg new file mode 100644 index 0000000..451f606 --- /dev/null +++ b/compass/public/assets/compass.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/compass/public/assets/styles.css b/compass/public/assets/styles.css index e69de29..0c984bc 100644 --- a/compass/public/assets/styles.css +++ b/compass/public/assets/styles.css @@ -0,0 +1,67 @@ + +.splash { + width: 500px; + margin: 60px auto 0 auto; + border-radius: 6px; +} + +.splash .logo { + width: 200px; + margin: 0 auto; +} + +.splash .login { + width: 300px; + margin: 0 auto; +} +.splash .login input { + width: 200px; +} + + +.corner-logo { + position: absolute; + top: 6px; + left: 20px; +} +.logged-in { + position: absolute; + top: 6px; + right: 20px; + font-size: 80%; +} +.logged-in span { + margin-left: 16px; +} + + +.dashboard { + max-width: 500px; + margin: 60px auto 0 auto; +} + +.databases { + list-style-type: none; + margin: 0; + padding: 0; +} +.databases li { + margin: 0; + padding: 0; + margin-bottom: 20px; +} +.databases .db { + font-size: 22px; +} +.databases a { + text-decoration: none; +} + +.databases .error { + padding: 6px 10px; + color: #a94442; + background-color: #f2dede; + border: 1px #ebccd1 solid; + border-radius: 4px; + margin-bottom: 4px; +} diff --git a/compass/resources/views/dashboard.blade.php b/compass/resources/views/dashboard.blade.php new file mode 100644 index 0000000..e9e2fb7 --- /dev/null +++ b/compass/resources/views/dashboard.blade.php @@ -0,0 +1,35 @@ +@extends('layouts.master') + +@section('content') + + + +
+ {{ $displayURL }} + sign out +
+ +
+ +

Databases

+ + +
+ +@endsection \ No newline at end of file diff --git a/compass/resources/views/index.blade.php b/compass/resources/views/index.blade.php index 408e934..0454628 100644 --- a/compass/resources/views/index.blade.php +++ b/compass/resources/views/index.blade.php @@ -2,15 +2,17 @@ @section('content') -@if(session('me')) -

{{ session('me') }}

-

sign out

-@else -Log in: +
-
- -
-@endif + + +
+
+ + +
+
+ +
@endsection \ No newline at end of file