App Engine Python SDK version 1.8.1
[gae.git] / python / php / sdk / google / appengine / api / users / UserService.php
blob0faa0919eb70bc93402aa5ab257656cafd855a33
1 <?php
2 /**
3 * Copyright 2007 Google Inc.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 /**
20 namespace google\appengine\api\users;
22 use \google\appengine\CreateLoginURLRequest;
23 use \google\appengine\CreateLoginURLResponse;
24 use \google\appengine\CreateLogoutURLRequest;
25 use \google\appengine\CreateLogoutURLResponse;
26 use \google\appengine\runtime\ApiProxy;
27 use \google\appengine\runtime\ApplicationError;
28 use \google\appengine\UserServiceError\ErrorCode;
30 require_once 'google/appengine/api/user_service_pb.php';
31 require_once 'google/appengine/api/users/User.php';
32 require_once 'google/appengine/api/users/UsersException.php';
33 require_once 'google/appengine/runtime/ApiProxy.php';
34 require_once 'google/appengine/runtime/ApplicationError.php';
36 final class UserService {
37 /**
38 * Computes the login URL for redirection.
40 * @param string $destination_url The desired final destination URL for the
41 * user once login is complete. If 'destinationURL' does not
42 * have a host specified, we will use the host from the
43 * current request.
45 * @param string $federated_identity The parameter is used to trigger OpenId
46 * Login flow, an empty value will trigger Google OpenID Login
47 * by default.
49 * @return string Login URL. If federatedIdentity is set, this will be
50 * a federated login using the specified identity. If not, this
51 * will use Google Accounts.
53 * @throws UsersException If there was a problem using the Users service.
55 public static function createLoginURL(
56 $destination_url = null, $federated_identity = null) {
57 $req = new CreateLoginURLRequest();
58 $resp = new CreateLoginURLResponse();
59 if ($destination_url !== null) {
60 $req->setDestinationUrl($destination_url);
61 } else {
62 $req->setDestinationUrl('');
64 if ($federated_identity !== null) {
65 $req->setFederatedIdentity($federated_identity);
68 try {
69 ApiProxy::makeSyncCall('user', 'CreateLoginURL', $req, $resp);
70 } catch (ApplicationError $e) {
71 throw self::applicationErrorToException($e, $destination_url);
73 return $resp->getLoginUrl();
76 /**
77 * Computes the logout URL for this request and specified destination URL,
78 * for both federated login App and Google Accounts App.
80 * @param string $destination_url The desired final destination
81 * URL for the user once logout is complete.
82 * If 'destinationURL' does not have a host specified, we will
83 * use the host from the current request.
85 * @return string Logout URL.
87 * @throws UsersException If there was a problem using the Users service.
89 public static function createLogoutURL($destination_url) {
90 $req = new CreateLogoutURLRequest();
91 $resp = new CreateLogoutURLResponse();
92 $req->setDestinationUrl($destination_url);
94 try {
95 ApiProxy::makeSyncCall('user', 'CreateLogoutURL', $req, $resp);
96 } catch (ApplicationError $e) {
97 throw self::applicationErrorToException($e, $destination_url);
99 return $resp->getLogoutUrl();
103 * @return User The object representing the current signed in user, or null
104 * if no user is signed in.
106 public static function getCurrentUser() {
107 $email = getenv('USER_EMAIL');
108 $userId = getenv('USER_ID');
109 $federatedIdentity = getenv('FEDERATED_IDENTITY');
110 $federatedProvider = getenv('FEDERATED_PROVIDER');
112 if (!$email && !$federatedIdentity) {
113 return null;
116 if (!$federatedIdentity) {
117 $federatedIdentity = null;
120 if (!$federatedProvider) {
121 $federatedProvider = null;
124 // We set this to maintain compatibility with the
125 // datastore_types.FromPropertyPb creation of a User object, which will set
126 // an empty string for the email (since it is a required field of the
127 // underlying data representation of this class in the datastore.
128 if ($email === FALSE) {
129 $email = '';
132 // User.user_id() should only return a
133 // string of length > 0 or null.
134 if (!$userId || $userId == '') {
135 $userId = null;
138 return new User($email, $federatedIdentity, $federatedProvider, $userId);
142 * Return true if the user making this request is an admin for this
143 * application, false otherwise.
145 * We specifically make this a separate function, and not a member function
146 * of the User class, because admin status is not persisted in the
147 * datastore. It only exists for the user making this request right now.
149 * @return boolean Whether the current user is an administrator of the
150 * application.
152 public static function isCurrentUserAdmin() {
153 return getenv('USER_IS_ADMIN') == '1';
156 private static function applicationErrorToException($error,
157 $destination_url) {
158 switch ($error->getApplicationError()) {
159 case ErrorCode::REDIRECT_URL_TOO_LONG:
160 return new UsersException('URL too long: ' . $destination_url);
161 case ErrorCode::NOT_ALLOWED:
162 return new UsersException('Action not allowed.');
163 default:
164 return new UsersException(
165 'Error code: ' . $error->getApplicationError());
168 } // class UserService