1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (http://nette.org)
5: *
6: * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
7: *
8: * For the full copyright and license information, please view
9: * the file license.txt that was distributed with this source code.
10: */
11:
12: namespace Nette\Security;
13:
14: use Nette;
15:
16:
17:
18: /**
19: * Default implementation of IIdentity.
20: *
21: * @author David Grudl
22: *
23: * @serializationVersion 1.0
24: *
25: * @property mixed $id
26: * @property array $roles
27: * @property-read array $data
28: */
29: class Identity extends Nette\FreezableObject implements IIdentity
30: {
31: /** @var mixed */
32: private $id;
33:
34: /** @var array */
35: private $roles;
36:
37: /** @var array */
38: private $data;
39:
40:
41: /**
42: * @param mixed identity ID
43: * @param mixed roles
44: * @param array user data
45: */
46: public function __construct($id, $roles = NULL, $data = NULL)
47: {
48: $this->setId($id);
49: $this->setRoles((array) $roles);
50: $this->data = $data instanceof \Traversable ? iterator_to_array($data) : (array) $data;
51: }
52:
53:
54:
55: /**
56: * Sets the ID of user.
57: * @param mixed
58: * @return Identity provides a fluent interface
59: */
60: public function setId($id)
61: {
62: $this->updating();
63: $this->id = is_numeric($id) ? 1 * $id : $id;
64: return $this;
65: }
66:
67:
68:
69: /**
70: * Returns the ID of user.
71: * @return mixed
72: */
73: public function getId()
74: {
75: return $this->id;
76: }
77:
78:
79:
80: /**
81: * Sets a list of roles that the user is a member of.
82: * @param array
83: * @return Identity provides a fluent interface
84: */
85: public function setRoles(array $roles)
86: {
87: $this->updating();
88: $this->roles = $roles;
89: return $this;
90: }
91:
92:
93:
94: /**
95: * Returns a list of roles that the user is a member of.
96: * @return array
97: */
98: public function getRoles()
99: {
100: return $this->roles;
101: }
102:
103:
104:
105: /**
106: * Returns a user data.
107: * @return array
108: */
109: public function getData()
110: {
111: return $this->data;
112: }
113:
114:
115:
116: /**
117: * Sets user data value.
118: * @param string property name
119: * @param mixed property value
120: * @return void
121: */
122: public function __set($key, $value)
123: {
124: $this->updating();
125: if (parent::__isset($key)) {
126: parent::__set($key, $value);
127:
128: } else {
129: $this->data[$key] = $value;
130: }
131: }
132:
133:
134:
135: /**
136: * Returns user data value.
137: * @param string property name
138: * @return mixed
139: */
140: public function &__get($key)
141: {
142: if (parent::__isset($key)) {
143: return parent::__get($key);
144:
145: } else {
146: return $this->data[$key];
147: }
148: }
149:
150:
151:
152: /**
153: * Is property defined?
154: * @param string property name
155: * @return bool
156: */
157: public function __isset($key)
158: {
159: return isset($this->data[$key]) || parent::__isset($key);
160: }
161:
162:
163:
164: /**
165: * Removes property.
166: * @param string property name
167: * @return void
168: * @throws Nette\MemberAccessException
169: */
170: public function __unset($name)
171: {
172: ObjectMixin::remove($this, $name);
173: }
174:
175: }
176: