Skip to content

Commit

Permalink
Merge pull request #626 from DirectoryTree/FEATURE-621
Browse files Browse the repository at this point in the history
Add `RulePassed` and `RuleFailed` authentication events
  • Loading branch information
stevebauman committed Feb 4, 2024
2 parents aaaa1ad + 979aa21 commit 02867cc
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Auth/Validator.php
Expand Up @@ -3,6 +3,8 @@
namespace LdapRecord\Laravel\Auth;

use Illuminate\Database\Eloquent\Model as Eloquent;
use LdapRecord\Laravel\Events\Auth\RuleFailed;
use LdapRecord\Laravel\Events\Auth\RulePassed;
use LdapRecord\Models\Model as LdapRecord;

class Validator
Expand Down Expand Up @@ -31,8 +33,12 @@ public function passes(LdapRecord $user, Eloquent $model = null): bool
{
foreach ($this->rules as $rule) {
if (! $rule->passes($user, $model)) {
event(new RuleFailed($rule, $user, $model));

return false;
}

event(new RulePassed($rule, $user, $model));
}

return true;
Expand Down
25 changes: 25 additions & 0 deletions src/Events/Auth/RuleEvent.php
@@ -0,0 +1,25 @@
<?php

namespace LdapRecord\Laravel\Events\Auth;

use Illuminate\Database\Eloquent\Model as EloquentModel;
use LdapRecord\Laravel\Auth\Rule;
use LdapRecord\Models\Model as LdapModel;

abstract class RuleEvent extends Event
{
/**
* The authentication rule.
*/
public Rule $rule;

/**
* Constructor.
*/
public function __construct(Rule $rule, LdapModel $object, EloquentModel $eloquent = null)
{
parent::__construct($object, $eloquent);

$this->rule = $rule;
}
}
8 changes: 8 additions & 0 deletions src/Events/Auth/RuleFailed.php
@@ -0,0 +1,8 @@
<?php

namespace LdapRecord\Laravel\Events\Auth;

class RuleFailed extends RuleEvent
{
//
}
8 changes: 8 additions & 0 deletions src/Events/Auth/RulePassed.php
@@ -0,0 +1,8 @@
<?php

namespace LdapRecord\Laravel\Events\Auth;

class RulePassed extends RuleEvent
{
//
}
11 changes: 11 additions & 0 deletions tests/Unit/ValidatorTest.php
Expand Up @@ -4,8 +4,11 @@

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Support\Facades\Event;
use LdapRecord\Laravel\Auth\Rule;
use LdapRecord\Laravel\Auth\Validator;
use LdapRecord\Laravel\Events\Auth\RuleFailed;
use LdapRecord\Laravel\Events\Auth\RulePassed;
use LdapRecord\Laravel\Tests\TestCase;
use LdapRecord\Models\Entry;
use LdapRecord\Models\Model as LdapRecord;
Expand All @@ -28,14 +31,22 @@ public function test_rules_can_be_added()

public function test_passing_validation_rule()
{
Event::fake(RulePassed::class);

$rule = new TestPassingRule();
$this->assertTrue((new Validator([$rule]))->passes(new Entry, new TestRuleModelStub));

Event::assertDispatched(RulePassed::class);
}

public function test_failing_validation_rule()
{
Event::fake(RuleFailed::class);

$rule = new TestFailingRule();
$this->assertFalse((new Validator([$rule]))->passes(new Entry, new TestRuleModelStub));

Event::assertDispatched(RuleFailed::class);
}

public function test_all_rules_are_validated()
Expand Down

0 comments on commit 02867cc

Please sign in to comment.