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 | |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 11 | import json |
| 12 | import time |
| 13 | |
| 14 | from framework import cloud_tasks_helpers |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 15 | from framework import flaskservlet |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 16 | from framework import framework_helpers |
| 17 | from framework import permissions |
| 18 | from framework import jsonfeed |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 19 | from framework import urls |
| 20 | |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame^] | 21 | |
| 22 | class BanSpammer(flaskservlet.FlaskServlet): |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 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 | |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame^] | 59 | def PostBanSpammerPage(self, **kwargs): |
| 60 | return self.handler(**kwargs) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 61 | |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 62 | |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame^] | 63 | class BanSpammerTask(jsonfeed.FlaskInternalTask): |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 64 | """This task will update all of the comments and issues created by the |
| 65 | target user with is_spam=True, and also add a manual verdict attached |
| 66 | to the user who originated the ban request. This is a potentially long |
| 67 | running operation, so it is implemented as an async task. |
| 68 | """ |
| 69 | |
| 70 | def HandleRequest(self, mr): |
| 71 | spammer_id = mr.GetPositiveIntParam('spammer_id') |
| 72 | reporter_id = mr.GetPositiveIntParam('reporter_id') |
| 73 | is_spammer = mr.GetBoolParam('is_spammer') |
| 74 | |
| 75 | # Get all of the issues reported by the spammer. |
| 76 | issue_ids = self.services.issue.GetIssueIDsReportedByUser(mr.cnxn, |
| 77 | spammer_id) |
| 78 | |
| 79 | issues = [] |
| 80 | |
| 81 | if len(issue_ids) > 0: |
| 82 | issues = self.services.issue.GetIssues( |
| 83 | mr.cnxn, issue_ids, use_cache=False) |
| 84 | |
| 85 | # Mark them as spam/ham in bulk. |
| 86 | self.services.spam.RecordManualIssueVerdicts(mr.cnxn, self.services.issue, |
| 87 | issues, reporter_id, is_spammer) |
| 88 | |
| 89 | # Get all of the comments |
| 90 | comments = self.services.issue.GetCommentsByUser(mr.cnxn, spammer_id) |
| 91 | |
| 92 | for comment in comments: |
| 93 | self.services.spam.RecordManualCommentVerdict(mr.cnxn, |
| 94 | self.services.issue, self.services.user, comment.id, |
| 95 | reporter_id, is_spammer) |
| 96 | |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame^] | 97 | return json.dumps({ |
| 98 | 'comments': len(comments), |
| 99 | 'issues': len(issues), |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 100 | }) |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 101 | |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame^] | 102 | def PostBanSpammer(self, **kwargs): |
| 103 | return self.handler(**kwargs) |