Extension.pm 30.9 KB
Newer Older
1 2 3
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
#
5 6
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.
7 8

package Bugzilla::Extension::Example;
9 10

use 5.10.1;
11
use strict;
12 13
use warnings;

14
use base qw(Bugzilla::Extension);
15

16
use Bugzilla::Constants;
17
use Bugzilla::Error;
18 19
use Bugzilla::Group;
use Bugzilla::User;
20
use Bugzilla::User::Setting;
21
use Bugzilla::Util qw(diff_arrays html_quote remote_ip);
22
use Bugzilla::Status qw(is_open_state);
23
use Bugzilla::Install::Filesystem;
24
use Bugzilla::WebService::Constants;
25 26 27 28

# This is extensions/Example/lib/Util.pm. I can load this here in my
# Extension.pm only because I have a Config.pm.
use Bugzilla::Extension::Example::Util;
29 30 31

use Data::Dumper;

32 33 34
# See bugmail_relationships.
use constant REL_EXAMPLE => -127;

35 36
our $VERSION = '1.0';

37
sub user_can_administer {
38 39
  my ($self, $args) = @_;
  my $can_administer = $args->{can_administer};
40

41 42 43 44 45 46
  # If you add an option to the admin pages (e.g. by using the Hooks in
  # template/en/default/admin/admin.html.tmpl), you may want to allow
  # users in another group view admin.cgi
  #if (Bugzilla->user->in_group('other_group')) {
  #    $$can_administer = 1;
  #}
47 48
}

49
sub admin_editusers_action {
50 51 52 53 54 55 56 57 58 59 60 61
  my ($self, $args) = @_;
  my ($vars, $action, $user) = @$args{qw(vars action user)};
  my $template = Bugzilla->template;

  if ($action eq 'my_action') {

    # Allow to restrict the search to any group the user is allowed to bless.
    $vars->{'restrictablegroups'} = $user->bless_groups();
    $template->process('admin/users/search.html.tmpl', $vars)
      || ThrowTemplateError($template->error());
    exit;
  }
62 63
}

64
sub attachment_process_data {
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
  my ($self, $args) = @_;
  my $type     = $args->{attributes}->{mimetype};
  my $filename = $args->{attributes}->{filename};

  # Make sure images have the correct extension.
  # Uncomment the two lines below to make this check effective.
  if ($type =~ /^image\/(\w+)$/) {
    my $format = $1;
    if ($filename =~ /^(.+)(:?\.[^\.]+)$/) {
      my $name = $1;

      #$args->{attributes}->{filename} = "${name}.$format";
    }
    else {
      # The file has no extension. We append it.
      #$args->{attributes}->{filename} .= ".$format";
81
    }
82
  }
83 84 85
}

sub auth_login_methods {
86 87 88 89 90
  my ($self, $args) = @_;
  my $modules = $args->{modules};
  if (exists $modules->{Example}) {
    $modules->{Example} = 'Bugzilla/Extension/Example/Auth/Login.pm';
  }
91 92 93
}

sub auth_verify_methods {
94 95 96 97 98
  my ($self, $args) = @_;
  my $modules = $args->{modules};
  if (exists $modules->{Example}) {
    $modules->{Example} = 'Bugzilla/Extension/Example/Auth/Verify.pm';
  }
99 100
}

101
sub bug_check_can_change_field {
102
  my ($self, $args) = @_;
103

104 105 106 107
  my ($bug, $field, $new_value, $old_value, $priv_results)
    = @$args{qw(bug field new_value old_value priv_results)};

  my $user = Bugzilla->user;
108

109 110 111 112 113 114
  # Disallow a bug from being reopened if currently closed unless user
  # is in 'admin' group
  if ($field eq 'bug_status' && $bug->product_obj->name eq 'Example') {
    if (!is_open_state($old_value)
      && is_open_state($new_value)
      && !$user->in_group('admin'))
115
    {
116 117
      push(@$priv_results, PRIVILEGES_REQUIRED_EMPOWERED);
      return;
118
    }
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
  }

  # Disallow a bug's keywords from being edited unless user is the
  # reporter of the bug
  if ( $field eq 'keywords'
    && $bug->product_obj->name eq 'Example'
    && $user->login ne $bug->reporter->login)
  {
    push(@$priv_results, PRIVILEGES_REQUIRED_REPORTER);
    return;
  }

  # Allow updating of priority even if user cannot normally edit the bug
  # and they are in group 'engineering'
  if ( $field eq 'priority'
    && $bug->product_obj->name eq 'Example'
    && $user->in_group('engineering'))
  {
    push(@$priv_results, PRIVILEGES_REQUIRED_NONE);
    return;
  }
140 141
}

142
sub bug_columns {
143 144 145
  my ($self, $args) = @_;
  my $columns = $args->{'columns'};
  push(@$columns, "delta_ts AS example");
146 147 148
}

sub bug_end_of_create {
149 150 151 152 153 154 155 156
  my ($self, $args) = @_;

  # This code doesn't actually *do* anything, it's just here to show you
  # how to use this hook.
  my $bug       = $args->{'bug'};
  my $timestamp = $args->{'timestamp'};

  my $bug_id = $bug->id;
157

158 159 160
  # Uncomment this line to see a line in your webserver's error log whenever
  # you file a bug.
  # warn "Bug $bug_id has been filed!";
161 162 163
}

sub bug_end_of_create_validators {
164
  my ($self, $args) = @_;
165

166 167 168
  # This code doesn't actually *do* anything, it's just here to show you
  # how to use this hook.
  my $bug_params = $args->{'params'};
169

170 171 172
  # Uncomment this line below to see a line in your webserver's error log
  # containing all validated bug field values every time you file a bug.
  # warn Dumper($bug_params);
173

174 175 176 177
  # This would remove all ccs from the bug, preventing ANY ccs from being
  # added on bug creation.
  # $bug_params->{cc} = [];
}
178

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
sub bug_start_of_update {
  my ($self, $args) = @_;

  # This code doesn't actually *do* anything, it's just here to show you
  # how to use this hook.
  my ($bug, $old_bug, $timestamp, $changes)
    = @$args{qw(bug old_bug timestamp changes)};

  foreach my $field (keys %$changes) {
    my $used_to_be = $changes->{$field}->[0];
    my $now_it_is  = $changes->{$field}->[1];
  }

  my $old_summary = $old_bug->short_desc;

  my $status_message;
  if (my $status_change = $changes->{'bug_status'}) {
    my $old_status = new Bugzilla::Status({name => $status_change->[0]});
    my $new_status = new Bugzilla::Status({name => $status_change->[1]});
    if ($new_status->is_open && !$old_status->is_open) {
      $status_message = "Bug re-opened!";
    }
    if (!$new_status->is_open && $old_status->is_open) {
      $status_message = "Bug closed!";
203
    }
204 205 206 207 208 209
  }

  my $bug_id      = $bug->id;
  my $num_changes = scalar keys %$changes;
  my $result      = "There were $num_changes changes to fields on bug $bug_id"
    . " at $timestamp.";
210

211 212 213
  # Uncomment this line to see $result in your webserver's error log whenever
  # you update a bug.
  # warn $result;
214 215
}

216
sub bug_end_of_update {
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
  my ($self, $args) = @_;

  # This code doesn't actually *do* anything, it's just here to show you
  # how to use this hook.
  my ($bug, $old_bug, $timestamp, $changes)
    = @$args{qw(bug old_bug timestamp changes)};

  foreach my $field (keys %$changes) {
    my $used_to_be = $changes->{$field}->[0];
    my $now_it_is  = $changes->{$field}->[1];
  }

  my $old_summary = $old_bug->short_desc;

  my $status_message;
  if (my $status_change = $changes->{'bug_status'}) {
    my $old_status = new Bugzilla::Status({name => $status_change->[0]});
    my $new_status = new Bugzilla::Status({name => $status_change->[1]});
    if ($new_status->is_open && !$old_status->is_open) {
      $status_message = "Bug re-opened!";
237
    }
238 239
    if (!$new_status->is_open && $old_status->is_open) {
      $status_message = "Bug closed!";
240
    }
241 242 243 244 245 246 247 248 249 250
  }

  my $bug_id      = $bug->id;
  my $num_changes = scalar keys %$changes;
  my $result      = "There were $num_changes changes to fields on bug $bug_id"
    . " at $timestamp.";

  # Uncomment this line to see $result in your webserver's error log whenever
  # you update a bug.
  # warn $result;
251 252 253
}

sub bug_fields {
254
  my ($self, $args) = @_;
255

256 257
  my $fields = $args->{'fields'};
  push(@$fields, "example");
258 259 260
}

sub bug_format_comment {
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
  my ($self, $args) = @_;

  # This replaces every occurrence of the word "foo" with the word
  # "bar"

  my $regexes = $args->{'regexes'};
  push(@$regexes, {match => qr/\bfoo\b/, replace => 'bar'});

  # And this links every occurrence of the word "bar" to example.com,
  # but it won't affect "foo"s that have already been turned into "bar"
  # above (because each regex is run in order, and later regexes don't modify
  # earlier matches, due to some cleverness in Bugzilla's internals).
  #
  # For example, the phrase "foo bar" would become:
  # bar <a href="http://example.com/bar">bar</a>
  my $bar_match = qr/\b(bar)\b/;
  push(@$regexes, {match => $bar_match, replace => \&_replace_bar});
278 279 280 281
}

# Used by bug_format_comment--see its code for an explanation.
sub _replace_bar {
282 283 284 285 286 287 288 289 290 291 292
  my $args = shift;

  # $match is the first parentheses match in the $bar_match regex
  # in bug-format_comment.pl. We get up to 10 regex matches as
  # arguments to this function.
  my $match = $args->{matches}->[0];

  # Remember, you have to HTML-escape any data that you are returning!
  $match = html_quote($match);
  return qq{<a href="http://example.com/">$match</a>};
}
293 294

sub buglist_columns {
295 296 297 298 299 300
  my ($self, $args) = @_;

  my $columns = $args->{'columns'};
  $columns->{'example'} = {'name' => 'bugs.delta_ts', 'title' => 'Example'};
  $columns->{'product_desc'}
    = {'name' => 'prod_desc.description', 'title' => 'Product Description'};
301 302 303
}

sub buglist_column_joins {
304 305
  my ($self, $args) = @_;
  my $joins = $args->{'column_joins'};
306

307 308 309 310 311 312 313 314
  # This column is added using the "buglist_columns" hook
  $joins->{'product_desc'} = {
    from  => 'product_id',
    to    => 'id',
    table => 'products',
    as    => 'prod_desc',
    join  => 'INNER',
  };
315 316
}

317
sub search_operator_field_override {
318
  my ($self, $args) = @_;
319

320 321 322 323 324 325
  my $operators = $args->{'operators'};

  my $original = $operators->{component}->{_non_changed};
  $operators->{component} = {
    _non_changed => sub { _component_nonchanged($original, @_) }
  };
326 327 328
}

sub _component_nonchanged {
329 330 331 332
  my $original = shift;
  my ($invocant, $args) = @_;

  $invocant->$original($args);
333

334 335 336
  # Actually, it does not change anything in the result,
  # just an example.
  $args->{term} = $args->{term} . " OR 1=2";
337 338
}

339
sub bugmail_recipients {
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
  my ($self, $args) = @_;
  my $recipients = $args->{recipients};
  my $bug        = $args->{bug};

  my $user = new Bugzilla::User({name => Bugzilla->params->{'maintainer'}});

  if ($bug->id == 1) {

    # Uncomment the line below to add the maintainer to the recipients
    # list of every bugmail from bug 1 as though that the maintainer
    # were on the CC list.
    #$recipients->{$user->id}->{+REL_CC} = 1;

    # And this line adds the maintainer as though they had the
    # "REL_EXAMPLE" relationship from the bugmail_relationships hook below.
    #$recipients->{$user->id}->{+REL_EXAMPLE} = 1;
  }
357 358
}

359
sub bugmail_relationships {
360 361 362
  my ($self, $args) = @_;
  my $relationships = $args->{relationships};
  $relationships->{+REL_EXAMPLE} = 'Example';
363 364
}

365
sub cgi_headers {
366 367
  my ($self, $args) = @_;
  my $headers = $args->{'headers'};
Byron Jones's avatar
Byron Jones committed
368

369
  $headers->{'-x_test_header'} = "Test header from Example extension";
370 371
}

372
sub config_add_panels {
373 374 375 376
  my ($self, $args) = @_;

  my $modules = $args->{panel_modules};
  $modules->{Example} = "Bugzilla::Extension::Example::Config";
377 378 379
}

sub config_modify_panels {
380 381 382
  my ($self, $args) = @_;

  my $panels = $args->{panels};
383

384 385 386 387
  # Add the "Example" auth methods.
  my $auth_params = $panels->{'auth'}->{params};
  my ($info_class)   = grep($_->{name} eq 'user_info_class',   @$auth_params);
  my ($verify_class) = grep($_->{name} eq 'user_verify_class', @$auth_params);
388

389 390 391 392 393 394 395 396 397 398 399 400
  push(@{$info_class->{choices}},   'CGI,Example');
  push(@{$verify_class->{choices}}, 'Example');

  push(
    @$auth_params,
    {
      name    => 'param_example',
      type    => 't',
      default => 0,
      checker => \&check_numeric
    }
  );
401 402
}

403
sub db_schema_abstract_schema {
404 405
  my ($self, $args) = @_;

406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
#    $args->{'schema'}->{'example_table'} = {
#        FIELDS => [
#            id       => {TYPE => 'SMALLSERIAL', NOTNULL => 1,
#                     PRIMARYKEY => 1},
#            for_key  => {TYPE => 'INT3', NOTNULL => 1,
#                           REFERENCES  => {TABLE  =>  'example_table2',
#                                           COLUMN =>  'id',
#                                           DELETE => 'CASCADE'}},
#            col_3    => {TYPE => 'varchar(64)', NOTNULL => 1},
#        ],
#        INDEXES => [
#            id_index_idx   => {FIELDS => ['col_3'], TYPE => 'UNIQUE'},
#            for_id_idx => ['for_key'],
#        ],
#    };
}

423
sub email_in_before_parse {
424
  my ($self, $args) = @_;
425

426 427 428 429 430 431
  my $subject = $args->{mail}->header('Subject');

  # Correctly extract the bug ID from email subjects of the form [Bug comp/NNN].
  if ($subject =~ /\[.*(\d+)\].*/) {
    $args->{fields}->{bug_id} = $1;
  }
432 433 434
}

sub email_in_after_parse {
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
  my ($self, $args) = @_;
  my $reporter = $args->{fields}->{reporter};
  my $dbh      = Bugzilla->dbh;

  # No other check needed if this is a valid regular user.
  return if login_to_id($reporter);

  # The reporter is not a regular user. We create an account for them,
  # but they can only comment on existing bugs.
  # This is useful for people who reply by email to bugmails received
  # in mailing-lists.
  if ($args->{fields}->{bug_id}) {

    # WARNING: we return now to skip the remaining code below.
    # You must understand that removing this line would make the code
    # below effective! Do it only if you are OK with the behavior
    # described here.
    return;

    Bugzilla::User->create({login_name => $reporter, cryptpassword => '*'});

    # For security reasons, delete all fields unrelated to comments.
    foreach my $field (keys %{$args->{fields}}) {
      next if $field =~ /^(?:bug_id|comment|reporter)$/;
      delete $args->{fields}->{$field};
460
    }
461 462 463 464
  }
  else {
    ThrowUserError('invalid_username', {name => $reporter});
  }
465 466
}

467
sub enter_bug_entrydefaultvars {
468 469 470 471
  my ($self, $args) = @_;

  my $vars = $args->{vars};
  $vars->{'example'} = 1;
472 473
}

474
sub error_catch {
475 476 477 478 479 480 481 482 483
  my ($self, $args) = @_;

  # Customize the error message displayed when someone tries to access
  # page.cgi with an invalid page ID, and keep track of this attempt
  # in the web server log.
  return unless Bugzilla->error_mode == ERROR_MODE_WEBPAGE;
  return unless $args->{error} eq 'bad_page_cgi_id';

  my $page_id = $args->{vars}->{page_id};
484
  my $login   = Bugzilla->user->identity || "Someone";
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
  warn "$login attempted to access page.cgi with id = $page_id";

  my $page          = $args->{message};
  my $new_error_msg = "Ah ah, you tried to access $page_id? Good try!";
  $new_error_msg = html_quote($new_error_msg);

  # There are better tools to parse an HTML page, but it's just an example.
  # Since Perl 5.16, we can no longer write "class" inside look-behind
  # assertions, because "ss" is also seen as the german ß character, which
  # makes Perl 5.16 complain. The right fix is to use the /aa modifier,
  # but it's only understood since Perl 5.14. So the workaround is to write
  # "clas[s]" instead of "class". Stupid and ugly hack, but it works with
  # all Perl versions.
  $$page
    =~ s/(?<=<td id="error_msg" clas[s]="throw_error">).*(?=<\/td>)/$new_error_msg/si;
500 501
}

502
sub flag_end_of_update {
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
  my ($self, $args) = @_;

  # This code doesn't actually *do* anything, it's just here to show you
  # how to use this hook.
  my $flag_params = $args;
  my ($object, $timestamp, $old_flags, $new_flags)
    = @$flag_params{qw(object timestamp old_flags new_flags)};
  my ($removed, $added) = diff_arrays($old_flags, $new_flags);
  my ($granted, $denied) = (0, 0);
  foreach my $new_flag (@$added) {
    $granted++ if $new_flag =~ /\+$/;
    $denied++  if $new_flag =~ /-$/;
  }
  my $bug_id = $object->isa('Bugzilla::Bug') ? $object->id : $object->bug_id;
  my $result = "$granted flags were granted and $denied flags were denied"
    . " on bug $bug_id at $timestamp.";

  # Uncomment this line to see $result in your webserver's error log whenever
  # you update flags.
  # warn $result;
523 524
}

525
sub group_before_delete {
526 527 528 529 530 531 532
  my ($self, $args) = @_;

  # This code doesn't actually *do* anything, it's just here to show you
  # how to use this hook.

  my $group    = $args->{'group'};
  my $group_id = $group->id;
533

534 535 536
  # Uncomment this line to see a line in your webserver's error log whenever
  # you file a bug.
  # warn "Group $group_id is about to be deleted!";
537 538 539
}

sub group_end_of_create {
540
  my ($self, $args) = @_;
541

542 543 544 545 546 547 548 549 550
  # This code doesn't actually *do* anything, it's just here to show you
  # how to use this hook.
  my $group = $args->{'group'};

  my $group_id = $group->id;

  # Uncomment this line to see a line in your webserver's error log whenever
  # you create a new group.
  #warn "Group $group_id has been created!";
551 552 553
}

sub group_end_of_update {
554
  my ($self, $args) = @_;
555

556 557
  # This code doesn't actually *do* anything, it's just here to show you
  # how to use this hook.
558

559 560 561 562 563 564
  my ($group, $changes) = @$args{qw(group changes)};

  foreach my $field (keys %$changes) {
    my $used_to_be = $changes->{$field}->[0];
    my $now_it_is  = $changes->{$field}->[1];
  }
565

566 567 568 569 570 571 572
  my $group_id    = $group->id;
  my $num_changes = scalar keys %$changes;
  my $result = "There were $num_changes changes to fields on group $group_id.";

  # Uncomment this line to see $result in your webserver's error log whenever
  # you update a group.
  #warn $result;
573 574
}

575
sub install_before_final_checks {
576 577 578 579 580 581 582 583 584 585 586 587
  my ($self, $args) = @_;
  print "Install-before_final_checks hook\n" unless $args->{silent};

  # Add a new user setting like this:
  #
  # add_setting('product_chooser',           # setting name
  #             ['pretty', 'full', 'small'], # options
  #             'pretty');                   # default
  #
  # To add descriptions for the setting and choices, add extra values to
  # the hash defined in global/setting-descs.none.tmpl. Do this in a hook:
  # hook/global/setting-descs-settings.none.tmpl .
588 589
}

590
sub install_filesystem {
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
  my ($self, $args) = @_;
  my $create_dirs  = $args->{'create_dirs'};
  my $recurse_dirs = $args->{'recurse_dirs'};
  my $htaccess     = $args->{'htaccess'};

  # Create a new directory in datadir specifically for this extension.
  # The directory will need to allow files to be created by the extension
  # code as well as allow the webserver to server content from it.
  # my $data_path = bz_locations->{'datadir'} . "/" . __PACKAGE__->NAME;
  # $create_dirs->{$data_path} = Bugzilla::Install::Filesystem::DIR_CGI_WRITE;

  # Update the permissions of any files and directories that currently reside
  # in the extension's directory.
  # $recurse_dirs->{$data_path} = {
  #     files => Bugzilla::Install::Filesystem::CGI_READ,
  #     dirs  => Bugzilla::Install::Filesystem::DIR_CGI_WRITE
  # };

  # Create a htaccess file that allows specific content to be served from the
  # extension's directory.
  # $htaccess->{"$data_path/.htaccess"} = {
  #     perms    => Bugzilla::Install::Filesystem::WS_SERVE,
  #     contents => Bugzilla::Install::Filesystem::HT_DEFAULT_DENY
  # };
615 616
}

617
sub install_update_db {
618 619
  my $dbh = Bugzilla->dbh;

620 621 622 623 624 625
#    $dbh->bz_add_column('example', 'new_column',
#                        {TYPE => 'INT2', NOTNULL => 1, DEFAULT => 0});
#    $dbh->bz_add_index('example', 'example_new_column_idx', [qw(value)]);
}

sub install_update_db_fielddefs {
626 627 628
  my $dbh = Bugzilla->dbh;

#    $dbh->bz_add_column('fielddefs', 'example_column',
629
#                        {TYPE => 'MEDIUMTEXT', NOTNULL => 1, DEFAULT => ''});
630
}
631

632
sub job_map {
633 634 635 636 637 638 639 640 641 642 643
  my ($self, $args) = @_;

  my $job_map = $args->{job_map};

  # This adds the named class (an instance of TheSchwartz::Worker) as a
  # handler for when a job is added with the name "some_task".
  $job_map->{'some_task'} = 'Bugzilla::Extension::Example::Job::SomeClass';

  # Schedule a job like this:
  # my $queue = Bugzilla->job_queue();
  # $queue->insert('some_task', { some_parameter => $some_variable });
644 645
}

646
sub mailer_before_send {
647 648 649 650 651 652 653 654
  my ($self, $args) = @_;

  my $email = $args->{email};

  # If you add a header to an email, it's best to start it with
  # 'X-Bugzilla-<Extension>' so that you don't conflict with
  # other extensions.
  $email->header_set('X-Bugzilla-Example-Header', 'Example');
655 656 657
}

sub object_before_create {
658 659 660 661 662 663 664 665 666 667
  my ($self, $args) = @_;

  my $class         = $args->{'class'};
  my $object_params = $args->{'params'};

  # Note that this is a made-up class, for this example.
  if ($class->isa('Bugzilla::ExampleObject')) {
    warn "About to create an ExampleObject!";
    warn "Got the following parameters: " . join(', ', keys(%$object_params));
  }
668 669
}

670
sub object_before_delete {
671
  my ($self, $args) = @_;
672

673
  my $object = $args->{'object'};
674

675 676 677 678 679
  # Note that this is a made-up class, for this example.
  if ($object->isa('Bugzilla::ExampleObject')) {
    my $id = $object->id;
    warn "An object with id $id is about to be deleted!";
  }
680 681
}

682
sub object_before_set {
683 684 685 686 687 688 689 690
  my ($self, $args) = @_;

  my ($object, $field, $value) = @$args{qw(object field value)};

  # Note that this is a made-up class, for this example.
  if ($object->isa('Bugzilla::ExampleObject')) {
    warn "The field $field is changing from " . $object->{$field} . " to $value!";
  }
691 692
}

693
sub object_columns {
694 695
  my ($self,  $args)    = @_;
  my ($class, $columns) = @$args{qw(class columns)};
696

697 698 699
  if ($class->isa('Bugzilla::ExampleObject')) {
    push(@$columns, 'example');
  }
700 701
}

702
sub object_end_of_create {
703 704 705 706
  my ($self, $args) = @_;

  my $class  = $args->{'class'};
  my $object = $args->{'object'};
707

708
  warn "Created a new $class object!";
709 710
}

711
sub object_end_of_create_validators {
712 713 714 715 716 717 718 719 720 721 722 723
  my ($self, $args) = @_;

  my $class         = $args->{'class'};
  my $object_params = $args->{'params'};

  # Note that this is a made-up class, for this example.
  if ($class->isa('Bugzilla::ExampleObject')) {

    # Always set example_field to 1, even if the validators said otherwise.
    $object_params->{example_field} = 1;
  }

724 725
}

726
sub object_end_of_set {
727
  my ($self, $args) = @_;
728

729
  my ($object, $field) = @$args{qw(object field)};
730

731 732 733 734
  # Note that this is a made-up class, for this example.
  if ($object->isa('Bugzilla::ExampleObject')) {
    warn "The field $field has changed to " . $object->{$field};
  }
735 736
}

737
sub object_end_of_set_all {
738 739 740 741 742 743 744 745 746
  my ($self, $args) = @_;

  my $object        = $args->{'object'};
  my $object_params = $args->{'params'};

  # Note that this is a made-up class, for this example.
  if ($object->isa('Bugzilla::ExampleObject')) {
    if ($object_params->{example_field} == 1) {
      $object->{example_field} = 1;
747
    }
748 749
  }

750 751 752
}

sub object_end_of_update {
753 754 755 756 757 758 759 760 761
  my ($self, $args) = @_;

  my ($object, $old_object, $changes) = @$args{qw(object old_object changes)};

  # Note that this is a made-up class, for this example.
  if ($object->isa('Bugzilla::ExampleObject')) {
    if (defined $changes->{'name'}) {
      my ($old, $new) = @{$changes->{'name'}};
      print "The name field changed from $old to $new!";
762
    }
763
  }
764 765
}

766
sub object_update_columns {
767 768
  my ($self,   $args)    = @_;
  my ($object, $columns) = @$args{qw(object columns)};
769

770 771 772
  if ($object->isa('Bugzilla::ExampleObject')) {
    push(@$columns, 'example');
  }
773 774
}

775
sub object_validators {
776 777 778 779 780 781 782 783 784 785 786 787 788 789
  my ($self,  $args)       = @_;
  my ($class, $validators) = @$args{qw(class validators)};

  if ($class->isa('Bugzilla::Bug')) {

    # This is an example of adding a new validator.
    # See the _check_example subroutine below.
    $validators->{example} = \&_check_example;

    # This is an example of overriding an existing validator.
    # See the check_short_desc validator below.
    my $original = $validators->{short_desc};
    $validators->{short_desc} = sub { _check_short_desc($original, @_) };
  }
790 791 792
}

sub _check_example {
793 794 795
  my ($invocant, $value, $field) = @_;
  warn "I was called to validate the value of $field.";
  warn "The value of $field that I was passed in is: $value";
796

797 798 799
  # Make the value always be 1.
  my $fixed_value = 1;
  return $fixed_value;
800 801 802
}

sub _check_short_desc {
803 804 805 806 807 808 809 810 811 812
  my $original = shift;
  my $invocant = shift;
  my $value    = $invocant->$original(@_);
  if ($value !~ /example/i) {

    # Use this line to make Bugzilla throw an error every time
    # you try to file a bug or update a bug without the word "example"
    # in the summary.
    if (0) {
      ThrowUserError('example_short_desc_invalid');
813
    }
814 815
  }
  return $value;
816 817
}

818
sub page_before_template {
819 820 821 822 823 824 825 826
  my ($self, $args) = @_;

  my ($vars, $page) = @$args{qw(vars page_id)};

  # You can see this hook in action by loading page.cgi?id=example.html
  if ($page eq 'example.html') {
    $vars->{cgi_variables} = {Bugzilla->cgi->Vars};
  }
827 828
}

829
sub path_info_whitelist {
830 831 832
  my ($self, $args) = @_;
  my $whitelist = $args->{whitelist};
  push(@$whitelist, "page.cgi");
833 834
}

835
sub post_bug_after_creation {
836 837 838 839
  my ($self, $args) = @_;

  my $vars = $args->{vars};
  $vars->{'example'} = 1;
840 841
}

842
sub product_confirm_delete {
843 844 845 846
  my ($self, $args) = @_;

  my $vars = $args->{vars};
  $vars->{'example'} = 1;
847 848
}

849 850

sub product_end_of_create {
851 852 853
  my ($self, $args) = @_;

  my $product = $args->{product};
854

855 856
  # For this example, any lines of code that actually make changes to your
  # database have been commented out.
857

858 859 860 861
  # This section will take a group that exists in your installation
  # (possible called test_group) and automatically makes the new
  # product hidden to only members of the group. Just remove
  # the restriction if you want the new product to be public.
862

863
  my $example_group = new Bugzilla::Group({name => 'example_group'});
864

865 866 867 868 869 870 871 872 873
  if ($example_group) {
    $product->set_group_controls(
      $example_group,
      {
        entry         => 1,
        membercontrol => CONTROLMAPMANDATORY,
        othercontrol  => CONTROLMAPMANDATORY
      }
    );
874 875

#        $product->update();
876
  }
877

878 879
  # This section will automatically add a default component
  # to the new product called 'No Component'.
880

881 882 883 884
  my $default_assignee
    = new Bugzilla::User({name => Bugzilla->params->{maintainer}});

  if ($default_assignee) {
885 886 887 888

#        Bugzilla::Component->create(
#            { name             => 'No Component',
#              product          => $product,
889
#              description      => 'Select this component if one does not ' .
890 891
#                                  'exist in the current list of components',
#              initialowner     => $default_assignee });
892
  }
893 894
}

895
sub quicksearch_map {
896 897
  my ($self, $args) = @_;
  my $map = $args->{'map'};
898

899 900
  # This demonstrates adding a shorter alias for a long custom field name.
  $map->{'impact'} = $map->{'cf_long_field_name_for_impact_field'};
901 902
}

903
sub sanitycheck_check {
904 905 906 907 908 909 910 911 912 913 914 915
  my ($self, $args) = @_;

  my $dbh = Bugzilla->dbh;
  my $sth;

  my $status = $args->{'status'};

  # Check that all users are Australian
  $status->('example_check_au_user');

  $sth = $dbh->prepare(
    "SELECT userid, login_name
916
                            FROM profiles
917 918 919 920 921 922 923 924 925 926 927 928 929
                           WHERE login_name NOT LIKE '%.au'"
  );
  $sth->execute;

  my $seen_nonau = 0;
  while (my ($userid, $login, $numgroups) = $sth->fetchrow_array) {
    $status->(
      'example_check_au_user_alert', {userid => $userid, login => $login}, 'alert'
    );
    $seen_nonau = 1;
  }

  $status->('example_check_au_user_prompt') if $seen_nonau;
930 931 932
}

sub sanitycheck_repair {
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
  my ($self, $args) = @_;

  my $cgi = Bugzilla->cgi;
  my $dbh = Bugzilla->dbh;

  my $status = $args->{'status'};

  if ($cgi->param('example_repair_au_user')) {
    $status->('example_repair_au_user_start');

    #$dbh->do("UPDATE profiles
    #             SET login_name = CONCAT(login_name, '.au')
    #           WHERE login_name NOT LIKE '%.au'");

    $status->('example_repair_au_user_end');
  }
949 950 951
}

sub template_before_create {
952 953 954 955 956 957 958 959
  my ($self, $args) = @_;

  my $config = $args->{'config'};

  # This will be accessible as "example_global_variable" in every
  # template in Bugzilla. See Bugzilla/Template.pm's create() function
  # for more things that you can set.
  $config->{VARIABLES}->{example_global_variable} = sub { return 'value' };
960 961
}

962
sub template_after_create {
963 964 965 966 967 968 969 970 971 972
  my ($self, $args) = @_;
  my $context = $args->{template}->context;

  # define a pluck method on template toolkit lists.
  $context->define_vmethod(
    list => pluck => sub {
      my ($list, $field) = @_;
      return [map { $_->$field } @$list];
    }
  );
973 974
}

975
sub template_before_process {
976
  my ($self, $args) = @_;
977

978 979 980 981 982
  my ($vars, $file, $context) = @$args{qw(vars file context)};

  if ($file eq 'bug/edit.html.tmpl') {
    $vars->{'viewing_the_bug_form'} = 1;
  }
983 984
}

985
sub user_check_account_creation {
986
  my ($self, $args) = @_;
987

988 989
  my $login = $args->{login};
  my $ip    = remote_ip();
990

991 992
  # Log all requests.
  warn "USER ACCOUNT CREATION REQUEST FOR $login ($ip)";
993

994 995 996 997
  # Reject requests based on their email address.
  if ($login =~ /\@evil\.com$/) {
    ThrowUserError('account_creation_restricted');
  }
998

999 1000 1001 1002
  # Reject requests based on their IP address.
  if ($ip =~ /^192\.168\./) {
    ThrowUserError('account_creation_restricted');
  }
1003 1004
}

1005
sub user_preferences {
1006 1007 1008 1009
  my ($self, $args) = @_;
  my $tab     = $args->{current_tab};
  my $save    = $args->{save_changes};
  my $handled = $args->{handled};
1010

1011
  return unless $tab eq 'my_tab';
1012

1013 1014 1015 1016 1017 1018 1019
  my $value = Bugzilla->input_params->{'example_pref'};
  if ($save) {

    # Validate your data and update the DB accordingly.
    $value =~ s/\s+/:/g;
  }
  $args->{'vars'}->{example_pref} = $value;
1020

1021 1022 1023
  # Set the 'handled' scalar reference to true so that the caller
  # knows the panel name is valid and that an extension took care of it.
  $$handled = 1;
1024 1025
}

1026
sub webservice {
1027
  my ($self, $args) = @_;
1028

1029 1030
  my $dispatch = $args->{dispatch};
  $dispatch->{Example} = "Bugzilla::Extension::Example::WebService";
1031 1032 1033
}

sub webservice_error_codes {
1034
  my ($self, $args) = @_;
1035

1036 1037
  my $error_map = $args->{error_map};
  $error_map->{'example_my_error'} = 10001;
1038 1039
}

1040
sub webservice_status_code_map {
1041
  my ($self, $args) = @_;
1042

1043 1044 1045 1046 1047
  my $status_code_map = $args->{status_code_map};

  # Uncomment this line to override the status code for the
  # error 'object_does_not_exist' to STATUS_BAD_REQUEST
  #$status_code_map->{51} = STATUS_BAD_REQUEST;
1048 1049
}

1050
sub webservice_before_call {
1051
  my ($self, $args) = @_;
1052

1053 1054 1055 1056
  # This code doesn't actually *do* anything, it's just here to show you
  # how to use this hook.
  my $method      = $args->{method};
  my $full_method = $args->{full_method};
1057

1058 1059 1060 1061
  # Uncomment this line to see a line in your webserver's error log whenever
  # a webservice call is made
  #warn "RPC call $full_method made by ",
  #   Bugzilla->user->login || 'an anonymous user', "\n";
1062 1063
}

1064
sub webservice_fix_credentials {
1065 1066 1067 1068 1069 1070 1071 1072 1073
  my ($self, $args) = @_;
  my $rpc    = $args->{'rpc'};
  my $params = $args->{'params'};

  # Allow user to pass in username=foo&password=bar
  if (exists $params->{'username'} && exists $params->{'password'}) {
    $params->{'Bugzilla_login'}    = $params->{'username'};
    $params->{'Bugzilla_password'} = $params->{'password'};
  }
1074 1075 1076
}

sub webservice_rest_request {
1077 1078 1079 1080 1081 1082 1083 1084 1085
  my ($self, $args) = @_;
  my $rpc    = $args->{'rpc'};
  my $params = $args->{'params'};

  # Internally we may have a field called 'cf_test_field' but we allow users
  # to use the shorter 'test_field' name.
  if (exists $params->{'test_field'}) {
    $params->{'test_field'} = delete $params->{'cf_test_field'};
  }
1086 1087 1088
}

sub webservice_rest_resources {
1089 1090 1091 1092 1093 1094 1095 1096
  my ($self, $args) = @_;
  my $rpc       = $args->{'rpc'};
  my $resources = $args->{'resources'};

  # Add a new resource that allows for /rest/example/hello
  # to call Example.hello
  $resources->{'Bugzilla::Extension::Example::WebService'}
    = [qr{^/example/hello$}, {GET => {method => 'hello',}}];
1097 1098 1099
}

sub webservice_rest_response {
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
  my ($self, $args) = @_;
  my $rpc      = $args->{'rpc'};
  my $result   = $args->{'result'};
  my $response = $args->{'response'};

  # Convert a list of bug hashes to a single bug hash if only one is
  # being returned.
  if ( ref $$result eq 'HASH'
    && exists $$result->{'bugs'}
    && scalar @{$$result->{'bugs'}} == 1)
  {
    $$result = $$result->{'bugs'}->[0];
  }
1113 1114
}

1115 1116
# This must be the last line of your extension.
__PACKAGE__->NAME;