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