8 * This source file is subject to the new BSD license that is bundled
9 * with this package in the file LICENSE.txt.
10 * It is also available through the world-wide-web at this URL:
11 * http://framework.zend.com/license/new-bsd
12 * If you did not receive a copy of the license and are unable to
13 * obtain it through the world-wide-web, please send an email
14 * to license@zend.com so we can send you a copy immediately.
19 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
20 * @license http://framework.zend.com/license/new-bsd New BSD License
26 require_once 'Zend/Gdata.php';
29 * @see Zend_Gdata_Gapps_UserFeed
31 require_once 'Zend/Gdata/Gapps/UserFeed.php';
34 * @see Zend_Gdata_Gapps_NicknameFeed
36 require_once 'Zend/Gdata/Gapps/NicknameFeed.php';
39 * @see Zend_Gdata_Gapps_EmailListFeed
41 require_once 'Zend/Gdata/Gapps/EmailListFeed.php';
44 * @see Zend_Gdata_Gapps_EmailListRecipientFeed
46 require_once 'Zend/Gdata/Gapps/EmailListRecipientFeed.php';
50 * Service class for interacting with the Google Apps Provisioning API.
52 * Like other service classes in this module, this class provides access via
53 * an HTTP client to Google servers for working with entries and feeds.
55 * Because of the nature of this API, all access must occur over an
56 * authenticated connection.
58 * @link http://code.google.com/apis/apps/gdata_provisioning_api_v2.0_reference.html
63 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
64 * @license http://framework.zend.com/license/new-bsd New BSD License
66 class Zend_Gdata_Gapps
extends Zend_Gdata
69 const APPS_BASE_FEED_URI
= 'https://apps-apis.google.com/a/feeds';
70 const AUTH_SERVICE_NAME
= 'apps';
73 * Path to user feeds on the Google Apps server.
75 const APPS_USER_PATH
= '/user/2.0';
78 * Path to nickname feeds on the Google Apps server.
80 const APPS_NICKNAME_PATH
= '/nickname/2.0';
83 * Path to email list feeds on the Google Apps server.
85 const APPS_EMAIL_LIST_PATH
= '/emailList/2.0';
88 * Path to email list recipient feeds on the Google Apps server.
90 const APPS_EMAIL_LIST_RECIPIENT_POSTFIX
= '/recipient';
93 * The domain which is being administered via the Provisioning API.
97 protected $_domain = null;
100 * Namespaces used for Zend_Gdata_Gapps
104 public static $namespaces = array(
105 array('apps', 'http://schemas.google.com/apps/2006', 1, 0)
109 * Create Gdata_Gapps object
111 * @param Zend_Http_Client $client (optional) The HTTP client to use when
112 * when communicating with the Google Apps servers.
113 * @param string $domain (optional) The Google Apps domain which is to be
115 * @param string $applicationId The identity of the app in the form of Company-AppName-Version
117 public function __construct($client = null, $domain = null, $applicationId = 'MyCompany-MyApp-1.0')
119 $this->registerPackage('Zend_Gdata_Gapps');
120 $this->registerPackage('Zend_Gdata_Gapps_Extension');
121 parent
::__construct($client, $applicationId);
122 $this->_httpClient
->setParameterPost('service', self
::AUTH_SERVICE_NAME
);
123 $this->_domain
= $domain;
127 * Convert an exception to an ServiceException if an AppsForYourDomain
128 * XML document is contained within the original exception's HTTP
129 * response. If conversion fails, throw the original error.
131 * @param Zend_Gdata_Exception $e The exception to convert.
132 * @throws Zend_Gdata_Gapps_ServiceException
135 public static function throwServiceExceptionIfDetected($e) {
136 // Check to make sure that there actually response!
137 // This can happen if the connection dies before the request
138 // completes. (See ZF-5949)
139 $response = $e->getResponse();
141 require_once('Zend/Gdata/App/IOException.php');
142 throw new Zend_Gdata_App_IOException('No HTTP response received (possible connection failure)');
146 // Check to see if there is an AppsForYourDomainErrors
147 // datastructure in the response. If so, convert it to
148 // an exception and throw it.
149 require_once 'Zend/Gdata/Gapps/ServiceException.php';
150 $error = new Zend_Gdata_Gapps_ServiceException();
151 $error->importFromString($response->getBody());
153 } catch (Zend_Gdata_App_Exception
$e2) {
154 // Unable to convert the response to a ServiceException,
155 // most likely because the server didn't return an
156 // AppsForYourDomainErrors document. Throw the original
163 * Imports a feed located at $uri.
164 * This method overrides the default behavior of Zend_Gdata_App,
165 * providing support for Zend_Gdata_Gapps_ServiceException.
168 * @param Zend_Http_Client $client (optional) The client used for
170 * @param string $className (optional) The class which is used as the
172 * @throws Zend_Gdata_App_Exception
173 * @throws Zend_Gdata_App_HttpException
174 * @throws Zend_Gdata_Gapps_ServiceException
175 * @return Zend_Gdata_App_Feed
177 public static function import($uri, $client = null, $className='Zend_Gdata_App_Feed')
180 return parent
::import($uri, $client, $className);
181 } catch (Zend_Gdata_App_HttpException
$e) {
182 self
::throwServiceExceptionIfDetected($e);
187 * GET a URI using client object.
188 * This method overrides the default behavior of Zend_Gdata_App,
189 * providing support for Zend_Gdata_Gapps_ServiceException.
191 * @param string $uri GET URI
192 * @param array $extraHeaders Extra headers to add to the request, as an
193 * array of string-based key/value pairs.
194 * @throws Zend_Gdata_App_HttpException
195 * @throws Zend_Gdata_Gapps_ServiceException
196 * @return Zend_Http_Response
198 public function get($uri, $extraHeaders = array())
201 return parent
::get($uri, $extraHeaders);
202 } catch (Zend_Gdata_App_HttpException
$e) {
203 self
::throwServiceExceptionIfDetected($e);
208 * POST data with client object.
209 * This method overrides the default behavior of Zend_Gdata_App,
210 * providing support for Zend_Gdata_Gapps_ServiceException.
212 * @param mixed $data The Zend_Gdata_App_Entry or XML to post
213 * @param string $uri (optional) POST URI
214 * @param integer $remainingRedirects (optional)
215 * @param string $contentType Content-type of the data
216 * @param array $extraHaders Extra headers to add tot he request
217 * @return Zend_Http_Response
218 * @throws Zend_Gdata_App_HttpException
219 * @throws Zend_Gdata_App_InvalidArgumentException
220 * @throws Zend_Gdata_Gapps_ServiceException
222 public function post($data, $uri = null, $remainingRedirects = null,
223 $contentType = null, $extraHeaders = null)
226 return parent
::post($data, $uri, $remainingRedirects, $contentType, $extraHeaders);
227 } catch (Zend_Gdata_App_HttpException
$e) {
228 self
::throwServiceExceptionIfDetected($e);
233 * PUT data with client object
234 * This method overrides the default behavior of Zend_Gdata_App,
235 * providing support for Zend_Gdata_Gapps_ServiceException.
237 * @param mixed $data The Zend_Gdata_App_Entry or XML to post
238 * @param string $uri (optional) PUT URI
239 * @param integer $remainingRedirects (optional)
240 * @param string $contentType Content-type of the data
241 * @param array $extraHaders Extra headers to add tot he request
242 * @return Zend_Http_Response
243 * @throws Zend_Gdata_App_HttpException
244 * @throws Zend_Gdata_App_InvalidArgumentException
245 * @throws Zend_Gdata_Gapps_ServiceException
247 public function put($data, $uri = null, $remainingRedirects = null,
248 $contentType = null, $extraHeaders = null)
251 return parent
::put($data, $uri, $remainingRedirects, $contentType, $extraHeaders);
252 } catch (Zend_Gdata_App_HttpException
$e) {
253 self
::throwServiceExceptionIfDetected($e);
258 * DELETE entry with client object
259 * This method overrides the default behavior of Zend_Gdata_App,
260 * providing support for Zend_Gdata_Gapps_ServiceException.
262 * @param mixed $data The Zend_Gdata_App_Entry or URL to delete
263 * @param integer $remainingRedirects (optional)
265 * @throws Zend_Gdata_App_HttpException
266 * @throws Zend_Gdata_App_InvalidArgumentException
267 * @throws Zend_Gdata_Gapps_ServiceException
269 public function delete($data, $remainingRedirects = null)
272 return parent
::delete($data, $remainingRedirects);
273 } catch (Zend_Gdata_App_HttpException
$e) {
274 self
::throwServiceExceptionIfDetected($e);
279 * Set domain for this service instance. This should be a fully qualified
280 * domain, such as 'foo.example.com'.
282 * This value is used when calculating URLs for retrieving and posting
283 * entries. If no value is specified, a URL will have to be manually
284 * constructed prior to using any methods which interact with the Google
285 * Apps provisioning service.
287 * @param string $value The domain to be used for this session.
289 public function setDomain($value)
291 $this->_domain
= $value;
295 * Get domain for this service instance. This should be a fully qualified
296 * domain, such as 'foo.example.com'. If no domain is set, null will be
299 * @return string The domain to be used for this session, or null if not
302 public function getDomain()
304 return $this->_domain
;
308 * Returns the base URL used to access the Google Apps service, based
309 * on the current domain. The current domain can be temporarily
310 * overridden by providing a fully qualified domain as $domain.
312 * @param string $domain (optional) A fully-qualified domain to use
313 * instead of the default domain for this service instance.
314 * @throws Zend_Gdata_App_InvalidArgumentException
316 public function getBaseUrl($domain = null)
318 if ($domain !== null) {
319 return self
::APPS_BASE_FEED_URI
. '/' . $domain;
320 } else if ($this->_domain
!== null) {
321 return self
::APPS_BASE_FEED_URI
. '/' . $this->_domain
;
323 require_once 'Zend/Gdata/App/InvalidArgumentException.php';
324 throw new Zend_Gdata_App_InvalidArgumentException(
325 'Domain must be specified.');
330 * Retrieve a UserFeed containing multiple UserEntry objects.
332 * @param mixed $location (optional) The location for the feed, as a URL
334 * @return Zend_Gdata_Gapps_UserFeed
335 * @throws Zend_Gdata_App_Exception
336 * @throws Zend_Gdata_App_HttpException
337 * @throws Zend_Gdata_Gapps_ServiceException
339 public function getUserFeed($location = null)
341 if ($location === null) {
342 $uri = $this->getBaseUrl() . self
::APPS_USER_PATH
;
343 } else if ($location instanceof Zend_Gdata_Query
) {
344 $uri = $location->getQueryUrl();
348 return parent
::getFeed($uri, 'Zend_Gdata_Gapps_UserFeed');
352 * Retreive NicknameFeed object containing multiple NicknameEntry objects.
354 * @param mixed $location (optional) The location for the feed, as a URL
356 * @return Zend_Gdata_Gapps_NicknameFeed
357 * @throws Zend_Gdata_App_Exception
358 * @throws Zend_Gdata_App_HttpException
359 * @throws Zend_Gdata_Gapps_ServiceException
361 public function getNicknameFeed($location = null)
363 if ($location === null) {
364 $uri = $this->getBaseUrl() . self
::APPS_NICKNAME_PATH
;
365 } else if ($location instanceof Zend_Gdata_Query
) {
366 $uri = $location->getQueryUrl();
370 return parent
::getFeed($uri, 'Zend_Gdata_Gapps_NicknameFeed');
374 * Retreive EmailListFeed object containing multiple EmailListEntry
377 * @param mixed $location (optional) The location for the feed, as a URL
379 * @return Zend_Gdata_Gapps_EmailListFeed
380 * @throws Zend_Gdata_App_Exception
381 * @throws Zend_Gdata_App_HttpException
382 * @throws Zend_Gdata_Gapps_ServiceException
384 public function getEmailListFeed($location = null)
386 if ($location === null) {
387 $uri = $this->getBaseUrl() . self
::APPS_NICKNAME_PATH
;
388 } else if ($location instanceof Zend_Gdata_Query
) {
389 $uri = $location->getQueryUrl();
393 return parent
::getFeed($uri, 'Zend_Gdata_Gapps_EmailListFeed');
397 * Retreive EmailListRecipientFeed object containing multiple
398 * EmailListRecipientEntry objects.
400 * @param mixed $location The location for the feed, as a URL or Query.
401 * @return Zend_Gdata_Gapps_EmailListRecipientFeed
402 * @throws Zend_Gdata_App_Exception
403 * @throws Zend_Gdata_App_HttpException
404 * @throws Zend_Gdata_Gapps_ServiceException
406 public function getEmailListRecipientFeed($location)
408 if ($location === null) {
409 require_once 'Zend/Gdata/App/InvalidArgumentException.php';
410 throw new Zend_Gdata_App_InvalidArgumentException(
411 'Location must not be null');
412 } else if ($location instanceof Zend_Gdata_Query
) {
413 $uri = $location->getQueryUrl();
417 return parent
::getFeed($uri, 'Zend_Gdata_Gapps_EmailListRecipientFeed');
421 * Retreive a single UserEntry object.
423 * @param mixed $location The location for the feed, as a URL or Query.
424 * @return Zend_Gdata_Gapps_UserEntry
425 * @throws Zend_Gdata_App_Exception
426 * @throws Zend_Gdata_App_HttpException
427 * @throws Zend_Gdata_Gapps_ServiceException
429 public function getUserEntry($location)
431 if ($location === null) {
432 require_once 'Zend/Gdata/App/InvalidArgumentException.php';
433 throw new Zend_Gdata_App_InvalidArgumentException(
434 'Location must not be null');
435 } else if ($location instanceof Zend_Gdata_Query
) {
436 $uri = $location->getQueryUrl();
440 return parent
::getEntry($uri, 'Zend_Gdata_Gapps_UserEntry');
444 * Retreive a single NicknameEntry object.
446 * @param mixed $location The location for the feed, as a URL or Query.
447 * @return Zend_Gdata_Gapps_NicknameEntry
448 * @throws Zend_Gdata_App_Exception
449 * @throws Zend_Gdata_App_HttpException
450 * @throws Zend_Gdata_Gapps_ServiceException
452 public function getNicknameEntry($location)
454 if ($location === null) {
455 require_once 'Zend/Gdata/App/InvalidArgumentException.php';
456 throw new Zend_Gdata_App_InvalidArgumentException(
457 'Location must not be null');
458 } else if ($location instanceof Zend_Gdata_Query
) {
459 $uri = $location->getQueryUrl();
463 return parent
::getEntry($uri, 'Zend_Gdata_Gapps_NicknameEntry');
467 * Retreive a single EmailListEntry object.
469 * @param mixed $location The location for the feed, as a URL or Query.
470 * @return Zend_Gdata_Gapps_EmailListEntry
471 * @throws Zend_Gdata_App_Exception
472 * @throws Zend_Gdata_App_HttpException
473 * @throws Zend_Gdata_Gapps_ServiceException
475 public function getEmailListEntry($location)
477 if ($location === null) {
478 require_once 'Zend/Gdata/App/InvalidArgumentException.php';
479 throw new Zend_Gdata_App_InvalidArgumentException(
480 'Location must not be null');
481 } else if ($location instanceof Zend_Gdata_Query
) {
482 $uri = $location->getQueryUrl();
486 return parent
::getEntry($uri, 'Zend_Gdata_Gapps_EmailListEntry');
490 * Retreive a single EmailListRecipientEntry object.
492 * @param mixed $location The location for the feed, as a URL or Query.
493 * @return Zend_Gdata_Gapps_EmailListRecipientEntry
494 * @throws Zend_Gdata_App_Exception
495 * @throws Zend_Gdata_App_HttpException
496 * @throws Zend_Gdata_Gapps_ServiceException
498 public function getEmailListRecipientEntry($location)
500 if ($location === null) {
501 require_once 'Zend/Gdata/App/InvalidArgumentException.php';
502 throw new Zend_Gdata_App_InvalidArgumentException(
503 'Location must not be null');
504 } else if ($location instanceof Zend_Gdata_Query
) {
505 $uri = $location->getQueryUrl();
509 return parent
::getEntry($uri, 'Zend_Gdata_Gapps_EmailListRecipientEntry');
513 * Create a new user from a UserEntry.
515 * @param Zend_Gdata_Gapps_UserEntry $user The user entry to insert.
516 * @param string $uri (optional) The URI where the user should be
517 * uploaded to. If null, the default user creation URI for
518 * this domain will be used.
519 * @return Zend_Gdata_Gapps_UserEntry The inserted user entry as
520 * returned by the server.
521 * @throws Zend_Gdata_App_Exception
522 * @throws Zend_Gdata_App_HttpException
523 * @throws Zend_Gdata_Gapps_ServiceException
525 public function insertUser($user, $uri = null)
528 $uri = $this->getBaseUrl() . self
::APPS_USER_PATH
;
530 $newEntry = $this->insertEntry($user, $uri, 'Zend_Gdata_Gapps_UserEntry');
535 * Create a new nickname from a NicknameEntry.
537 * @param Zend_Gdata_Gapps_NicknameEntry $nickname The nickname entry to
539 * @param string $uri (optional) The URI where the nickname should be
540 * uploaded to. If null, the default nickname creation URI for
541 * this domain will be used.
542 * @return Zend_Gdata_Gapps_NicknameEntry The inserted nickname entry as
543 * returned by the server.
544 * @throws Zend_Gdata_App_Exception
545 * @throws Zend_Gdata_App_HttpException
546 * @throws Zend_Gdata_Gapps_ServiceException
548 public function insertNickname($nickname, $uri = null)
551 $uri = $this->getBaseUrl() . self
::APPS_NICKNAME_PATH
;
553 $newEntry = $this->insertEntry($nickname, $uri, 'Zend_Gdata_Gapps_NicknameEntry');
558 * Create a new email list from an EmailListEntry.
560 * @param Zend_Gdata_Gapps_EmailListEntry $emailList The email list entry
562 * @param string $uri (optional) The URI where the email list should be
563 * uploaded to. If null, the default email list creation URI for
564 * this domain will be used.
565 * @return Zend_Gdata_Gapps_EmailListEntry The inserted email list entry
566 * as returned by the server.
567 * @throws Zend_Gdata_App_Exception
568 * @throws Zend_Gdata_App_HttpException
569 * @throws Zend_Gdata_Gapps_ServiceException
571 public function insertEmailList($emailList, $uri = null)
574 $uri = $this->getBaseUrl() . self
::APPS_EMAIL_LIST_PATH
;
576 $newEntry = $this->insertEntry($emailList, $uri, 'Zend_Gdata_Gapps_EmailListEntry');
581 * Create a new email list recipient from an EmailListRecipientEntry.
583 * @param Zend_Gdata_Gapps_EmailListRecipientEntry $recipient The recipient
585 * @param string $uri (optional) The URI where the recipient should be
586 * uploaded to. If null, the default recipient creation URI for
587 * this domain will be used.
588 * @return Zend_Gdata_Gapps_EmailListRecipientEntry The inserted
589 * recipient entry as returned by the server.
590 * @throws Zend_Gdata_App_Exception
591 * @throws Zend_Gdata_App_HttpException
592 * @throws Zend_Gdata_Gapps_ServiceException
594 public function insertEmailListRecipient($recipient, $uri = null)
597 require_once 'Zend/Gdata/App/InvalidArgumentException.php';
598 throw new Zend_Gdata_App_InvalidArgumentException(
599 'URI must not be null');
600 } elseif ($uri instanceof Zend_Gdata_Gapps_EmailListEntry
) {
601 $uri = $uri->getLink('edit')->href
;
603 $newEntry = $this->insertEntry($recipient, $uri, 'Zend_Gdata_Gapps_EmailListRecipientEntry');
608 * Provides a magic factory method to instantiate new objects with
609 * shorter syntax than would otherwise be required by the Zend Framework
610 * naming conventions. For more information, see Zend_Gdata_App::__call().
612 * This overrides the default behavior of __call() so that query classes
613 * do not need to have their domain manually set when created with
614 * a magic factory method.
616 * @see Zend_Gdata_App::__call()
617 * @param string $method The method name being called
618 * @param array $args The arguments passed to the call
619 * @throws Zend_Gdata_App_Exception
621 public function __call($method, $args) {
622 if (preg_match('/^new(\w+Query)/', $method, $matches)) {
623 $class = $matches[1];
624 $foundClassName = null;
625 foreach ($this->_registeredPackages
as $name) {
627 if (!class_exists($name . '_' . $class)) {
628 require_once 'Zend/Loader.php';
629 @Zend_Loader
::loadClass($name . '_' . $class);
631 $foundClassName = $name . '_' . $class;
633 } catch (Zend_Exception
$e) {
634 // package wasn't here- continue searching
637 if ($foundClassName != null) {
638 $reflectionObj = new ReflectionClass($foundClassName);
639 // Prepend the domain to the query
640 $args = array_merge(array($this->getDomain()), $args);
641 return $reflectionObj->newInstanceArgs($args);
643 require_once 'Zend/Gdata/App/Exception.php';
644 throw new Zend_Gdata_App_Exception(
645 "Unable to find '${class}' in registered packages");
648 return parent
::__call($method, $args);
653 // Convenience methods
654 // Specified at http://code.google.com/apis/apps/gdata_provisioning_api_v2.0_reference.html#appendix_e
657 * Create a new user entry and send it to the Google Apps servers.
659 * @param string $username The username for the new user.
660 * @param string $givenName The given name for the new user.
661 * @param string $familyName The family name for the new user.
662 * @param string $password The password for the new user as a plaintext string
663 * (if $passwordHashFunction is null) or a SHA-1 hashed
664 * value (if $passwordHashFunction = 'SHA-1').
665 * @param string $quotaLimitInMB (optional) The quota limit for the new user in MB.
666 * @return Zend_Gdata_Gapps_UserEntry (optional) The new user entry as returned by
668 * @throws Zend_Gdata_App_Exception
669 * @throws Zend_Gdata_App_HttpException
670 * @throws Zend_Gdata_Gapps_ServiceException
672 public function createUser ($username, $givenName, $familyName, $password,
673 $passwordHashFunction = null, $quotaLimitInMB = null) {
674 $user = $this->newUserEntry();
675 $user->login
= $this->newLogin();
676 $user->login
->username
= $username;
677 $user->login
->password
= $password;
678 $user->login
->hashFunctionName
= $passwordHashFunction;
679 $user->name
= $this->newName();
680 $user->name
->givenName
= $givenName;
681 $user->name
->familyName
= $familyName;
682 if ($quotaLimitInMB !== null) {
683 $user->quota
= $this->newQuota();
684 $user->quota
->limit
= $quotaLimitInMB;
686 return $this->insertUser($user);
690 * Retrieve a user based on their username.
692 * @param string $username The username to search for.
693 * @return Zend_Gdata_Gapps_UserEntry The username to search for, or null
695 * @throws Zend_Gdata_App_InvalidArgumentException
696 * @throws Zend_Gdata_App_HttpException
698 public function retrieveUser ($username) {
699 $query = $this->newUserQuery($username);
701 $user = $this->getUserEntry($query);
702 } catch (Zend_Gdata_Gapps_ServiceException
$e) {
703 // Set the user to null if not found
704 if ($e->hasError(Zend_Gdata_Gapps_Error
::ENTITY_DOES_NOT_EXIST
)) {
714 * Retrieve a page of users in alphabetical order, starting with the
717 * @param string $startUsername (optional) The first username to retrieve.
718 * If null or not declared, the page will begin with the first
719 * user in the domain.
720 * @return Zend_Gdata_Gapps_UserFeed Collection of Zend_Gdata_UserEntry
721 * objects representing all users in the domain.
722 * @throws Zend_Gdata_App_Exception
723 * @throws Zend_Gdata_App_HttpException
724 * @throws Zend_Gdata_Gapps_ServiceException
726 public function retrievePageOfUsers ($startUsername = null) {
727 $query = $this->newUserQuery();
728 $query->setStartUsername($startUsername);
729 return $this->getUserFeed($query);
733 * Retrieve all users in the current domain. Be aware that
734 * calling this function on a domain with many users will take a
735 * signifigant amount of time to complete. On larger domains this may
736 * may cause execution to timeout without proper precautions in place.
738 * @return Zend_Gdata_Gapps_UserFeed Collection of Zend_Gdata_UserEntry
739 * objects representing all users in the domain.
740 * @throws Zend_Gdata_App_Exception
741 * @throws Zend_Gdata_App_HttpException
742 * @throws Zend_Gdata_Gapps_ServiceException
744 public function retrieveAllUsers () {
745 return $this->retrieveAllEntriesForFeed($this->retrievePageOfUsers());
749 * Overwrite a specified username with the provided UserEntry. The
750 * UserEntry does not need to contain an edit link.
752 * This method is provided for compliance with the Google Apps
753 * Provisioning API specification. Normally users will instead want to
754 * call UserEntry::save() instead.
756 * @see Zend_Gdata_App_Entry::save
757 * @param string $username The username whose data will be overwritten.
758 * @param Zend_Gdata_Gapps_UserEntry $userEntry The user entry which
759 * will be overwritten.
760 * @return Zend_Gdata_Gapps_UserEntry The UserEntry returned by the
762 * @throws Zend_Gdata_App_Exception
763 * @throws Zend_Gdata_App_HttpException
764 * @throws Zend_Gdata_Gapps_ServiceException
766 public function updateUser($username, $userEntry) {
767 return $this->updateEntry($userEntry, $this->getBaseUrl() .
768 self
::APPS_USER_PATH
. '/' . $username);
772 * Mark a given user as suspended.
774 * @param string $username The username associated with the user who
775 * should be suspended.
776 * @return Zend_Gdata_Gapps_UserEntry The UserEntry for the modified
778 * @throws Zend_Gdata_App_Exception
779 * @throws Zend_Gdata_App_HttpException
780 * @throws Zend_Gdata_Gapps_ServiceException
782 public function suspendUser($username) {
783 $user = $this->retrieveUser($username);
784 $user->login
->suspended
= true;
785 return $user->save();
789 * Mark a given user as not suspended.
791 * @param string $username The username associated with the user who
792 * should be restored.
793 * @return Zend_Gdata_Gapps_UserEntry The UserEntry for the modified
795 * @throws Zend_Gdata_App_Exception
796 * @throws Zend_Gdata_App_HttpException
797 * @throws Zend_Gdata_Gapps_ServiceException
799 public function restoreUser($username) {
800 $user = $this->retrieveUser($username);
801 $user->login
->suspended
= false;
802 return $user->save();
806 * Delete a user by username.
808 * @param string $username The username associated with the user who
810 * @throws Zend_Gdata_App_Exception
811 * @throws Zend_Gdata_App_HttpException
812 * @throws Zend_Gdata_Gapps_ServiceException
814 public function deleteUser($username) {
815 $this->delete($this->getBaseUrl() . self
::APPS_USER_PATH
. '/' .
820 * Create a nickname for a given user.
822 * @param string $username The username to which the new nickname should
824 * @param string $nickname The new nickname to be created.
825 * @return Zend_Gdata_Gapps_NicknameEntry The nickname entry which was
826 * created by the server.
827 * @throws Zend_Gdata_App_Exception
828 * @throws Zend_Gdata_App_HttpException
829 * @throws Zend_Gdata_Gapps_ServiceException
831 public function createNickname($username, $nickname) {
832 $entry = $this->newNicknameEntry();
833 $nickname = $this->newNickname($nickname);
834 $login = $this->newLogin($username);
835 $entry->nickname
= $nickname;
836 $entry->login
= $login;
837 return $this->insertNickname($entry);
841 * Retrieve the entry for a specified nickname.
843 * @param string $nickname The nickname to be retrieved.
844 * @return Zend_Gdata_Gapps_NicknameEntry The requested nickname entry.
845 * @throws Zend_Gdata_App_Exception
846 * @throws Zend_Gdata_App_HttpException
847 * @throws Zend_Gdata_Gapps_ServiceException
849 public function retrieveNickname($nickname) {
850 $query = $this->newNicknameQuery();
851 $query->setNickname($nickname);
853 $nickname = $this->getNicknameEntry($query);
854 } catch (Zend_Gdata_Gapps_ServiceException
$e) {
855 // Set the nickname to null if not found
856 if ($e->hasError(Zend_Gdata_Gapps_Error
::ENTITY_DOES_NOT_EXIST
)) {
866 * Retrieve all nicknames associated with a specific username.
868 * @param string $username The username whose nicknames should be
870 * @return Zend_Gdata_Gapps_NicknameFeed A feed containing all nicknames
871 * for the given user, or null if
872 * @throws Zend_Gdata_App_Exception
873 * @throws Zend_Gdata_App_HttpException
874 * @throws Zend_Gdata_Gapps_ServiceException
876 public function retrieveNicknames($username) {
877 $query = $this->newNicknameQuery();
878 $query->setUsername($username);
879 $nicknameFeed = $this->retrieveAllEntriesForFeed(
880 $this->getNicknameFeed($query));
881 return $nicknameFeed;
885 * Retrieve a page of nicknames in alphabetical order, starting with the
888 * @param string $startNickname (optional) The first nickname to
889 * retrieve. If null or not declared, the page will begin with
890 * the first nickname in the domain.
891 * @return Zend_Gdata_Gapps_NicknameFeed Collection of Zend_Gdata_NicknameEntry
892 * objects representing all nicknames in the domain.
893 * @throws Zend_Gdata_App_Exception
894 * @throws Zend_Gdata_App_HttpException
895 * @throws Zend_Gdata_Gapps_ServiceException
897 public function retrievePageOfNicknames ($startNickname = null) {
898 $query = $this->newNicknameQuery();
899 $query->setStartNickname($startNickname);
900 return $this->getNicknameFeed($query);
904 * Retrieve all nicknames in the current domain. Be aware that
905 * calling this function on a domain with many nicknames will take a
906 * signifigant amount of time to complete. On larger domains this may
907 * may cause execution to timeout without proper precautions in place.
909 * @return Zend_Gdata_Gapps_NicknameFeed Collection of Zend_Gdata_NicknameEntry
910 * objects representing all nicknames in the domain.
911 * @throws Zend_Gdata_App_Exception
912 * @throws Zend_Gdata_App_HttpException
913 * @throws Zend_Gdata_Gapps_ServiceException
915 public function retrieveAllNicknames () {
916 return $this->retrieveAllEntriesForFeed($this->retrievePageOfNicknames());
920 * Delete a specified nickname.
922 * @param string $nickname The name of the nickname to be deleted.
923 * @throws Zend_Gdata_App_Exception
924 * @throws Zend_Gdata_App_HttpException
925 * @throws Zend_Gdata_Gapps_ServiceException
927 public function deleteNickname($nickname) {
928 $this->delete($this->getBaseUrl() . self
::APPS_NICKNAME_PATH
. '/' . $nickname);
932 * Create a new email list.
934 * @param string $emailList The name of the email list to be created.
935 * @return Zend_Gdata_Gapps_EmailListEntry The email list entry
936 * as created on the server.
937 * @throws Zend_Gdata_App_Exception
938 * @throws Zend_Gdata_App_HttpException
939 * @throws Zend_Gdata_Gapps_ServiceException
941 public function createEmailList($emailList) {
942 $entry = $this->newEmailListEntry();
943 $list = $this->newEmailList();
944 $list->name
= $emailList;
945 $entry->emailList
= $list;
946 return $this->insertEmailList($entry);
950 * Retrieve all email lists associated with a recipient.
952 * @param string $username The recipient whose associated email lists
953 * should be returned.
954 * @return Zend_Gdata_Gapps_EmailListFeed The list of email lists found as
955 * Zend_Gdata_EmailListEntry objects.
956 * @throws Zend_Gdata_App_Exception
957 * @throws Zend_Gdata_App_HttpException
958 * @throws Zend_Gdata_Gapps_ServiceException
960 public function retrieveEmailLists($recipient) {
961 $query = $this->newEmailListQuery();
962 $query->recipient
= $recipient;
963 return $this->getEmailListFeed($query);
967 * Retrieve a page of email lists in alphabetical order, starting with the
968 * provided email list.
970 * @param string $startEmailListName (optional) The first list to
971 * retrieve. If null or not defined, the page will begin
972 * with the first email list in the domain.
973 * @return Zend_Gdata_Gapps_EmailListFeed Collection of Zend_Gdata_EmailListEntry
974 * objects representing all nicknames in the domain.
975 * @throws Zend_Gdata_App_Exception
976 * @throws Zend_Gdata_App_HttpException
977 * @throws Zend_Gdata_Gapps_ServiceException
979 public function retrievePageOfEmailLists ($startNickname = null) {
980 $query = $this->newEmailListQuery();
981 $query->setStartEmailListName($startNickname);
982 return $this->getEmailListFeed($query);
986 * Retrieve all email lists associated with the curent domain. Be aware that
987 * calling this function on a domain with many email lists will take a
988 * signifigant amount of time to complete. On larger domains this may
989 * may cause execution to timeout without proper precautions in place.
991 * @return Zend_Gdata_Gapps_EmailListFeed The list of email lists found
992 * as Zend_Gdata_Gapps_EmailListEntry objects.
993 * @throws Zend_Gdata_App_Exception
994 * @throws Zend_Gdata_App_HttpException
995 * @throws Zend_Gdata_Gapps_ServiceException
997 public function retrieveAllEmailLists() {
998 return $this->retrieveAllEntriesForFeed($this->retrievePageOfEmailLists());
1002 * Delete a specified email list.
1004 * @param string $emailList The name of the emailList to be deleted.
1005 * @throws Zend_Gdata_App_Exception
1006 * @throws Zend_Gdata_App_HttpException
1007 * @throws Zend_Gdata_Gapps_ServiceException
1009 public function deleteEmailList($emailList) {
1010 $this->delete($this->getBaseUrl() . self
::APPS_EMAIL_LIST_PATH
. '/'
1015 * Add a specified recipient to an existing emailList.
1017 * @param string $recipientAddress The address of the recipient to be
1018 * added to the email list.
1019 * @param string $emailList The name of the email address to which the
1020 * recipient should be added.
1021 * @return Zend_Gdata_Gapps_EmailListRecipientEntry The recipient entry
1022 * created by the server.
1023 * @throws Zend_Gdata_App_Exception
1024 * @throws Zend_Gdata_App_HttpException
1025 * @throws Zend_Gdata_Gapps_ServiceException
1027 public function addRecipientToEmailList($recipientAddress, $emailList) {
1028 $entry = $this->newEmailListRecipientEntry();
1029 $who = $this->newWho();
1030 $who->email
= $recipientAddress;
1032 $address = $this->getBaseUrl() . self
::APPS_EMAIL_LIST_PATH
. '/' .
1033 $emailList . self
::APPS_EMAIL_LIST_RECIPIENT_POSTFIX
. '/';
1034 return $this->insertEmailListRecipient($entry, $address);
1038 * Retrieve a page of email list recipients in alphabetical order,
1039 * starting with the provided email list recipient.
1041 * @param string $emaiList The email list which should be searched.
1042 * @param string $startRecipient (optinal) The address of the first
1043 * recipient, or null to start with the first recipient in
1045 * @return Zend_Gdata_Gapps_EmailListRecipientFeed Collection of
1046 * Zend_Gdata_EmailListRecipientEntry objects representing all
1047 * recpients in the specified list.
1048 * @throws Zend_Gdata_App_Exception
1049 * @throws Zend_Gdata_App_HttpException
1050 * @throws Zend_Gdata_Gapps_ServiceException
1052 public function retrievePageOfRecipients ($emailList,
1053 $startRecipient = null) {
1054 $query = $this->newEmailListRecipientQuery();
1055 $query->setEmailListName($emailList);
1056 $query->setStartRecipient($startRecipient);
1057 return $this->getEmailListRecipientFeed($query);
1061 * Retrieve all recipients associated with an email list. Be aware that
1062 * calling this function on a domain with many email lists will take a
1063 * signifigant amount of time to complete. On larger domains this may
1064 * may cause execution to timeout without proper precautions in place.
1066 * @param string $emaiList The email list which should be searched.
1067 * @return Zend_Gdata_Gapps_EmailListRecipientFeed The list of email lists
1068 * found as Zend_Gdata_Gapps_EmailListRecipientEntry objects.
1069 * @throws Zend_Gdata_App_Exception
1070 * @throws Zend_Gdata_App_HttpException
1071 * @throws Zend_Gdata_Gapps_ServiceException
1073 public function retrieveAllRecipients($emailList) {
1074 return $this->retrieveAllEntriesForFeed(
1075 $this->retrievePageOfRecipients($emailList));
1079 * Remove a specified recipient from an email list.
1081 * @param string $recipientAddress The recipient to be removed.
1082 * @param string $emailList The list from which the recipient should
1084 * @throws Zend_Gdata_App_Exception
1085 * @throws Zend_Gdata_App_HttpException
1086 * @throws Zend_Gdata_Gapps_ServiceException
1088 public function removeRecipientFromEmailList($recipientAddress, $emailList) {
1089 $this->delete($this->getBaseUrl() . self
::APPS_EMAIL_LIST_PATH
. '/'
1090 . $emailList . self
::APPS_EMAIL_LIST_RECIPIENT_POSTFIX
. '/'
1091 . $recipientAddress);