1# This Source Code Form is subject to the terms of the Mozilla Public
2# License, v. 2.0. If a copy of the MPL was not distributed with this
3# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4#
5# This Source Code Form is "Incompatible With Secondary Licenses", as
6# defined by the Mozilla Public License, v. 2.0.
7
8package Bugzilla::Extension::Example;
9
10use 5.10.1;
11use strict;
12use warnings;
13
14use parent qw(Bugzilla::Extension);
15
16use Bugzilla::Constants;
17use Bugzilla::Error;
18use Bugzilla::Group;
19use Bugzilla::User;
20use Bugzilla::User::Setting;
21use Bugzilla::Util qw(diff_arrays html_quote remote_ip);
22use Bugzilla::Status qw(is_open_state);
23use Bugzilla::Install::Filesystem;
24use Bugzilla::WebService::Constants;
25
26# This is extensions/Example/lib/Util.pm. I can load this here in my
27# Extension.pm only because I have a Config.pm.
28use Bugzilla::Extension::Example::Util;
29
30use Data::Dumper;
31
32# See bugmail_relationships.
33use constant REL_EXAMPLE => -127;
34
35our $VERSION = '1.0';
36
37sub user_can_administer {
38    my ($self, $args) = @_;
39    my $can_administer = $args->{can_administer};
40
41    # If you add an option to the admin pages (e.g. by using the Hooks in
42    # template/en/default/admin/admin.html.tmpl), you may want to allow
43    # users in another group view admin.cgi
44    #if (Bugzilla->user->in_group('other_group')) {
45    #    $$can_administer = 1;
46    #}
47}
48
49sub admin_editusers_action {
50    my ($self, $args) = @_;
51    my ($vars, $action, $user) = @$args{qw(vars action user)};
52    my $template = Bugzilla->template;
53
54    if ($action eq 'my_action') {
55        # Allow to restrict the search to any group the user is allowed to bless.
56        $vars->{'restrictablegroups'} = $user->bless_groups();
57        $template->process('admin/users/search.html.tmpl', $vars)
58            || ThrowTemplateError($template->error());
59        exit;
60    }
61}
62
63sub attachment_process_data {
64    my ($self, $args) = @_;
65    my $type     = $args->{attributes}->{mimetype};
66    my $filename = $args->{attributes}->{filename};
67
68    # Make sure images have the correct extension.
69    # Uncomment the two lines below to make this check effective.
70    if ($type =~ /^image\/(\w+)$/) {
71        my $format = $1;
72        if ($filename =~ /^(.+)(:?\.[^\.]+)$/) {
73            my $name = $1;
74            #$args->{attributes}->{filename} = "${name}.$format";
75        }
76        else {
77            # The file has no extension. We append it.
78            #$args->{attributes}->{filename} .= ".$format";
79        }
80    }
81}
82
83sub auth_login_methods {
84    my ($self, $args) = @_;
85    my $modules = $args->{modules};
86    if (exists $modules->{Example}) {
87        $modules->{Example} = 'Bugzilla/Extension/Example/Auth/Login.pm';
88    }
89}
90
91sub auth_verify_methods {
92    my ($self, $args) = @_;
93    my $modules = $args->{modules};
94    if (exists $modules->{Example}) {
95        $modules->{Example} = 'Bugzilla/Extension/Example/Auth/Verify.pm';
96    }
97}
98
99sub bug_check_can_change_field {
100    my ($self, $args) = @_;
101
102    my ($bug, $field, $new_value, $old_value, $priv_results)
103        = @$args{qw(bug field new_value old_value priv_results)};
104
105    my $user = Bugzilla->user;
106
107    # Disallow a bug from being reopened if currently closed unless user
108    # is in 'admin' group
109    if ($field eq 'bug_status' && $bug->product_obj->name eq 'Example') {
110        if (!is_open_state($old_value) && is_open_state($new_value)
111            && !$user->in_group('admin'))
112        {
113            push(@$priv_results, PRIVILEGES_REQUIRED_EMPOWERED);
114            return;
115        }
116    }
117
118    # Disallow a bug's keywords from being edited unless user is the
119    # reporter of the bug
120    if ($field eq 'keywords' && $bug->product_obj->name eq 'Example'
121        && $user->login ne $bug->reporter->login)
122    {
123        push(@$priv_results, PRIVILEGES_REQUIRED_REPORTER);
124        return;
125    }
126
127    # Allow updating of priority even if user cannot normally edit the bug
128    # and they are in group 'engineering'
129    if ($field eq 'priority' && $bug->product_obj->name eq 'Example'
130        && $user->in_group('engineering'))
131    {
132        push(@$priv_results, PRIVILEGES_REQUIRED_NONE);
133        return;
134    }
135}
136
137sub bug_columns {
138    my ($self, $args) = @_;
139    my $columns = $args->{'columns'};
140    push (@$columns, "delta_ts AS example")
141}
142
143sub bug_end_of_create {
144    my ($self, $args) = @_;
145
146    # This code doesn't actually *do* anything, it's just here to show you
147    # how to use this hook.
148    my $bug = $args->{'bug'};
149    my $timestamp = $args->{'timestamp'};
150
151    my $bug_id = $bug->id;
152    # Uncomment this line to see a line in your webserver's error log whenever
153    # you file a bug.
154    # warn "Bug $bug_id has been filed!";
155}
156
157sub bug_end_of_create_validators {
158    my ($self, $args) = @_;
159
160    # This code doesn't actually *do* anything, it's just here to show you
161    # how to use this hook.
162    my $bug_params = $args->{'params'};
163
164    # Uncomment this line below to see a line in your webserver's error log
165    # containing all validated bug field values every time you file a bug.
166    # warn Dumper($bug_params);
167
168    # This would remove all ccs from the bug, preventing ANY ccs from being
169    # added on bug creation.
170    # $bug_params->{cc} = [];
171}
172
173sub bug_start_of_update {
174    my ($self, $args) = @_;
175
176    # This code doesn't actually *do* anything, it's just here to show you
177    # how to use this hook.
178    my ($bug, $old_bug, $timestamp, $changes) =
179        @$args{qw(bug old_bug timestamp changes)};
180
181    foreach my $field (keys %$changes) {
182        my $used_to_be = $changes->{$field}->[0];
183        my $now_it_is  = $changes->{$field}->[1];
184    }
185
186    my $old_summary = $old_bug->short_desc;
187
188    my $status_message;
189    if (my $status_change = $changes->{'bug_status'}) {
190        my $old_status = new Bugzilla::Status({ name => $status_change->[0] });
191        my $new_status = new Bugzilla::Status({ name => $status_change->[1] });
192        if ($new_status->is_open && !$old_status->is_open) {
193            $status_message = "Bug re-opened!";
194        }
195        if (!$new_status->is_open && $old_status->is_open) {
196            $status_message = "Bug closed!";
197        }
198    }
199
200    my $bug_id = $bug->id;
201    my $num_changes = scalar keys %$changes;
202    my $result = "There were $num_changes changes to fields on bug $bug_id"
203                 . " at $timestamp.";
204    # Uncomment this line to see $result in your webserver's error log whenever
205    # you update a bug.
206    # warn $result;
207}
208
209sub bug_end_of_update {
210    my ($self, $args) = @_;
211
212    # This code doesn't actually *do* anything, it's just here to show you
213    # how to use this hook.
214    my ($bug, $old_bug, $timestamp, $changes) =
215        @$args{qw(bug old_bug timestamp changes)};
216
217    foreach my $field (keys %$changes) {
218        my $used_to_be = $changes->{$field}->[0];
219        my $now_it_is  = $changes->{$field}->[1];
220    }
221
222    my $old_summary = $old_bug->short_desc;
223
224    my $status_message;
225    if (my $status_change = $changes->{'bug_status'}) {
226        my $old_status = new Bugzilla::Status({ name => $status_change->[0] });
227        my $new_status = new Bugzilla::Status({ name => $status_change->[1] });
228        if ($new_status->is_open && !$old_status->is_open) {
229            $status_message = "Bug re-opened!";
230        }
231        if (!$new_status->is_open && $old_status->is_open) {
232            $status_message = "Bug closed!";
233        }
234    }
235
236    my $bug_id = $bug->id;
237    my $num_changes = scalar keys %$changes;
238    my $result = "There were $num_changes changes to fields on bug $bug_id"
239                 . " at $timestamp.";
240    # Uncomment this line to see $result in your webserver's error log whenever
241    # you update a bug.
242    # warn $result;
243}
244
245sub bug_fields {
246    my ($self, $args) = @_;
247
248    my $fields = $args->{'fields'};
249    push (@$fields, "example")
250}
251
252sub bug_format_comment {
253    my ($self, $args) = @_;
254
255    # This replaces every occurrence of the word "foo" with the word
256    # "bar"
257
258    my $regexes = $args->{'regexes'};
259    push(@$regexes, { match => qr/\bfoo\b/, replace => 'bar' });
260
261    # And this links every occurrence of the word "bar" to example.com,
262    # but it won't affect "foo"s that have already been turned into "bar"
263    # above (because each regex is run in order, and later regexes don't modify
264    # earlier matches, due to some cleverness in Bugzilla's internals).
265    #
266    # For example, the phrase "foo bar" would become:
267    # bar <a href="http://example.com/bar">bar</a>
268    my $bar_match = qr/\b(bar)\b/;
269    push(@$regexes, { match => $bar_match, replace => \&_replace_bar });
270}
271
272# Used by bug_format_comment--see its code for an explanation.
273sub _replace_bar {
274    my $args = shift;
275    # $match is the first parentheses match in the $bar_match regex
276    # in bug-format_comment.pl. We get up to 10 regex matches as
277    # arguments to this function.
278    my $match = $args->{matches}->[0];
279    # Remember, you have to HTML-escape any data that you are returning!
280    $match = html_quote($match);
281    return qq{<a href="http://example.com/">$match</a>};
282};
283
284sub buglist_columns {
285    my ($self, $args) = @_;
286
287    my $columns = $args->{'columns'};
288    $columns->{'example'} = { 'name' => 'bugs.delta_ts' , 'title' => 'Example' };
289    $columns->{'product_desc'} = { 'name'  => 'prod_desc.description',
290                                   'title' => 'Product Description' };
291}
292
293sub buglist_column_joins {
294    my ($self, $args) = @_;
295    my $joins = $args->{'column_joins'};
296
297    # This column is added using the "buglist_columns" hook
298    $joins->{'product_desc'} = {
299        from  => 'product_id',
300        to    => 'id',
301        table => 'products',
302        as    => 'prod_desc',
303        join  => 'INNER',
304    };
305}
306
307sub search_operator_field_override {
308    my ($self, $args) = @_;
309
310    my $operators = $args->{'operators'};
311
312    my $original = $operators->{component}->{_non_changed};
313    $operators->{component} = {
314        _non_changed => sub { _component_nonchanged($original, @_) }
315    };
316}
317
318sub _component_nonchanged {
319    my $original = shift;
320    my ($invocant, $args) = @_;
321
322    $invocant->$original($args);
323    # Actually, it does not change anything in the result,
324    # just an example.
325    $args->{term} = $args->{term} . " OR 1=2";
326}
327
328sub bugmail_recipients {
329    my ($self, $args) = @_;
330    my $recipients = $args->{recipients};
331    my $bug = $args->{bug};
332
333    my $user =
334        new Bugzilla::User({ name => Bugzilla->params->{'maintainer'} });
335
336    if ($bug->id == 1) {
337        # Uncomment the line below to add the maintainer to the recipients
338        # list of every bugmail from bug 1 as though that the maintainer
339        # were on the CC list.
340        #$recipients->{$user->id}->{+REL_CC} = 1;
341
342        # And this line adds the maintainer as though they had the
343        # "REL_EXAMPLE" relationship from the bugmail_relationships hook below.
344        #$recipients->{$user->id}->{+REL_EXAMPLE} = 1;
345    }
346}
347
348sub bugmail_relationships {
349    my ($self, $args) = @_;
350    my $relationships = $args->{relationships};
351    $relationships->{+REL_EXAMPLE} = 'Example';
352}
353
354sub cgi_headers {
355    my ($self, $args) = @_;
356    my $headers = $args->{'headers'};
357
358    $headers->{'-x_test_header'} = "Test header from Example extension";
359}
360
361sub config_add_panels {
362    my ($self, $args) = @_;
363
364    my $modules = $args->{panel_modules};
365    $modules->{Example} = "Bugzilla::Extension::Example::Config";
366}
367
368sub config_modify_panels {
369    my ($self, $args) = @_;
370
371    my $panels = $args->{panels};
372
373    # Add the "Example" auth methods.
374    my $auth_params = $panels->{'auth'}->{params};
375    my ($info_class)   = grep($_->{name} eq 'user_info_class', @$auth_params);
376    my ($verify_class) = grep($_->{name} eq 'user_verify_class', @$auth_params);
377
378    push(@{ $info_class->{choices} },   'CGI,Example');
379    push(@{ $verify_class->{choices} }, 'Example');
380
381    push(@$auth_params, { name => 'param_example',
382                          type => 't',
383                          default => 0,
384                          checker => \&check_numeric });
385}
386
387sub db_schema_abstract_schema {
388    my ($self, $args) = @_;
389#    $args->{'schema'}->{'example_table'} = {
390#        FIELDS => [
391#            id       => {TYPE => 'SMALLSERIAL', NOTNULL => 1,
392#                     PRIMARYKEY => 1},
393#            for_key  => {TYPE => 'INT3', NOTNULL => 1,
394#                           REFERENCES  => {TABLE  =>  'example_table2',
395#                                           COLUMN =>  'id',
396#                                           DELETE => 'CASCADE'}},
397#            col_3    => {TYPE => 'varchar(64)', NOTNULL => 1},
398#        ],
399#        INDEXES => [
400#            id_index_idx   => {FIELDS => ['col_3'], TYPE => 'UNIQUE'},
401#            for_id_idx => ['for_key'],
402#        ],
403#    };
404}
405
406sub email_in_before_parse {
407    my ($self, $args) = @_;
408
409    my $subject = $args->{mail}->header('Subject');
410    # Correctly extract the bug ID from email subjects of the form [Bug comp/NNN].
411    if ($subject =~ /\[.*(\d+)\].*/) {
412        $args->{fields}->{bug_id} = $1;
413    }
414}
415
416sub email_in_after_parse {
417    my ($self, $args) = @_;
418    my $reporter = $args->{fields}->{reporter};
419    my $dbh = Bugzilla->dbh;
420
421    # No other check needed if this is a valid regular user.
422    return if login_to_id($reporter);
423
424    # The reporter is not a regular user. We create an account for them,
425    # but they can only comment on existing bugs.
426    # This is useful for people who reply by email to bugmails received
427    # in mailing-lists.
428    if ($args->{fields}->{bug_id}) {
429        # WARNING: we return now to skip the remaining code below.
430        # You must understand that removing this line would make the code
431        # below effective! Do it only if you are OK with the behavior
432        # described here.
433        return;
434
435        Bugzilla::User->create({ login_name => $reporter, cryptpassword => '*' });
436
437        # For security reasons, delete all fields unrelated to comments.
438        foreach my $field (keys %{$args->{fields}}) {
439            next if $field =~ /^(?:bug_id|comment|reporter)$/;
440            delete $args->{fields}->{$field};
441        }
442    }
443    else {
444        ThrowUserError('invalid_username', { name => $reporter });
445    }
446}
447
448sub enter_bug_entrydefaultvars {
449    my ($self, $args) = @_;
450
451    my $vars = $args->{vars};
452    $vars->{'example'} = 1;
453}
454
455sub error_catch {
456    my ($self, $args) = @_;
457    # Customize the error message displayed when someone tries to access
458    # page.cgi with an invalid page ID, and keep track of this attempt
459    # in the web server log.
460    return unless Bugzilla->error_mode == ERROR_MODE_WEBPAGE;
461    return unless $args->{error} eq 'bad_page_cgi_id';
462
463    my $page_id = $args->{vars}->{page_id};
464    my $login = Bugzilla->user->identity || "Someone";
465    warn "$login attempted to access page.cgi with id = $page_id";
466
467    my $page = $args->{message};
468    my $new_error_msg = "Ah ah, you tried to access $page_id? Good try!";
469    $new_error_msg = html_quote($new_error_msg);
470    # There are better tools to parse an HTML page, but it's just an example.
471    # Since Perl 5.16, we can no longer write "class" inside look-behind
472    # assertions, because "ss" is also seen as the german ß character, which
473    # makes Perl 5.16 complain. The right fix is to use the /aa modifier,
474    # but it's only understood since Perl 5.14. So the workaround is to write
475    # "clas[s]" instead of "class". Stupid and ugly hack, but it works with
476    # all Perl versions.
477    $$page =~ s/(?<=<td id="error_msg" clas[s]="throw_error">).*(?=<\/td>)/$new_error_msg/si;
478}
479
480sub flag_end_of_update {
481    my ($self, $args) = @_;
482
483    # This code doesn't actually *do* anything, it's just here to show you
484    # how to use this hook.
485    my $flag_params = $args;
486    my ($object, $timestamp, $old_flags, $new_flags) =
487        @$flag_params{qw(object timestamp old_flags new_flags)};
488    my ($removed, $added) = diff_arrays($old_flags, $new_flags);
489    my ($granted, $denied) = (0, 0);
490    foreach my $new_flag (@$added) {
491        $granted++ if $new_flag =~ /\+$/;
492        $denied++ if $new_flag =~ /-$/;
493    }
494    my $bug_id = $object->isa('Bugzilla::Bug') ? $object->id
495                                               : $object->bug_id;
496    my $result = "$granted flags were granted and $denied flags were denied"
497                 . " on bug $bug_id at $timestamp.";
498    # Uncomment this line to see $result in your webserver's error log whenever
499    # you update flags.
500    # warn $result;
501}
502
503sub group_before_delete {
504    my ($self, $args) = @_;
505    # This code doesn't actually *do* anything, it's just here to show you
506    # how to use this hook.
507
508    my $group = $args->{'group'};
509    my $group_id = $group->id;
510    # Uncomment this line to see a line in your webserver's error log whenever
511    # you file a bug.
512    # warn "Group $group_id is about to be deleted!";
513}
514
515sub group_end_of_create {
516    my ($self, $args) = @_;
517    # This code doesn't actually *do* anything, it's just here to show you
518    # how to use this hook.
519    my $group = $args->{'group'};
520
521    my $group_id = $group->id;
522    # Uncomment this line to see a line in your webserver's error log whenever
523    # you create a new group.
524    #warn "Group $group_id has been created!";
525}
526
527sub group_end_of_update {
528    my ($self, $args) = @_;
529    # This code doesn't actually *do* anything, it's just here to show you
530    # how to use this hook.
531
532    my ($group, $changes) = @$args{qw(group changes)};
533
534    foreach my $field (keys %$changes) {
535        my $used_to_be = $changes->{$field}->[0];
536        my $now_it_is  = $changes->{$field}->[1];
537    }
538
539    my $group_id = $group->id;
540    my $num_changes = scalar keys %$changes;
541    my $result =
542        "There were $num_changes changes to fields on group $group_id.";
543    # Uncomment this line to see $result in your webserver's error log whenever
544    # you update a group.
545    #warn $result;
546}
547
548sub install_before_final_checks {
549    my ($self, $args) = @_;
550    print "Install-before_final_checks hook\n" unless $args->{silent};
551
552    # Add a new user setting like this:
553    #
554    # add_setting('product_chooser',           # setting name
555    #             ['pretty', 'full', 'small'], # options
556    #             'pretty');                   # default
557    #
558    # To add descriptions for the setting and choices, add extra values to
559    # the hash defined in global/setting-descs.none.tmpl. Do this in a hook:
560    # hook/global/setting-descs-settings.none.tmpl .
561}
562
563sub install_filesystem {
564    my ($self, $args) = @_;
565    my $create_dirs  = $args->{'create_dirs'};
566    my $recurse_dirs = $args->{'recurse_dirs'};
567    my $htaccess     = $args->{'htaccess'};
568
569    # Create a new directory in datadir specifically for this extension.
570    # The directory will need to allow files to be created by the extension
571    # code as well as allow the webserver to server content from it.
572    # my $data_path = bz_locations->{'datadir'} . "/" . __PACKAGE__->NAME;
573    # $create_dirs->{$data_path} = Bugzilla::Install::Filesystem::DIR_CGI_WRITE;
574
575    # Update the permissions of any files and directories that currently reside
576    # in the extension's directory.
577    # $recurse_dirs->{$data_path} = {
578    #     files => Bugzilla::Install::Filesystem::CGI_READ,
579    #     dirs  => Bugzilla::Install::Filesystem::DIR_CGI_WRITE
580    # };
581
582    # Create a htaccess file that allows specific content to be served from the
583    # extension's directory.
584    # $htaccess->{"$data_path/.htaccess"} = {
585    #     perms    => Bugzilla::Install::Filesystem::WS_SERVE,
586    #     contents => Bugzilla::Install::Filesystem::HT_DEFAULT_DENY
587    # };
588}
589
590sub install_update_db {
591    my $dbh = Bugzilla->dbh;
592#    $dbh->bz_add_column('example', 'new_column',
593#                        {TYPE => 'INT2', NOTNULL => 1, DEFAULT => 0});
594#    $dbh->bz_add_index('example', 'example_new_column_idx', [qw(value)]);
595}
596
597sub install_update_db_fielddefs {
598    my $dbh = Bugzilla->dbh;
599#    $dbh->bz_add_column('fielddefs', 'example_column',
600#                        {TYPE => 'MEDIUMTEXT', NOTNULL => 1, DEFAULT => ''});
601}
602
603sub job_map {
604    my ($self, $args) = @_;
605
606    my $job_map = $args->{job_map};
607
608    # This adds the named class (an instance of TheSchwartz::Worker) as a
609    # handler for when a job is added with the name "some_task".
610    $job_map->{'some_task'} = 'Bugzilla::Extension::Example::Job::SomeClass';
611
612    # Schedule a job like this:
613    # my $queue = Bugzilla->job_queue();
614    # $queue->insert('some_task', { some_parameter => $some_variable });
615}
616
617sub mailer_before_send {
618    my ($self, $args) = @_;
619
620    my $email = $args->{email};
621    # If you add a header to an email, it's best to start it with
622    # 'X-Bugzilla-<Extension>' so that you don't conflict with
623    # other extensions.
624    $email->header_set('X-Bugzilla-Example-Header', 'Example');
625}
626
627sub object_before_create {
628    my ($self, $args) = @_;
629
630    my $class = $args->{'class'};
631    my $object_params = $args->{'params'};
632
633    # Note that this is a made-up class, for this example.
634    if ($class->isa('Bugzilla::ExampleObject')) {
635        warn "About to create an ExampleObject!";
636        warn "Got the following parameters: "
637             . join(', ', keys(%$object_params));
638    }
639}
640
641sub object_before_delete {
642    my ($self, $args) = @_;
643
644    my $object = $args->{'object'};
645
646    # Note that this is a made-up class, for this example.
647    if ($object->isa('Bugzilla::ExampleObject')) {
648        my $id = $object->id;
649        warn "An object with id $id is about to be deleted!";
650    }
651}
652
653sub object_before_set {
654    my ($self, $args) = @_;
655
656    my ($object, $field, $value) = @$args{qw(object field value)};
657
658    # Note that this is a made-up class, for this example.
659    if ($object->isa('Bugzilla::ExampleObject')) {
660        warn "The field $field is changing from " . $object->{$field}
661             . " to $value!";
662    }
663}
664
665sub object_columns {
666    my ($self, $args) = @_;
667    my ($class, $columns) = @$args{qw(class columns)};
668
669    if ($class->isa('Bugzilla::ExampleObject')) {
670        push(@$columns, 'example');
671    }
672}
673
674sub object_end_of_create {
675    my ($self, $args) = @_;
676
677    my $class  = $args->{'class'};
678    my $object = $args->{'object'};
679
680    warn "Created a new $class object!";
681}
682
683sub object_end_of_create_validators {
684    my ($self, $args) = @_;
685
686    my $class = $args->{'class'};
687    my $object_params = $args->{'params'};
688
689    # Note that this is a made-up class, for this example.
690    if ($class->isa('Bugzilla::ExampleObject')) {
691        # Always set example_field to 1, even if the validators said otherwise.
692        $object_params->{example_field} = 1;
693    }
694
695}
696
697sub object_end_of_set {
698    my ($self, $args) = @_;
699
700    my ($object, $field) = @$args{qw(object field)};
701
702    # Note that this is a made-up class, for this example.
703    if ($object->isa('Bugzilla::ExampleObject')) {
704        warn "The field $field has changed to " . $object->{$field};
705    }
706}
707
708sub object_end_of_set_all {
709    my ($self, $args) = @_;
710
711    my $object = $args->{'object'};
712    my $object_params = $args->{'params'};
713
714    # Note that this is a made-up class, for this example.
715    if ($object->isa('Bugzilla::ExampleObject')) {
716        if ($object_params->{example_field} == 1) {
717            $object->{example_field} = 1;
718        }
719    }
720
721}
722
723sub object_end_of_update {
724    my ($self, $args) = @_;
725
726    my ($object, $old_object, $changes) =
727        @$args{qw(object old_object changes)};
728
729    # Note that this is a made-up class, for this example.
730    if ($object->isa('Bugzilla::ExampleObject')) {
731        if (defined $changes->{'name'}) {
732            my ($old, $new) = @{ $changes->{'name'} };
733            print "The name field changed from $old to $new!";
734        }
735    }
736}
737
738sub object_update_columns {
739    my ($self, $args) = @_;
740    my ($object, $columns) = @$args{qw(object columns)};
741
742    if ($object->isa('Bugzilla::ExampleObject')) {
743        push(@$columns, 'example');
744    }
745}
746
747sub object_validators {
748    my ($self, $args) = @_;
749    my ($class, $validators) = @$args{qw(class validators)};
750
751    if ($class->isa('Bugzilla::Bug')) {
752        # This is an example of adding a new validator.
753        # See the _check_example subroutine below.
754        $validators->{example} = \&_check_example;
755
756        # This is an example of overriding an existing validator.
757        # See the check_short_desc validator below.
758        my $original = $validators->{short_desc};
759        $validators->{short_desc} = sub { _check_short_desc($original, @_) };
760    }
761}
762
763sub _check_example {
764    my ($invocant, $value, $field) = @_;
765    warn "I was called to validate the value of $field.";
766    warn "The value of $field that I was passed in is: $value";
767
768    # Make the value always be 1.
769    my $fixed_value = 1;
770    return $fixed_value;
771}
772
773sub _check_short_desc {
774    my $original = shift;
775    my $invocant = shift;
776    my $value = $invocant->$original(@_);
777    if ($value !~ /example/i) {
778        # Use this line to make Bugzilla throw an error every time
779        # you try to file a bug or update a bug without the word "example"
780        # in the summary.
781        if (0) {
782            ThrowUserError('example_short_desc_invalid');
783        }
784    }
785    return $value;
786}
787
788sub page_before_template {
789    my ($self, $args) = @_;
790
791    my ($vars, $page) = @$args{qw(vars page_id)};
792
793    # You can see this hook in action by loading page.cgi?id=example.html
794    if ($page eq 'example.html') {
795        $vars->{cgi_variables} = { Bugzilla->cgi->Vars };
796    }
797}
798
799sub path_info_whitelist {
800    my ($self, $args) = @_;
801    my $whitelist = $args->{whitelist};
802    push(@$whitelist, "page.cgi");
803}
804
805sub post_bug_after_creation {
806    my ($self, $args) = @_;
807
808    my $vars = $args->{vars};
809    $vars->{'example'} = 1;
810}
811
812sub product_confirm_delete {
813    my ($self, $args) = @_;
814
815    my $vars = $args->{vars};
816    $vars->{'example'} = 1;
817}
818
819
820sub product_end_of_create {
821    my ($self, $args) = @_;
822
823    my $product = $args->{product};
824
825    # For this example, any lines of code that actually make changes to your
826    # database have been commented out.
827
828    # This section will take a group that exists in your installation
829    # (possible called test_group) and automatically makes the new
830    # product hidden to only members of the group. Just remove
831    # the restriction if you want the new product to be public.
832
833    my $example_group = new Bugzilla::Group({ name => 'example_group' });
834
835    if ($example_group) {
836        $product->set_group_controls($example_group,
837                { entry          => 1,
838                  membercontrol  => CONTROLMAPMANDATORY,
839                  othercontrol   => CONTROLMAPMANDATORY });
840#        $product->update();
841    }
842
843    # This section will automatically add a default component
844    # to the new product called 'No Component'.
845
846    my $default_assignee = new Bugzilla::User(
847        { name => Bugzilla->params->{maintainer} });
848
849    if ($default_assignee) {
850#        Bugzilla::Component->create(
851#            { name             => 'No Component',
852#              product          => $product,
853#              description      => 'Select this component if one does not ' .
854#                                  'exist in the current list of components',
855#              initialowner     => $default_assignee });
856    }
857}
858
859sub quicksearch_map {
860    my ($self, $args) = @_;
861    my $map = $args->{'map'};
862
863    # This demonstrates adding a shorter alias for a long custom field name.
864    $map->{'impact'} = $map->{'cf_long_field_name_for_impact_field'};
865}
866
867sub sanitycheck_check {
868    my ($self, $args) = @_;
869
870    my $dbh = Bugzilla->dbh;
871    my $sth;
872
873    my $status = $args->{'status'};
874
875    # Check that all users are Australian
876    $status->('example_check_au_user');
877
878    $sth = $dbh->prepare("SELECT userid, login_name
879                            FROM profiles
880                           WHERE login_name NOT LIKE '%.au'");
881    $sth->execute;
882
883    my $seen_nonau = 0;
884    while (my ($userid, $login, $numgroups) = $sth->fetchrow_array) {
885        $status->('example_check_au_user_alert',
886                  { userid => $userid, login => $login },
887                  'alert');
888        $seen_nonau = 1;
889    }
890
891    $status->('example_check_au_user_prompt') if $seen_nonau;
892}
893
894sub sanitycheck_repair {
895    my ($self, $args) = @_;
896
897    my $cgi = Bugzilla->cgi;
898    my $dbh = Bugzilla->dbh;
899
900    my $status = $args->{'status'};
901
902    if ($cgi->param('example_repair_au_user')) {
903        $status->('example_repair_au_user_start');
904
905        #$dbh->do("UPDATE profiles
906        #             SET login_name = CONCAT(login_name, '.au')
907        #           WHERE login_name NOT LIKE '%.au'");
908
909        $status->('example_repair_au_user_end');
910    }
911}
912
913sub template_before_create {
914    my ($self, $args) = @_;
915
916    my $config = $args->{'config'};
917    # This will be accessible as "example_global_variable" in every
918    # template in Bugzilla. See Bugzilla/Template.pm's create() function
919    # for more things that you can set.
920    $config->{VARIABLES}->{example_global_variable} = sub { return 'value' };
921}
922
923sub template_before_process {
924    my ($self, $args) = @_;
925
926    my ($vars, $file, $context) = @$args{qw(vars file context)};
927
928    if ($file eq 'bug/edit.html.tmpl') {
929        $vars->{'viewing_the_bug_form'} = 1;
930    }
931}
932
933sub user_check_account_creation {
934    my ($self, $args) = @_;
935
936    my $login = $args->{login};
937    my $ip = remote_ip();
938
939    # Log all requests.
940    warn "USER ACCOUNT CREATION REQUEST FOR $login ($ip)";
941
942    # Reject requests based on their email address.
943    if ($login =~ /\@evil\.com$/) {
944        ThrowUserError('account_creation_restricted');
945    }
946
947    # Reject requests based on their IP address.
948    if ($ip =~ /^192\.168\./) {
949        ThrowUserError('account_creation_restricted');
950    }
951}
952
953sub user_preferences {
954    my ($self, $args) = @_;
955    my $tab = $args->{current_tab};
956    my $save = $args->{save_changes};
957    my $handled = $args->{handled};
958
959    return unless $tab eq 'my_tab';
960
961    my $value = Bugzilla->input_params->{'example_pref'};
962    if ($save) {
963        # Validate your data and update the DB accordingly.
964        $value =~ s/\s+/:/g;
965    }
966    $args->{'vars'}->{example_pref} = $value;
967
968    # Set the 'handled' scalar reference to true so that the caller
969    # knows the panel name is valid and that an extension took care of it.
970    $$handled = 1;
971}
972
973sub webservice {
974    my ($self, $args) = @_;
975
976    my $dispatch = $args->{dispatch};
977    $dispatch->{Example} = "Bugzilla::Extension::Example::WebService";
978}
979
980sub webservice_error_codes {
981    my ($self, $args) = @_;
982
983    my $error_map = $args->{error_map};
984    $error_map->{'example_my_error'} = 10001;
985}
986
987sub webservice_status_code_map {
988    my ($self, $args) = @_;
989
990    my $status_code_map = $args->{status_code_map};
991    # Uncomment this line to override the status code for the
992    # error 'object_does_not_exist' to STATUS_BAD_REQUEST
993    #$status_code_map->{51} = STATUS_BAD_REQUEST;
994}
995
996sub webservice_before_call {
997    my ($self, $args) = @_;
998
999    # This code doesn't actually *do* anything, it's just here to show you
1000    # how to use this hook.
1001    my $method      = $args->{method};
1002    my $full_method = $args->{full_method};
1003
1004    # Uncomment this line to see a line in your webserver's error log whenever
1005    # a webservice call is made
1006    #warn "RPC call $full_method made by ",
1007    #   Bugzilla->user->login || 'an anonymous user', "\n";
1008}
1009
1010sub webservice_fix_credentials {
1011    my ($self, $args) = @_;
1012    my $rpc    = $args->{'rpc'};
1013    my $params = $args->{'params'};
1014    # Allow user to pass in username=foo&password=bar
1015    if (exists $params->{'username'} && exists $params->{'password'}) {
1016        $params->{'Bugzilla_login'}    = $params->{'username'};
1017        $params->{'Bugzilla_password'} = $params->{'password'};
1018    }
1019}
1020
1021sub webservice_rest_request {
1022    my ($self, $args) = @_;
1023    my $rpc    = $args->{'rpc'};
1024    my $params = $args->{'params'};
1025    # Internally we may have a field called 'cf_test_field' but we allow users
1026    # to use the shorter 'test_field' name.
1027    if (exists $params->{'test_field'}) {
1028        $params->{'test_field'} = delete $params->{'cf_test_field'};
1029    }
1030}
1031
1032sub webservice_rest_resources {
1033    my ($self, $args) = @_;
1034    my $rpc = $args->{'rpc'};
1035    my $resources = $args->{'resources'};
1036    # Add a new resource that allows for /rest/example/hello
1037    # to call Example.hello
1038    $resources->{'Bugzilla::Extension::Example::WebService'} = [
1039        qr{^/example/hello$}, {
1040            GET => {
1041                method => 'hello',
1042            }
1043        }
1044    ];
1045}
1046
1047sub webservice_rest_response {
1048    my ($self, $args) = @_;
1049    my $rpc      = $args->{'rpc'};
1050    my $result   = $args->{'result'};
1051    my $response = $args->{'response'};
1052    # Convert a list of bug hashes to a single bug hash if only one is
1053    # being returned.
1054    if (ref $$result eq 'HASH'
1055        && exists $$result->{'bugs'}
1056        && scalar @{ $$result->{'bugs'} } == 1)
1057    {
1058        $$result = $$result->{'bugs'}->[0];
1059    }
1060}
1061
1062# This must be the last line of your extension.
1063__PACKAGE__->NAME;
1064