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