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