| OLD | NEW |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import collections | 5 import collections |
| 6 import json | 6 import json |
| 7 import logging | 7 import logging |
| 8 | 8 |
| 9 from google.appengine.api import users | 9 from google.appengine.api import users |
| 10 import jinja2 | 10 import jinja2 |
| 11 import webapp2 | 11 import webapp2 |
| 12 | 12 |
| 13 from common import auth_util |
| 13 from common import constants | 14 from common import constants |
| 14 | 15 |
| 15 | 16 |
| 16 JINJA_ENVIRONMENT = jinja2.Environment( | 17 JINJA_ENVIRONMENT = jinja2.Environment( |
| 17 loader=jinja2.FileSystemLoader(constants.HTML_TEMPLATE_DIR), | 18 loader=jinja2.FileSystemLoader(constants.HTML_TEMPLATE_DIR), |
| 18 extensions=['jinja2.ext.autoescape'], | 19 extensions=['jinja2.ext.autoescape'], |
| 19 autoescape=True) | 20 autoescape=True) |
| 20 | 21 |
| 21 | 22 |
| 22 def ToJson(data): | 23 def ToJson(data): |
| (...skipping 18 matching lines...) Expand all Loading... |
| 41 if (self.request.headers.get('X-AppEngine-QueueName') or | 42 if (self.request.headers.get('X-AppEngine-QueueName') or |
| 42 self.request.headers.get('X-AppEngine-Cron')): | 43 self.request.headers.get('X-AppEngine-Cron')): |
| 43 # Requests from task queues or cron jobs could access all HTTP endpoints. | 44 # Requests from task queues or cron jobs could access all HTTP endpoints. |
| 44 return True | 45 return True |
| 45 elif self.PERMISSION_LEVEL == Permission.ANYONE: | 46 elif self.PERMISSION_LEVEL == Permission.ANYONE: |
| 46 return True | 47 return True |
| 47 elif self.PERMISSION_LEVEL == Permission.CORP_USER: | 48 elif self.PERMISSION_LEVEL == Permission.CORP_USER: |
| 48 # Only give access to google accounts or admins. | 49 # Only give access to google accounts or admins. |
| 49 return self.IsCorpUserOrAdmin() | 50 return self.IsCorpUserOrAdmin() |
| 50 elif self.PERMISSION_LEVEL == Permission.ADMIN: | 51 elif self.PERMISSION_LEVEL == Permission.ADMIN: |
| 51 return users.is_current_user_admin() | 52 return auth_util.IsCurrentUserAdmin() |
| 52 else: | 53 else: |
| 53 logging.error('Unknown permission level: %s' % self.PERMISSION_LEVEL) | 54 logging.error('Unknown permission level: %s' % self.PERMISSION_LEVEL) |
| 54 return False | 55 return False |
| 55 | 56 |
| 56 def IsCorpUserOrAdmin(self): | 57 def IsCorpUserOrAdmin(self): |
| 57 """Returns True if the user logged in with corp account or as admin.""" | 58 """Returns True if the user logged in with corp account or as admin.""" |
| 58 user = users.get_current_user() | 59 user_email = auth_util.GetUserEmail() |
| 59 return ((user and user.email().endswith('@google.com')) or | 60 return ((user_email and user_email.endswith('@google.com')) or |
| 60 users.is_current_user_admin()) | 61 auth_util.IsCurrentUserAdmin()) |
| 61 | 62 |
| 62 @staticmethod | 63 @staticmethod |
| 63 def CreateError(error_message, return_code=500): | 64 def CreateError(error_message, return_code=500): |
| 64 logging.error('Error occurred: %s', error_message) | 65 logging.error('Error occurred: %s', error_message) |
| 65 return { | 66 return { |
| 66 'template': 'error.html', | 67 'template': 'error.html', |
| 67 'data': {'error_message': error_message}, | 68 'data': {'error_message': error_message}, |
| 68 'return_code': return_code, | 69 'return_code': return_code, |
| 69 } | 70 } |
| 70 | 71 |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 else: | 138 else: |
| 138 content_type = 'text/html' | 139 content_type = 'text/html' |
| 139 | 140 |
| 140 if cache_expiry is not None: | 141 if cache_expiry is not None: |
| 141 self.response.headers['cache-control'] = ( | 142 self.response.headers['cache-control'] = ( |
| 142 'max-age=%s, public' % cache_expiry) | 143 'max-age=%s, public' % cache_expiry) |
| 143 self.response.headers['Content-Type'] = content_type | 144 self.response.headers['Content-Type'] = content_type |
| 144 self.response.write(data) | 145 self.response.write(data) |
| 145 | 146 |
| 146 def GetLoginUrl(self): | 147 def GetLoginUrl(self): |
| 147 if self.request.referer: | 148 """Returns the login url.""" |
| 148 return users.create_login_url(self.request.referer) | 149 return users.create_login_url(self.request.referer or self.request.uri) |
| 149 else: | |
| 150 return users.create_login_url(self.request.uri) | |
| 151 | 150 |
| 152 def _Handle(self, handler_func): | 151 def _Handle(self, handler_func): |
| 153 try: | 152 try: |
| 154 if not self._HasPermission(): | 153 if not self._HasPermission(): |
| 155 template = 'error.html' | 154 template = 'error.html' |
| 156 data = { | 155 data = { |
| 157 'error_message': | 156 'error_message': |
| 158 ('Either not login or no permission. ' | 157 ('Either not login or no permission. ' |
| 159 'Please login with your google.com account.'), | 158 'Please login with your google.com account.'), |
| 160 'login_url': self.GetLoginUrl(), | 159 'login_url': self.GetLoginUrl(), |
| (...skipping 19 matching lines...) Expand all Loading... |
| 180 return_code = 500 | 179 return_code = 500 |
| 181 cache_expiry = None | 180 cache_expiry = None |
| 182 | 181 |
| 183 self._SendResponse(template, data, return_code, cache_expiry) | 182 self._SendResponse(template, data, return_code, cache_expiry) |
| 184 | 183 |
| 185 def get(self): | 184 def get(self): |
| 186 self._Handle(self.HandleGet) | 185 self._Handle(self.HandleGet) |
| 187 | 186 |
| 188 def post(self): | 187 def post(self): |
| 189 self._Handle(self.HandlePost) | 188 self._Handle(self.HandlePost) |
| OLD | NEW |