Install.pm 14.8 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 9

package Bugzilla::Install;

10
# Functions in this this package can assume that the database
11 12 13 14 15 16 17
# has been set up, params are available, localconfig is
# available, and any module can be used.
#
# If you want to write an installation function that can't
# make those assumptions, then it should go into one of the
# packages under the Bugzilla::Install namespace.

18
use 5.10.1;
19
use strict;
20
use warnings;
21

22
use Bugzilla::Component;
23
use Bugzilla::Config qw(:admin);
24 25
use Bugzilla::Constants;
use Bugzilla::Error;
26 27
use Bugzilla::Group;
use Bugzilla::Product;
28
use Bugzilla::User;
29
use Bugzilla::User::Setting;
30
use Bugzilla::Util qw(get_text);
31
use Bugzilla::Version;
32

33
use constant STATUS_WORKFLOW => (
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
  [undef,         'UNCONFIRMED'],
  [undef,         'CONFIRMED'],
  [undef,         'IN_PROGRESS'],
  ['UNCONFIRMED', 'CONFIRMED'],
  ['UNCONFIRMED', 'IN_PROGRESS'],
  ['UNCONFIRMED', 'RESOLVED'],
  ['CONFIRMED',   'IN_PROGRESS'],
  ['CONFIRMED',   'RESOLVED'],
  ['IN_PROGRESS', 'CONFIRMED'],
  ['IN_PROGRESS', 'RESOLVED'],
  ['RESOLVED',    'UNCONFIRMED'],
  ['RESOLVED',    'CONFIRMED'],
  ['RESOLVED',    'VERIFIED'],
  ['VERIFIED',    'UNCONFIRMED'],
  ['VERIFIED',    'CONFIRMED'],
49 50
);

51
sub SETTINGS {
52
  return {
53
    # 2005-03-03 travis@sedsystems.ca -- Bug 41972
54 55
    display_quips => {options => ["on", "off"], default => "on"},

56
    # 2005-03-10 travis@sedsystems.ca -- Bug 199048
57 58 59 60 61 62
    comment_sort_order => {
      options =>
        ["oldest_to_newest", "newest_to_oldest", "newest_to_oldest_desc_first"],
      default => "oldest_to_newest"
    },

63
    # 2005-05-12 bugzilla@glob.com.au -- Bug 63536
64 65 66
    post_bug_submit_action =>
      {options => ["next_bug", "same_bug", "nothing"], default => "next_bug"},

67
    # 2005-06-29 wurblzap@gmail.com -- Bug 257767
68 69
    csv_colsepchar => {options => [',', ';'], default => ','},

70
    # 2005-10-26 wurblzap@gmail.com -- Bug 291459
71 72
    zoom_textareas => {options => ["on", "off"], default => "on"},

73
    # 2006-05-01 olav@bkor.dhs.org -- Bug 7710
74 75 76 77 78
    state_addselfcc => {
      options => ['always', 'never', 'cc_unless_role'],
      default => 'cc_unless_role'
    },

79
    # 2006-08-04 wurblzap@gmail.com -- Bug 322693
80 81
    skin => {subclass => 'Skin', default => 'Dusk'},

82
    # 2006-12-10 LpSolit@gmail.com -- Bug 297186
83 84
    lang => {subclass => 'Lang', default => ${Bugzilla->languages}[0]},

85
    # 2007-07-02 altlist@gmail.com -- Bug 225731
86 87 88 89 90
    quote_replies => {
      options => ['quoted_reply', 'simple_reply', 'off'],
      default => "quoted_reply"
    },

91
    # 2009-02-01 mozilla@matt.mchenryfamily.org -- Bug 398473
92 93 94 95 96
    comment_box_position => {
      options => ['before_comments', 'after_comments'],
      default => 'before_comments'
    },

97
    # 2008-08-27 LpSolit@gmail.com -- Bug 182238
98 99
    timezone => {subclass => 'Timezone', default => 'local'},

100
    # 2011-02-07 dkl@mozilla.com -- Bug 580490
101 102
    quicksearch_fulltext => {options => ['on', 'off'], default => 'on'},

103
    # 2011-06-21 glob@mozilla.com -- Bug 589128
104 105
    email_format => {options => ['html', 'text_only'], default => 'html'},

106
    # 2011-10-11 glob@mozilla.com -- Bug 301656
107 108
    requestee_cc => {options => ['on', 'off'], default => 'on'},

109
    # 2012-04-30 glob@mozilla.com -- Bug 663747
110 111
    bugmail_new_prefix => {options => ['on', 'off'], default => 'on'},

112
    # 2013-07-26 joshi_sunil@in.com -- Bug 669535
113 114 115
    possible_duplicates => {options => ['on', 'off'], default => 'on'},
  };
}
116

117
use constant SYSTEM_GROUPS => (
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
  {name => 'admin',        description => 'Administrators'},
  {name => 'tweakparams',  description => 'Can change Parameters'},
  {name => 'editusers',    description => 'Can edit or disable users'},
  {name => 'creategroups', description => 'Can create and destroy groups'},
  {
    name        => 'editclassifications',
    description => 'Can create, destroy, and edit classifications'
  },
  {
    name        => 'editcomponents',
    description => 'Can create, destroy, and edit components'
  },
  {
    name        => 'editkeywords',
    description => 'Can create, destroy, and edit keywords'
  },
  {
    name        => 'editbugs',
    description => 'Can edit all bug fields',
    userregexp  => '.*'
  },
  {
    name        => 'canconfirm',
    description => 'Can confirm a bug or mark it a duplicate'
  },
  {
    name        => 'bz_canusewhineatothers',
    description => 'Can configure whine reports for other users',
  },
  {
    name        => 'bz_canusewhines',
    description => 'User can configure whine reports for self',

    # inherited_by means that users in the groups listed below are
    # automatically members of bz_canusewhines.
    inherited_by => ['editbugs', 'bz_canusewhineatothers'],
  },
  {name => 'bz_sudoers', description => 'Can perform actions as other users',},
  {
    name         => 'bz_sudo_protect',
    description  => 'Can not be impersonated by other users',
    inherited_by => ['bz_sudoers'],
  },
  {name => 'bz_quip_moderators', description => 'Can moderate quips',},
162 163
);

164 165
use constant DEFAULT_CLASSIFICATION =>
  {name => 'Unclassified', description => 'Not assigned to any classification'};
166 167

use constant DEFAULT_PRODUCT => {
168 169 170 171 172 173 174
  name        => 'TestProduct',
  description => 'This is a test product.'
    . ' This ought to be blown away and replaced with real stuff in a'
    . ' finished installation of bugzilla.',
  version          => Bugzilla::Version::DEFAULT_VERSION,
  classification   => 'Unclassified',
  defaultmilestone => DEFAULT_MILESTONE,
175 176 177
};

use constant DEFAULT_COMPONENT => {
178 179 180 181
  name        => 'TestComponent',
  description => 'This is a test component in the test product database.'
    . ' This ought to be blown away and replaced with real stuff in'
    . ' a finished installation of Bugzilla.'
182 183
};

184
sub update_settings {
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
  my $dbh = Bugzilla->dbh;

  # If we're setting up settings for the first time, we want to be quieter.
  my $any_settings
    = $dbh->selectrow_array('SELECT 1 FROM setting ' . $dbh->sql_limit(1));
  if (!$any_settings) {
    say get_text('install_setting_setup');
  }

  my %settings = %{SETTINGS()};
  foreach my $setting (keys %settings) {
    add_setting(
      $setting,
      $settings{$setting}->{options},
      $settings{$setting}->{default},
      $settings{$setting}->{subclass},
      undef, !$any_settings
    );
  }

  # Delete the obsolete 'per_bug_queries' user preference. Bug 616191.
  $dbh->do('DELETE FROM setting WHERE name = ?', undef, 'per_bug_queries');
207 208
}

209
sub update_system_groups {
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
  my $dbh = Bugzilla->dbh;

  $dbh->bz_start_transaction();

  # If there is no editbugs group, this is the first time we're
  # adding groups.
  my $editbugs_exists = new Bugzilla::Group({name => 'editbugs'});
  if (!$editbugs_exists) {
    say get_text('install_groups_setup');
  }

  # Create most of the system groups
  foreach my $definition (SYSTEM_GROUPS) {
    my $exists = new Bugzilla::Group({name => $definition->{name}});
    if (!$exists) {
      $definition->{isbuggroup} = 0;
      $definition->{silently}   = !$editbugs_exists;
      my $inherited_by = delete $definition->{inherited_by};
      my $created      = Bugzilla::Group->create($definition);

      # Each group in inherited_by is automatically a member of this
      # group.
      if ($inherited_by) {
        foreach my $name (@$inherited_by) {
          my $member = Bugzilla::Group->check($name);
          $dbh->do(
            'INSERT INTO group_group_map (grantor_id, 
                                          member_id) VALUES (?,?)', undef, $created->id,
            $member->id
          );
240
        }
241
      }
242
    }
243
  }
244

245
  $dbh->bz_commit_transaction();
246 247
}

248
sub create_default_classification {
249 250 251 252 253 254 255 256 257
  my $dbh = Bugzilla->dbh;

  # Make the default Classification if it doesn't already exist.
  if (!$dbh->selectrow_array('SELECT 1 FROM classifications')) {
    print get_text('install_default_classification',
      {name => DEFAULT_CLASSIFICATION->{name}})
      . "\n";
    Bugzilla::Classification->create(DEFAULT_CLASSIFICATION);
  }
258 259 260 261
}

# This function should be called only after creating the admin user.
sub create_default_product {
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
  my $dbh = Bugzilla->dbh;

  # And same for the default product/component.
  if (!$dbh->selectrow_array('SELECT 1 FROM products')) {
    print get_text('install_default_product', {name => DEFAULT_PRODUCT->{name}})
      . "\n";

    my $product = Bugzilla::Product->create(DEFAULT_PRODUCT);

    # Get the user who will be the owner of the Component.
    # We pick the admin with the lowest id, which is probably the
    # admin checksetup.pl just created.
    my $admin_group = new Bugzilla::Group({name => 'admin'});
    my ($admin_id) = $dbh->selectrow_array(
      'SELECT user_id FROM user_group_map WHERE group_id = ?
           ORDER BY user_id ' . $dbh->sql_limit(1), undef, $admin_group->id
    );
    my $admin = Bugzilla::User->new($admin_id);

    Bugzilla::Component->create({
      %{DEFAULT_COMPONENT()}, product => $product, initialowner => $admin->login
    });
  }
285 286 287

}

288
sub init_workflow {
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
  my $dbh          = Bugzilla->dbh;
  my $has_workflow = $dbh->selectrow_array('SELECT 1 FROM status_workflow');
  return if $has_workflow;

  say get_text('install_workflow_init');

  my %status_ids = @{
    $dbh->selectcol_arrayref('SELECT value, id FROM bug_status',
      {Columns => [1, 2]})
  };

  foreach my $pair (STATUS_WORKFLOW) {
    my $old_id = $pair->[0] ? $status_ids{$pair->[0]} : undef;
    my $new_id = $status_ids{$pair->[1]};
    $dbh->do(
      'INSERT INTO status_workflow (old_status, new_status)
                       VALUES (?,?)', undef, $old_id, $new_id
    );
  }
308 309
}

310
sub create_admin {
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
  my ($params) = @_;
  my $dbh      = Bugzilla->dbh;
  my $template = Bugzilla->template;

  my $admin_group = new Bugzilla::Group({name => 'admin'});
  my $admin_inheritors
    = Bugzilla::Group->flatten_group_membership($admin_group->id);
  my $admin_group_ids = join(',', @$admin_inheritors);

  my ($admin_count) = $dbh->selectrow_array(
    "SELECT COUNT(*) FROM user_group_map 
          WHERE group_id IN ($admin_group_ids)"
  );

  return if $admin_count;

  my %answer    = %{Bugzilla->installation_answers};
  my $login     = $answer{'ADMIN_EMAIL'};
  my $password  = $answer{'ADMIN_PASSWORD'};
  my $full_name = $answer{'ADMIN_REALNAME'};

  if (!$login || !$password || !$full_name) {
    say "\n" . get_text('install_admin_setup') . "\n";
  }

  while (!$login) {
    print get_text('install_admin_get_email') . ' ';
    $login = <STDIN>;
    chomp $login;
    eval { Bugzilla::User->check_login_name($login); };
    if ($@) {
      say $@;
      undef $login;
344
    }
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
  }

  while (!defined $full_name) {
    print get_text('install_admin_get_name') . ' ';
    $full_name = <STDIN>;
    chomp($full_name);
  }

  if (!$password) {
    $password = _prompt_for_password(get_text('install_admin_get_password'));
  }

  my $admin
    = Bugzilla::User->create({
    login_name => $login, realname => $full_name, cryptpassword => $password
    });
  make_admin($admin);
362 363 364
}

sub make_admin {
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
  my ($user) = @_;
  my $dbh = Bugzilla->dbh;

  $user
    = ref($user) ? $user : new Bugzilla::User(login_to_id($user, THROW_ERROR));

  my $group_insert = $dbh->prepare(
    'INSERT INTO user_group_map (user_id, group_id, isbless, grant_type)
              VALUES (?, ?, ?, ?)'
  );

  # Admins get explicit membership and bless capability for the admin group
  my $admin_group = new Bugzilla::Group({name => 'admin'});

  # These are run in an eval so that we can ignore the error of somebody
  # already being granted these things.
  eval { $group_insert->execute($user->id, $admin_group->id, 0, GRANT_DIRECT); };
  eval { $group_insert->execute($user->id, $admin_group->id, 1, GRANT_DIRECT); };

  # Admins should also have editusers directly, even though they'll usually
  # inherit it. People could have changed their inheritance structure.
  my $editusers = new Bugzilla::Group({name => 'editusers'});
  eval { $group_insert->execute($user->id, $editusers->id, 0, GRANT_DIRECT); };

  # If there is no maintainer set, make this user the maintainer.
  if (!Bugzilla->params->{'maintainer'}) {
    SetParam('maintainer', $user->email);
    write_params();
  }

  # Make sure the new admin isn't disabled
  if ($user->disabledtext) {
    $user->set_disabledtext('');
    $user->update();
  }
400

401 402 403
  if (Bugzilla->usage_mode == USAGE_MODE_CMDLINE) {
    say "\n", get_text('install_admin_created', {user => $user});
  }
404 405
}

406
sub _prompt_for_password {
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
  my $prompt = shift;

  my $password;
  while (!$password) {

    # trap a few interrupts so we can fix the echo if we get aborted.
    local $SIG{HUP}  = \&_password_prompt_exit;
    local $SIG{INT}  = \&_password_prompt_exit;
    local $SIG{QUIT} = \&_password_prompt_exit;
    local $SIG{TERM} = \&_password_prompt_exit;

    system("stty", "-echo") unless ON_WINDOWS;    # disable input echoing

    print $prompt, ' ';
    $password = <STDIN>;
    chomp $password;
    print "\n", get_text('install_confirm_password'), ' ';
    my $pass2 = <STDIN>;
    chomp $pass2;
    eval { validate_password($password, $pass2); };
    if ($@) {
      say "\n$@";
      undef $password;
430
    }
431 432 433
    system("stty", "echo") unless ON_WINDOWS;
  }
  return $password;
434 435 436 437
}

# This is just in case we get interrupted while getting a password.
sub _password_prompt_exit {
438 439 440 441

  # re-enable input echoing
  system("stty", "echo") unless ON_WINDOWS;
  exit 1;
442 443
}

444
sub reset_password {
445 446 447 448 449 450 451
  my $login    = shift;
  my $user     = Bugzilla::User->check($login);
  my $prompt   = "\n" . get_text('install_reset_password', {user => $user});
  my $password = _prompt_for_password($prompt);
  $user->set_password($password);
  $user->update();
  say "\n", get_text('install_reset_password_done');
452 453
}

454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
1;

__END__

=head1 NAME

Bugzilla::Install - Functions and variables having to do with
  installation.

=head1 SYNOPSIS

 use Bugzilla::Install;
 Bugzilla::Install::update_settings();

=head1 DESCRIPTION

This module is used primarily by L<checksetup.pl> during installation.
This module contains functions that deal with general installation
issues after the database is completely set up and configured.

=head1 CONSTANTS

=over

=item C<SETTINGS>

Contains information about Settings, used by L</update_settings()>.

=back

=head1 SUBROUTINES

=over

=item C<update_settings()>

Description: Adds and updates Settings for users.

Params:      none

Returns:     nothing.

496 497 498 499 500
=item C<create_default_classification>

Creates the default "Unclassified" L<Classification|Bugzilla::Classification>
if it doesn't already exist

501 502
=item C<create_default_product()>

503
Description: Creates the default product and component if
504 505 506 507 508 509
             they don't exist.

Params:      none

Returns:     nothing

510
=back
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526

=head1 B<Methods in need of POD>

=over

=item update_system_groups

=item reset_password

=item make_admin

=item create_admin

=item init_workflow

=back