31 |
31 |
32 |
32 |
33 import os |
33 import os |
34 from google.appengine.api import apiproxy_stub_map |
34 from google.appengine.api import apiproxy_stub_map |
35 from google.appengine.api import user_service_pb |
35 from google.appengine.api import user_service_pb |
36 from google.appengine.api import api_base_pb |
|
37 from google.appengine.runtime import apiproxy_errors |
36 from google.appengine.runtime import apiproxy_errors |
38 |
37 |
39 |
38 |
40 class Error(Exception): |
39 class Error(Exception): |
41 """Base User error type.""" |
40 """Base User error type.""" |
48 |
47 |
49 class RedirectTooLongError(Error): |
48 class RedirectTooLongError(Error): |
50 """Raised by UserService calls if the generated redirect URL was too long. |
49 """Raised by UserService calls if the generated redirect URL was too long. |
51 """ |
50 """ |
52 |
51 |
|
52 |
53 class NotAllowedError(Error): |
53 class NotAllowedError(Error): |
54 """Raised by UserService calls if the requested redirect URL is not allowed. |
54 """Raised by UserService calls if the requested redirect URL is not allowed. |
55 """ |
55 """ |
56 |
56 |
57 |
57 |
58 class User(object): |
58 class User(object): |
59 """A user. |
59 """A user. |
60 |
60 |
61 We provide here the email address, nickname, and auth domain for a user. |
61 We provide the email address, nickname, auth domain, and id for a user. |
62 |
62 |
63 A nickname is a human-readable string which uniquely identifies a Google |
63 A nickname is a human-readable string which uniquely identifies a Google |
64 user, akin to a username. It will be an email address for some users, but |
64 user, akin to a username. It will be an email address for some users, but |
65 not all. |
65 not all. |
66 """ |
66 """ |
67 |
67 |
68 |
68 |
69 def __init__(self, email=None, _auth_domain=None): |
69 __user_id = None |
|
70 |
|
71 def __init__(self, email=None, _auth_domain=None, _user_id=None): |
70 """Constructor. |
72 """Constructor. |
71 |
73 |
72 Args: |
74 Args: |
73 # email is optional. it defaults to the current user. |
75 email: An optional string of the user's email address. It defaults to |
74 email: string |
76 the current user's email address. |
|
77 |
|
78 Raises: |
|
79 UserNotFoundError: Raised if the user is not logged in and the email |
|
80 argument is empty. |
75 """ |
81 """ |
76 if _auth_domain is None: |
82 if _auth_domain is None: |
77 _auth_domain = os.environ.get('AUTH_DOMAIN') |
83 _auth_domain = os.environ.get('AUTH_DOMAIN') |
78 else: |
84 else: |
79 assert email is not None |
85 assert email is not None |
81 assert _auth_domain |
87 assert _auth_domain |
82 |
88 |
83 if email is None: |
89 if email is None: |
84 assert 'USER_EMAIL' in os.environ |
90 assert 'USER_EMAIL' in os.environ |
85 email = os.environ['USER_EMAIL'] |
91 email = os.environ['USER_EMAIL'] |
|
92 if _user_id is None and 'USER_ID' in os.environ: |
|
93 _user_id = os.environ['USER_ID'] |
86 |
94 |
87 if not email: |
95 if not email: |
88 raise UserNotFoundError |
96 raise UserNotFoundError |
89 |
97 |
90 self.__email = email |
98 self.__email = email |
91 self.__auth_domain = _auth_domain |
99 self.__auth_domain = _auth_domain |
|
100 self.__user_id = _user_id or None |
92 |
101 |
93 def nickname(self): |
102 def nickname(self): |
94 """Return this user's nickname. |
103 """Return this user's nickname. |
95 |
104 |
96 The nickname will be a unique, human readable identifier for this user |
105 The nickname will be a unique, human readable identifier for this user |
106 |
115 |
107 def email(self): |
116 def email(self): |
108 """Return this user's email address.""" |
117 """Return this user's email address.""" |
109 return self.__email |
118 return self.__email |
110 |
119 |
|
120 def user_id(self): |
|
121 """Return either a permanent unique identifying string or None. |
|
122 |
|
123 If the email address was set explicity, this will return None. |
|
124 """ |
|
125 return self.__user_id |
|
126 |
111 def auth_domain(self): |
127 def auth_domain(self): |
112 """Return this user's auth domain.""" |
128 """Return this user's auth domain.""" |
113 return self.__auth_domain |
129 return self.__auth_domain |
114 |
130 |
115 def __unicode__(self): |
131 def __unicode__(self): |
117 |
133 |
118 def __str__(self): |
134 def __str__(self): |
119 return str(self.nickname()) |
135 return str(self.nickname()) |
120 |
136 |
121 def __repr__(self): |
137 def __repr__(self): |
122 return "users.User(email='%s')" % self.email() |
138 if self.__user_id: |
|
139 return "users.User(email='%s',_user_id='%s')" % (self.email(), |
|
140 self.user_id()) |
|
141 else: |
|
142 return "users.User(email='%s')" % self.email() |
123 |
143 |
124 def __hash__(self): |
144 def __hash__(self): |
125 return hash((self.__email, self.__auth_domain)) |
145 return hash((self.__email, self.__auth_domain)) |
126 |
146 |
127 def __cmp__(self, other): |
147 def __cmp__(self, other): |
150 except apiproxy_errors.ApplicationError, e: |
170 except apiproxy_errors.ApplicationError, e: |
151 if (e.application_error == |
171 if (e.application_error == |
152 user_service_pb.UserServiceError.REDIRECT_URL_TOO_LONG): |
172 user_service_pb.UserServiceError.REDIRECT_URL_TOO_LONG): |
153 raise RedirectTooLongError |
173 raise RedirectTooLongError |
154 elif (e.application_error == |
174 elif (e.application_error == |
155 user_service_pb.UserServiceError.NOT_ALLOWED): |
175 user_service_pb.UserServiceError.NOT_ALLOWED): |
156 raise NotAllowedError |
176 raise NotAllowedError |
157 else: |
177 else: |
158 raise e |
178 raise e |
159 return resp.value() |
179 return resp.value() |
160 |
180 |
203 |
223 |
204 We specifically make this a separate function, and not a member function of |
224 We specifically make this a separate function, and not a member function of |
205 the User class, because admin status is not persisted in the datastore. It |
225 the User class, because admin status is not persisted in the datastore. It |
206 only exists for the user making this request right now. |
226 only exists for the user making this request right now. |
207 """ |
227 """ |
208 return (os.environ.get('USER_IS_ADMIN', '0')) == "1" |
228 return (os.environ.get('USER_IS_ADMIN', '0')) == '1' |
209 |
229 |
210 IsCurrentUserAdmin = is_current_user_admin |
230 IsCurrentUserAdmin = is_current_user_admin |