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