2015-08-17 12:18:07 +00:00
|
|
|
#! /usr/bin/env perl
|
2013-11-06 12:28:45 +00:00
|
|
|
|
|
|
|
use strict;
|
|
|
|
use Hydra::Schema;
|
|
|
|
use Hydra::Helper::Nix;
|
|
|
|
use Hydra::Model::DB;
|
|
|
|
use Getopt::Long qw(:config gnu_getopt);
|
|
|
|
use Digest::SHA1 qw(sha1_hex);
|
|
|
|
|
|
|
|
sub showHelp {
|
|
|
|
print <<EOF;
|
|
|
|
Usage: $0 NAME
|
|
|
|
[--rename-from NAME]
|
2016-10-20 12:14:04 +00:00
|
|
|
[--type hydra|google]
|
2013-11-06 12:28:45 +00:00
|
|
|
[--full-name FULLNAME]
|
|
|
|
[--email-address EMAIL-ADDRESS]
|
|
|
|
[--password PASSWORD]
|
2017-10-08 10:55:51 +00:00
|
|
|
[--password-hash SHA1-HASH]
|
2013-11-06 12:28:45 +00:00
|
|
|
[--wipe-roles]
|
|
|
|
[--role ROLE]...
|
|
|
|
|
|
|
|
Create a new Hydra user account, or update or an existing one. The
|
|
|
|
--role flag can be given multiple times. If the account already
|
|
|
|
exists, roles are added to the existing roles unless --wipe-roles is
|
|
|
|
specified. If --rename-from is given, the specified account is
|
|
|
|
renamed.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
\$ hydra-create-user alice --password foobar --role admin
|
|
|
|
EOF
|
|
|
|
exit 0;
|
|
|
|
}
|
|
|
|
|
2017-10-08 10:55:51 +00:00
|
|
|
my ($renameFrom, $type, $fullName, $emailAddress, $password, $passwordHash);
|
2013-11-06 12:28:45 +00:00
|
|
|
my $wipeRoles = 0;
|
|
|
|
my @roles;
|
|
|
|
|
|
|
|
GetOptions("rename-from=s" => \$renameFrom,
|
|
|
|
"type=s" => \$type,
|
|
|
|
"full-name=s" => \$fullName,
|
|
|
|
"email-address=s" => \$emailAddress,
|
|
|
|
"password=s" => \$password,
|
2017-10-08 10:55:51 +00:00
|
|
|
"password-hash=s" => \$passwordHash,
|
2013-11-06 12:28:45 +00:00
|
|
|
"wipe-roles" => \$wipeRoles,
|
|
|
|
"role=s" => \@roles,
|
|
|
|
"help" => sub { showHelp() }
|
|
|
|
) or exit 1;
|
|
|
|
|
|
|
|
die "$0: one user name required\n" if scalar @ARGV != 1;
|
|
|
|
my $userName = $ARGV[0];
|
|
|
|
|
2016-10-20 12:14:04 +00:00
|
|
|
die "$0: type must be `hydra' or `google'\n"
|
|
|
|
if defined $type && $type ne "hydra" && $type ne "google";
|
2013-11-06 12:28:45 +00:00
|
|
|
|
|
|
|
my $db = Hydra::Model::DB->new();
|
|
|
|
|
2020-04-10 16:13:36 +00:00
|
|
|
$db->txn_do(sub {
|
2013-11-06 12:28:45 +00:00
|
|
|
my $user = $db->resultset('Users')->find({ username => $renameFrom // $userName });
|
|
|
|
if ($renameFrom) {
|
|
|
|
die "$0: user `$renameFrom' does not exist\n" unless $user;
|
|
|
|
$user->update({ username => $userName });
|
|
|
|
} elsif ($user) {
|
|
|
|
print STDERR "updating existing user `$userName'\n";
|
|
|
|
} else {
|
|
|
|
print STDERR "creating new user `$userName'\n";
|
|
|
|
$user = $db->resultset('Users')->create(
|
|
|
|
{ username => $userName, type => "hydra", emailaddress => "", password => "!" });
|
|
|
|
}
|
|
|
|
|
2016-10-20 12:14:04 +00:00
|
|
|
die "$0: Google user names must be email addresses\n"
|
|
|
|
if $user->type eq "google" && $userName !~ /\@/;
|
2013-11-06 12:28:45 +00:00
|
|
|
|
|
|
|
$user->update({ type => $type }) if defined $type;
|
|
|
|
|
|
|
|
$user->update({ fullname => $fullName eq "" ? undef : $fullName }) if defined $fullName;
|
|
|
|
|
2016-10-20 12:14:04 +00:00
|
|
|
if ($user->type eq "google") {
|
|
|
|
die "$0: Google accounts do not have an explicitly set email address.\n"
|
2013-11-06 12:28:45 +00:00
|
|
|
if defined $emailAddress;
|
2016-10-20 12:14:04 +00:00
|
|
|
die "$0: Google accounts do not have a password.\n"
|
2013-11-06 12:28:45 +00:00
|
|
|
if defined $password;
|
2017-10-08 10:55:51 +00:00
|
|
|
die "$0: Google accounts do not have a password.\n"
|
|
|
|
if defined $passwordHash;
|
2013-11-06 12:28:45 +00:00
|
|
|
$user->update({ emailaddress => $userName, password => "!" });
|
|
|
|
} else {
|
|
|
|
$user->update({ emailaddress => $emailAddress }) if defined $emailAddress;
|
2017-10-08 10:55:51 +00:00
|
|
|
if (defined $password && !(defined $passwordHash)) {
|
|
|
|
$passwordHash = sha1_hex($password);
|
|
|
|
}
|
|
|
|
$user->update({ password => $passwordHash }) if defined $passwordHash;
|
2013-11-06 12:28:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$user->userroles->delete if $wipeRoles;
|
|
|
|
$user->userroles->update_or_create({ role => $_ }) foreach @roles;
|
|
|
|
});
|