Skip to content

Convert the conf/database.conf.dist file to a Perl module. #2702

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 9 additions & 43 deletions bin/addcourse
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ be granted professor privileges.

=over

=item B<--db-layout>=I<LAYOUT>

The specified database layout will be used in place of the default specified in
F<defaults.config>.

=item B<--users>=I<FILE>

The users listed in the comma-separated text file I<FILE> will be added to the
Expand Down Expand Up @@ -63,30 +58,26 @@ BEGIN {
use lib "$ENV{WEBWORK_ROOT}/lib";

use WeBWorK::CourseEnvironment;

# Grab course environment (by reading webwork2/conf/defaults.config)
my $ce = WeBWorK::CourseEnvironment->new;

use WeBWorK::DB;
use WeBWorK::File::Classlist;
use WeBWorK::Utils qw(runtime_use cryptPassword);
use WeBWorK::Utils::CourseManagement qw(addCourse);
use WeBWorK::File::Classlist qw(parse_classlist);
use WeBWorK::DB::Record::User;
use WeBWorK::DB::Record::Password;
use WeBWorK::DB::Record::PermissionLevel;

sub usage_error {
warn "@_\n";
warn "usage: $0 [options] COURSEID\n";
warn "Options:\n";
warn " [--db-layout=LAYOUT]\n";
warn " [--users=FILE [--professors=USERID[,USERID]...] ]\n";
exit;
}

my ($dbLayout, $users, $templates_from) = ('', '', '');
my ($users, $templates_from) = ('', '');
my @professors;

GetOptions(
"db-layout=s" => \$dbLayout,
"users=s" => \$users,
"professors=s" => \@professors,
"templates-from=s" => \$templates_from,
Expand All @@ -96,33 +87,16 @@ my $courseID = shift;

usage_error('The COURSEID must be provided.') unless $courseID;

$ce = WeBWorK::CourseEnvironment->new({ courseName => $courseID });
my $ce = WeBWorK::CourseEnvironment->new({ courseName => $courseID });

die "Aborting addcourse: Course ID cannot exceed $ce->{maxCourseIdLength} characters."
if length($courseID) > $ce->{maxCourseIdLength};

if ($dbLayout) {
die "Database layout $dbLayout does not exist in the course environment.",
" (It must be defined in defaults.config.)\n"
unless exists $ce->{dbLayouts}{$dbLayout};
} else {
$dbLayout = $ce->{dbLayoutName};
}

usage_error("Can't specify --professors without also specifying --users.")
if @professors && !$users;

my @users;
if ($users) {
# This is a hack to create records without bringing up a DB object
my $userClass = $ce->{dbLayouts}{$dbLayout}{user}{record};
my $passwordClass = $ce->{dbLayouts}{$dbLayout}{password}{record};
my $permissionClass = $ce->{dbLayouts}{$dbLayout}{permission}{record};

runtime_use($userClass);
runtime_use($passwordClass);
runtime_use($permissionClass);

my @classlist = parse_classlist($users);
for my $record (@classlist) {
my %record = %$record;
Expand Down Expand Up @@ -154,9 +128,9 @@ if ($users) {

push @users,
[
$userClass->new(%record),
$record{password} ? $passwordClass->new(user_id => $user_id, password => $record{password}) : undef,
$permissionClass->new(
WeBWorK::DB::Record::User->new(%record),
WeBWorK::DB::Record::Password->new(user_id => $user_id, password => $record{password}),
WeBWorK::DB::Record::PermissionLevel->new(
user_id => $user_id,
permission => defined $professors{$user_id}
? $ce->{userRoles}{professor}
Expand All @@ -176,15 +150,7 @@ if ($templates_from) {
$optional_arguments{copyTemplatesHtml} = 1;
}

eval {
addCourse(
courseID => $courseID,
ce => $ce,
courseOptions => { dbLayoutName => $dbLayout },
users => \@users,
%optional_arguments,
);
};
eval { addCourse(courseID => $courseID, ce => $ce, users => \@users, %optional_arguments,); };

die "$@\n" if $@;

Expand Down
2 changes: 1 addition & 1 deletion bin/change_user_id
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ my $ce = WeBWorK::CourseEnvironment->new({
courseName => $courseID
});

my $db = new WeBWorK::DB($ce->{dbLayout});
my $db = WeBWorK::DB->new($ce);
die "Error: $old_user_id does not exist!" unless $db->existsUser($old_user_id);

unless($db->existsUser($new_user_id)) {
Expand Down
10 changes: 0 additions & 10 deletions bin/check_database_charsets.pl

This file was deleted.

2 changes: 1 addition & 1 deletion bin/dump-past-answers.pl
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ sub write_past_answers_csv {
next if $courseID eq ($minimal_ce->{admin_course_id} // 'admin') || $courseID eq 'modelCourse';

my $ce = WeBWorK::CourseEnvironment->new({ webwork_dir => $ENV{WEBWORK_ROOT}, courseName => $courseID });
my $db = WeBWorK::DB->new($ce->{dbLayout});
my $db = WeBWorK::DB->new($ce);

my %permissionLabels = reverse %{ $ce->{userRoles} };

Expand Down
4 changes: 2 additions & 2 deletions bin/importClassList.pl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ BEGIN

use WeBWorK::CourseEnvironment;

use WeBWorK::DB qw(check_user_id);
use WeBWorK::DB;
use WeBWorK::File::Classlist;
use WeBWorK::Utils qw(cryptPassword);
use WeBWorK::File::Classlist qw(parse_classlist);
Expand All @@ -35,7 +35,7 @@ BEGIN
courseName => $courseID
});

my $db = WeBWorK::DB->new($ce->{dbLayout});
my $db = WeBWorK::DB->new($ce);

my $createNew = 1; # Always set to true, so add new users
my $replaceExisting = "none"; # Always set to "none" so no existing accounts are changed
Expand Down
2 changes: 1 addition & 1 deletion bin/newpassword
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ my $ce = WeBWorK::CourseEnvironment->new({
courseName => $courseID
});

my $db = new WeBWorK::DB($ce->{dbLayout});
my $db = WeBWorK::DB->new($ce);

dopasswd($db, $user, $newP);
print "Changed password for $user in $courseID\n";
2 changes: 1 addition & 1 deletion bin/upgrade-database-to-utf8mb4.pl
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ BEGIN
},
);

my $db = WeBWorK::DB->new($ce->{dbLayouts}{ $ce->{dbLayoutName} });
my $db = WeBWorK::DB->new($ce);
my @table_types = sort(grep { !$db->{$_}{params}{non_native} } keys %$db);

sub checkAndUpdateTableColumnTypes {
Expand Down
2 changes: 1 addition & 1 deletion bin/ww_purge_old_nonces
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ my $ce = WeBWorK::CourseEnvironment->new({
courseName => $course,
});

my $db = WeBWorK::DB->new($ce->{dbLayout});
my $db = WeBWorK::DB->new($ce);

my @errors;

Expand Down
115 changes: 0 additions & 115 deletions bin/wwdb

This file was deleted.

2 changes: 1 addition & 1 deletion bin/wwsh
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ $ce = WeBWorK::CourseEnvironment->new({
});


$db = WeBWorK::DB->new($ce->{dbLayout});
$db = WeBWorK::DB->new($ce);

print <<'EOF';
wwsh - The WeBWorK Shell
Expand Down
2 changes: 0 additions & 2 deletions conf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ Basic webwork2 configuration files.
- `localOverrides.conf.dist` should be copied to `localOverrides.conf`. `localOverrides.conf` will be read after the
`defaults.config` file is processed and will overwrite configurations in `defaults.config`. Use this file to make
changes to the settings in `defaults.config`.
- `database.conf.dist` contains database configuration parameters. It is included by `defaults.config`. This file
should not be copied or modified unless you really know what you are doing.

Configuration extension files.

Expand Down
4 changes: 1 addition & 3 deletions conf/authen_CAS.conf.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
########################################################################################

# Set CAS as the authentication module to use.
$authen{user_module} = {
"*" => "WeBWorK::Authen::CAS",
};
$authen{user_module} = 'WeBWorK::Authen::CAS';

# List of authentication modules that may be used to enter the admin course.
# This is used instead of $authen{user_module} when logging into the admin course.
Expand Down
6 changes: 3 additions & 3 deletions conf/authen_LTI.conf.dist
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ $debug_lti_grade_passback = 0;
# the LTIAdvantage will be used. If you know a site will not use one or the other, it can be
# commented out. Failover to Basic_TheLastOption is necessary to authenticate with cookie keys.
$authen{user_module} = [
{ '*' => 'WeBWorK::Authen::LTIAdvantage' }, # first try LTI 1.3
{ '*' => 'WeBWorK::Authen::LTIAdvanced' }, # next try LTI 1.1
{ '*' => 'WeBWorK::Authen::Basic_TheLastOption' } # fallback authorization method
'WeBWorK::Authen::LTIAdvantage', # first try LTI 1.3
'WeBWorK::Authen::LTIAdvanced', # next try LTI 1.1
'WeBWorK::Authen::Basic_TheLastOption' # fallback authorization method
];

# List of authentication modules that may be used to enter the admin course.
Expand Down
2 changes: 1 addition & 1 deletion conf/authen_ldap.conf.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
########################################################################################

# Set LDAP as the authentication module to use.
$authen{user_module} = { "*" => "WeBWorK::Authen::LDAP" };
$authen{user_module} = 'WeBWorK::Authen::LDAP';

# List of authentication modules that may be used to enter the admin course.
# This is used instead of $authen{user_module} when logging into the admin course.
Expand Down
Loading