Skip to content

Commit

Permalink
Refactor+Improve: Eliminate abstract requirements from NullAuthentica…
Browse files Browse the repository at this point in the history
…table
  • Loading branch information
mpyw committed Dec 11, 2019
1 parent dd2e6a7 commit 42fd167
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 118 deletions.
37 changes: 15 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,31 +124,24 @@ use Mpyw\NullAuth\NullAuthenticatable;
class User extends Model implements Authenticatable
{
use NullAuthenticatable;

/**
* Get the name of the unique identifier for the user.
*
* @return string
*/
public function getAuthIdentifierName()
{
return $this->getKeyName();
}

/**
* Get the unique identifier for the user.
*
* @return mixed
*/
public function getAuthIdentifier()
{
return $this->{$this->getKeyName()};
}
}
```

You only need to implement **[`getAuthIdentifierName()`]** and **[`getAuthIdentifier()`]** along with **`NullAuthenticatable`** trait,
but you don't have to worry about anything else.
```php
<?php

$user = User::find(1);

// Minimal implementation for Authenticatable
var_dump($user->getAuthIdentifierName()); // string(2) "id"
var_dump($user->getAuthIdentifier()); // int(1)

// Useless implementation for Authenticatable when we don't use StatefulGuard
var_dump($user->getAuthPassword()); // string(0) ""
var_dump($user->getRememberTokenName()); // string(0) ""
var_dump($user->getRememberToken()); // string(0) ""
$user->setRememberToken(); // Does nothing
```

### Middleware-based Authentication

Expand Down
31 changes: 31 additions & 0 deletions src/Concerns/HasEloquentIdentifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Mpyw\NullAuth\Concerns;

/**
* Trait HasEloquentIdentifier
*
* @mixin \Illuminate\Database\Eloquent\Model
*/
trait HasEloquentIdentifier
{
/**
* Get the name of the unique identifier for the user.
*
* @return string
*/
public function getAuthIdentifierName()
{
return $this->getKeyName();
}

/**
* Get the unique identifier for the user.
*
* @return mixed
*/
public function getAuthIdentifier()
{
return $this->{$this->getKeyName()};
}
}
20 changes: 20 additions & 0 deletions src/Concerns/RequiresIdentifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Mpyw\NullAuth\Concerns;

trait RequiresIdentifier
{
/**
* Get the name of the unique identifier for the user.
*
* @return string
*/
abstract public function getAuthIdentifierName();

/**
* Get the unique identifier for the user.
*
* @return mixed
*/
abstract public function getAuthIdentifier();
}
48 changes: 48 additions & 0 deletions src/GenericNullAuthenticatable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Mpyw\NullAuth;

trait GenericNullAuthenticatable
{
use Concerns\RequiresIdentifier;

/**
* Get the password for the user.
*
* @return string
*/
public function getAuthPassword()
{
return '';
}

/**
* Get the token value for the "remember me" session.
*
* @return string
*/
public function getRememberToken()
{
return '';
}

/**
* Set the token value for the "remember me" session.
*
* @param string $value
* @return void
*/
public function setRememberToken($value)
{
}

/**
* Get the column name for the "remember me" token.
*
* @return string
*/
public function getRememberTokenName()
{
return '';
}
}
50 changes: 50 additions & 0 deletions src/GenericStrictNullAuthenticatable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Mpyw\NullAuth;

use LogicException;

trait GenericStrictNullAuthenticatable
{
use Concerns\RequiresIdentifier;

/**
* Get the password for the user.
*
* @return string
*/
public function getAuthPassword()
{
throw new LogicException('Not implemented');
}

/**
* Get the token value for the "remember me" session.
*
* @return string
*/
public function getRememberToken()
{
throw new LogicException('Not implemented');
}

/**
* Set the token value for the "remember me" session.
*
* @param string $value
*/
public function setRememberToken($value)
{
throw new LogicException('Not implemented');
}

/**
* Get the column name for the "remember me" token.
*
* @return string
*/
public function getRememberTokenName()
{
throw new LogicException('Not implemented');
}
}
55 changes: 2 additions & 53 deletions src/NullAuthenticatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,6 @@

trait NullAuthenticatable
{
/**
* Get the name of the unique identifier for the user.
*
* @return string
*/
abstract public function getAuthIdentifierName();

/**
* Get the unique identifier for the user.
*
* @return mixed
*/
abstract public function getAuthIdentifier();

/**
* Get the password for the user.
*
* @return string
*/
public function getAuthPassword()
{
return '';
}

/**
* Get the token value for the "remember me" session.
*
* @return string
*/
public function getRememberToken()
{
return '';
}

/**
* Set the token value for the "remember me" session.
*
* @param string $value
* @return void
*/
public function setRememberToken($value)
{
}

/**
* Get the column name for the "remember me" token.
*
* @return string
*/
public function getRememberTokenName()
{
return '';
}
use GenericNullAuthenticatable,
Concerns\HasEloquentIdentifier;
}
45 changes: 2 additions & 43 deletions src/StrictNullAuthenticatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,8 @@

namespace Mpyw\NullAuth;

use LogicException;

trait StrictNullAuthenticatable
{
use NullAuthenticatable;

/**
* Get the password for the user.
*
* @return string
*/
public function getAuthPassword()
{
throw new LogicException('Not implemented');
}

/**
* Get the token value for the "remember me" session.
*
* @return string
*/
public function getRememberToken()
{
throw new LogicException('Not implemented');
}

/**
* Set the token value for the "remember me" session.
*
* @param string $value
*/
public function setRememberToken($value)
{
throw new LogicException('Not implemented');
}

/**
* Get the column name for the "remember me" token.
*
* @return string
*/
public function getRememberTokenName()
{
throw new LogicException('Not implemented');
}
use GenericStrictNullAuthenticatable,
Concerns\HasEloquentIdentifier;
}
10 changes: 10 additions & 0 deletions tests/Unit/NullAuthenticatableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,20 @@ public function setUp(): void

$this->user = new class($this->attributes) extends GenericUser {
use NullAuthenticatable;

public function getKeyName()
{
return 'id';
}
};

$this->strict = new class($this->attributes) extends GenericUser {
use StrictNullAuthenticatable;

public function getKeyName()
{
return 'id';
}
};
}

Expand Down

0 comments on commit 42fd167

Please sign in to comment.