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