/php/sdk/google/appengine/api/users/UserService.php

https://github.com/theosp/google_appengine · PHP · 164 lines · 80 code · 15 blank · 69 comment · 13 complexity · 50691125fe6affc4fd5d57469261d119 MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright 2007 Google 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. */
  19. namespace google\appengine\api\users;
  20. use google\appengine\CreateLoginURLRequest;
  21. use google\appengine\CreateLoginURLResponse;
  22. use google\appengine\CreateLogoutURLRequest;
  23. use google\appengine\CreateLogoutURLResponse;
  24. use google\appengine\runtime\ApiProxy;
  25. use google\appengine\runtime\ApplicationError;
  26. use google\appengine\UserServiceError\ErrorCode;
  27. final class UserService {
  28. /**
  29. * Computes the login URL for redirection.
  30. *
  31. * @param string $destination_url The desired final destination URL for the
  32. * user once login is complete. If 'destinationURL' does not
  33. * have a host specified, we will use the host from the
  34. * current request.
  35. *
  36. * @param string $federated_identity The parameter is used to trigger OpenId
  37. * Login flow, an empty value will trigger Google OpenID Login
  38. * by default.
  39. *
  40. * @return string Login URL. If federatedIdentity is set, this will be
  41. * a federated login using the specified identity. If not, this
  42. * will use Google Accounts.
  43. *
  44. * @throws UsersException If there was a problem using the Users service.
  45. */
  46. public static function createLoginURL(
  47. $destination_url = null, $federated_identity = null) {
  48. $req = new CreateLoginURLRequest();
  49. $resp = new CreateLoginURLResponse();
  50. if ($destination_url !== null) {
  51. $req->setDestinationUrl($destination_url);
  52. } else {
  53. $req->setDestinationUrl('');
  54. }
  55. if ($federated_identity !== null) {
  56. $req->setFederatedIdentity($federated_identity);
  57. }
  58. try {
  59. ApiProxy::makeSyncCall('user', 'CreateLoginURL', $req, $resp);
  60. } catch (ApplicationError $e) {
  61. throw self::applicationErrorToException(
  62. $e, htmlspecialchars($destination_url));
  63. }
  64. return $resp->getLoginUrl();
  65. }
  66. /**
  67. * Computes the logout URL for this request and specified destination URL,
  68. * for both federated login App and Google Accounts App.
  69. *
  70. * @param string $destination_url The desired final destination
  71. * URL for the user once logout is complete.
  72. * If 'destinationURL' does not have a host specified, we will
  73. * use the host from the current request.
  74. *
  75. * @return string Logout URL.
  76. *
  77. * @throws UsersException If there was a problem using the Users service.
  78. */
  79. public static function createLogoutURL($destination_url) {
  80. $req = new CreateLogoutURLRequest();
  81. $resp = new CreateLogoutURLResponse();
  82. $req->setDestinationUrl($destination_url);
  83. try {
  84. ApiProxy::makeSyncCall('user', 'CreateLogoutURL', $req, $resp);
  85. } catch (ApplicationError $e) {
  86. throw self::applicationErrorToException(
  87. $e, htmlspecialchars($destination_url));
  88. }
  89. return $resp->getLogoutUrl();
  90. }
  91. /**
  92. * @return User The object representing the current signed in user, or null
  93. * if no user is signed in.
  94. */
  95. public static function getCurrentUser() {
  96. $email = getenv('USER_EMAIL');
  97. $userId = getenv('USER_ID');
  98. $federatedIdentity = getenv('FEDERATED_IDENTITY');
  99. $federatedProvider = getenv('FEDERATED_PROVIDER');
  100. if (!$email && !$federatedIdentity) {
  101. return null;
  102. }
  103. if (!$federatedIdentity) {
  104. $federatedIdentity = null;
  105. }
  106. if (!$federatedProvider) {
  107. $federatedProvider = null;
  108. }
  109. // We set this to maintain compatibility with the
  110. // datastore_types.FromPropertyPb creation of a User object, which will set
  111. // an empty string for the email (since it is a required field of the
  112. // underlying data representation of this class in the datastore.
  113. if ($email === false) {
  114. $email = '';
  115. }
  116. // User.user_id() should only return a
  117. // string of length > 0 or null.
  118. if (!$userId || $userId == '') {
  119. $userId = null;
  120. }
  121. return new User($email, $federatedIdentity, $federatedProvider, $userId);
  122. }
  123. /**
  124. * Return true if the user making this request is an admin for this
  125. * application, false otherwise.
  126. *
  127. * We specifically make this a separate function, and not a member function
  128. * of the User class, because admin status is not persisted in the
  129. * datastore. It only exists for the user making this request right now.
  130. *
  131. * @return boolean Whether the current user is an administrator of the
  132. * application.
  133. */
  134. public static function isCurrentUserAdmin() {
  135. return getenv('USER_IS_ADMIN') == '1';
  136. }
  137. private static function applicationErrorToException($error,
  138. $destination_url) {
  139. switch ($error->getApplicationError()) {
  140. case ErrorCode::REDIRECT_URL_TOO_LONG:
  141. return new UsersException('URL too long: ' . $destination_url);
  142. case ErrorCode::NOT_ALLOWED:
  143. return new UsersException('Action not allowed.');
  144. default:
  145. return new UsersException(
  146. 'Error code: ' . $error->getApplicationError());
  147. }
  148. }
  149. } // class UserService