# 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/. # # This Source Code Form is "Incompatible With Secondary Licenses", as # defined by the Mozilla Public License, v. 2.0. package Bugzilla::Extension::Example; use 5.10.1; use strict; use parent qw(Bugzilla::Extension); use Bugzilla::Constants; use Bugzilla::Error; use Bugzilla::Group; use Bugzilla::User; use Bugzilla::User::Setting; use Bugzilla::Util qw(diff_arrays html_quote); use Bugzilla::Status qw(is_open_state); use Bugzilla::Install::Filesystem; # 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; use Data::Dumper; # See bugmail_relationships. use constant REL_EXAMPLE => -127; our $VERSION = '1.0'; sub admin_editusers_action { 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; } } sub attachment_process_data { 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"; } } } sub auth_login_methods { my ($self, $args) = @_; my $modules = $args->{modules}; if (exists $modules->{Example}) { $modules->{Example} = 'Bugzilla/Extension/Example/Auth/Login.pm'; } } sub auth_verify_methods { my ($self, $args) = @_; my $modules = $args->{modules}; if (exists $modules->{Example}) { $modules->{Example} = 'Bugzilla/Extension/Example/Auth/Verify.pm'; } } sub bug_check_can_change_field { my ($self, $args) = @_; my ($bug, $field, $new_value, $old_value, $priv_results) = @$args{qw(bug field new_value old_value priv_results)}; my $user = Bugzilla->user; # 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')) { push(@$priv_results, PRIVILEGES_REQUIRED_EMPOWERED); return; } } # 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; } } sub bug_columns { my ($self, $args) = @_; my $columns = $args->{'columns'}; push (@$columns, "delta_ts AS example") } sub bug_end_of_create { 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; # 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!"; } sub bug_end_of_create_validators { my ($self, $args) = @_; # This code doesn't actually *do* anything, it's just here to show you # how to use this hook. my $bug_params = $args->{'params'}; # 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); # This would remove all ccs from the bug, preventing ANY ccs from being # added on bug creation. # $bug_params->{cc} = []; } 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!"; } } 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; } sub bug_end_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!"; } } 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; } sub bug_fields { my ($self, $args) = @_; my $fields = $args->{'fields'}; push (@$fields, "example") } sub bug_format_comment { 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 bar my $bar_match = qr/\b(bar)\b/; push(@$regexes, { match => $bar_match, replace => \&_replace_bar }); } # Used by bug_format_comment--see its code for an explanation. sub _replace_bar { 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{$match}; }; sub buglist_columns { 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' }; } sub buglist_column_joins { my ($self, $args) = @_; my $joins = $args->{'column_joins'}; # This column is added using the "buglist_columns" hook $joins->{'product_desc'} = { from => 'product_id', to => 'id', table => 'products', as => 'prod_desc', join => 'INNER', }; } sub search_operator_field_override { my ($self, $args) = @_; my $operators = $args->{'operators'}; my $original = $operators->{component}->{_non_changed}; $operators->{component} = { _non_changed => sub { _component_nonchanged($original, @_) } }; } sub _component_nonchanged { my $original = shift; my ($invocant, $args) = @_; $invocant->$original($args); # Actually, it does not change anything in the result, # just an example. $args->{term} = $args->{term} . " OR 1=2"; } sub bugmail_recipients { 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 he had the "REL_EXAMPLE" # relationship from the bugmail_relationships hook below. #$recipients->{$user->id}->{+REL_EXAMPLE} = 1; } } sub bugmail_relationships { my ($self, $args) = @_; my $relationships = $args->{relationships}; $relationships->{+REL_EXAMPLE} = 'Example'; } sub config_add_panels { my ($self, $args) = @_; my $modules = $args->{panel_modules}; $modules->{Example} = "Bugzilla::Extension::Example::Config"; } sub config_modify_panels { my ($self, $args) = @_; my $panels = $args->{panels}; # 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); push(@{ $info_class->{choices} }, 'CGI,Example'); push(@{ $verify_class->{choices} }, 'Example'); push(@$auth_params, { name => 'param_example', type => 't', default => 0, checker => \&check_numeric }); } sub db_schema_abstract_schema { my ($self, $args) = @_; # $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'], # ], # }; } sub email_in_before_parse { my ($self, $args) = @_; 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; } } sub email_in_after_parse { 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 him, # but he 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}; } } else { ThrowUserError('invalid_username', { name => $reporter }); } } sub enter_bug_entrydefaultvars { my ($self, $args) = @_; my $vars = $args->{vars}; $vars->{'example'} = 1; } sub error_catch { 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}; my $login = Bugzilla->user->identity || "Someone"; 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/(?<=