Commit 73f557cf authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

Merge branch '40-log-successfuly-door-entries' into 'master'

Resolve "Log successfuly door entries"

Closes #40

See merge request kretschmar/doorcode!35
parents c56ca4a8 0c5a93fd
Pipeline #5719 passed with stages
in 3 minutes and 45 seconds
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Attempt extends Model
{
protected $fillable = ['door_id'];
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function door(): BelongsTo
{
return $this->belongsTo(Door::class);
}
}
......@@ -2,6 +2,7 @@
namespace App;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
......@@ -17,6 +18,22 @@ class Door extends Authenticatable
return $this->belongsToMany(Group::class);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function entries(): HasMany
{
return $this->hasMany(Entry::class);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function attempts(): HasMany
{
return $this->hasMany(Attempt::class);
}
public static function boot()
{
parent::boot();
......@@ -24,6 +41,18 @@ class Door extends Authenticatable
static::deleting(static function (Door $door) {
// Detach all groups
$door->groups()->detach();
// Delete all entries
/** @var \App\Entry $entry */
foreach ($door->entries() as $entry) {
$entry->delete();
}
// Delete all attempts
/** @var \App\Attempt $attempt */
foreach ($door->attempts() as $attempt) {
$attempt->delete();
}
});
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Entry extends Model
{
protected $fillable = ['door_id', 'user_id', 'success'];
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function door(): BelongsTo
{
return $this->belongsTo(Door::class);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
}
<?php
namespace App\Http\Controllers;
use Illuminate\Http\JsonResponse;
use Source\Authorization\Permissions;
use Source\UseCases\Attempts\GetAttempts\GetAttemptsUseCase;
use Source\UseCases\Attempts\GetAttemptsForDoor\GetAttemptsForDoorUseCase;
use Source\UseCases\Attempts\GetAttempts\APIPresenter as GetAttemptsAPIPresenter;
use Source\UseCases\Attempts\GetAttemptCountForDoor\GetAttemptCountForDoorUseCase;
use Source\UseCases\Attempts\GetAttemptCountForDoor\APIPresenter as AttemptCountAPIPresenter;
use Source\UseCases\Attempts\GetAttemptsForDoor\APIPresenter as GetAttemptsForDoorAPIPresenter;
class AttemptsController extends ApiController
{
/**
* @param \Source\UseCases\Attempts\GetAttemptCountForDoor\GetAttemptCountForDoorUseCase $count
* @param string $doorId
* @return \Illuminate\Http\JsonResponse
* @throws \Illuminate\Validation\ValidationException
* @throws \Source\Exceptions\AuthorizationException
* @throws \Source\Exceptions\EntityNotFoundException
* @throws \Exception
*/
public function count(GetAttemptCountForDoorUseCase $count, string $doorId): JsonResponse
{
$this->authorizer->protect(Permissions::ADMIN);
$this->validate($this->request, [
'start' => 'required|date|before:end',
'end' => 'required|date|after:start',
]);
$presenter = new AttemptCountAPIPresenter();
$count->count($doorId, $this->request->input('start'), $this->request->input('end'), $presenter);
return $this->respondWithData($presenter->getViewModel());
}
/**
* @param \Source\UseCases\Attempts\GetAttempts\GetAttemptsUseCase $attempts
* @return \Illuminate\Http\JsonResponse
* @throws \Illuminate\Validation\ValidationException
* @throws \Source\Exceptions\AuthorizationException
* @throws \Source\Exceptions\EntityNotFoundException
* @throws \Exception
*/
public function index(GetAttemptsUseCase $attempts): JsonResponse
{
$this->authorizer->protect(Permissions::ADMIN);
$this->validate($this->request, [
'start' => 'required|date|before:end',
'end' => 'required|date|after:start',
]);
$presenter = new GetAttemptsAPIPresenter();
$attempts->getBetweenDates($this->request->input('start'), $this->request->input('end'), $presenter);
return $this->respondWithData($presenter->getViewModel());
}
/**
* @param \Source\UseCases\Attempts\GetAttemptsForDoor\GetAttemptsForDoorUseCase $doorAttempts
* @param string $doorId
* @return \Illuminate\Http\JsonResponse
* @throws \Illuminate\Validation\ValidationException
* @throws \Source\Exceptions\AuthorizationException
* @throws \Source\Exceptions\EntityNotFoundException
* @throws \Exception
*/
public function doorAttempts(GetAttemptsForDoorUseCase $doorAttempts, string $doorId): JsonResponse
{
$this->authorizer->protect(Permissions::ADMIN);
$this->validate($this->request, [
'start' => 'required|date|before:end',
'end' => 'required|date|after:start',
]);
$presenter = new GetAttemptsForDoorAPIPresenter();
$doorAttempts->getAttemptsForDoorBetween(
$doorId,
$this->request->input('start'),
$this->request->input('end'),
$presenter
);
return $this->respondWithData($presenter->getViewModel());
}
}
<?php
namespace App\Http\Controllers;
use Illuminate\Http\JsonResponse;
use Source\Authorization\Permissions;
use Source\UseCases\Entries\APIPresenter;
use Source\UseCases\Entries\GetEntriesForDoor\GetEntriesForDoorUseCase;
use Source\UseCases\Entries\GetEntriesForUser\GetEntriesForUserUseCase;
use Source\UseCases\Entries\GetEntriesForDoorAndUser\GetEntriesForDoorAndUserUseCase;
class EntriesController extends ApiController
{
/**
* @param \Source\UseCases\Entries\GetEntriesForDoor\GetEntriesForDoorUseCase $doorEntries
* @param string $doorId
* @return \Illuminate\Http\JsonResponse
* @throws \Illuminate\Validation\ValidationException
* @throws \Source\Exceptions\AuthorizationException
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function doorEntries(GetEntriesForDoorUseCase $doorEntries, string $doorId): JsonResponse
{
$this->authorizer->protect(Permissions::ADMIN);
$this->validate($this->request, [
'start' => 'required|date|before:end',
'end' => 'required|date|after:start',
]);
$presenter = new APIPresenter();
$doorEntries->getEntriesForDoorBetween(
$doorId,
$this->request->input('start'),
$this->request->input('end'),
$presenter
);
return $this->respondWithData($presenter->getViewModel());
}
/**
* @param \Source\UseCases\Entries\GetEntriesForUser\GetEntriesForUserUseCase $userEntries
* @param string $userId
* @return \Illuminate\Http\JsonResponse
* @throws \Illuminate\Validation\ValidationException
* @throws \Source\Exceptions\AuthorizationException
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function userEntries(GetEntriesForUserUseCase $userEntries, string $userId): JsonResponse
{
$this->authorizer->protect(Permissions::ADMIN);
$this->validate($this->request, [
'start' => 'required|date|before:end',
'end' => 'required|date|after:start',
]);
$presenter = new APIPresenter();
$userEntries->getEntriesForUserBetween(
$userId,
$this->request->input('start'),
$this->request->input('end'),
$presenter
);
return $this->respondWithData($presenter->getViewModel());
}
/**
* @param \Source\UseCases\Entries\GetEntriesForDoorAndUser\GetEntriesForDoorAndUserUseCase $doorUserEntries
* @param string $doorId
* @param string $userId
* @return \Illuminate\Http\JsonResponse
* @throws \Illuminate\Validation\ValidationException
* @throws \Source\Exceptions\AuthorizationException
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function doorUserEntries(
GetEntriesForDoorAndUserUseCase $doorUserEntries,
string $doorId,
string $userId
): JsonResponse {
$this->authorizer->protect(Permissions::ADMIN);
$this->validate($this->request, [
'start' => 'required|date|before:end',
'end' => 'required|date|after:start',
]);
$presenter = new APIPresenter();
$doorUserEntries->getEntriesForDoorAndUserBetween(
$doorId,
$userId,
$this->request->input('start'),
$this->request->input('end'),
$presenter
);
return $this->respondWithData($presenter->getViewModel());
}
}
......@@ -9,7 +9,9 @@ use Source\Gateways\Doors\DoorsRepositoryServiceProvider;
use Source\Gateways\Users\UsersRepositoryServiceProvider;
use Source\Gateways\Groups\GroupsRepositoryServiceProvider;
use Source\Gateways\Tokens\TokensRepositoryServiceProvider;
use Source\Gateways\Entries\EntriesRepositoryServiceProvider;
use Source\UseCases\Door\Access\AccessUseCaseServiceProvider;
use Source\Gateways\Attempts\AttemptsRepositoryServiceProvider;
use Source\Gateways\DoorUser\DoorUserRepositoryServiceProvider;
use Source\UseCases\Doors\GetDoor\GetDoorUseCaseServiceProvider;
use Source\UseCases\Users\GetUser\GetUserUseCaseServiceProvider;
......@@ -32,6 +34,7 @@ use Source\UseCases\Tokens\CreateToken\CreateTokenUseCaseServiceProvider;
use Source\UseCases\Tokens\ExpireToken\ExpireTokenUseCaseServiceProvider;
use Source\UseCases\Tokens\UpdateToken\UpdateTokenUseCaseServiceProvider;
use Source\UseCases\Token\Authenticate\AuthenticateUseCaseServiceProvider;
use Source\UseCases\Attempts\GetAttempts\GetAttemptsUseCaseServiceProvider;
use Source\UseCases\Groups\GetAllGroups\GetAllGroupsUseCaseServiceProvider;
use Source\UseCases\Tokens\GetAllTokens\GetAllTokensUseCaseServiceProvider;
use Source\UseCases\DoorGroup\GetDoorGroups\GetDoorGroupsUseCaseServiceProvider;
......@@ -42,8 +45,13 @@ use Source\UseCases\TokenUser\GetUserTokens\GetUserTokensUseCaseServiceProvider;
use Source\UseCases\DoorGroup\AddDoorToGroup\AddDoorToGroupUseCaseServiceProvider;
use Source\UseCases\GroupUser\AddUserToGroup\AddUserToGroupUseCaseServiceProvider;
use Source\UseCases\Doors\GenerateDoorToken\GenerateDoorTokenUseCaseServiceProvider;
use Source\UseCases\Entries\GetEntriesForDoor\GetEntriesForDoorUseCaseServiceProvider;
use Source\UseCases\Entries\GetEntriesForUser\GetEntriesForUserUseCaseServiceProvider;
use Source\UseCases\Attempts\GetAttemptsForDoor\GetAttemptsForDoorUseCaseServiceProvider;
use Source\UseCases\DoorGroup\RemoveDoorFromGroup\RemoveDoorFromGroupUseCaseServiceProvider;
use Source\UseCases\GroupUser\RemoveUserFromGroup\RemoveUserFromGroupUseCaseServiceProvider;
use Source\UseCases\Attempts\GetAttemptCountForDoor\GetAttemptCountForDoorUseCaseServiceProvider;
use Source\UseCases\Entries\GetEntriesForDoorAndUser\GetEntriesForDoorAndUserUseCaseServiceProvider;
use Source\UseCases\Door\Authenticate\AuthenticateUseCaseServiceProvider as DoorAuthenticateUseCaseServiceProvider;
use Source\UseCases\Users\Authenticate\AuthenticateUseCaseServiceProvider as UserAuthenticateUseCaseServiceProvider;
......@@ -59,6 +67,8 @@ class AppServiceProvider extends ServiceProvider
DoorsRepositoryServiceProvider::class,
TokensRepositoryServiceProvider::class,
GroupsRepositoryServiceProvider::class,
EntriesRepositoryServiceProvider::class,
AttemptsRepositoryServiceProvider::class,
DoorUserRepositoryServiceProvider::class,
DoorGroupRepositoryServiceProvider::class,
GroupUserRepositoryServiceProvider::class,
......@@ -117,6 +127,17 @@ class AppServiceProvider extends ServiceProvider
// TokenUser
GetUserTokensUseCaseServiceProvider::class,
// Attempts
GetAttemptsUseCaseServiceProvider::class,
GetAttemptsForDoorUseCaseServiceProvider::class,
GetAttemptCountForDoorUseCaseServiceProvider::class,
// Entries
GetEntriesForDoorUseCaseServiceProvider::class,
GetEntriesForUserUseCaseServiceProvider::class,
GetEntriesForDoorAndUserUseCaseServiceProvider::class,
];
/**
......
......@@ -32,6 +32,14 @@ class User extends Authenticatable
return $this->hasMany(Token::class);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function entries(): HasMany
{
return $this->hasMany(Entry::class);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
......@@ -53,6 +61,11 @@ class User extends Authenticatable
foreach ($user->tokens() as $token) {
$token->delete();
}
/** @var \App\Entry $entry */
foreach ($user->entries() as $entry) {
$entry->delete();
}
});
}
}
......@@ -15,7 +15,7 @@ class CreateUsersTable extends Migration
{
// This table holds the user objects in the system
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->id();
$table->string('first_name');
$table->string('last_name');
$table->string('display_name');
......
......@@ -13,8 +13,8 @@ class CreateFailedJobsTable extends Migration
*/
public function up()
{
Schema::create('failed_jobs', function (Blueprint $table) {
$table->bigIncrements('id');
Schema::create('failed_jobs', static function (Blueprint $table) {
$table->id();
$table->text('connection');
$table->text('queue');
$table->longText('payload');
......
......@@ -14,8 +14,8 @@ class CreateGroupsTable extends Migration
public function up()
{
// This table stores the groups objects stored in the system
Schema::create('groups', function (Blueprint $table) {
$table->bigIncrements('id');
Schema::create('groups', static function (Blueprint $table) {
$table->id();
$table->string('title')->unique();
$table->string('description');
$table->timestamps();
......
......@@ -14,8 +14,8 @@ class CreateGroupUserTable extends Migration
public function up()
{
// This table is a pivot table mapping users into groups
Schema::create('group_user', function (Blueprint $table) {
$table->bigIncrements('id');
Schema::create('group_user', static function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
$table->unsignedBigInteger('group_id');
......
......@@ -14,8 +14,8 @@ class CreateDoorsTable extends Migration
public function up()
{
// This table contains the base door objects stored on the system
Schema::create('doors', function (Blueprint $table) {
$table->bigIncrements('id');
Schema::create('doors', static function (Blueprint $table) {
$table->id();
$table->string('location');
$table->string('name')->unique();
// hashed
......
......@@ -15,8 +15,8 @@ class CreateUserDoorTable extends Migration
{
// This table maps special cases of users getting access to doors
// without being in a special group.
Schema::create('user_door', function (Blueprint $table) {
$table->bigIncrements('id');
Schema::create('user_door', static function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('door_id');
$table->foreign('door_id')->references('id')->on('doors');
$table->unsignedBigInteger('user_id');
......
......@@ -14,8 +14,8 @@ class CreateDoorGroupTable extends Migration
public function up()
{
// This table maps the permissions a specific group has to a specific door
Schema::create('door_group', function (Blueprint $table) {
$table->bigIncrements('id');
Schema::create('door_group', static function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('door_id');
$table->foreign('door_id')->references('id')->on('doors');
$table->unsignedBigInteger('group_id');
......
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateEventsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
// This table houses all event objects
// Add option for code or open mode
Schema::create('events', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title');
$table->string('description');
// Do I want this here?
$table->boolean('repeat');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('events');
}
}
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateEventScheduleTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
// This table houses time periods for schedules
Schema::create('event_schedule', function (Blueprint $table) {
$table->bigIncrements('id');
// Setup repeat schedule for times? and tie to events
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('event_schedule');
}
}
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateGroupScheduleTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
// This table maps time specific permissions to groups
Schema::create('group_schedule', function (Blueprint $table) {
$table->bigIncrements('id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('group_schedule');
}
}
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUserScheduleTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
// This table maps time specific permissions to users
Schema::create('user_schedule', function (Blueprint $table) {