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\Database\Resource;
19:
20: use OpenCloud\Common\Exceptions;
21: use OpenCloud\Common\Lang;
22: use OpenCloud\Common\Resource\PersistentResource;
23:
24: /**
25: * This class represents a User in the Rackspace "Red Dwarf"
26: * database-as-a-service product.
27: */
28: class User extends PersistentResource
29: {
30: /** @var string The user name */
31: public $name;
32:
33: /** @var string The user's password */
34: public $password;
35:
36: /** @var array A list of database names assigned to the user */
37: public $databases = array();
38:
39: protected static $json_name = 'user';
40: protected static $url_resource = 'users';
41:
42: /**
43: * Creates a new database object
44: *
45: * Unlike other objects (Servers, DataObjects, etc.), passing a database
46: * name to the constructor does *not* pull information from the database.
47: * For example, if you pass an ID to the `Server()` constructor, it will
48: * attempt to retrieve the information on that server from the service,
49: * and will return an error if it is not found. However, the Cloud
50: * Users service does not permit retrieval of information on
51: * individual databases (only via Collection), and thus passing in a
52: * name via the `$info` parameter only creates an in-memory object that
53: * is not necessarily tied to an actual database.
54: *
55: * @param Instance $instance the parent DbService\Instance of the database
56: * @param mixed $info if an array or object, treated as properties to set;
57: * if a string, treated as the database name
58: * @param array $db a list of database names to associate with the User
59: * @return void
60: * @throws UserNameError if `$info` is not a string, object, or array
61: */
62: public function __construct(Instance $instance, $info = null, $db = array())
63: {
64: $this->setParent($instance);
65:
66: if (!empty($db)) {
67: $this->databases = $db;
68: }
69:
70: // Lazy...
71: if (is_string($info)) {
72: $info = array('name' => $info);
73: }
74:
75: return parent::__construct($instance->getService(), $info);
76: }
77:
78: /**
79: * Returns name of this user. Because it's so important (i.e. as an
80: * identifier), it will throw an error if not set/empty.
81: *
82: * @return type
83: * @throws Exceptions\DatabaseNameError
84: */
85: public function getName()
86: {
87: if (empty($this->name)) {
88: throw new Exceptions\DatabaseNameError(
89: Lang::translate('This user does not have a name yet')
90: );
91: }
92:
93: return $this->name;
94: }
95:
96: /**
97: * {@inheritDoc}
98: */
99: public function primaryKeyField()
100: {
101: return 'name';
102: }
103:
104: /**
105: * Adds a new database to the list of databases for the user
106: *
107: * @api
108: * @param string $dbname the database name to be added
109: * @return void
110: */
111: public function addDatabase($dbname)
112: {
113: $this->databases[] = $dbname;
114: }
115:
116: /**
117: * {@inheritDoc}
118: */
119: public function update($params = array())
120: {
121: return $this->noUpdate();
122: }
123:
124: /**
125: * Deletes a database user
126: *
127: * @api
128: * @return \OpenCloud\HttpResponse
129: * @throws UserDeleteError if HTTP response is not Success
130: */
131: public function delete()
132: {
133: return $this->getClient()->delete($this->url())->send();
134: }
135:
136: /**
137: * {@inheritDoc}
138: */
139: protected function createJson()
140: {
141: $user = (object) array(
142: 'name' => $this->name,
143: 'password' => $this->password,
144: 'databases' => array()
145: );
146:
147: foreach ($this->databases as $dbName) {
148: $user->databases[] = (object) array('name' => $dbName);
149: }
150:
151: return (object) array(
152: 'users' => array($user)
153: );
154: }
155: }
156: