1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16:
17:
18: namespace OpenCloud\Identity\Resource;
19:
20: use OpenCloud\Common\Collection\PaginatedIterator;
21: use OpenCloud\Common\Http\Message\Formatter;
22: use OpenCloud\Common\PersistentObject;
23: use OpenCloud\Rackspace;
24:
25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35:
36: class User extends PersistentObject
37: {
38:
39: private $defaultRegion;
40:
41:
42: private $domainId;
43:
44:
45: private $id;
46:
47:
48: private $username;
49:
50:
51: private $email;
52:
53:
54: private $enabled;
55:
56:
57: private $password;
58:
59: protected $createKeys = array('username', 'email', 'enabled', 'password');
60: protected $updateKeys = array('username', 'email', 'enabled', 'RAX-AUTH:defaultRegion', 'RAX-AUTH:domainId', 'id');
61:
62: protected $aliases = array(
63: 'name' => 'username',
64: 'RAX-AUTH:defaultRegion' => 'defaultRegion',
65: 'RAX-AUTH:domainId' => 'domainId',
66: 'OS-KSADM:password' => 'password'
67: );
68:
69: protected static $url_resource = 'users';
70: protected static $json_name = 'user';
71:
72: public function createJson()
73: {
74: $json = parent::createJson();
75:
76: if ($this->getClient() instanceof Rackspace) {
77: $json->user->username = $json->user->name;
78: unset($json->user->name);
79: }
80:
81: return $json;
82: }
83:
84: 85: 86:
87: public function setDefaultRegion($region)
88: {
89: $this->defaultRegion = $region;
90: }
91:
92: 93: 94:
95: public function getDefaultRegion()
96: {
97: return $this->defaultRegion;
98: }
99:
100: 101: 102:
103: public function setDomainId($domainId)
104: {
105: $this->domainId = $domainId;
106: }
107:
108: 109: 110:
111: public function getDomainId()
112: {
113: return $this->domainId;
114: }
115:
116: 117: 118:
119: public function setId($id)
120: {
121: $this->id = $id;
122: }
123:
124: 125: 126:
127: public function getId()
128: {
129: return $this->id;
130: }
131:
132: 133: 134:
135: public function setUsername($username)
136: {
137: $this->username = $username;
138: }
139:
140: 141: 142:
143: public function getUsername()
144: {
145: return $this->username;
146: }
147:
148: 149: 150:
151: public function setEmail($email)
152: {
153: $this->email = $email;
154: }
155:
156: 157: 158:
159: public function getEmail()
160: {
161: return $this->email;
162: }
163:
164: 165: 166:
167: public function setEnabled($enabled)
168: {
169: $this->enabled = $enabled;
170: }
171:
172: 173: 174:
175: public function getEnabled()
176: {
177: return $this->enabled;
178: }
179:
180: 181: 182:
183: public function isEnabled()
184: {
185: return $this->enabled === true;
186: }
187:
188: 189: 190:
191: public function setPassword($password)
192: {
193: $this->password = $password;
194: }
195:
196: 197: 198:
199: public function getPassword()
200: {
201: return $this->password;
202: }
203:
204: 205: 206:
207: public function primaryKeyField()
208: {
209: return 'id';
210: }
211:
212: public function updateJson($params = array())
213: {
214: $array = array();
215: foreach ($this->updateKeys as $key) {
216: if (isset($this->$key)) {
217: $array[$key] = $this->$key;
218: }
219: }
220:
221: return (object) array('user' => $array);
222: }
223:
224: 225: 226: 227: 228: 229:
230: public function updatePassword($newPassword)
231: {
232: $array = array(
233: 'username' => $this->username,
234: 'OS-KSADM:password' => $newPassword
235: );
236:
237: $json = json_encode((object) array('user' => $array));
238:
239: return $this->getClient()->post($this->getUrl(), self::getJsonHeader(), $json)->send();
240: }
241:
242: 243: 244: 245: 246:
247: public function getOtherCredentials()
248: {
249: $url = $this->getUrl();
250: $url->addPath('OS-KSADM')->addPath('credentials');
251:
252: $response = $this->getClient()->get($url)->send();
253:
254: if ($body = Formatter::decode($response)) {
255: return isset($body->credentials) ? $body->credentials : null;
256: }
257: }
258:
259: 260: 261: 262: 263:
264: public function getApiKey()
265: {
266: $url = $this->getUrl();
267: $url->addPath('OS-KSADM')->addPath('credentials')->addPath('RAX-KSKEY:apiKeyCredentials');
268:
269: $response = $this->getClient()->get($url)->send();
270:
271: if ($body = Formatter::decode($response)) {
272: return isset($body->{'RAX-KSKEY:apiKeyCredentials'}->apiKey)
273: ? $body->{'RAX-KSKEY:apiKeyCredentials'}->apiKey
274: : null;
275: }
276: }
277:
278: 279: 280: 281: 282:
283: public function resetApiKey()
284: {
285: $url = $this->getUrl();
286: $url->addPath('OS-KSADM')
287: ->addPath('credentials')
288: ->addPath('RAX-KSKEY:apiKeyCredentials')
289: ->addPath('RAX-AUTH')
290: ->addPath('reset');
291:
292: $response = $this->getClient()->post($url)->send();
293:
294: if ($body = Formatter::decode($response)) {
295: return isset($body->{'RAX-KSKEY:apiKeyCredentials'}->apiKey)
296: ? $body->{'RAX-KSKEY:apiKeyCredentials'}->apiKey
297: : null;
298: }
299: }
300:
301: 302: 303: 304: 305: 306:
307: public function addRole($roleId)
308: {
309: $url = $this->getUrl();
310: $url->addPath('roles')->addPath('OS-KSADM')->addPath($roleId);
311:
312: return $this->getClient()->put($url)->send();
313: }
314:
315: 316: 317: 318: 319: 320:
321: public function removeRole($roleId)
322: {
323: $url = $this->getUrl();
324: $url->addPath('roles')->addPath('OS-KSADM')->addPath($roleId);
325:
326: return $this->getClient()->delete($url)->send();
327: }
328:
329: 330: 331: 332: 333:
334: public function getRoles()
335: {
336: $url = $this->getUrl();
337: $url->addPath('roles');
338:
339: return PaginatedIterator::factory($this, array(
340: 'baseUrl' => $url,
341: 'resourceClass' => 'Role',
342: 'key.collection' => 'roles',
343: 'key.links' => 'roles_links'
344: ));
345: }
346:
347: public function update($params = array())
348: {
349: if (!empty($params)) {
350: $this->populate($params);
351: }
352:
353: $json = json_encode($this->updateJson($params));
354: $this->checkJsonError();
355:
356: return $this->getClient()->post($this->getUrl(), self::getJsonHeader(), $json)->send();
357: }
358: }
359: