Skip to content

Commit

Permalink
Extend amusewiki-reset-password to create users
Browse files Browse the repository at this point in the history
Close #457
  • Loading branch information
melmothx committed Jan 23, 2024
1 parent 7f06125 commit fd67436
Showing 1 changed file with 65 additions and 3 deletions.
68 changes: 65 additions & 3 deletions script/amusewiki-reset-password
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use warnings;
use lib 'lib';
use AmuseWikiFarm::Schema;
use Crypt::XkcdPassword;
use Getopt::Long;
use Pod::Usage;


=pod
Expand All @@ -19,30 +22,89 @@ amusewiki-reset-password - reset amusewiki passwords from the command line
=head1 SYNOPSIS
Usage: amusewiki-reset-password <username> [<password>]
Usage: amusewiki-reset-password <username> [<password>] [ --create-with-role ROLE ] [ --add-to-site SITE-ID ]
Reset the password of an arbitrary user. The password is optional and
will be generated automatically if not provided.
You need to have dbic.yaml in the current directory, or in
~/.dbic.yaml with the C<amuse> stanza with the settings.
=head2 OPTIONS
=over 4
=item --create-with-role ROLE
If the user does not exist, create it with the given role. ROLE can be
C<root>, C<admin> or C<librarian>.
=item --add-to-site SITE-ID
Add the user to the given site. Root users do not need this.
=back
=cut

my %roles = map { $_ => $_ } (qw/root admin librarian/);

my $create_with_role;
my @sites;

GetOptions(
'create-with-role=s' => \$create_with_role,
'add-to-site=s' => \@sites,
) or die;

my $schema = AmuseWikiFarm::Schema->connect('amuse');

my ($username, $password) = @ARGV;
$password ||= Crypt::XkcdPassword->new(words => 'IT')->make_password(5, qr{\A[0-9a-zA-Z]{3,}\z});

die "Usage: $0 <username> [<password>]\n" unless $username && $password;

if (my $user = $schema->resultset('User')->find({ username => $username })) {
my $user = $schema->resultset('User')->find({ username => $username });

if ($user) {
$user->password($password);
$user->update;
print qq{Password for $username is now '$password'\n};
}
elsif ($create_with_role) {
if (my $role = $roles{$create_with_role}) {
$user = $schema->resultset('User')->create({
username => $username,
password => $password,
});
$user->set_roles([{ role => $role }]);
print "User $username created with password '$password' and role $role\n";
}
else {
die "Invalid role $create_with_role. Must be root, admin or librarian";
}
}
else {
die "No such user $username!";
print "User not found and --create-with-role not passed!\n\n";
pod2usage;
exit 2;
}

if (@sites) {
my $rs = $schema->resultset('Site')->search({ id => \@sites });
if ($rs->count) {
foreach my $site ($rs->all) {
if ($site->users->find({ username => $user->username })) {
print "$username already in " . $site->id . "\n";
}
else {
$site->add_to_users($user);
print "Added $username to " . $site->id . "\n";
}
}
}
else {
die "No site found with id " . join(" ", @sites) . "\n";
}
}

0 comments on commit fd67436

Please sign in to comment.