From ee2eb7de3865e97dd48030da6cf48606d95fe152 Mon Sep 17 00:00:00 2001 From: "mkanat%bugzilla.org" <> Date: Mon, 2 Jul 2007 09:16:08 +0000 Subject: Bug 384651: Make CC adding and removal use Bugzilla::Bug in process_bug Patch By Max Kanat-Alexander r=LpSolit, a=LpSolit --- Bugzilla/Bug.pm | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Bugzilla/User.pm | 13 ++++++++++ 2 files changed, 92 insertions(+) (limited to 'Bugzilla') diff --git a/Bugzilla/Bug.pm b/Bugzilla/Bug.pm index c3be53e96..56018cebd 100755 --- a/Bugzilla/Bug.pm +++ b/Bugzilla/Bug.pm @@ -471,6 +471,40 @@ sub update { return $changes; } +# XXX Temporary hack until all of process_bug uses update(). +sub update_cc { + my $self = shift; + + my $dbh = Bugzilla->dbh; + my $delta_ts = shift || $dbh->selectrow_array("SELECT NOW()"); + + my $old_bug = $self->new($self->id); + my @old_cc = map {$_->id} @{$old_bug->cc_users}; + my @new_cc = map {$_->id} @{$self->cc_users}; + my ($removed, $added) = diff_arrays(\@old_cc, \@new_cc); + + if (scalar @$removed) { + $dbh->do('DELETE FROM cc WHERE bug_id = ? AND who IN (' . + join(',', @$removed) . ')', undef, $self->id); + } + foreach my $user_id (@$added) { + $dbh->do('INSERT INTO cc (bug_id, who) VALUES (?,?)', + undef, $self->id, $user_id); + } + my $removed_users = Bugzilla::User->new_from_list($removed); + my $added_users = Bugzilla::User->new_from_list($added); + + # If any changes were found, record it in the activity log + if (scalar @$removed || scalar @$added) { + my $removed_names = join ', ', (map {$_->login} @$removed_users); + my $added_names = join ', ', (map {$_->login} @$added_users); + LogActivityEntry($self->id, "cc", $removed_names, $added_names, + Bugzilla->user->id, $delta_ts); + } + + return ($removed_users, $added_users); +} + # This is the correct way to delete bugs from the DB. # No bug should be deleted from anywhere else except from here. # @@ -1034,6 +1068,38 @@ sub set_status { # "Add/Remove" Methods # ######################## +# Accepts a User object or a username. Adds the user only if they +# don't already exist as a CC on the bug. +sub add_cc { + # XXX $product is a temporary hack until all of process_bug uses Bug + # objects for updating. + my ($self, $user_or_name, $product) = @_; + return if !$user_or_name; + my $user = ref $user_or_name ? $user_or_name + : Bugzilla::User::check($user_or_name); + + my $product_id = $product ? $product->id : $self->{product_id}; + if (Bugzilla->params->{strict_isolation} + && !$user->can_edit_product($product_id)) + { + ThrowUserError('invalid_user_group', { users => $user->login, + bug_id => $self->id }); + } + + my $cc_users = $self->cc_users; + push(@$cc_users, $user) if !grep($_->id == $user->id, @$cc_users); +} + +# Accepts a User object or a username. Removes the User if they exist +# in the list, but doesn't throw an error if they don't exist. +sub remove_cc { + my ($self, $user_or_name) = @_; + my $user = ref $user_or_name ? $user_or_name + : Bugzilla::User::check($user_or_name); + my $cc_users = $self->cc_users; + @$cc_users = grep { $_->id != $user->id } @$cc_users; +} + # $bug->add_comment("comment", {isprivate => 1, work_time => 10.5, # type => CMT_NORMAL, extra_data => $data}); sub add_comment { @@ -1185,6 +1251,19 @@ sub cc { return $self->{'cc'}; } +# XXX Eventually this will become the standard "cc" method used everywhere. +sub cc_users { + my $self = shift; + return $self->{'cc_users'} if exists $self->{'cc_users'}; + return [] if $self->{'error'}; + + my $dbh = Bugzilla->dbh; + my $cc_ids = $dbh->selectcol_arrayref( + 'SELECT who FROM cc WHERE bug_id = ?', undef, $self->id); + $self->{'cc_users'} = Bugzilla::User->new_from_list($cc_ids); + return $self->{'cc_users'}; +} + sub component { my ($self) = @_; return $self->{component} if exists $self->{component}; diff --git a/Bugzilla/User.pm b/Bugzilla/User.pm index a14549f84..f13b94fbf 100644 --- a/Bugzilla/User.pm +++ b/Bugzilla/User.pm @@ -131,6 +131,14 @@ sub new { return $class->SUPER::new(@_); } +sub check { + my ($username) = @_; + $username = trim($username); + my $user = new Bugzilla::User({ name => $username }) + || ThrowUserError('invalid_username', { name => $username }); + return $user; +} + sub update { my $self = shift; my $changes = $self->SUPER::update(@_); @@ -2072,6 +2080,11 @@ Params: login_name - B The login name for the new user. disable_mail - If 1, bug-related mail will not be sent to this user; if 0, mail will be sent depending on the user's email preferences. +=item C + +Takes a username as its only argument. Throws an error if there is no +user with that username. Returns a C object. + =item C Returns a boolean indicating whether or not the supplied username is -- cgit v1.2.3-24-g4f1b