Skip to content

fix(alerts): Make clear that superuser may edit comments #18455

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@ def convert_args(self, request, activity_id, *args, **kwargs):
args, kwargs = super(CommentDetailsEndpoint, self).convert_args(request, *args, **kwargs)

try:
# Superusers may mutate any comment
user_filter = {} if request.user.is_superuser else {"user": request.user}

kwargs["activity"] = IncidentActivity.objects.get(
id=activity_id,
user=request.user,
incident=kwargs["incident"],
# Only allow modifying comments
type=IncidentActivityType.COMMENT.value,
**user_filter
)
except IncidentActivity.DoesNotExist:
raise ResourceDoesNotExist
Expand Down
53 changes: 32 additions & 21 deletions src/sentry/static/sentry/app/components/activity/note/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import ConfigStore from 'app/stores/configStore';
import LinkWithConfirmation from 'app/components/links/linkWithConfirmation';
import {User} from 'app/types';
import {Theme} from 'app/utils/theme';
import Tooltip from 'app/components/tooltip';

import EditorTools from './editorTools';

Expand All @@ -17,28 +18,38 @@ type Props = {
onDelete: () => void;
};

function canEdit(editingUser: User) {
const user = ConfigStore.get('user');
return user && (user.isSuperuser || user.id === editingUser.id);
}
const NoteHeader = ({authorName, user, onEdit, onDelete}: Props) => {
const activeUser = ConfigStore.get('user');
const canEdit = activeUser && (activeUser.isSuperuser || user.id === activeUser.id);

const NoteHeader = ({authorName, user, onEdit, onDelete}: Props) => (
<div>
<ActivityAuthor>{authorName}</ActivityAuthor>
{canEdit(user) && (
<EditorTools>
<Edit onClick={onEdit}>{t('Edit')}</Edit>
<LinkWithConfirmation
title={t('Remove')}
message={t('Are you sure you wish to delete this comment?')}
onConfirm={onDelete}
>
<Remove>{t('Remove')}</Remove>
</LinkWithConfirmation>
</EditorTools>
)}
</div>
);
return (
<div>
<ActivityAuthor>{authorName}</ActivityAuthor>
{canEdit && (
<EditorTools>
<Tooltip
title={t('You can edit this comment due to your superuser status')}
disabled={!activeUser.isSuperuser}
>
<Edit onClick={onEdit}>{t('Edit')}</Edit>
</Tooltip>
<Tooltip
title={t('You can delete this comment due to your superuser status')}
disabled={!activeUser.isSuperuser}
>
<LinkWithConfirmation
title={t('Remove')}
message={t('Are you sure you wish to delete this comment?')}
onConfirm={onDelete}
>
<Remove>{t('Remove')}</Remove>
</LinkWithConfirmation>
</Tooltip>
</EditorTools>
)}
</div>
);
};

const getActionStyle = (p: {theme: Theme}) => `
padding: 0 7px;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ def setUp(self):
self.incident, user=self.user, type=IncidentActivityType.DETECTED.value
)

user2 = self.create_user()
self.user2_activity = self.create_incident_comment(
incident=self.incident, user=user2, comment="hello from another user"
)

@fixture
def organization(self):
return self.create_organization()
Expand Down Expand Up @@ -76,6 +81,34 @@ def test_simple(self):
assert activity.user == self.user
assert activity.comment == comment

def test_cannot_edit_others_comment(self):
with self.feature("organizations:incidents"):
self.get_valid_response(
self.organization.slug,
self.incident.identifier,
self.user2_activity.id,
comment="edited comment",
status_code=404,
)

def test_superuser_can_edit(self):
self.user.is_superuser = True
self.user.save()

edited_comment = "this comment has been edited"

with self.feature("organizations:incidents"):
self.get_valid_response(
self.organization.slug,
self.incident.identifier,
self.user2_activity.id,
comment=edited_comment,
status_code=200,
)
activity = IncidentActivity.objects.get(id=self.user2_activity.id)
assert activity.user != self.user
assert activity.comment == edited_comment


class OrganizationIncidentCommentDeleteEndpointTest(BaseIncidentCommentDetailsTest, APITestCase):
method = "delete"
Expand All @@ -86,3 +119,25 @@ def test_simple(self):
self.organization.slug, self.incident.identifier, self.activity.id, status_code=204
)
assert not IncidentActivity.objects.filter(id=self.activity.id).exists()

def test_cannot_delete_others_comments(self):
with self.feature("organizations:incidents"):
self.get_valid_response(
self.organization.slug,
self.incident.identifier,
self.user2_activity.id,
status_code=404,
)

def test_superuser_can_delete(self):
self.user.is_superuser = True
self.user.save()

with self.feature("organizations:incidents"):
self.get_valid_response(
self.organization.slug,
self.incident.identifier,
self.user2_activity.id,
status_code=204,
)
assert not IncidentActivity.objects.filter(id=self.user2_activity.id).exists()