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