Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 1 | # Copyright 2016 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 | """Classes for banning spammer users""" |
| 7 | from __future__ import print_function |
| 8 | from __future__ import division |
| 9 | from __future__ import absolute_import |
| 10 | |
| 11 | import logging |
| 12 | import json |
| 13 | import time |
| 14 | |
| 15 | from framework import cloud_tasks_helpers |
| 16 | from framework import framework_helpers |
| 17 | from framework import permissions |
| 18 | from framework import jsonfeed |
| 19 | from framework import servlet |
| 20 | from framework import urls |
| 21 | |
| 22 | class BanSpammer(servlet.Servlet): |
| 23 | """Ban a user and mark their content as spam""" |
| 24 | |
| 25 | def AssertBasePermission(self, mr): |
| 26 | """Check whether the user has any permission to ban users. |
| 27 | |
| 28 | Args: |
| 29 | mr: commonly used info parsed from the request. |
| 30 | """ |
| 31 | super(BanSpammer, self).AssertBasePermission(mr) |
| 32 | if not permissions.CanBan(mr, self.services): |
| 33 | raise permissions.PermissionException( |
| 34 | 'User is not allowed to ban users.') |
| 35 | |
| 36 | def ProcessFormData(self, mr, post_data): |
| 37 | self.AssertBasePermission(mr) |
| 38 | viewed_user_id = mr.viewed_user_auth.user_pb.user_id |
| 39 | reporter_id = mr.auth.user_id |
| 40 | |
| 41 | # First ban or un-ban the user as a spammer. |
| 42 | framework_helpers.UserSettings.ProcessBanForm( |
| 43 | mr.cnxn, self.services.user, post_data, mr.viewed_user_auth.user_id, |
| 44 | mr.viewed_user_auth.user_pb) |
| 45 | |
| 46 | params = { |
| 47 | 'spammer_id': viewed_user_id, |
| 48 | 'reporter_id': reporter_id, |
| 49 | 'is_spammer': 'banned' in post_data |
| 50 | } |
| 51 | task = cloud_tasks_helpers.generate_simple_task( |
| 52 | urls.BAN_SPAMMER_TASK + '.do', params) |
| 53 | cloud_tasks_helpers.create_task(task) |
| 54 | |
| 55 | return framework_helpers.FormatAbsoluteURL( |
| 56 | mr, mr.viewed_user_auth.user_view.profile_url, include_project=False, |
| 57 | saved=1, ts=int(time.time())) |
| 58 | |
| 59 | |
| 60 | class BanSpammerTask(jsonfeed.InternalTask): |
| 61 | """This task will update all of the comments and issues created by the |
| 62 | target user with is_spam=True, and also add a manual verdict attached |
| 63 | to the user who originated the ban request. This is a potentially long |
| 64 | running operation, so it is implemented as an async task. |
| 65 | """ |
| 66 | |
| 67 | def HandleRequest(self, mr): |
| 68 | spammer_id = mr.GetPositiveIntParam('spammer_id') |
| 69 | reporter_id = mr.GetPositiveIntParam('reporter_id') |
| 70 | is_spammer = mr.GetBoolParam('is_spammer') |
| 71 | |
| 72 | # Get all of the issues reported by the spammer. |
| 73 | issue_ids = self.services.issue.GetIssueIDsReportedByUser(mr.cnxn, |
| 74 | spammer_id) |
| 75 | |
| 76 | issues = [] |
| 77 | |
| 78 | if len(issue_ids) > 0: |
| 79 | issues = self.services.issue.GetIssues( |
| 80 | mr.cnxn, issue_ids, use_cache=False) |
| 81 | |
| 82 | # Mark them as spam/ham in bulk. |
| 83 | self.services.spam.RecordManualIssueVerdicts(mr.cnxn, self.services.issue, |
| 84 | issues, reporter_id, is_spammer) |
| 85 | |
| 86 | # Get all of the comments |
| 87 | comments = self.services.issue.GetCommentsByUser(mr.cnxn, spammer_id) |
| 88 | |
| 89 | for comment in comments: |
| 90 | self.services.spam.RecordManualCommentVerdict(mr.cnxn, |
| 91 | self.services.issue, self.services.user, comment.id, |
| 92 | reporter_id, is_spammer) |
| 93 | |
| 94 | self.response.body = json.dumps({ |
| 95 | 'comments': len(comments), |
| 96 | 'issues': len(issues), |
| 97 | }) |