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