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