App Engine Python SDK version 1.8.8
[gae.git] / python / php / sdk / google / appengine / api / users / User.php
blob5ac7d25ba1144664fc463e317312390bf404deba
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 require_once 'google/appengine/util/string_util.php';
24 use google\appengine\util as util;
26 /**
27 * A user.
29 * We provide the email address, nickname, and id for a user.
31 * A nickname is a human-readable string which uniquely identifies a Google
32 * user, akin to a username. It will be an email address for some users, but
33 * not all.
35 * A user could be a Google Accounts user or a federated login user.
37 * Federated identity and federated provider are only avaliable for
38 * federated users.
40 final class User {
41 private $user_id = null;
42 private $federated_identity = null;
43 private $federated_provider = null;
45 /**
46 * Constructor.
48 * @param string $email An optional string of the user's email address. It
49 * defaults to the current user's email address.
50 * @param string $federated_identity The federated identity of user. It
51 * defaults to the current user's federated identity.
52 * @param string $federated_provider The federated provider url of user.
54 * @throws \InvalidArgumentException Thrown if both email and federated
55 * identity are empty.
57 public function __construct(
58 $email = null,
59 $federated_identity = null,
60 $federated_provider = null,
61 $user_id = null) {
62 $auth_domain = getenv('AUTH_DOMAIN');
63 assert($auth_domain !== false);
65 if ($email === null and $federated_identity === null) {
66 throw new \InvalidArgumentException(
67 'One of $email or $federated_identity must be set.');
70 $this->email = $email;
71 $this->federated_identity = $federated_identity;
72 $this->federated_provider = $federated_provider;
73 $this->auth_domain = $auth_domain;
74 $this->user_id = $user_id;
77 /**
78 * Return this user's nickname.
80 * The nickname will be a unique, human readable identifier for this user
81 * with respect to this application. It will be an email address for some
82 * users, part of the email address for some users, and the federated identity
83 * for federated users who have not asserted an email address.
85 * @return string The user's nickname.
87 public function getNickname() {
88 if ($this->email != null && $this->auth_domain != null &&
89 util\endsWith($this->email, '@' . $this->auth_domain)) {
90 $suffixLen = strlen($this->auth_domain) + 1;
91 return substr($this->email, 0, -$suffixLen);
92 } else if ($this->federated_identity) {
93 return $this->federated_identity;
94 } else {
95 return $this->email;
99 /**
100 * Return this user's email address.
102 * @return string The user's email address.
104 public function getEmail() {
105 return $this->email;
109 * Return either a permanent unique identifying string or null.
111 * If the email address was set explicity, this will return null.
113 * @return string The user's UserId.
115 public function getUserId() {
116 return $this->user_id;
120 * Return this user's auth domain.
122 * This method is internal and should not be used by client applications.
124 * @return string The user's authentication domain.
126 public function getAuthDomain() {
127 return $this->auth_domain;
131 * Return this user's federated identity, null if not a federated user.
133 * @return string The user's federated identity.
135 public function getFederatedIdentity() {
136 return $this->federated_identity;
140 * Return this user's federated provider, null if not a federated user.
142 * @return string The user's federated provider.
144 public function getFederatedProvider() {
145 return $this->federated_provider;
149 * Magic method that PHP uses when the object is treated like a string.
151 * @return string The attributes of this user.
153 public function __toString() {
154 $res = array();
155 if ($this->email != null) {
156 $res[] = sprintf("email='%s'", $this->email);
158 if ($this->federated_identity != null) {
159 $res[] = sprintf("federated_identity='%s'", $this->federated_identity);
161 if ($this->user_id != null) {
162 $res[] = sprintf("user_id='%s'", $this->user_id);
164 return sprintf('User(%s)', join(',', $res));
166 } // class User