Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
df61b8a
commit d9ddd1b
Showing
10 changed files
with
274 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace BristolSU\Module\AssignRoles\Http\Middleware; | ||
|
||
use BristolSU\ControlDB\Contracts\Repositories\Pivots\UserGroup; | ||
use BristolSU\ControlDB\Contracts\Repositories\Pivots\UserRole; | ||
use BristolSU\ControlDB\Contracts\Repositories\Role; | ||
use BristolSU\Module\AssignRoles\Support\LogicRoleRepository; | ||
use BristolSU\Module\AssignRoles\Support\LogicUserGroupRepository; | ||
use BristolSU\Support\Logic\Contracts\LogicTester; | ||
use Illuminate\Http\Request; | ||
|
||
class BindRoleRepository | ||
{ | ||
|
||
/** | ||
* @var LogicTester | ||
*/ | ||
private $logicTester; | ||
|
||
public function __construct(LogicTester $logicTester) | ||
{ | ||
$this->logicTester = $logicTester; | ||
} | ||
|
||
public function handle(Request $request, \Closure $next) | ||
{ | ||
app()->extend(Role::class, function($roleRepository, $app) { | ||
return new LogicRoleRepository($roleRepository, $this->logicTester); | ||
}); | ||
|
||
app()->extend(UserRole::class, function($userRole, $app) { | ||
return new LogicUserGroupRepository($userRole, $this->logicTester); | ||
}); | ||
|
||
return $next($request); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?php | ||
|
||
namespace BristolSU\Module\AssignRoles\Support; | ||
|
||
use BristolSU\ControlDB\Contracts\Models\Role as RoleModel; | ||
use BristolSU\ControlDB\Contracts\Repositories\Role; | ||
use BristolSU\Support\Logic\Contracts\LogicRepository; | ||
use BristolSU\Support\Logic\Facade\LogicTester; | ||
use Illuminate\Support\Collection; | ||
|
||
class LogicRoleRepository implements Role | ||
{ | ||
|
||
/** | ||
* @var Role | ||
*/ | ||
private $roleRepository; | ||
/** | ||
* @var \BristolSU\Support\Logic\Contracts\LogicTester | ||
*/ | ||
private $logicTester; | ||
|
||
public function __construct(Role $roleRepository, \BristolSU\Support\Logic\Contracts\LogicTester $logicTester) | ||
{ | ||
$this->roleRepository = $roleRepository; | ||
$this->logicTester = $logicTester; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getById(int $id): RoleModel | ||
{ | ||
return $this->roleRepository->getById($id); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function all(): Collection | ||
{ | ||
return $this->filter($this->roleRepository->all()); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getByDataProviderId(int $dataProviderId): RoleModel | ||
{ | ||
return $this->roleRepository->getByDataProviderId($dataProviderId); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function create(int $positionId, int $groupId, int $dataProviderId): RoleModel | ||
{ | ||
return $this->roleRepository->create($positionId, $groupId, $dataProviderId); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function delete(int $id): void | ||
{ | ||
$this->roleRepository->delete($id); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function allThroughGroup(\BristolSU\ControlDB\Contracts\Models\Group $group): Collection | ||
{ | ||
return $this->filter($this->roleRepository->allThroughGroup($group)); | ||
|
||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function allThroughPosition(\BristolSU\ControlDB\Contracts\Models\Position $position): Collection | ||
{ | ||
return $this->filter($this->roleRepository->allThroughPosition($position)); | ||
} | ||
|
||
private function hasLogicGroup() | ||
{ | ||
return $this->logicGroupId() !== null; | ||
} | ||
|
||
private function logicGroupId() | ||
{ | ||
$id = settings('logic_group', null); | ||
if($id === null) { | ||
return null; | ||
} | ||
return (int) $id; | ||
} | ||
|
||
private function logicGroup() | ||
{ | ||
return app(LogicRepository::class)->getById( | ||
$this->logicGroupId() | ||
); | ||
} | ||
|
||
private function isInLogicGroup(\BristolSU\ControlDB\Contracts\Models\Role $role) | ||
{ | ||
return $this->logicTester->evaluate($this->logicGroup(), null, $role->group(), $role); | ||
} | ||
|
||
private function filter(Collection $roles) | ||
{ | ||
if($this->hasLogicGroup()) { | ||
return $roles->filter(function(\BristolSU\ControlDB\Contracts\Models\Role $role) { | ||
return $this->isInLogicGroup($role); | ||
})->values(); | ||
} | ||
return $roles; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
namespace BristolSU\Module\AssignRoles\Support; | ||
|
||
use BristolSU\ControlDB\Contracts\Models\Role; | ||
use BristolSU\ControlDB\Contracts\Models\User; | ||
use BristolSU\ControlDB\Contracts\Repositories\Pivots\UserRole; | ||
use BristolSU\Support\Logic\Contracts\LogicRepository; | ||
use Illuminate\Support\Collection; | ||
|
||
class LogicUserGroupRepository implements UserRole | ||
{ | ||
|
||
/** | ||
* @var UserRole | ||
*/ | ||
private $userRoleRepository; | ||
/** | ||
* @var \BristolSU\Support\Logic\Contracts\LogicTester | ||
*/ | ||
private $logicTester; | ||
|
||
public function __construct(UserRole $userRoleRepository, \BristolSU\Support\Logic\Contracts\LogicTester $logicTester) | ||
{ | ||
$this->userRoleRepository = $userRoleRepository; | ||
$this->logicTester = $logicTester; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getUsersThroughRole(Role $role): Collection | ||
{ | ||
return $this->userRoleRepository->getUsersThroughRole($role); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getRolesThroughUser(User $user): Collection | ||
{ | ||
return $this->filter($this->userRoleRepository->getRolesThroughUser($user)); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function addUserToRole(User $user, Role $role): void | ||
{ | ||
$this->userRoleRepository->addUserToRole($user, $role); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function removeUserFromRole(User $user, Role $role): void | ||
{ | ||
$this->userRoleRepository->removeUserFromRole($user, $role); | ||
} | ||
|
||
private function hasLogicGroup() | ||
{ | ||
return $this->logicGroupId() !== null; | ||
} | ||
|
||
private function logicGroupId() | ||
{ | ||
$id = settings('logic_group', null); | ||
if($id === null) { | ||
return null; | ||
} | ||
return (int) $id; | ||
} | ||
|
||
private function logicGroup() | ||
{ | ||
return app(LogicRepository::class)->getById( | ||
$this->logicGroupId() | ||
); | ||
} | ||
|
||
private function isInLogicGroup(\BristolSU\ControlDB\Contracts\Models\Role $role) | ||
{ | ||
return $this->logicTester->evaluate($this->logicGroup(), null, $role->group(), $role); | ||
} | ||
|
||
private function filter(Collection $roles) | ||
{ | ||
if($this->hasLogicGroup()) { | ||
return $roles->filter(function(\BristolSU\ControlDB\Contracts\Models\Role $role) { | ||
return $this->isInLogicGroup($role); | ||
})->values(); | ||
} | ||
return $roles; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters