      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:
     24: /**
     25:  * User class which encapsulates functionality for a user.
     26:  *
     27:  * A user is a digital representation of a person, system, or service who consumes cloud services. Users have
     28:  * credentials and may be assigned tokens; based on these credentials and tokens, the authentication service validates
     29:  * that incoming requests are being made by the user who claims to be making the request, and that the user has the
     30:  * right to access the requested resources. Users may be directly assigned to a particular tenant and behave as if they
     31:  * are contained within that tenant.
     32:  *
     33:  * @package OpenCloud\Identity\Resource
     34:  */
     35: class User extends PersistentObject
     36: {
     37:     /** @var string The default region for this region. Can be ORD, DFW, IAD, LON, HKG or SYD */
     38:     private $defaultRegion;
     39:
     40:     /** @var string */
     41:     private $domainId;
     42:
     43:     /** @var int The ID of this user */
     44:     private $id;
     45:
     46:     /** @var string The username of this user */
     47:     private $username;
     48:
     49:     /** @var string The email address of this user */
     50:     private $email;
     51:
     52:     /** @var bool Whether or not this user is enabled or not */
     53:     private $enabled;
     54:
     55:     /** @var string The string password for this user */
     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:      * @param $region Set the default region
     73:      */
     74:     public function setDefaultRegion($region)
     75:     {
     76:         $this->defaultRegion = $region;
     77:     }
     78:
     79:     /**
     80:      * @return string Get the default region
     81:      */
     82:     public function getDefaultRegion()
     83:     {
     84:         return $this->defaultRegion;
     85:     }
     86:
     87:     /**
     88:      * @param $domainId Set the domain ID
     89:      */
     90:     public function setDomainId($domainId)
     91:     {
     92:         $this->domainId = $domainId;
     93:     }
     94:
     95:     /**
     96:      * @return string Get the domain ID
     97:      */
     98:     public function getDomainId()
     99:     {
    100:         return $this->domainId;
    101:     }
    102:
    103:     /**
    104:      * @param $id Set the ID
    105:      */
    106:     public function setId($id)
    107:     {
    108:         $this->id = $id;
    109:     }
    110:
    111:     /**
    112:      * @return int Get the ID
    113:      */
    114:     public function getId()
    115:     {
    116:         return $this->id;
    117:     }
    118:
    119:     /**
    120:      * @param $username Set the username
    121:      */
    122:     public function setUsername($username)
    123:     {
    124:         $this->username = $username;
    125:     }
    126:
    127:     /**
    128:      * @return string Get the username
    129:      */
    130:     public function getUsername()
    131:     {
    132:         return $this->username;
    133:     }
    134:
    135:     /**
    136:      * @param $email Sets the email
    137:      */
    138:     public function setEmail($email)
    139:     {
    140:         $this->email = $email;
    141:     }
    142:
    143:     /**
    144:      * @return string Get the email
    145:      */
    146:     public function getEmail()
    147:     {
    148:         return $this->email;
    149:     }
    150:
    151:     /**
    152:      * @param $enabled Sets the enabled flag
    153:      */
    154:     public function setEnabled($enabled)
    155:     {
    156:         $this->enabled = $enabled;
    157:     }
    158:
    159:     /**
    160:      * @return bool Get the enabled flag
    161:      */
    162:     public function getEnabled()
    163:     {
    164:         return $this->enabled;
    165:     }
    166:
    167:     /**
    168:      * @return bool Check whether this user is enabled or not
    169:      */
    170:     public function isEnabled()
    171:     {
    172:         return $this->enabled === true;
    173:     }
    174:
    175:     /**
    176:      * @param $password Set the password
    177:      */
    178:     public function setPassword($password)
    179:     {
    180:         $this->password = $password;
    181:     }
    182:
    183:     /**
    184:      * @return string Get the password
    185:      */
    186:     public function getPassword()
    187:     {
    188:         return $this->password;
    189:     }
    190:
    191:     /**
    192:      * @return string
    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:      * This operation will set the user's password to a new value.
    213:      *
    214:      * @param $newPassword The new password to use for this user
    215:      * @return \Guzzle\Http\Message\Response
    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:      * This operation lists a user's non-password credentials for all authentication methods available to the user.
    231:      *
    232:      * @return array|null
    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:      * Get the API key for this user.
    248:      *
    249:      * @return string|null
    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:      * Reset the API key for this user to a new arbitrary value (which is returned).
    267:      *
    268:      * @return string|null
    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:      * Add a role, specified by its ID, to a user.
    290:      *
    291:      * @param $roleId
    292:      * @return \Guzzle\Http\Message\Response
    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:      * Remove a role, specified by its ID, from a user.
    304:      *
    305:      * @param $roleId
    306:      * @return \Guzzle\Http\Message\Response
    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:      * Get all the roles for which this user is associated with.
    318:      *
    319:      * @return \OpenCloud\Common\Collection\PaginatedIterator
    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: