|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Linna Framework. |
| 5 | + * |
| 6 | + * @author Sebastian Rapetti <sebastian.rapetti@tim.it> |
| 7 | + * @copyright (c) 2020, Sebastian Rapetti |
| 8 | + * @license http://opensource.org/licenses/MIT MIT License |
| 9 | + */ |
| 10 | +declare(strict_types=1); |
| 11 | + |
| 12 | +namespace Linna\Authorization; |
| 13 | + |
| 14 | +use DateTimeImmutable; |
| 15 | +use Linna\DataMapper\DomainObjectInterface; |
| 16 | +use Linna\Storage\ExtendedPDO; |
| 17 | +use stdClass; |
| 18 | + |
| 19 | +/** |
| 20 | + * PermissionMapper. |
| 21 | + */ |
| 22 | +class PermissionExtendedMapper extends PermissionMapper implements PermissionExtendedMapperInterface |
| 23 | +{ |
| 24 | + /** |
| 25 | + * Constructor. |
| 26 | + * |
| 27 | + * @param ExtendedPDO $pdo |
| 28 | + */ |
| 29 | + public function __construct( |
| 30 | + /** @var ExtendedPDO Database Connection */ |
| 31 | + protected ExtendedPDO $pdo, |
| 32 | + |
| 33 | + /** @var RoleMapperInterface Permission Mapper. */ |
| 34 | + protected RoleMapperInterface $roleMapper, |
| 35 | + |
| 36 | + /** @var UserMapperInterface Enhanced User Mapper. */ |
| 37 | + protected UserMapperInterface $userMapper |
| 38 | + ) { |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Hydrate an array of objects. |
| 43 | + * |
| 44 | + * @param array<int, stdClass> $array The array containing the resultset from database. |
| 45 | + * |
| 46 | + * @return array<int, EnhancedUser> |
| 47 | + */ |
| 48 | + protected function hydrator(array $array): array |
| 49 | + { |
| 50 | + $tmp = []; |
| 51 | + |
| 52 | + foreach ($array as $value) { |
| 53 | + |
| 54 | + //get users and roles |
| 55 | + $users = $this->userMapper->fetchByPermissionId($value->permission_id); |
| 56 | + $roles = $this->roleMapper->fetchByPermissionId($value->permission_id); |
| 57 | + |
| 58 | + $tmp[] = new PermissionExtended( |
| 59 | + id: $value->permission_id, |
| 60 | + name: $value->name, |
| 61 | + description: $value->description, |
| 62 | + inherited: $value->inherited, |
| 63 | + created: new DateTimeImmutable($value->created), |
| 64 | + lastUpdate: new DateTimeImmutable($value->last_update), |
| 65 | + users: $users, |
| 66 | + roles: $roles |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + return $tmp; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Hydrate an object. |
| 75 | + * |
| 76 | + * @param object $object The object containing the resultset from database. |
| 77 | + * |
| 78 | + * @return DomainObjectInterface |
| 79 | + */ |
| 80 | + protected function hydratorSingle(object $object): DomainObjectInterface |
| 81 | + { |
| 82 | + //get users and roles |
| 83 | + $users = $this->userMapper->fetchByPermissionId($object->permission_id); |
| 84 | + $roles = $this->roleMapper->fetchByPermissionId($object->permission_id); |
| 85 | + |
| 86 | + return new PermissionExtended( |
| 87 | + id: $object->permission_id, |
| 88 | + name: $object->name, |
| 89 | + description: $object->description, |
| 90 | + inherited: $object->inherited, |
| 91 | + created: new DateTimeImmutable($object->created), |
| 92 | + lastUpdate: new DateTimeImmutable($object->last_update), |
| 93 | + users: $users, |
| 94 | + roles: $roles |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * {@inheritdoc} |
| 100 | + */ |
| 101 | + /*public function permissionExistById(int|string $permissionId): bool |
| 102 | + { |
| 103 | + //make query |
| 104 | + $stmt = $this->pdo->prepare('SELECT permission_id FROM permission WHERE permission_id = :id'); |
| 105 | +
|
| 106 | + $stmt->bindParam(':id', $permissionId, PDO::PARAM_INT); |
| 107 | + $stmt->execute(); |
| 108 | +
|
| 109 | + return ($stmt->rowCount() > 0) ? true : false; |
| 110 | + }*/ |
| 111 | + |
| 112 | + /** |
| 113 | + * {@inheritdoc} |
| 114 | + */ |
| 115 | + /*public function permissionExistByName(string $permissionName): bool |
| 116 | + { |
| 117 | + //make query |
| 118 | + $stmt = $this->pdo->prepare('SELECT permission_id FROM permission WHERE name = :name'); |
| 119 | +
|
| 120 | + $stmt->bindParam(':name', $permissionName, PDO::PARAM_STR); |
| 121 | + $stmt->execute(); |
| 122 | +
|
| 123 | + return ($stmt->rowCount() > 0) ? true : false; |
| 124 | + }*/ |
| 125 | + |
| 126 | + /** |
| 127 | + * {@inheritdoc} |
| 128 | + */ |
| 129 | + protected function concreteCreate(): DomainObjectInterface |
| 130 | + { |
| 131 | + return new PermissionExtended(); |
| 132 | + } |
| 133 | +} |
0 commit comments