Skip to content

Commit

Permalink
Merge a7eba65 into af3d397
Browse files Browse the repository at this point in the history
  • Loading branch information
MGatner committed Mar 1, 2021
2 parents af3d397 + a7eba65 commit 22c672a
Show file tree
Hide file tree
Showing 11 changed files with 236 additions and 13 deletions.
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"php": "^7.3 || ^8.0"
},
"require-dev": {
"agungsugiarto/codeigniter4-authentication": "dev-master",
"codeigniter4/codeigniter4": "dev-develop",
"lonnieezell/codigniter-shield": "dev-develop",
"myth/auth": "dev-develop",
Expand All @@ -41,6 +42,10 @@
}
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/agungsugiarto/codeigniter4-authentication"
},
{
"type": "vcs",
"url": "https://github.com/codeigniter4/CodeIgniter4"
Expand Down
76 changes: 76 additions & 0 deletions src/Entities/FluentEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php namespace Tatter\Users\Entities;

use Fluent\Auth\Entities\User;
use Tatter\Users\UserEntity;

/**
* Fluent User Entity
*/
class FluentEntity extends User implements UserEntity
{
/**
* Returns the name of the column used to
* uniquely identify this user, typically 'id'.
*
* @return string
*/
public function getIdentifier(): string
{
return $this->getAuthIdColumn();
}

/**
* Returns the value for the identifier,
* or `null` for "uncreated" users.
*
* @return string|int|null
*/
public function getId()
{
return $this->getAuthId();
}

/**
* Returns the email address.
*
* @return string|null
*/
public function getEmail(): ?string
{
return $this->getAuthEmail();
}

/**
* Returns the username.
*
* @return string|null
*/
public function getUsername(): ?string
{
return $this->attributes['username'] ?? null;
}

/**
* Returns the name for this user.
* If names are stored as parts "first",
* "middle", "last" they should be
* concatenated with spaces.
*
* @return string|null
*/
public function getName(): ?string
{
return null;
}

/**
* Returns whether this user is eligible
* for authentication.
*
* @return bool
*/
public function isActive(): bool
{
return empty($this->attributes['deleted_at']);
}
}
34 changes: 30 additions & 4 deletions src/Entities/MythEntity.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php namespace Tatter\Users\Entities;

use Myth\Auth\Entities\User;
use Tatter\Users\UserEntity;
use RuntimeException;
use Tatter\Users\Interfaces\HasGroup;
use Tatter\Users\Interfaces\HasPermission;

/**
* Myth User Entity
*/
class MythEntity extends User implements UserEntity
class MythEntity extends User implements HasGroup, HasPermission
{
/**
* Returns the name of the column used to
Expand Down Expand Up @@ -61,7 +61,7 @@ public function getUsername(): ?string
*/
public function getName(): ?string
{
throw new RuntimeException('That attribute is not supported.');
return null;
}

/**
Expand All @@ -74,4 +74,30 @@ public function isActive(): bool
{
return $this->isActivated();
}

/**
* Returns whether this user is a
* member of the given group.
*
* @param string $group The group name
*
* @return bool
*/
public function hasGroup(string $group): bool
{
return in_array(strtolower($group), $this->getRoles());
}

/**
* Returns whether this user has
* a certain permission.
*
* @param string $permission The permission name
*
* @return bool
*/
public function hasPermission(string $permission): bool
{
return $this->can($permission);
}
}
3 changes: 1 addition & 2 deletions src/Entities/ShieldEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use Sparks\Shield\Entities\User;
use Tatter\Users\UserEntity;
use RuntimeException;

/**
* Shield User Entity
Expand Down Expand Up @@ -61,7 +60,7 @@ public function getUsername(): ?string
*/
public function getName(): ?string
{
throw new RuntimeException('That attribute is not supported.');
return null;
}

/**
Expand Down
61 changes: 61 additions & 0 deletions src/Factories/FluentFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php namespace Tatter\Users\Factories;

use Fluent\Auth\Models\UserModel;
use Tatter\Users\Entities\FluentEntity;
use Tatter\Users\UserEntity;
use Tatter\Users\UserFactory;

/**
* Fluent User Factory
*/
class FluentFactory extends UserModel implements UserFactory
{
/**
* The format that the results should be returned as.
*
* @var string
*/
protected $returnType = FluentEntity::class;

/**
* Locates a user by its primary identifier.
*
* @param string|int $id
*
* @return FluentEntity|null
*/
public function findById($id): ?UserEntity
{
/** @var FluentEntity|null $result */
$result = parent::findById($id);
return $result;
}

/**
* Locates a user by its email.
*
* @param string $email
*
* @return FluentEntity|null
*/
public function findByEmail(string $email): ?UserEntity
{
/** @var FluentEntity|null $result */
$result = $this->findByCredentials(['email' => $email]);
return $result;
}

/**
* Locates a user by its username.
*
* @param string $username
*
* @return FluentEntity|null
*/
public function findByUsername(string $username): ?UserEntity
{
/** @var FluentEntity|null $result */
$result = $this->findByCredentials(['username' => $username]);
return $result;
}
}
22 changes: 22 additions & 0 deletions src/Interfaces/HasGroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php namespace Tatter\Users\Interfaces;

use Tatter\Users\UserEntity;

/**
* Has Group Interface
*
* User extension for entities that
* can verify group membership.
*/
interface HasGroup extends UserEntity
{
/**
* Returns whether this user is a
* member of the given group.
*
* @param string $group The group name
*
* @return bool
*/
public function hasGroup(string $group): bool;
}
22 changes: 22 additions & 0 deletions src/Interfaces/HasPermission.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php namespace Tatter\Users\Interfaces;

use Tatter\Users\UserEntity;

/**
* Has Permission Interface
*
* User extension for entities that
* can test a designated permission.
*/
interface HasPermission extends UserEntity
{
/**
* Returns whether this user has
* a certain permission.
*
* @param string $permission The permission name
*
* @return bool
*/
public function hasPermission(string $permission): bool;
}
3 changes: 2 additions & 1 deletion src/UserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ class UserProvider
{
/**
* Known factories. Keys are the
* class to check for to validate.
* class to check availability.
*
* @var array<string,string>
*/
protected static $factories = [
'Myth\Auth\Models\UserModel' => Factories\MythFactory::class,
'Sparks\Shield\Models\UserModel' => Factories\ShieldFactory::class,
'Fluent\Auth\Models\UserModel' => Factories\FluentFactory::class,
];

/**
Expand Down
9 changes: 3 additions & 6 deletions tests/unit/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,16 @@ public function testEntityMethods($class)
$this->assertEquals($this->data['id'], $entity->getId());
$this->assertEquals($this->data['email'], $entity->getEmail());
$this->assertEquals($this->data['username'], $entity->getUsername());
$this->assertEquals(true, $entity->isActive());

$this->expectException('RuntimeException');
$this->expectExceptionMessage('That attribute is not supported.');

$entity->getName();
$this->assertTrue($entity->isActive());
$this->assertNull($entity->getName());
}

public function entityProvider(): array
{
return [
['Tatter\Users\Entities\MythEntity'],
['Tatter\Users\Entities\ShieldEntity'],
['Tatter\Users\Entities\FluentEntity'],
];
}
}
12 changes: 12 additions & 0 deletions tests/unit/FluentFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

use Fluent\Auth\Models\UserModel;
use Tatter\Users\Factories\FluentFactory;
use Tests\Support\FactoryTestCase;

class FluentFactoryTest extends FactoryTestCase
{
protected $namespace = 'Fluent\Auth';
protected $class = FluentFactory::class;
protected $faker = UserModel::class;
}
2 changes: 2 additions & 0 deletions tests/unit/ProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public function testGetFactories()
$expected = [
'Myth\Auth\Models\UserModel' => 'Tatter\Users\Factories\MythFactory',
'Sparks\Shield\Models\UserModel' => 'Tatter\Users\Factories\ShieldFactory',
'Fluent\Auth\Models\UserModel' => 'Tatter\Users\Factories\FluentFactory',
];

$this->assertSame($expected, UserProvider::getFactories());
Expand All @@ -30,6 +31,7 @@ public function testAddFactory()
'foo' => 'bar',
'Myth\Auth\Models\UserModel' => 'Tatter\Users\Factories\MythFactory',
'Sparks\Shield\Models\UserModel' => 'Tatter\Users\Factories\ShieldFactory',
'Fluent\Auth\Models\UserModel' => 'Tatter\Users\Factories\FluentFactory',
];

UserProvider::addFactory('foo', 'bar');
Expand Down

0 comments on commit 22c672a

Please sign in to comment.