Overview

Namespaces

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

Classes

  • Database
  • Instance
  • User
  • Overview
  • Namespace
  • Class
  • Tree
  • Download
  1: <?php
  2: /**
  3:  * PHP OpenCloud library.
  4:  * 
  5:  * @copyright 2013 Rackspace Hosting, Inc. See LICENSE for information.
  6:  * @license   https://www.apache.org/licenses/LICENSE-2.0
  7:  * @author    Glen Campbell <glen.campbell@rackspace.com>
  8:  * @author    Jamie Hannaford <jamie.hannaford@rackspace.com>
  9:  */
 10: 
 11: namespace OpenCloud\Database\Resource;
 12: 
 13: use OpenCloud\Common\PersistentObject;
 14: use OpenCloud\Common\Exceptions;
 15: use OpenCloud\Common\Lang;
 16: 
 17: /**
 18:  * This class represents a User in the Rackspace "Red Dwarf"
 19:  * database-as-a-service product.
 20:  */
 21: class User extends PersistentObject
 22: {
 23: 
 24:     /**
 25:      * @var string $name the user name
 26:      * @var string $password the user's password
 27:      * @var array $databases a list of database names assigned to the user
 28:      */
 29:     public $name;
 30:     public $password;
 31:     public $databases = array();
 32:     
 33:     protected static $json_name = 'user';
 34:     protected static $url_resource = 'users';
 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 primaryKeyField()
 93:     {
 94:         return 'name';
 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 update($params = array())
113:     {
114:         return $this->noUpdate();
115:     }
116: 
117:     /**
118:      * Deletes a database user
119:      *
120:      * @api
121:      * @return \OpenCloud\HttpResponse
122:      * @throws UserDeleteError if HTTP response is not Success
123:      */
124:     public function delete()
125:     {
126:         return $this->getClient()->delete($this->url())->send();
127:     }
128: 
129:     /**
130:      * {@inheritDoc}
131:      */
132:     protected function createJson()
133:     {        
134:         $user = (object) array(
135:             'name'      => $this->name,
136:             'password'  => $this->password,
137:             'databases' => array()
138:         );
139:         
140:         foreach ($this->databases as $dbName) {
141:             $user->databases[] = (object) array('name' => $dbName);
142:         }
143: 
144:         return (object) array(
145:             'users' => array($user)
146:         );
147:     }
148: }
149: 
PHP OpenCloud API API documentation generated by ApiGen 2.8.0