Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 1 | # Copyright 2018 The Chromium Authors. All rights reserved. |
| 2 | # Use of this source code is governed 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 | """Context object to hold utility objects used during request processing. |
| 7 | """ |
| 8 | from __future__ import print_function |
| 9 | from __future__ import division |
| 10 | from __future__ import absolute_import |
| 11 | |
| 12 | import logging |
| 13 | |
| 14 | from framework import authdata |
| 15 | from framework import permissions |
| 16 | from framework import profiler |
| 17 | from framework import sql |
| 18 | from framework import template_helpers |
| 19 | |
| 20 | |
| 21 | class MonorailContext(object): |
| 22 | """Context with objects used in request handling mechanics. |
| 23 | |
| 24 | Attrributes: |
| 25 | cnxn: MonorailConnection to the SQL DB. |
| 26 | auth: AuthData object that identifies the account making the request. |
| 27 | perms: PermissionSet for requesting user, set by LookupLoggedInUserPerms(). |
| 28 | profiler: Profiler object. |
| 29 | warnings: A list of warnings to present to the user. |
| 30 | errors: A list of errors to present to the user. |
| 31 | |
| 32 | Unlike MonorailRequest, this object does not parse any part of the request, |
| 33 | retrieve any business objects (other than the User PB for the requesting |
| 34 | user), or check any permissions. |
| 35 | """ |
| 36 | |
| 37 | def __init__( |
| 38 | self, services, cnxn=None, requester=None, auth=None, perms=None, |
| 39 | autocreate=True): |
| 40 | """Construct a MonorailContext. |
| 41 | |
| 42 | Args: |
| 43 | services: Connection to backends. |
| 44 | cnxn: Optional connection to SQL database. |
| 45 | requester: String email address of user making the request or None. |
| 46 | auth: AuthData object used during testing. |
| 47 | perms: PermissionSet used during testing. |
| 48 | autocreate: Set to False to require that a row in the User table already |
| 49 | exists for this user, otherwise raise NoSuchUserException. |
| 50 | """ |
| 51 | self.cnxn = cnxn or sql.MonorailConnection() |
| 52 | self.auth = auth or authdata.AuthData.FromEmail( |
| 53 | self.cnxn, requester, services, autocreate=autocreate) |
| 54 | self.perms = perms # Usually None until LookupLoggedInUserPerms() called. |
| 55 | self.profiler = profiler.Profiler() |
| 56 | |
| 57 | # TODO(jrobbins): make self.errors not be UI-centric. |
| 58 | self.warnings = [] |
| 59 | self.errors = template_helpers.EZTError() |
| 60 | |
| 61 | def LookupLoggedInUserPerms(self, project): |
| 62 | """Look up perms for user making a request in project (can be None).""" |
| 63 | with self.profiler.Phase('looking up signed in user permissions'): |
| 64 | self.perms = permissions.GetPermissions( |
| 65 | self.auth.user_pb, self.auth.effective_ids, project) |
| 66 | |
| 67 | def CleanUp(self): |
| 68 | """Close the DB cnxn and any other clean up.""" |
| 69 | if self.cnxn: |
| 70 | self.cnxn.Close() |
| 71 | self.cnxn = None |
| 72 | |
| 73 | def __repr__(self): |
| 74 | """Return a string more useful for debugging.""" |
| 75 | return '%s(cnxn=%r, auth=%r, perms=%r)' % ( |
| 76 | self.__class__.__name__, self.cnxn, self.auth, self.perms) |