Commit 8cbaa798 authored by Nigel McNie's avatar Nigel McNie
Browse files

Removed maharactl from the public repo. It might be back some day, but in a...

Removed maharactl from the public repo. It might be back some day, but in a different form as a separate download.
parent 4ea5b4d2
#
# Handles available actions for maharactl
#
# Author: Nigel McNie <nigel@catalyst.net.nz>
#
package Mahara::Action;
use strict;
use warnings;
use Carp;
use DBI;
use Data::RandomPerson;
use Data::Random::WordList;
#use Smart::Comments;
use Data::Dumper;
use IO::Prompt;
sub new {
my ($class,$config) = @_;
croak 'Need to specify config object' unless defined $config;
my $self = {};
$self->{config} = $config;
my $connect_string = 'dbi:Pg:dbname=' . $config->get('dbname');
my $host = $config->get('dbhost');
$connect_string .= ';host=' . $host if $host and $host =~ /\S/;
my $port = $config->get('dbport');
$connect_string .= ';port=' . $port if $port and $port =~ /\S/;
$self->{dbh} = DBI->connect($connect_string, $config->get('dbuser'), $config->get('dbpass'), { RaiseError => 1, PrintError => 0 });
$self->{dbh}->{HandleError} = sub { confess(shift) };
bless $self, $class;
return $self;
}
sub add_user {
my ($self) = @_;
my ($username, $password, $institution, $firstname, $lastname, $studentid, $preferredname, $email);
my $prefix = $self->{config}->get('dbprefix');
print "Creating a new user\n";
# IOprompt's interface is poos...
$username = prompt("Username: ", -while => qr{^[a-zA-Z0-9]+$}) until $username;
$password = prompt("Password: ", -echo => '*');
$institution = prompt("Institution: ", -while => qr{^[a-z]+$}, -default => 'mahara') until $institution;
$email = prompt("E-mail: ") until $email;
$firstname = prompt("First Name: ") until $firstname;
$preferredname = prompt("Preferred Name: ", -default => $firstname);
$lastname = prompt("Last Name: ") until $lastname;
$studentid = prompt("Student ID: ");
print "Creating user '$username'\n";
$self->{dbh}->begin_work;
$self->{dbh}->do('INSERT INTO ' . $prefix . 'usr
(username,password,institution,passwordchange,firstname,lastname,studentid,preferredname,email,quota)
VALUES (?,?,?,?,?,?,?,?,?,
(SELECT value::bigint FROM ' . $prefix . 'artefact_config WHERE plugin = \'file\' AND field = \'defaultquota\')
)',
undef,
$username,
$password,
$institution,
1,
$firstname,
$lastname,
$studentid,
$preferredname,
$email
);
# First name, last name and e-mail address are all required
$self->{dbh}->do('INSERT INTO ' . $prefix . 'artefact
(artefacttype,owner,ctime,mtime,atime,title)
VALUES (?,currval(\'' . $prefix . 'usr_id_seq\'),current_timestamp,current_timestamp,current_timestamp,?)', undef,
'firstname',
$firstname
);
$self->{dbh}->do('INSERT INTO ' . $prefix . 'artefact
(artefacttype,owner,ctime,mtime,atime,title)
VALUES (?,currval(\'' . $prefix . 'usr_id_seq\'),current_timestamp,current_timestamp,current_timestamp,?)', undef,
'lastname',
$lastname
);
$self->{dbh}->do('INSERT INTO ' . $prefix . 'artefact
(artefacttype,owner,ctime,mtime,atime,title)
VALUES (?,currval(\'' . $prefix . 'usr_id_seq\'),current_timestamp,current_timestamp,current_timestamp,?)', undef,
'email',
$email
);
# Insert record for primary e-mail address
$self->{dbh}->do('INSERT INTO ' . $prefix . 'artefact_internal_profile_email
(owner, email, verified, principal, artefact)
VALUES (currval(\'' . $prefix . 'usr_id_seq\'), ?, 1, 1, currval(\'' . $prefix . 'artefact_id_seq\'))', undef,
$email,
);
# These fields are optional
$self->{dbh}->do('INSERT INTO ' . $prefix . 'artefact
(artefacttype,owner,ctime,mtime,atime,title)
VALUES (?,currval(\'' . $prefix . 'usr_id_seq\'),current_timestamp,current_timestamp,current_timestamp,?)', undef,
'studentid',
$studentid
) if $studentid;
$self->{dbh}->do('INSERT INTO ' . $prefix . 'artefact
(artefacttype,owner,ctime,mtime,atime,title)
VALUES (?,currval(\'' . $prefix . 'usr_id_seq\'),current_timestamp,current_timestamp,current_timestamp,?)', undef,
'preferredname',
$preferredname
) if $preferredname;
$self->{dbh}->commit;
}
1;
package Mahara::Config;
use Perl6::Slurp;
use Carp;
sub new {
my ($class,$config_file) = @_;
croak 'Need to spefify config file' unless defined $config_file;
my $self = {};
$self->{config_file} = $config_file;
my $data = slurp($self->{config_file});
while ( $data =~ m{ ^ \s+ \$cfg-> ( [^=\s]+ ) \s* = \s* ([^;]+) }gxms ) {
my ($key, $value) = ($1, $2);
$value =~ s{ \A ' ( .*? ) ' \z }{$1}xms;
$self->{config}{$key} = $value;
}
bless $self, $class;
return $self;
}
sub get {
my ($self, $key) = @_;
return $self->{config}{$key};
}
1;
This diff is collapsed.
#!/usr/bin/env perl
use warnings;
use strict;
use FindBin;
use lib qq{$FindBin::Bin/lib/};
use Carp;
$Carp::Verbose = 1;
use Data::Dumper;
use Getopt::Declare;
use Mahara::Config;
use Mahara::Action;
my $actions = [qw(adduser)];
my $args = Getopt::Declare->new(q(
[strict]
<action> Action to perform ) . '(' . join(', ', @{$actions}) . ')' . q( [required]
-c <configfile> What config.php to use (defaults to ../htdocs/config.php)
-p Pretend (just print what _would_ happen)
-v Verbose (like pretend but print _and_ insert)
));
exit unless defined $args;
$args->{-c} ||= qq{$FindBin::Bin/../htdocs/config.php};
my $config = Mahara::Config->new($args->{-c});
unless ( grep { $args->{'<action>'} eq $_ } @{$actions} ) {
croak q{Action must be one of } . join(', ', map { qq{'$_'} } @{$actions} );
}
my $action = Mahara::Action->new($config);
my $what = $args->{'<action>'};
if ($what eq 'adduser') {
$action->add_user();
}
#BEGIN;
#INSERT INTO usr
# (username, password, institution, passwordchange, firstname, lastname, studentid, preferredname, email, quota)
# VALUES
# ('terryn', 'itpnz2007', 'mahara', 1, 'Neal', 'Terry', NULL, 'Neal', 'terryn@itpnz.ac.nz', 62914560);
#INSERT INTO artefact
# (artefacttype, owner, ctime, mtime, atime, title)
# VALUES
# ('firstname', (SELECT id FROM usr WHERE username = 'terryn'), CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 'Neal');
#INSERT INTO artefact
# (artefacttype, owner, ctime, mtime, atime, title)
# VALUES
# ('lastname', (SELECT id FROM usr WHERE username = 'terryn'), CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 'Terry');
#INSERT INTO artefact
# (artefacttype, owner, ctime, mtime, atime, title)
# VALUES
# ('email', (SELECT id FROM usr WHERE username = 'terryn'), CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 'terryn@itpnz.ac.nz');
#INSERT INTO artefact
# (artefacttype, owner, ctime, mtime, atime, title)
# VALUES
# ('preferredname', (SELECT id FROM usr WHERE username = 'terryn'), CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 'Neal');
#INSERT INTO artefact_internal_profile_email
# (owner, email, verified, principal, artefact)
# VALUES
# ((SELECT id FROM usr WHERE username = 'terryn'), 'terryn@itpnz.ac.nz', 1, 1, (SELECT id FROM artefact WHERE owner = (SELECT id FROM usr WHERE username = 'terryn') AND title = 'terryn@itpnz.ac.nz'));
#COMMIT;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment