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 """A set of functions that provide persistence for users. |
| 7 |
| 8 Business objects are described in user_pb2.py. |
| 9 """ |
| 10 |
| 11 import logging |
| 12 |
| 13 import settings |
| 14 from framework import actionlimit |
| 15 from framework import framework_bizobj |
| 16 from framework import framework_constants |
| 17 from framework import framework_helpers |
| 18 from framework import sql |
| 19 from framework import validate |
| 20 from proto import user_pb2 |
| 21 from services import caches |
| 22 |
| 23 |
| 24 USER_TABLE_NAME = 'User' |
| 25 ACTIONLIMIT_TABLE_NAME = 'ActionLimit' |
| 26 DISMISSEDCUES_TABLE_NAME = 'DismissedCues' |
| 27 |
| 28 USER_COLS = [ |
| 29 'user_id', 'email', 'is_site_admin', 'notify_issue_change', |
| 30 'notify_starred_issue_change', 'banned', 'after_issue_update', |
| 31 'keep_people_perms_open', 'preview_on_hover', 'ignore_action_limits', |
| 32 'obscure_email'] |
| 33 ACTIONLIMIT_COLS = [ |
| 34 'user_id', 'action_kind', 'recent_count', 'reset_timestamp', |
| 35 'lifetime_count', 'lifetime_limit', 'period_soft_limit', |
| 36 'period_hard_limit'] |
| 37 DISMISSEDCUES_COLS = ['user_id', 'cue'] |
| 38 |
| 39 |
| 40 class UserTwoLevelCache(caches.AbstractTwoLevelCache): |
| 41 """Class to manage RAM and memcache for User PBs.""" |
| 42 |
| 43 def __init__(self, cache_manager, user_service): |
| 44 super(UserTwoLevelCache, self).__init__( |
| 45 cache_manager, 'user', 'user:', user_pb2.User, |
| 46 max_size=settings.user_cache_max_size) |
| 47 self.user_service = user_service |
| 48 |
| 49 def _DeserializeUsersByID( |
| 50 self, user_rows, actionlimit_rows, dismissedcue_rows): |
| 51 """Convert database row tuples into User PBs. |
| 52 |
| 53 Args: |
| 54 user_rows: rows from the User DB table. |
| 55 actionlimit_rows: rows from the ActionLimit DB table. |
| 56 dismissedcue_rows: rows from the DismissedCues DB table. |
| 57 |
| 58 Returns: |
| 59 A dict {user_id: user_pb} for all the users referenced in user_rows. |
| 60 """ |
| 61 result_dict = {} |
| 62 |
| 63 # Make one User PB for each row in user_rows. |
| 64 for row in user_rows: |
| 65 (user_id, email, is_site_admin, |
| 66 notify_issue_change, notify_starred_issue_change, banned, |
| 67 after_issue_update, keep_people_perms_open, preview_on_hover, |
| 68 ignore_action_limits, obscure_email) = row |
| 69 user = user_pb2.MakeUser() |
| 70 user.email = email |
| 71 user.is_site_admin = bool(is_site_admin) |
| 72 user.notify_issue_change = bool(notify_issue_change) |
| 73 user.notify_starred_issue_change = bool(notify_starred_issue_change) |
| 74 user.obscure_email = bool(obscure_email) |
| 75 if banned: |
| 76 user.banned = banned |
| 77 if after_issue_update: |
| 78 user.after_issue_update = user_pb2.IssueUpdateNav( |
| 79 after_issue_update.upper()) |
| 80 user.keep_people_perms_open = bool(keep_people_perms_open) |
| 81 user.preview_on_hover = bool(preview_on_hover) |
| 82 user.ignore_action_limits = bool(ignore_action_limits) |
| 83 result_dict[user_id] = user |
| 84 |
| 85 # Make an ActionLimit for each actionlimit row and attach it to a User PB. |
| 86 for row in actionlimit_rows: |
| 87 (user_id, action_type_name, recent_count, reset_timestamp, |
| 88 lifetime_count, lifetime_limit, period_soft_limit, |
| 89 period_hard_limit) = row |
| 90 if user_id not in result_dict: |
| 91 logging.error('Found action limits for missing user %r', user_id) |
| 92 continue |
| 93 user = result_dict[user_id] |
| 94 action_type = actionlimit.ACTION_TYPE_NAMES[action_type_name] |
| 95 al = actionlimit.GetLimitPB(user, action_type) |
| 96 al.recent_count = recent_count |
| 97 al.reset_timestamp = reset_timestamp |
| 98 al.lifetime_count = lifetime_count |
| 99 al.lifetime_limit = lifetime_limit |
| 100 al.period_soft_limit = period_soft_limit |
| 101 al.period_hard_limit = period_hard_limit |
| 102 |
| 103 # Build up a list of dismissed "cue card" help items for the users. |
| 104 for user_id, cue in dismissedcue_rows: |
| 105 if user_id not in result_dict: |
| 106 logging.error('Found dismissed cues for missing user %r', user_id) |
| 107 continue |
| 108 result_dict[user_id].dismissed_cues.append(cue) |
| 109 |
| 110 return result_dict |
| 111 |
| 112 def FetchItems(self, cnxn, keys): |
| 113 """On RAM and memcache miss, retrieve User objects from the database. |
| 114 |
| 115 Args: |
| 116 cnxn: connection to SQL database. |
| 117 keys: list of user IDs to retrieve. |
| 118 |
| 119 Returns: |
| 120 A dict {user_id: user_pb} for each user that satisfies the conditions. |
| 121 """ |
| 122 user_rows = self.user_service.user_tbl.Select( |
| 123 cnxn, cols=USER_COLS, user_id=keys) |
| 124 actionlimit_rows = self.user_service.actionlimit_tbl.Select( |
| 125 cnxn, cols=ACTIONLIMIT_COLS, user_id=keys) |
| 126 dismissedcues_rows = self.user_service.dismissedcues_tbl.Select( |
| 127 cnxn, cols=DISMISSEDCUES_COLS, user_id=keys) |
| 128 return self._DeserializeUsersByID( |
| 129 user_rows, actionlimit_rows, dismissedcues_rows) |
| 130 |
| 131 |
| 132 class UserService(object): |
| 133 """The persistence layer for all user data.""" |
| 134 |
| 135 def __init__(self, cache_manager): |
| 136 """Constructor. |
| 137 |
| 138 Args: |
| 139 cache_manager: local cache with distributed invalidation. |
| 140 """ |
| 141 self.user_tbl = sql.SQLTableManager(USER_TABLE_NAME) |
| 142 self.actionlimit_tbl = sql.SQLTableManager(ACTIONLIMIT_TABLE_NAME) |
| 143 self.dismissedcues_tbl = sql.SQLTableManager(DISMISSEDCUES_TABLE_NAME) |
| 144 |
| 145 # Like a dictionary {user_id: email} |
| 146 self.email_cache = cache_manager.MakeCache('user', max_size=50000) |
| 147 |
| 148 # Like a dictionary {email: user_id}. |
| 149 # This will never invaidate, and it doesn't need to. |
| 150 self.user_id_cache = cache_manager.MakeCache('user', max_size=50000) |
| 151 |
| 152 # Like a dictionary {user_id: user_pb} |
| 153 self.user_2lc = UserTwoLevelCache(cache_manager, self) |
| 154 |
| 155 ### Creating users |
| 156 |
| 157 def _CreateUsers(self, cnxn, emails): |
| 158 """Create many users in the database.""" |
| 159 emails = [email.lower() for email in emails] |
| 160 ids = [framework_helpers.MurmurHash3_x86_32(email) for email in emails] |
| 161 row_values = [ |
| 162 (user_id, email, not framework_bizobj.IsPriviledgedDomainUser(email)) |
| 163 for (user_id, email) in zip(ids, emails)] |
| 164 self.user_tbl.InsertRows( |
| 165 cnxn, ['user_id', 'email', 'obscure_email'], row_values) |
| 166 self.user_2lc.InvalidateKeys(cnxn, ids) |
| 167 |
| 168 ### Lookup of user ID and email address |
| 169 |
| 170 def LookupUserEmails(self, cnxn, user_ids): |
| 171 """Return a dict of email addresses for the given user IDs. |
| 172 |
| 173 Args: |
| 174 cnxn: connection to SQL database. |
| 175 user_ids: list of int user IDs to look up. |
| 176 |
| 177 Returns: |
| 178 A dict {user_id: email_addr} for all the requested IDs. |
| 179 |
| 180 Raises: |
| 181 NoSuchUserException: if any requested user cannot be found. |
| 182 """ |
| 183 self.email_cache.CacheItem(framework_constants.NO_USER_SPECIFIED, '') |
| 184 emails_dict, missed_ids = self.email_cache.GetAll(user_ids) |
| 185 if missed_ids: |
| 186 logging.info('got %d user emails from cache', len(emails_dict)) |
| 187 rows = self.user_tbl.Select( |
| 188 cnxn, cols=['user_id', 'email'], user_id=missed_ids) |
| 189 retrieved_dict = dict(rows) |
| 190 logging.info('looked up users %r', retrieved_dict) |
| 191 self.email_cache.CacheAll(retrieved_dict) |
| 192 emails_dict.update(retrieved_dict) |
| 193 |
| 194 # Check if there are any that we could not find. ID 0 means "no user". |
| 195 nonexist_ids = [user_id for user_id in user_ids |
| 196 if user_id and user_id not in emails_dict] |
| 197 if nonexist_ids: |
| 198 raise NoSuchUserException( |
| 199 'No email addresses found for users %r' % nonexist_ids) |
| 200 |
| 201 return emails_dict |
| 202 |
| 203 def LookupUserEmail(self, cnxn, user_id): |
| 204 """Get the email address of the given user. |
| 205 |
| 206 Args: |
| 207 cnxn: connection to SQL database. |
| 208 user_id: int user ID of the user whose email address is needed. |
| 209 |
| 210 Returns: |
| 211 String email address of that user or None if user_id is invalid. |
| 212 |
| 213 Raises: |
| 214 NoSuchUserException: if no email address was found for that user. |
| 215 """ |
| 216 if not user_id: |
| 217 return None |
| 218 emails_dict = self.LookupUserEmails(cnxn, [user_id]) |
| 219 return emails_dict[user_id] |
| 220 |
| 221 def LookupExistingUserIDs(self, cnxn, emails): |
| 222 """Return a dict of user IDs for the given emails for users that exist. |
| 223 |
| 224 Args: |
| 225 cnxn: connection to SQL database. |
| 226 emails: list of string email addresses. |
| 227 |
| 228 Returns: |
| 229 A dict {email_addr: user_id} for the requested emails. |
| 230 """ |
| 231 # Look up these users in the RAM cache |
| 232 user_id_dict, missed_emails = self.user_id_cache.GetAll(emails) |
| 233 logging.info('hit %d emails, missed %r', len(user_id_dict), missed_emails) |
| 234 |
| 235 # Hit the DB to lookup any user IDs that were not cached. |
| 236 if missed_emails: |
| 237 rows = self.user_tbl.Select( |
| 238 cnxn, cols=['email', 'user_id'], email=missed_emails) |
| 239 retrieved_dict = dict(rows) |
| 240 # Cache all the user IDs that we retrieved to make later requests faster. |
| 241 self.user_id_cache.CacheAll(retrieved_dict) |
| 242 user_id_dict.update(retrieved_dict) |
| 243 |
| 244 logging.info('looked up User IDs %r', user_id_dict) |
| 245 return user_id_dict |
| 246 |
| 247 def LookupUserIDs(self, cnxn, emails, autocreate=False, |
| 248 allowgroups=False): |
| 249 """Return a dict of user IDs for the given emails. |
| 250 |
| 251 Args: |
| 252 cnxn: connection to SQL database. |
| 253 emails: list of string email addresses. |
| 254 autocreate: set to True to create users that were not found. |
| 255 allowgroups: set to True to allow non-email user name for group |
| 256 creation. |
| 257 |
| 258 Returns: |
| 259 A dict {email_addr: user_id} for the requested emails. |
| 260 |
| 261 Raises: |
| 262 NoSuchUserException: if some users were not found and autocreate is |
| 263 False. |
| 264 """ |
| 265 # Skip any addresses that look like "--", because that means "no user". |
| 266 # Also, make sure all email addresses are lower case. |
| 267 needed_emails = [email.lower() for email in emails |
| 268 if not framework_constants.NO_VALUE_RE.match(email)] |
| 269 |
| 270 # Look up these users in the RAM cache |
| 271 user_id_dict = self.LookupExistingUserIDs(cnxn, needed_emails) |
| 272 if len(needed_emails) == len(user_id_dict): |
| 273 logging.info('found all %d emails', len(user_id_dict)) |
| 274 return user_id_dict |
| 275 |
| 276 # If any were not found in the DB, create them or raise an exception. |
| 277 nonexist_emails = [email for email in needed_emails |
| 278 if email not in user_id_dict] |
| 279 logging.info('nonexist_emails: %r, autocreate is %r', |
| 280 nonexist_emails, autocreate) |
| 281 if not autocreate: |
| 282 raise NoSuchUserException('%r' % nonexist_emails) |
| 283 |
| 284 if not allowgroups: |
| 285 # Only create accounts for valid email addresses. |
| 286 nonexist_emails = [email for email in nonexist_emails |
| 287 if validate.IsValidEmail(email)] |
| 288 if not nonexist_emails: |
| 289 return user_id_dict |
| 290 |
| 291 self._CreateUsers(cnxn, nonexist_emails) |
| 292 created_rows = self.user_tbl.Select( |
| 293 cnxn, cols=['email', 'user_id'], email=nonexist_emails) |
| 294 created_dict = dict(created_rows) |
| 295 # Cache all the user IDs that we retrieved to make later requests faster. |
| 296 self.user_id_cache.CacheAll(created_dict) |
| 297 user_id_dict.update(created_dict) |
| 298 |
| 299 logging.info('looked up User IDs %r', user_id_dict) |
| 300 return user_id_dict |
| 301 |
| 302 def LookupUserID(self, cnxn, email, autocreate=False, allowgroups=False): |
| 303 """Get one user ID for the given email address. |
| 304 |
| 305 Args: |
| 306 cnxn: connection to SQL database. |
| 307 email: string email address of the user to look up. |
| 308 autocreate: set to True to create users that were not found. |
| 309 allowgroups: set to True to allow non-email user name for group |
| 310 creation. |
| 311 |
| 312 Returns: |
| 313 The int user ID of the specified user. |
| 314 |
| 315 Raises: |
| 316 NoSuchUserException if the user was not found and autocreate is False. |
| 317 """ |
| 318 email = email.lower() |
| 319 email_dict = self.LookupUserIDs( |
| 320 cnxn, [email], autocreate=autocreate, allowgroups=allowgroups) |
| 321 if email not in email_dict: |
| 322 raise NoSuchUserException('%r not found' % email) |
| 323 return email_dict[email] |
| 324 |
| 325 ### Retrieval of user objects: with preferences, action limits, and cues |
| 326 |
| 327 def GetUsersByIDs(self, cnxn, user_ids, use_cache=True): |
| 328 """Return a dictionary of retrieved User PBs. |
| 329 |
| 330 Args: |
| 331 cnxn: connection to SQL database. |
| 332 user_ids: list of user IDs to fetch. |
| 333 use_cache: set to False to ignore cache and force DB lookup. |
| 334 |
| 335 Returns: |
| 336 A dict {user_id: user_pb} for each specified user ID. For any user ID |
| 337 that is not fount in the DB, a default User PB is created on-the-fly. |
| 338 """ |
| 339 # Check the RAM cache and memcache, as appropriate. |
| 340 result_dict, missed_ids = self.user_2lc.GetAll( |
| 341 cnxn, user_ids, use_cache=use_cache) |
| 342 |
| 343 # Provide default values for any user ID that was not found. |
| 344 result_dict.update( |
| 345 (user_id, user_pb2.MakeUser()) for user_id in missed_ids) |
| 346 |
| 347 return result_dict |
| 348 |
| 349 def GetUser(self, cnxn, user_id): |
| 350 """Load the specified user from the user details table.""" |
| 351 return self.GetUsersByIDs(cnxn, [user_id])[user_id] |
| 352 |
| 353 ### Updating user objects |
| 354 |
| 355 def UpdateUser(self, cnxn, user_id, user): |
| 356 """Store a user PB in the database. |
| 357 |
| 358 Args: |
| 359 cnxn: connection to SQL database. |
| 360 user_id: int user ID of the user to update. |
| 361 user: User PB to store. |
| 362 |
| 363 Returns: |
| 364 Nothing. |
| 365 """ |
| 366 delta = { |
| 367 'is_site_admin': user.is_site_admin, |
| 368 'notify_issue_change': user.notify_issue_change, |
| 369 'notify_starred_issue_change': user.notify_starred_issue_change, |
| 370 'banned': user.banned, |
| 371 'after_issue_update': str(user.after_issue_update or 'UP_TO_LIST'), |
| 372 'keep_people_perms_open': user.keep_people_perms_open, |
| 373 'preview_on_hover': user.preview_on_hover, |
| 374 'ignore_action_limits': user.ignore_action_limits, |
| 375 'obscure_email': user.obscure_email, |
| 376 } |
| 377 # Start sending UPDATE statements, but don't COMMIT until the end. |
| 378 self.user_tbl.Update(cnxn, delta, user_id=user_id, commit=False) |
| 379 |
| 380 # Add rows for any ActionLimits that are defined for this user. |
| 381 al_rows = [] |
| 382 if user.get_assigned_value('project_creation_limit'): |
| 383 al_rows.append(_ActionLimitToRow( |
| 384 user_id, 'project_creation', user.project_creation_limit)) |
| 385 if user.get_assigned_value('issue_comment_limit'): |
| 386 al_rows.append(_ActionLimitToRow( |
| 387 user_id, 'issue_comment', user.issue_comment_limit)) |
| 388 if user.get_assigned_value('issue_attachment_limit'): |
| 389 al_rows.append(_ActionLimitToRow( |
| 390 user_id, 'issue_attachment', user.issue_attachment_limit)) |
| 391 if user.get_assigned_value('issue_bulk_edit_limit'): |
| 392 al_rows.append(_ActionLimitToRow( |
| 393 user_id, 'issue_bulk_edit', user.issue_bulk_edit_limit)) |
| 394 if user.get_assigned_value('api_request_limit'): |
| 395 al_rows.append(_ActionLimitToRow( |
| 396 user_id, 'api_request', user.api_request_limit)) |
| 397 |
| 398 self.actionlimit_tbl.Delete(cnxn, user_id=user_id, commit=False) |
| 399 self.actionlimit_tbl.InsertRows( |
| 400 cnxn, ACTIONLIMIT_COLS, al_rows, commit=False) |
| 401 |
| 402 # Rewrite all the DismissedCues rows. |
| 403 cues_rows = [(user_id, cue) for cue in user.dismissed_cues] |
| 404 self.dismissedcues_tbl.Delete(cnxn, user_id=user_id, commit=False) |
| 405 self.dismissedcues_tbl.InsertRows( |
| 406 cnxn, DISMISSEDCUES_COLS, cues_rows, commit=False) |
| 407 |
| 408 cnxn.Commit() |
| 409 self.user_2lc.InvalidateKeys(cnxn, [user_id]) |
| 410 |
| 411 def UpdateUserSettings( |
| 412 self, cnxn, user_id, user, notify=None, notify_starred=None, |
| 413 obscure_email=None, after_issue_update=None, |
| 414 is_site_admin=None, ignore_action_limits=None, |
| 415 is_banned=None, banned_reason=None, action_limit_updates=None, |
| 416 dismissed_cues=None, keep_people_perms_open=None, preview_on_hover=None): |
| 417 """Update the preferences of the specified user. |
| 418 |
| 419 Args: |
| 420 cnxn: connection to SQL database. |
| 421 user_id: int user ID of the user whose settings we are updating. |
| 422 user: User PB of user before changes are applied. |
| 423 keyword args: dictionary of setting names mapped to new values. |
| 424 |
| 425 Returns: |
| 426 The user's new User PB. |
| 427 """ |
| 428 # notifications |
| 429 if notify is not None: |
| 430 user.notify_issue_change = notify |
| 431 if notify_starred is not None: |
| 432 user.notify_starred_issue_change = notify_starred |
| 433 |
| 434 # display options |
| 435 if after_issue_update is not None: |
| 436 user.after_issue_update = user_pb2.IssueUpdateNav(after_issue_update) |
| 437 if preview_on_hover is not None: |
| 438 user.preview_on_hover = preview_on_hover |
| 439 if dismissed_cues: # Note, we never set it back to []. |
| 440 user.dismissed_cues = dismissed_cues |
| 441 if keep_people_perms_open is not None: |
| 442 user.keep_people_perms_open = keep_people_perms_open |
| 443 |
| 444 # misc |
| 445 if obscure_email is not None: |
| 446 user.obscure_email = obscure_email |
| 447 |
| 448 # admin |
| 449 if is_site_admin is not None: |
| 450 user.is_site_admin = is_site_admin |
| 451 if ignore_action_limits is not None: |
| 452 user.ignore_action_limits = ignore_action_limits |
| 453 if is_banned is not None: |
| 454 if is_banned: |
| 455 user.banned = banned_reason or 'No reason given' |
| 456 else: |
| 457 user.reset('banned') |
| 458 |
| 459 # action limits |
| 460 if action_limit_updates: |
| 461 self._UpdateActionLimits(user, action_limit_updates) |
| 462 |
| 463 # Write the user settings to the database. |
| 464 self.UpdateUser(cnxn, user_id, user) |
| 465 |
| 466 def _UpdateActionLimits(self, user, action_limit_updates): |
| 467 """Apply action limit updates to a user's account.""" |
| 468 for action, new_limit_tuple in action_limit_updates.iteritems(): |
| 469 if action in actionlimit.ACTION_TYPE_NAMES: |
| 470 action_type = actionlimit.ACTION_TYPE_NAMES[action] |
| 471 if new_limit_tuple is None: |
| 472 actionlimit.ResetRecentActions(user, action_type) |
| 473 else: |
| 474 new_soft_limit, new_hard_limit, new_lifetime_limit = new_limit_tuple |
| 475 |
| 476 pb_getter = action + '_limit' |
| 477 old_lifetime_limit = getattr(user, pb_getter).lifetime_limit |
| 478 old_soft_limit = getattr(user, pb_getter).period_soft_limit |
| 479 old_hard_limit = getattr(user, pb_getter).period_hard_limit |
| 480 |
| 481 if ((new_lifetime_limit >= 0 and |
| 482 new_lifetime_limit != old_lifetime_limit) or |
| 483 (new_soft_limit >= 0 and new_soft_limit != old_soft_limit) or |
| 484 (new_hard_limit >= 0 and new_hard_limit != old_hard_limit)): |
| 485 actionlimit.CustomizeLimit(user, action_type, new_soft_limit, |
| 486 new_hard_limit, new_lifetime_limit) |
| 487 |
| 488 |
| 489 def _ActionLimitToRow(user_id, action_kind, al): |
| 490 """Return a tuple for an SQL table row for an action limit.""" |
| 491 return (user_id, action_kind, al.recent_count, al.reset_timestamp, |
| 492 al.lifetime_count, al.lifetime_limit, al.period_soft_limit, |
| 493 al.period_hard_limit) |
| 494 |
| 495 |
| 496 class Error(Exception): |
| 497 """Base class for errors from this module.""" |
| 498 pass |
| 499 |
| 500 |
| 501 class NoSuchUserException(Error): |
| 502 """No user with the specified name exists.""" |
| 503 pass |
OLD | NEW |