Commit 71ca1cb4 authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

Add the log lines to log entries. Added success onto the entries table

so inavlid attempts can also be logged
parent f79b3063
......@@ -7,6 +7,8 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Entry extends Model
{
protected $fillable = ['door_id', 'user_id', 'success'];
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
......
......@@ -10,7 +10,9 @@ use Source\Gateways\Users\UsersRepositoryServiceProvider;
use Source\Gateways\Groups\GroupsRepositoryServiceProvider;
use Source\Gateways\Tokens\TokensRepositoryServiceProvider;
use Source\UseCases\Door\Access\AccessUseCaseServiceProvider;
use Source\Gateways\Entries\EntriesRepositoryServiceProvider;
use Source\Gateways\DoorUser\DoorUserRepositoryServiceProvider;
use Source\Gateways\Attempts\AttemptsRepositoryServiceProvider;
use Source\UseCases\Doors\GetDoor\GetDoorUseCaseServiceProvider;
use Source\UseCases\Users\GetUser\GetUserUseCaseServiceProvider;
use Source\Gateways\DoorGroup\DoorGroupRepositoryServiceProvider;
......@@ -59,6 +61,8 @@ class AppServiceProvider extends ServiceProvider
DoorsRepositoryServiceProvider::class,
TokensRepositoryServiceProvider::class,
GroupsRepositoryServiceProvider::class,
EntriesRepositoryServiceProvider::class,
AttemptsRepositoryServiceProvider::class,
DoorUserRepositoryServiceProvider::class,
DoorGroupRepositoryServiceProvider::class,
GroupUserRepositoryServiceProvider::class,
......
......@@ -18,6 +18,7 @@ class CreateLogTable extends Migration
$table->id();
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('door_id');
$table->boolean('success');
$table->foreign('door_id')->references('id')->on('doors');
$table->foreign('user_id')->references('id')->on('users');
$table->timestamps();
......
......@@ -32,16 +32,23 @@ class Entry
*/
protected ?Carbon $updatedAt;
/**
* @var bool
*/
protected bool $success;
public function __construct(
int $id,
int $userId,
int $doorId,
bool $success,
?Carbon $createdAt = null,
?Carbon $updatedAt = null
) {
$this->id = $id;
$this->userId = $userId;
$this->doorId = $doorId;
$this->success = $success;
$this->createdAt = $createdAt;
$this->updatedAt = $updatedAt;
}
......@@ -86,6 +93,14 @@ class Entry
return $this->updatedAt;
}
/**
* @return bool
*/
public function wasSuccessful(): bool
{
return $this->success;
}
/**
* @param \Carbon\Carbon $begin
* @param \Carbon\Carbon $end
......
......@@ -21,6 +21,7 @@ class DatabaseEntriesRepository implements EntriesRepository
$entry->id,
$entry->user_id,
$entry->door_id,
$entry->success,
$entry->created_at,
$entry->updated_at
);
......@@ -77,6 +78,7 @@ class DatabaseEntriesRepository implements EntriesRepository
$e = new \App\Entry();
$e->user_id = $entry->getUserId();
$e->door_id = $entry->getDoorId();
$e->success = $entry->wasSuccessful();
return $e->save();
}
}
......@@ -2,14 +2,21 @@
namespace Source\UseCases\Door\Access;
use Source\Entities\Entry;
use Source\Entities\Attempt;
use Source\Sanitize\CastsTo;
use Source\Entities\HashedSearchable;
use Source\Gateways\Users\UsersRepository;
use Source\Exceptions\AuthorizationException;
use Source\Exceptions\AuthenticationException;
use Source\Gateways\Entries\EntriesRepository;
use Source\Gateways\DoorUser\DoorUserRepository;
use Source\Gateways\Attempts\AttemptsRepository;
class Access implements AccessUseCase
{
use CastsTo;
/**
* @var \Source\Gateways\Users\UsersRepository
*/
......@@ -25,14 +32,28 @@ class Access implements AccessUseCase
*/
protected ?string $doorId;
/**
* @var \Source\Gateways\Attempts\AttemptsRepository
*/
protected AttemptsRepository $attempts;
/**
* @var \Source\Gateways\Entries\EntriesRepository
*/
protected EntriesRepository $entries;
public function __construct(
?string $doorId,
UsersRepository $users,
DoorUserRepository $doorUser
DoorUserRepository $doorUser,
AttemptsRepository $attempts,
EntriesRepository $entries
) {
$this->users = $users;
$this->doorUser = $doorUser;
$this->doorId = $doorId;
$this->entries = $entries;
$this->attempts = $attempts;
$this->doorUser = $doorUser;
}
/**
......@@ -47,13 +68,31 @@ class Access implements AccessUseCase
$user = $this->users->findByDoorcode(HashedSearchable::hash($salt, $doorcode));
if (!$user) {
// Log invalid attempt
$this->attempts->add(new Attempt(0, $this->castToInt($this->doorId)));
throw new AuthorizationException();
}
$groupIntersect = $this->doorUser->getDoorUserGroupIntersection($this->doorId, $user->getId());
if (count($groupIntersect) < 1) {
// Log that the user does not have access, but tried to access the door
$this->entries->add(new Entry(
0,
$user->getId(),
$this->doorId,
false
));
throw new AuthorizationException();
}
// Log the successful entry
$this->entries->add(new Entry(
0,
$user->getId(),
$this->doorId,
true
));
}
}
......@@ -6,8 +6,10 @@ namespace Source\UseCases\Door\Access;
use App\Guards\DoorGuard;
use Illuminate\Support\ServiceProvider;
use Source\Gateways\Users\UsersRepository;
use Source\Gateways\Entries\EntriesRepository;
use Illuminate\Contracts\Foundation\Application;
use Source\Gateways\DoorUser\DoorUserRepository;
use Source\Gateways\Attempts\AttemptsRepository;
use Illuminate\Contracts\Support\DeferrableProvider;
/**
......@@ -26,7 +28,9 @@ class AccessUseCaseServiceProvider extends ServiceProvider implements Deferrable
return new Access(
$app->make(DoorGuard::class)->id(),
$app->make(UsersRepository::class),
$app->make(DoorUserRepository::class)
$app->make(DoorUserRepository::class),
$app->make(AttemptsRepository::class),
$app->make(EntriesRepository::class)
);
});
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment