Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Ford authored and Andy Ford committed Apr 22, 2020
2 parents 6c5511b + e927cb3 commit 060b050
Show file tree
Hide file tree
Showing 26 changed files with 533 additions and 38 deletions.
2 changes: 1 addition & 1 deletion app/Console/Commands/CleanSquawkAllocations.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function handle()
DB::table('squawk_allocation')->where(
'allocated_at',
'<',
Carbon::now()->subMinutes(env('APP_SQUAWK_ALLOCATION_MIN'))->format('Y-m-d H:i:s')
Carbon::now()->subMinutes(config('squawk.allocation_min'))->format('Y-m-d H:i:s')
)->delete();

Log::Info('Squawk allocations cleaned successfully');
Expand Down
4 changes: 2 additions & 2 deletions app/Helpers/User/UserConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function apiKey() : string
*/
public function apiUrl() : string
{
return env('APP_URL');
return config('app.url');
}

/**
Expand All @@ -56,7 +56,7 @@ public function apiUrl() : string
public function jsonSerialize()
{
return [
'api-url' => env('APP_URL'),
'api-url' => config('app.url'),
'api-key' => $this->accessToken,
];
}
Expand Down
25 changes: 24 additions & 1 deletion app/Http/Controllers/DependencyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
namespace App\Http\Controllers;

use App\Models\Dependency\Dependency;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Auth;

class DependencyController extends BaseController
{
Expand All @@ -13,11 +15,32 @@ class DependencyController extends BaseController
*/
public function getAllDependencies() : JsonResponse
{
$dependencies = Dependency::all()->map(function (Dependency $dependency) {
$dependencies = Dependency::with('user')->get()->map(function (Dependency $dependency) {
$updatedAt = $dependency->updated_at;
if ($dependency->per_user) {
if (!$dependency->user->first()) {
$dependency->user()->attach(
$dependency->id,
[
'user_id' => Auth::user()->id,
'updated_at' => Carbon::now(),
]
);
$dependency->load('user');
}

$updatedAt = $dependency->user->first()->pivot->updated_at;
} elseif (!$updatedAt) {
$dependency->updated_at = Carbon::now();
$dependency->save();
$updatedAt = $dependency->updated_at;
}

return [
'key' => $dependency->key,
'uri' => sprintf('%s/%s', config('app.url'), $dependency->uri),
'local_file' => $dependency->local_file,
'updated_at' => $updatedAt->timestamp,
];
});
return response()->json($dependencies);
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Http\Middleware\Authenticate;
use App\Http\Middleware\GithubAuth;
use App\Http\Middleware\LogAdminAction;
use App\Http\Middleware\UpdateDependency;
use App\Http\Middleware\UserIsBanned;
use App\Http\Middleware\UserIsDisabled;
use App\Http\Middleware\UserLastLogin;
Expand All @@ -23,6 +24,7 @@ class Kernel extends HttpKernel
'auth' => Authenticate::class,
'auth.github' => GithubAuth::class,
'admin.log' => LogAdminAction::class,
'dependency.update' => UpdateDependency::class,
'user.banned' => UserIsBanned::class,
'user.disabled' => UserIsDisabled::class,
'user.lastlogin' => UserLastLogin::class,
Expand Down
27 changes: 27 additions & 0 deletions app/Http/Middleware/UpdateDependency.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
namespace App\Http\Middleware;

use App\Services\DependencyService;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class UpdateDependency
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param array $dependencies
* @return mixed
*/
public function handle(Request $request, Closure $next, ...$dependencies)
{
foreach ($dependencies as $dependency) {
DependencyService::touchDependencyByKey($dependency, Auth::user());
}

return $next($request);
}
}
12 changes: 12 additions & 0 deletions app/Models/Dependency/Dependency.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,30 @@

namespace App\Models\Dependency;

use App\Models\User\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class Dependency extends Model
{
protected $fillable = [
'key',
'local_file',
'per_user',
'created_at',
];

protected $hidden = [
'created_at',
'updated_at',
];

protected $casts = [
'per_user' => 'boolean',
];

public function user(): BelongsToMany
{
return $this->belongsToMany(User::class)->withTimestamps();
}
}
11 changes: 9 additions & 2 deletions app/Models/User/User.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php
namespace App\Models\User;

use App\Models\Dependency\Dependency;
use Carbon\Carbon;
use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Laravel\Passport\HasApiTokens;
Expand All @@ -19,7 +21,7 @@
class User extends Model implements AuthenticatableContract, AuthorizableContract
{
use HasApiTokens, Authenticatable, Authorizable;

// The table name
protected $table = 'user';

Expand Down Expand Up @@ -98,7 +100,7 @@ public function ban() : User
$this->save();
return $this;
}

/**
* Marks the user as disabled
*
Expand Down Expand Up @@ -136,4 +138,9 @@ public function jsonSerialize() : array
'tokens' => $this->tokens,
];
}

public function dependencies(): BelongsToMany
{
return $this->belongsToMany(Dependency::class)->withTimestamps();
}
}
2 changes: 1 addition & 1 deletion app/Providers/RegionalPressureServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function register()
{
$this->app->bind(RegionalPressureService::class, function (Application $app) {
// Create dependencies
$metarUri = env('APP_REGIONAL_PRESSURES_URL', '');
$metarUri = config('metar.regional_url');
$http = new Client();
$metarParser = $app->make(MetarService::class);
return new RegionalPressureService($http, $metarUri, $metarParser);
Expand Down
47 changes: 47 additions & 0 deletions app/Services/DependencyService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace App\Services;

use App\Models\Dependency\Dependency;
use App\Models\User\User;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;
use LogicException;

class DependencyService
{
public static function touchDependencyByKey(string $key, ?User $user): void
{
$dependency = Dependency::where('key', $key)->first();

if (!$dependency) {
Log::error(sprintf('Dependency %s not found to update', $key));
return;
}

if ($dependency->per_user && $user === null) {
Log::error(sprintf('Dependency %s is per user but user was not specifieid', $key));
return;
}

if ($dependency->per_user) {
self::touchUserDependency($dependency, $user);
} else {
self::touchGlobalDependency($dependency);
}
}

public static function touchGlobalDependency(Dependency $dependency): void
{
$dependency->touch();
}

public static function touchUserDependency(Dependency $dependency, User $user): void
{
if (!$dependency->per_user) {
throw new LogicException(sprintf('Dependency %s is not a per-user dependency', $dependency->key));
}

$user->dependencies()->updateExistingPivot($dependency->id, ['updated_at' => Carbon::now()]);
}
}
2 changes: 1 addition & 1 deletion app/Services/MetarService.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function getQnhFromVatsimMetar(string $icao) : ?int
}

$metar = $this->httpClient->get(
env('VATSIM_METAR_URL'),
config('metar.vatsim_url'),
[
RequestOptions::ALLOW_REDIRECTS => true,
RequestOptions::HTTP_ERRORS => false,
Expand Down
7 changes: 7 additions & 0 deletions config/metar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

// Config for downloading METARs in the context of MSL and RPS
return [
'vatsim_url' => env('VATSIM_METAR_URL', 'metar.vatsim.net'),
'regional_url' => env('APP_REGIONAL_PRESSURES_URL', 'https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requesttype=retrieve&format=xml&hoursBeforeNow=3&mostRecentForEachStation=constraint&stationString=~gb&fields=raw_text,station_id'),
];
5 changes: 5 additions & 0 deletions config/squawk.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return [
'allocation_min' => env('APP_SQUAWK_ALLOCATION_MIN', 45),
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddUserDependencyColumn extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('dependencies', function (Blueprint $table) {
$table->boolean('per_user')
->default(false)
->after('local_file')
->comment('If the dependency is per-user');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('dependencies', function (Blueprint $table) {
$table->dropColumn('per_user');
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use App\Models\Dependency\Dependency;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

class AddPerUserDependencies extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Dependency::where('key', 'DEPENDENCY_HOLD_PROFILE')
->update(['per_user' => 1]);
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Dependency::where('key', 'DEPENDENCY_HOLD_PROFILE')
->update(['per_user' => 0]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddDependencyUserTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('dependency_user', function (Blueprint $table) {
$table->unsignedMediumInteger('dependency_id')->comment('The dependency');
$table->unsignedInteger('user_id')->comment('The user the dependency belongs to');
$table->timestamps();

$table->primary(['dependency_id', 'user_id']);
$table->foreign('dependency_id')->references('id')->on('dependencies')->onDelete('cascade');
$table->foreign('user_id')->references('id')->on('user')->onDelete('cascade');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('dependency_user');
}
}
1 change: 1 addition & 0 deletions database/seeds/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class DatabaseSeeder extends Seeder
'sid_prenotes',
],
DependencyTableSeeder::class => [
'dependency_user',
'dependencies',
],
AircraftTableSeeder::class => [
Expand Down
Loading

0 comments on commit 060b050

Please sign in to comment.