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: namespace Nette\Security;
9:
10: use Nette;
11:
12:
13: /**
14: * Default implementation of IIdentity.
15: *
16: * @property mixed $id
17: * @property array $roles
18: * @property array $data
19: */
20: class Identity implements IIdentity
21: {
22: use Nette\SmartObject {
23: __get as private parentGet;
24: __set as private parentSet;
25: __isset as private parentIsSet;
26: }
27:
28: /** @var mixed */
29: private $id;
30:
31: /** @var array */
32: private $roles;
33:
34: /** @var array */
35: private $data;
36:
37:
38: /**
39: * @param mixed identity ID
40: * @param mixed roles
41: * @param array user data
42: */
43: public function __construct($id, $roles = NULL, $data = NULL)
44: {
45: $this->setId($id);
46: $this->setRoles((array) $roles);
47: $this->data = $data instanceof \Traversable ? iterator_to_array($data) : (array) $data;
48: }
49:
50:
51: /**
52: * Sets the ID of user.
53: * @param mixed
54: * @return self
55: */
56: public function setId($id)
57: {
58: $this->id = is_numeric($id) && !is_float($tmp = $id * 1) ? $tmp : $id;
59: return $this;
60: }
61:
62:
63: /**
64: * Returns the ID of user.
65: * @return mixed
66: */
67: public function getId()
68: {
69: return $this->id;
70: }
71:
72:
73: /**
74: * Sets a list of roles that the user is a member of.
75: * @param array
76: * @return self
77: */
78: public function setRoles(array $roles)
79: {
80: $this->roles = $roles;
81: return $this;
82: }
83:
84:
85: /**
86: * Returns a list of roles that the user is a member of.
87: * @return array
88: */
89: public function getRoles()
90: {
91: return $this->roles;
92: }
93:
94:
95: /**
96: * Returns a user data.
97: * @return array
98: */
99: public function getData()
100: {
101: return $this->data;
102: }
103:
104:
105: /**
106: * Sets user data value.
107: * @param string property name
108: * @param mixed property value
109: * @return void
110: */
111: public function __set($key, $value)
112: {
113: if ($this->parentIsSet($key)) {
114: $this->parentSet($key, $value);
115:
116: } else {
117: $this->data[$key] = $value;
118: }
119: }
120:
121:
122: /**
123: * Returns user data value.
124: * @param string property name
125: * @return mixed
126: */
127: public function &__get($key)
128: {
129: if ($this->parentIsSet($key)) {
130: return $this->parentGet($key);
131:
132: } else {
133: return $this->data[$key];
134: }
135: }
136:
137:
138: /**
139: * Is property defined?
140: * @param string property name
141: * @return bool
142: */
143: public function __isset($key)
144: {
145: return isset($this->data[$key]) || $this->parentIsSet($key);
146: }
147:
148: }
149: