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