Browse Source

updates

* adds database for users and list of DBs
* create DB interface
* adds some styles so it looks less dumb
pull/5/head
Aaron Parecki 9 years ago
parent
commit
eafa2f8051
9 changed files with 209 additions and 15 deletions
  1. +10
    -0
      README.md
  2. +59
    -1
      compass/app/Http/Controllers/Controller.php
  3. +22
    -1
      compass/app/Http/Controllers/IndieAuth.php
  4. +3
    -3
      compass/app/Http/routes.php
  5. +1
    -1
      compass/bootstrap/app.php
  6. +1
    -0
      compass/public/assets/compass.svg
  7. +67
    -0
      compass/public/assets/styles.css
  8. +35
    -0
      compass/resources/views/dashboard.blade.php
  9. +11
    -9
      compass/resources/views/index.blade.php

+ 10
- 0
README.md View File

@ -1 +1,11 @@
# Compass # 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)

+ 59
- 1
compass/app/Http/Controllers/Controller.php View File

@ -3,8 +3,66 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Laravel\Lumen\Routing\Controller as BaseController; use Laravel\Lumen\Routing\Controller as BaseController;
use Illuminate\Http\Request;
use DB;
class Controller extends BaseController 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('/');
}
}
} }

+ 22
- 1
compass/app/Http/Controllers/IndieAuth.php View File

@ -5,6 +5,7 @@ namespace App\Http\Controllers;
use Laravel\Lumen\Routing\Controller as BaseController; use Laravel\Lumen\Routing\Controller as BaseController;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use GuzzleHttp; use GuzzleHttp;
use DB;
class IndieAuth extends BaseController class IndieAuth extends BaseController
{ {
@ -82,6 +83,7 @@ class IndieAuth extends BaseController
if($token && array_key_exists('me', $token)) { if($token && array_key_exists('me', $token)) {
session()->flush(); session()->flush();
session(['me' => $token['me']]); session(['me' => $token['me']]);
$this->_userLoggedIn($token['me']);
} }
return redirect('/'); return redirect('/');
@ -135,7 +137,9 @@ class IndieAuth extends BaseController
$data = json_decode($res->getBody()); $data = json_decode($res->getBody());
if(property_exists($data, 'login')) { if(property_exists($data, 'login')) {
session()->flush(); session()->flush();
session(['me' => 'https://github.com/' . $data->login]);
$me = 'https://github.com/' . $data->login;
session(['me' => $me]);
$this->_userLoggedIn($me);
return redirect('/'); return redirect('/');
} else { } else {
return view('auth/error', ['error' => 'Login failed']); 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) { public function logout(Request $request) {
session()->flush(); session()->flush();
return redirect('/'); return redirect('/');

+ 3
- 3
compass/app/Http/routes.php View File

@ -11,11 +11,11 @@
| |
*/ */
$app->get('/', function () use ($app) {
return view('index');
});
$app->get('/', 'Controller@index');
$app->post('/auth/start', 'IndieAuth@start'); $app->post('/auth/start', 'IndieAuth@start');
$app->get('/auth/callback', 'IndieAuth@callback'); $app->get('/auth/callback', 'IndieAuth@callback');
$app->get('/auth/github', 'IndieAuth@github'); $app->get('/auth/github', 'IndieAuth@github');
$app->get('/auth/logout', 'IndieAuth@logout'); $app->get('/auth/logout', 'IndieAuth@logout');
$app->post('/database/create', 'Controller@createDatabase');

+ 1
- 1
compass/bootstrap/app.php View File

@ -19,7 +19,7 @@ $app = new Laravel\Lumen\Application(
realpath(__DIR__.'/../') realpath(__DIR__.'/../')
); );
// $app->withFacades();
$app->withFacades();
// $app->withEloquent(); // $app->withEloquent();

+ 1
- 0
compass/public/assets/compass.svg View File

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" x="0px" y="0px" viewBox="0 0 100 100" enable-background="new 0 0 100 100" xml:space="preserve"><path d="M50,5C25.2,5,5,25.2,5,50s20.2,45,45,45s45-20.2,45-45S74.8,5,50,5L50,5z M50,16.2c18.6,0,33.8,15.1,33.8,33.8 S68.6,83.8,50,83.8S16.2,68.6,16.2,50S31.4,16.2,50,16.2z M42.3,48.2l-0.2,31.5l15.6-27.4l0.2-31.4L42.3,48.2z M55.7,51.8L44.2,72.1 l0.1-23.3l0-0.1L55.7,51.8L55.7,51.8z"/></svg>

+ 67
- 0
compass/public/assets/styles.css View File

@ -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;
}

+ 35
- 0
compass/resources/views/dashboard.blade.php View File

@ -0,0 +1,35 @@
@extends('layouts.master')
@section('content')
<div class="corner-logo"><img src="/assets/compass.svg" height="40"/></div>
<div class="logged-in">
<span>{{ $displayURL }}</span>
<span><a href="/auth/logout">sign out</a></span>
</div>
<div class="dashboard">
<h2>Databases</h2>
<ul class="databases">
@foreach($databases as $database)
<li class="db"><a href="/map/{{ $database->name }}">{{ $database->name }}</a></li>
@endforeach
<li>
<a href="javascript:$('.databases .create').removeClass('hidden');$('.create-link').addClass('hidden');" class="pure-button create-link {{ session('error') ? 'hidden' : '' }}">create database</a>
@if(session('error'))
<div class="error">{{ session('error') }}</div>
@endif
<span class="create {{ session('error') ? '' : 'hidden' }}">
<form action="/database/create" method="post" class="pure-form">
<input type="text" name="name" value="{{ session('database-name') }}">
<button type="submit" class="pure-button pure-button-primary">Create</button>
</form>
</span>
</li>
</ul>
</div>
@endsection

+ 11
- 9
compass/resources/views/index.blade.php View File

@ -2,15 +2,17 @@
@section('content') @section('content')
@if(session('me'))
<p>{{ session('me') }}</p>
<p><a href="/auth/logout">sign out</a></p>
@else
Log in:
<div class="splash">
<form action="/auth/start" method="post">
<input type="url" name="me">
</form>
@endif
<div class="logo"><img src="/assets/compass.svg" width="200"></div>
<form action="/auth/start" method="post" class="pure-form login">
<fieldset>
<input type="url" name="me">
<button type="submit" class="pure-button pure-button-primary">Sign in</button>
</fieldset>
</form>
</div>
@endsection @endsection

Loading…
Cancel
Save