Skip to content
Permalink
Browse files

basic event page rendering and user login via Vouch

  • Loading branch information
aaronpk committed Oct 7, 2019
1 parent d3c5a68 commit 707173e1b6763ee5b734944c8b9a2a4a3df69932
@@ -42,3 +42,7 @@ PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

SQL_LOGGER_SLOW_QUERIES_ENABLED=false

VOUCH_HOSTNAME=sso.indieweb.org.dev
@@ -10,3 +10,4 @@ Homestead.json
Homestead.yaml
npm-debug.log
yarn-error.log
.DS_Store
@@ -0,0 +1,91 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use DateTime, DateTimeZone;

class Event extends Model
{

public function responses() {
return $this->hasMany('\App\Response');
}

public function tags() {
return $this->belongsToMany('\App\Tag');
}

public function has_rsvps() {
return $this->responses->where('type', 'rsvp')->count();
}

public function has_photos() {
return $this->responses->where('type', 'photo')->count();
}

public function has_posts() {
return $this->responses->where('type', 'post')->count();
}

public function has_comments() {
return $this->responses->where('type', 'comment')->count();
}

public function rsvps() {
return $this->hasMany('\App\Response')->where('type', 'rsvp');
}

public function photos() {
return $this->hasMany('\App\Response')->where('type', 'photo');
}

public function posts() {
return $this->hasMany('\App\Response')->where('type', 'post');
}

public function comments() {
return $this->hasMany('\App\Response')->where('type', 'comment');
}

public function permalink() {
$date = new DateTime($this->start_date);
return '/' . $date->format('Y') . '/' . $date->format('m') . '/' . ($this->slug ? $this->slug.'-' : '') . $this->key;
}

public function date_summary() {
$start_date = new DateTime($this->start_date);

if($this->end_date) {
$end_date = new DateTime($this->end_date);

return $start_date->format('F j') . ' - ' . $end_date->format('F j, Y');

} else {
return $start_date->format('F j, Y');
}
}

public function location_summary() {
$str = [];
if($this->location_address) $str[] = $this->location_address;
if($this->location_locality) $str[] = $this->location_locality;
if($this->location_region) $str[] = $this->location_region;
if($this->location_country) $str[] = $this->location_country;
return implode(', ', $str);
}

public function html() {
if(!$this->description)
return '';

$markdown = $this->description;

$html = \Michelf\MarkdownExtra::defaultTransform($markdown);

$html = Utils\HTML::sanitizeHTML($html);

return $html;
}

}
@@ -6,9 +6,61 @@
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use App\Event;

class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

public function index() {
$events = Event::all();

return view('index', [
'events' => $events,
]);
}

public function event($year, $month, $key_or_slug, $key2=false) {

if($key2) {
$key = $key2;
$slug = $key_or_slug;
} else {
$key = $key_or_slug;
$slug = false;
}

$event = Event::where('key', $key)->first();

if(!$event) {
abort(404);
}

// Redirect to the canonical URL
if($event->slug && $event->slug != $slug) {
return redirect($event->permalink(), 301);
}

return view('event', [
'event' => $event,
'year' => $year,
'month' => $month,
'key' => $key,
'slug' => $slug,
]);
}

public function tag($tag) {
$events = Event::whereHas('tags', function($query) use ($tag){
$query->where('tag', $tag);
})->orderBy('events.start_date', 'desc')->get();

if(count($events) == 0) {
abort(404);
}

return view('index', [
'events' => $events,
]);
}
}
@@ -0,0 +1,19 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use App\Event;

class EventController extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

public function new_event() {
return view('new-event');
}

}
@@ -14,8 +14,10 @@ class Authenticate extends Middleware
*/
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
return route('login');
if(!$request->expectsJson()) {
return 'https://'.env('VOUCH_HOSTNAME').'/login?url='.urlencode($request->url());
} else {
return abort(401);
}
}
}
@@ -4,6 +4,8 @@

use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Gate;
use App\Services\Auth\VouchGuard;
use Auth;

class AuthServiceProvider extends ServiceProvider
{
@@ -25,6 +27,8 @@ public function boot()
{
$this->registerPolicies();

//
Auth::extend('vouch', function($app, $name, array $config) {
return new VouchGuard(Auth::createUserProvider($config['provider']), $app->make('request'));
});
}
}
@@ -23,7 +23,10 @@ class RouteServiceProvider extends ServiceProvider
*/
public function boot()
{
//
Route::pattern('year', '[0-9]{4}');
Route::pattern('month', '[0-9]{2}');
Route::pattern('key', '[0-9a-zA-Z]+');
Route::pattern('slug', '[0-9a-zA-Z\-]+');

parent::boot();
}
@@ -0,0 +1,19 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Response extends Model
{
public function event() {
return $this->belongsTo('\App\Event');
}

public function photos() {
if(!$this->photos)
return [];

return json_decode($this->photos, true);
}
}
@@ -0,0 +1,56 @@
<?php
namespace App\Services\Auth;

use Illuminate\Contracts\Auth\Guard;
use Illuminate\Http\Request;
use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Contracts\Auth\Authenticatable;
use App\User;

class VouchGuard implements Guard {

protected $request;
protected $provider;
protected $user;

public function __construct(UserProvider $provider, Request $request) {
$this->request = $request;
$this->provider = $provider;
$this->user = null;
}

public function check() {
$url = $this->request->server->get('HTTP_REMOTE_USER');
return $url == true;
}

public function guest() {
$url = $this->request->server->get('HTTP_REMOTE_USER');
return $url != true;
}

public function user() {
$url = $this->request->server->get('HTTP_REMOTE_USER');
$user = User::where('url', $url)->first();
if(!$user) {
$user = new User();
$user->url = $url;
$user->save();
}
return $user;
}

public function id() {
$user = $this->user();
return $user->id;
}

public function validate(array $credentials = []) {
return false;
}

public function setUser(Authenticatable $user) {
return false;
}

}
@@ -0,0 +1,16 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Tag extends Model
{
public function events() {
$this->belongsToMany('\App\Event');
}

public function url() {
return '/tag/' . $this->tag;
}
}
@@ -0,0 +1,37 @@
<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
use Notifiable;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'url'
];

/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
];

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
];
}

0 comments on commit 707173e

Please sign in to comment.
You can’t perform that action at this time.