OLD | NEW |
(Empty) | |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is govered by a BSD-style |
| 3 # license that can be found in the LICENSE file or at |
| 4 # https://developers.google.com/open-source/licenses/bsd |
| 5 |
| 6 """Helper functions used by the Monorail servlet base class.""" |
| 7 |
| 8 import datetime |
| 9 import logging |
| 10 import time |
| 11 |
| 12 from framework import permissions |
| 13 from framework import template_helpers |
| 14 |
| 15 |
| 16 _WEEKDAY = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', |
| 17 'Saturday', 'Sunday'] |
| 18 |
| 19 |
| 20 def GetBannerTime(timestamp): |
| 21 """Converts a timestamp into EZT-ready data so it can appear in the banner. |
| 22 |
| 23 Args: |
| 24 timestamp: timestamp expressed in the following format: |
| 25 [year,month,day,hour,minute,second] |
| 26 e.g. [2009,3,20,21,45,50] represents March 20 2009 9:45:50 PM |
| 27 |
| 28 Returns: |
| 29 EZT-ready data used to display the time inside the banner message. |
| 30 """ |
| 31 if timestamp is None: |
| 32 return None |
| 33 |
| 34 # Get the weekday and 'hour:min AM/PM' to display the timestamp |
| 35 # to users with javascript disabled |
| 36 ts = datetime.datetime(*[int(t) for t in timestamp]) |
| 37 weekday = _WEEKDAY[ts.weekday()] |
| 38 hour_min = datetime.datetime.strftime(ts, '%I:%M%p') |
| 39 |
| 40 # Convert the timestamp to milliseconds since the epoch to display |
| 41 # the timestamp to users with javascript |
| 42 ts_ms = time.mktime(ts.timetuple()) * 1000 |
| 43 |
| 44 return template_helpers.EZTItem( |
| 45 ts=ts_ms, year=ts.year, month=ts.month, day=ts.day, hour=ts.hour, |
| 46 minute=ts.minute, second=ts.second, weekday=weekday, hour_min=hour_min) |
| 47 |
| 48 |
| 49 def AssertBasePermissionForUser(user, user_view): |
| 50 """Verify user permissions and state. |
| 51 |
| 52 Args: |
| 53 user: user_pb2.User protocol buffer for the user |
| 54 user_view: framework.views.UserView for the user |
| 55 """ |
| 56 if permissions.IsBanned(user, user_view): |
| 57 raise permissions.BannedUserException( |
| 58 'You have been banned from using this site') |
| 59 |
| 60 |
| 61 def AssertBasePermission(mr): |
| 62 """Make sure that the logged in user can view the requested page. |
| 63 |
| 64 Args: |
| 65 mr: common information parsed from the HTTP request. |
| 66 |
| 67 Returns: |
| 68 Nothing |
| 69 |
| 70 Raises: |
| 71 BannedUserException: If the user is banned. |
| 72 PermissionException: If the user does not have permisssion to view. |
| 73 """ |
| 74 AssertBasePermissionForUser(mr.auth.user_pb, mr.auth.user_view) |
| 75 |
| 76 if mr.project_name and not CheckPerm(mr, permissions.VIEW): |
| 77 logging.info('your perms are %r', mr.perms) |
| 78 raise permissions.PermissionException( |
| 79 'User is not allowed to view this project') |
| 80 |
| 81 |
| 82 def CheckPerm(mr, perm, art=None, granted_perms=None): |
| 83 """Convenience method that makes permission checks easier. |
| 84 |
| 85 Args: |
| 86 mr: common information parsed from the HTTP request. |
| 87 perm: A permission constant, defined in module framework.permissions |
| 88 art: Optional artifact pb |
| 89 granted_perms: optional set of perms granted specifically in that artifact. |
| 90 |
| 91 Returns: |
| 92 A boolean, whether the request can be satisfied, given the permission. |
| 93 """ |
| 94 return mr.perms.CanUsePerm( |
| 95 perm, mr.auth.effective_ids, mr.project, |
| 96 permissions.GetRestrictions(art), granted_perms=granted_perms) |
| 97 |
| 98 |
| 99 def CheckPermForProject(mr, perm, project, art=None): |
| 100 """Convenience method that makes permission checks for projects easier. |
| 101 |
| 102 Args: |
| 103 mr: common information parsed from the HTTP request. |
| 104 perm: A permission constant, defined in module framework.permissions |
| 105 project: The project to enforce permissions for. |
| 106 art: Optional artifact pb |
| 107 |
| 108 Returns: |
| 109 A boolean, whether the request can be satisfied, given the permission. |
| 110 """ |
| 111 perms = permissions.GetPermissions( |
| 112 mr.auth.user_pb, mr.auth.effective_ids, project) |
| 113 return perms.CanUsePerm( |
| 114 perm, mr.auth.effective_ids, project, permissions.GetRestrictions(art)) |
OLD | NEW |