|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the Nette Framework (https://nette.org) |
| 5 | + * Copyright (c) 2004 David Grudl (https://davidgrudl.com) |
| 6 | + */ |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
| 10 | +namespace Nette\Security; |
| 11 | + |
| 12 | +use Nette; |
| 13 | + |
| 14 | + |
| 15 | +/** |
| 16 | + * Default implementation of IIdentity. |
| 17 | + * |
| 18 | + * @property mixed $id |
| 19 | + * @property array $roles |
| 20 | + * @property array $data |
| 21 | + */ |
| 22 | +class SimpleIdentity implements IIdentity, \Serializable |
| 23 | +{ |
| 24 | + use Nette\SmartObject { |
| 25 | + __get as private parentGet; |
| 26 | + __set as private parentSet; |
| 27 | + __isset as private parentIsSet; |
| 28 | + } |
| 29 | + |
| 30 | + /** @var mixed */ |
| 31 | + private $id; |
| 32 | + |
| 33 | + /** @var array */ |
| 34 | + private $roles; |
| 35 | + |
| 36 | + /** @var array */ |
| 37 | + private $data; |
| 38 | + |
| 39 | + |
| 40 | + public function __construct($id, $roles = null, iterable $data = null) |
| 41 | + { |
| 42 | + $this->setId($id); |
| 43 | + $this->setRoles((array) $roles); |
| 44 | + $this->data = $data instanceof \Traversable |
| 45 | + ? iterator_to_array($data) |
| 46 | + : (array) $data; |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | + /** |
| 51 | + * Sets the ID of user. |
| 52 | + * @param string|int $id |
| 53 | + * @return static |
| 54 | + */ |
| 55 | + public function setId($id) |
| 56 | + { |
| 57 | + if (!is_string($id) && !is_int($id)) { |
| 58 | + throw new Nette\InvalidArgumentException('Identity identifier must be string|int, but type "' . gettype($id) . '" given.'); |
| 59 | + } |
| 60 | + $this->id = $id; |
| 61 | + return $this; |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + /** |
| 66 | + * Returns the ID of user. |
| 67 | + * @return string|int |
| 68 | + */ |
| 69 | + public function getId() |
| 70 | + { |
| 71 | + return $this->id; |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + /** |
| 76 | + * Sets a list of roles that the user is a member of. |
| 77 | + * @return static |
| 78 | + */ |
| 79 | + public function setRoles(array $roles) |
| 80 | + { |
| 81 | + $this->roles = $roles; |
| 82 | + return $this; |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | + /** |
| 87 | + * Returns a list of roles that the user is a member of. |
| 88 | + */ |
| 89 | + public function getRoles(): array |
| 90 | + { |
| 91 | + return $this->roles; |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | + /** |
| 96 | + * Returns a user data. |
| 97 | + */ |
| 98 | + public function getData(): array |
| 99 | + { |
| 100 | + return $this->data; |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + /** |
| 105 | + * Sets user data value. |
| 106 | + */ |
| 107 | + public function __set(string $key, $value): void |
| 108 | + { |
| 109 | + if ($this->parentIsSet($key)) { |
| 110 | + $this->parentSet($key, $value); |
| 111 | + |
| 112 | + } else { |
| 113 | + $this->data[$key] = $value; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + |
| 118 | + /** |
| 119 | + * Returns user data value. |
| 120 | + * @return mixed |
| 121 | + */ |
| 122 | + public function &__get(string $key) |
| 123 | + { |
| 124 | + if ($this->parentIsSet($key)) { |
| 125 | + return $this->parentGet($key); |
| 126 | + |
| 127 | + } else { |
| 128 | + return $this->data[$key]; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + |
| 133 | + public function __isset(string $key): bool |
| 134 | + { |
| 135 | + return isset($this->data[$key]) || $this->parentIsSet($key); |
| 136 | + } |
| 137 | + |
| 138 | + |
| 139 | + public function serialize(): string |
| 140 | + { |
| 141 | + return serialize([ |
| 142 | + 'id' => $this->id, |
| 143 | + 'roles' => $this->roles, |
| 144 | + 'data' => $this->data, |
| 145 | + ]); |
| 146 | + } |
| 147 | + |
| 148 | + |
| 149 | + public function unserialize($serialized) |
| 150 | + { |
| 151 | + [ |
| 152 | + 'id' => $this->id, |
| 153 | + 'roles' => $this->roles, |
| 154 | + 'data' => $this->data, |
| 155 | + ] = unserialize($serialized); |
| 156 | + } |
| 157 | +} |
0 commit comments