1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16:
17:
18: namespace OpenCloud\Identity;
19:
20: use Guzzle\Http\ClientInterface;
21: use OpenCloud\Common\Base;
22: use OpenCloud\Common\Collection\PaginatedIterator;
23: use OpenCloud\Common\Collection\ResourceIterator;
24: use OpenCloud\Common\Http\Message\Formatter;
25: use OpenCloud\Common\Service\AbstractService;
26: use OpenCloud\Identity\Constants\User as UserConst;
27: use OpenCloud\OpenStack;
28:
29: 30: 31: 32: 33:
34: class Service extends AbstractService
35: {
36: 37: 38: 39: 40: 41:
42: public static function factory(ClientInterface $client)
43: {
44: $identity = new self();
45:
46: if (($client instanceof Base || $client instanceof OpenStack) && $client->hasLogger()) {
47: $identity->setLogger($client->getLogger());
48: }
49:
50: $identity->setClient($client);
51: $identity->setEndpoint(clone $client->getAuthUrl());
52:
53: return $identity;
54: }
55:
56: 57: 58: 59: 60:
61: public function getUrl($path = null)
62: {
63: $url = clone $this->getEndpoint();
64:
65: if ($path) {
66: $url->addPath($path);
67: }
68:
69: return $url;
70: }
71:
72: 73: 74: 75: 76:
77: public function getUsers()
78: {
79: $response = $this->getClient()->get($this->getUrl('users'))->send();
80:
81: if ($body = Formatter::decode($response)) {
82: return ResourceIterator::factory($this, array(
83: 'resourceClass' => 'User',
84: 'key.collection' => 'users'
85: ), $body->users);
86: }
87: }
88:
89: 90: 91:
92: public function user($info = null)
93: {
94: return $this->resource('User', $info);
95: }
96:
97: 98: 99: 100: 101: 102: 103:
104: public function getUser($search, $mode = UserConst::MODE_NAME)
105: {
106: $url = $this->getUrl('users');
107:
108: switch ($mode) {
109: default:
110: case UserConst::MODE_NAME:
111: $url->setQuery(array('name' => $search));
112: break;
113: case UserConst::MODE_ID:
114: $url->addPath($search);
115: break;
116: case UserConst::MODE_EMAIL:
117: $url->setQuery(array('email' => $search));
118: break;
119: }
120:
121: $user = $this->resource('User');
122: $user->refreshFromLocationUrl($url);
123:
124: return $user;
125: }
126:
127: 128: 129: 130: 131: 132:
133: public function createUser(array $params)
134: {
135: $user = $this->resource('User');
136: $user->create($params);
137:
138: return $user;
139: }
140:
141: 142: 143: 144: 145:
146: public function getRoles()
147: {
148: return PaginatedIterator::factory($this, array(
149: 'resourceClass' => 'Role',
150: 'baseUrl' => $this->getUrl()->addPath('OS-KSADM')->addPath('roles'),
151: 'key.marker' => 'id',
152: 'key.collection' => 'roles'
153: ));
154: }
155:
156: 157: 158: 159: 160: 161:
162: public function getRole($roleId)
163: {
164: return $this->resource('Role', $roleId);
165: }
166:
167: 168: 169: 170: 171: 172: 173:
174: public function generateToken($json, array $headers = array())
175: {
176: $url = $this->getUrl();
177: $url->addPath('tokens');
178:
179: $headers += self::getJsonHeader();
180:
181: return $this->getClient()->post($url, $headers, $json)->send();
182: }
183:
184: 185: 186: 187: 188: 189:
190: public function revokeToken($tokenId)
191: {
192: $token = $this->resource('Token');
193: $token->setId($tokenId);
194:
195: return $token->delete();
196: }
197:
198: 199: 200: 201: 202:
203: public function getTenants()
204: {
205: $url = $this->getUrl();
206: $url->addPath('tenants');
207:
208: $response = $this->getClient()->get($url)->send();
209:
210: if ($body = Formatter::decode($response)) {
211: return ResourceIterator::factory($this, array(
212: 'resourceClass' => 'Tenant',
213: 'key.collection' => 'tenants'
214: ), $body->tenants);
215: }
216: }
217: }
218: