Overview
  • Namespace
  • Class

Namespaces

  • OpenCloud
    • Autoscale
      • Resource
    • CloudMonitoring
      • Collection
      • Exception
      • Resource
    • Common
      • Collection
      • Constants
      • Exceptions
      • Http
        • Message
      • Log
      • Resource
      • Service
    • Compute
      • Constants
      • Exception
      • Resource
    • Database
      • Resource
    • DNS
      • Collection
      • Resource
    • Identity
      • Constants
      • Resource
    • Image
      • Enum
      • Resource
        • JsonPatch
        • Schema
    • LoadBalancer
      • Collection
      • Enum
      • Resource
    • Networking
      • Resource
    • ObjectStore
      • Constants
      • Exception
      • Resource
      • Upload
    • Orchestration
      • Resource
    • Queues
      • Collection
      • Exception
      • Resource
    • Volume
      • Resource

Classes

  • OpenCloud\Volume\Resource\Snapshot
  • OpenCloud\Volume\Resource\Volume
  • OpenCloud\Volume\Resource\VolumeType
  1 <?php
  2 /**
  3  * Copyright 2012-2014 Rackspace US, Inc.
  4  *
  5  * Licensed under the Apache License, Version 2.0 (the "License");
  6  * you may not use this file except in compliance with the License.
  7  * You may obtain a copy of the License at
  8  *
  9  * http://www.apache.org/licenses/LICENSE-2.0
 10  *
 11  * Unless required by applicable law or agreed to in writing, software
 12  * distributed under the License is distributed on an "AS IS" BASIS,
 13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  * See the License for the specific language governing permissions and
 15  * limitations under the License.
 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  * User class which encapsulates functionality for a user.
 27  *
 28  * A user is a digital representation of a person, system, or service who consumes cloud services. Users have
 29  * credentials and may be assigned tokens; based on these credentials and tokens, the authentication service validates
 30  * that incoming requests are being made by the user who claims to be making the request, and that the user has the
 31  * right to access the requested resources. Users may be directly assigned to a particular tenant and behave as if they
 32  * are contained within that tenant.
 33  *
 34  * @package OpenCloud\Identity\Resource
 35  */
 36 class User extends PersistentObject
 37 {
 38     /** @var string The default region for this region. Can be ORD, DFW, IAD, LON, HKG or SYD */
 39     private $defaultRegion;
 40 
 41     /** @var string */
 42     private $domainId;
 43 
 44     /** @var int The ID of this user */
 45     private $id;
 46 
 47     /** @var string The username of this user */
 48     private $username;
 49 
 50     /** @var string The email address of this user */
 51     private $email;
 52 
 53     /** @var bool Whether or not this user is enabled or not */
 54     private $enabled;
 55 
 56     /** @var string The string password for this user */
 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      * @param $region Set the default region
 86      */
 87     public function setDefaultRegion($region)
 88     {
 89         $this->defaultRegion = $region;
 90     }
 91 
 92     /**
 93      * @return string Get the default region
 94      */
 95     public function getDefaultRegion()
 96     {
 97         return $this->defaultRegion;
 98     }
 99 
100     /**
101      * @param $domainId Set the domain ID
102      */
103     public function setDomainId($domainId)
104     {
105         $this->domainId = $domainId;
106     }
107 
108     /**
109      * @return string Get the domain ID
110      */
111     public function getDomainId()
112     {
113         return $this->domainId;
114     }
115 
116     /**
117      * @param $id Set the ID
118      */
119     public function setId($id)
120     {
121         $this->id = $id;
122     }
123 
124     /**
125      * @return int Get the ID
126      */
127     public function getId()
128     {
129         return $this->id;
130     }
131 
132     /**
133      * @param $username Set the username
134      */
135     public function setUsername($username)
136     {
137         $this->username = $username;
138     }
139 
140     /**
141      * @return string Get the username
142      */
143     public function getUsername()
144     {
145         return $this->username;
146     }
147 
148     /**
149      * @param $email Sets the email
150      */
151     public function setEmail($email)
152     {
153         $this->email = $email;
154     }
155 
156     /**
157      * @return string Get the email
158      */
159     public function getEmail()
160     {
161         return $this->email;
162     }
163 
164     /**
165      * @param $enabled Sets the enabled flag
166      */
167     public function setEnabled($enabled)
168     {
169         $this->enabled = $enabled;
170     }
171 
172     /**
173      * @return bool Get the enabled flag
174      */
175     public function getEnabled()
176     {
177         return $this->enabled;
178     }
179 
180     /**
181      * @return bool Check whether this user is enabled or not
182      */
183     public function isEnabled()
184     {
185         return $this->enabled === true;
186     }
187 
188     /**
189      * @param $password Set the password
190      */
191     public function setPassword($password)
192     {
193         $this->password = $password;
194     }
195 
196     /**
197      * @return string Get the password
198      */
199     public function getPassword()
200     {
201         return $this->password;
202     }
203 
204     /**
205      * @return string
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      * This operation will set the user's password to a new value.
226      *
227      * @param $newPassword The new password to use for this user
228      * @return \Guzzle\Http\Message\Response
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      * This operation lists a user's non-password credentials for all authentication methods available to the user.
244      *
245      * @return array|null
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      * Get the API key for this user.
261      *
262      * @return string|null
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      * Reset the API key for this user to a new arbitrary value (which is returned).
280      *
281      * @return string|null
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      * Add a role, specified by its ID, to a user.
303      *
304      * @param $roleId
305      * @return \Guzzle\Http\Message\Response
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      * Remove a role, specified by its ID, from a user.
317      *
318      * @param $roleId
319      * @return \Guzzle\Http\Message\Response
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      * Get all the roles for which this user is associated with.
331      *
332      * @return \OpenCloud\Common\Collection\PaginatedIterator
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 
API documentation generated by ApiGen