Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(883)

Side by Side Diff: appengine/monorail/framework/servlet_helpers.py

Issue 1868553004: Open Source Monorail (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Rebase Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « appengine/monorail/framework/servlet.py ('k') | appengine/monorail/framework/sorting.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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))
OLDNEW
« no previous file with comments | « appengine/monorail/framework/servlet.py ('k') | appengine/monorail/framework/sorting.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698