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