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