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: * Sets the ID of user.
54: * @param mixed
55: * @return self
56: */
57: public function setId($id)
58: {
59: $this->updating();
60: $this->id = is_numeric($id) ? 1 * $id : $id;
61: return $this;
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: * Sets a list of roles that the user is a member of.
77: * @param array
78: * @return self
79: */
80: public function setRoles(array $roles)
81: {
82: $this->updating();
83: $this->roles = $roles;
84: return $this;
85: }
86:
87:
88: /**
89: * Returns a list of roles that the user is a member of.
90: * @return array
91: */
92: public function getRoles()
93: {
94: return $this->roles;
95: }
96:
97:
98: /**
99: * Returns a user data.
100: * @return array
101: */
102: public function getData()
103: {
104: return $this->data;
105: }
106:
107:
108: /**
109: * Sets user data value.
110: * @param string property name
111: * @param mixed property value
112: * @return void
113: */
114: public function __set($key, $value)
115: {
116: $this->updating();
117: if (parent::__isset($key)) {
118: parent::__set($key, $value);
119:
120: } else {
121: $this->data[$key] = $value;
122: }
123: }
124:
125:
126: /**
127: * Returns user data value.
128: * @param string property name
129: * @return mixed
130: */
131: public function &__get($key)
132: {
133: if (parent::__isset($key)) {
134: return parent::__get($key);
135:
136: } else {
137: return $this->data[$key];
138: }
139: }
140:
141:
142: /**
143: * Is property defined?
144: * @param string property name
145: * @return bool
146: */
147: public function __isset($key)
148: {
149: return isset($this->data[$key]) || parent::__isset($key);
150: }
151:
152:
153: /**
154: * Removes property.
155: * @param string property name
156: * @return void
157: * @throws MemberAccessException
158: */
159: public function __unset($name)
160: {
161: ObjectMixin::remove($this, $name);
162: }
163:
164: }
165: