Overview

Namespaces

  • None
  • OpenCloud
    • Autoscale
      • Resource
    • CloudMonitoring
      • Exception
      • Resource
    • Common
      • Exceptions
      • Log
      • Request
        • Response
    • Compute
    • Database
    • DNS
    • LoadBalancer
      • Resources
    • ObjectStore
      • Resource
    • Orchestration
    • Volume
  • PHP

Classes

  • Database
  • Instance
  • Service
  • User
  • Overview
  • Namespace
  • Class
  • Tree
  • Download
  1: <?php
  2: /**
  3:  * PHP OpenCloud library.
  4:  * 
  5:  * @copyright Copyright 2013 Rackspace US, Inc. See COPYING for licensing information.
  6:  * @license   https://www.apache.org/licenses/LICENSE-2.0 Apache 2.0
  7:  * @version   1.6.0
  8:  * @author    Glen Campbell <glen.campbell@rackspace.com>
  9:  * @author    Jamie Hannaford <jamie.hannaford@rackspace.com>
 10:  */
 11: 
 12: namespace OpenCloud\Database;
 13: 
 14: use OpenCloud\Common\PersistentObject;
 15: use OpenCloud\Common\Exceptions;
 16: use OpenCloud\Common\Lang;
 17: 
 18: /**
 19:  * This class represents a User in the Rackspace "Red Dwarf"
 20:  * database-as-a-service product.
 21:  */
 22: class User extends PersistentObject
 23: {
 24: 
 25:     /**
 26:      * @var string $name the user name
 27:      * @var string $password the user's password
 28:      * @var array $databases a list of database names assigned to the user
 29:      */
 30:     public $name;
 31:     public $password;
 32:     public $databases = array();
 33:     
 34:     protected static $json_name = 'user';
 35: 
 36:     /**
 37:      * Creates a new database object
 38:      *
 39:      * Unlike other objects (Servers, DataObjects, etc.), passing a database
 40:      * name to the constructor does *not* pull information from the database.
 41:      * For example, if you pass an ID to the `Server()` constructor, it will
 42:      * attempt to retrieve the information on that server from the service,
 43:      * and will return an error if it is not found. However, the Cloud
 44:      * Users service does not permit retrieval of information on
 45:      * individual databases (only via Collection), and thus passing in a
 46:      * name via the `$info` parameter only creates an in-memory object that
 47:      * is not necessarily tied to an actual database.
 48:      *
 49:      * @param Instance $instance the parent DbService\Instance of the database
 50:      * @param mixed $info if an array or object, treated as properties to set;
 51:      *      if a string, treated as the database name
 52:      * @param array $db a list of database names to associate with the User
 53:      * @return void
 54:      * @throws UserNameError if `$info` is not a string, object, or array
 55:      */
 56:     public function __construct(Instance $instance, $info = null, $db = array())
 57:     {
 58:         $this->setParent($instance);
 59: 
 60:         if (!empty($db)) {
 61:             $this->databases = $db;
 62:         }
 63:         
 64:         // Lazy...
 65:         if (is_string($info)) {
 66:             $info = array('name' => $info);
 67:         }
 68: 
 69:         return parent::__construct($instance->getService(), $info);
 70:     }
 71:     
 72:     /**
 73:      * Returns name of this user. Because it's so important (i.e. as an
 74:      * identifier), it will throw an error if not set/empty.
 75:      * 
 76:      * @return type
 77:      * @throws Exceptions\DatabaseNameError
 78:      */
 79:     public function getName()
 80:     {
 81:         if (empty($this->name)) {
 82:             throw new Exceptions\DatabaseNameError(
 83:                 Lang::translate('This user does not have a name yet')
 84:             );
 85:         }
 86:         return $this->name;
 87:     }
 88:     
 89:     /**
 90:      * {@inheritDoc}
 91:      */
 92:     public function url($subresource = '', $params = array())
 93:     {
 94:         return stripslashes($this->getParent()->url('users')) . '/' . $this->getName();
 95:     }
 96: 
 97:     /**
 98:      * Adds a new database to the list of databases for the user
 99:      *
100:      * @api
101:      * @param string $dbname the database name to be added
102:      * @return void
103:      */
104:     public function addDatabase($dbname)
105:     {
106:         $this->databases[] = $dbname;
107:     }
108:     
109:     /**
110:      * {@inheritDoc}
111:      */
112:     public function createUrl()
113:     {
114:         return $this->getParent()->url('users');
115:     }
116:     
117:     /**
118:      * {@inheritDoc}
119:      */
120:     public function update($params = array())
121:     {
122:         return $this->noUpdate();
123:     }
124: 
125:     /**
126:      * Deletes a database user
127:      *
128:      * @api
129:      * @return \OpenCloud\HttpResponse
130:      * @throws UserDeleteError if HTTP response is not Success
131:      */
132:     public function delete()
133:     {
134:         $response = $this->getParent()->getService()->request($this->url(), 'DELETE');
135: 
136:         // @codeCoverageIgnoreStart
137:         if ($response->HttpStatus() > 202) {
138:             throw new Exceptions\UserDeleteError(sprintf(
139:                 Lang::translate('Error deleting user [%s], status [%d] response [%s]',
140:                 $this->name,
141:                 $response->HttpStatus(),
142:                 $response->HttpBody())
143:             ));
144:         }
145:         // @codeCoverageIgnoreEnd
146: 
147:         return $response;
148:     }
149: 
150:     /**
151:      * {@inheritDoc}
152:      */
153:     protected function createJson()
154:     {        
155:         $user = (object) array(
156:             'name'      => $this->name,
157:             'password'  => $this->password,
158:             'databases' => array()
159:         );
160:         
161:         foreach ($this->databases as $dbName) {
162:             $user->databases[] = (object) array('name' => $dbName);
163:         }
164: 
165:         return (object) array(
166:             'users' => array($user)
167:         );
168:     }
169: }
170: 
PHP OpenCloud API API documentation generated by ApiGen 2.8.0