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