Commit e5683623 authored by Richard Mansfield's avatar Richard Mansfield
Browse files
parents a19008c1 6460fa9a
......@@ -94,6 +94,9 @@ class ArtefactTypeProfile extends ArtefactType {
if (!empty($data['owner'])) {
if ($a = get_record('artefact', 'artefacttype', $type, 'owner', $data['owner'])) {
return parent::__construct($a->id, $a);
}
else {
$this->owner = $data['owner'];
}
}
$this->ctime = time();
......
......@@ -549,7 +549,6 @@ function auth_get_login_form() {
function get_login_form_js($form) {
$form = str_replace('/', '\/', str_replace("'", "\'", (str_replace(array("\n", "\t"), '', $form))));
$strcookiesnotenabled = get_string('cookiesnotenabled');
$strjavascriptnotenabled = get_string('javascriptnotenabled');
$cookiename = get_config('cookieprefix') . 'ctest';
return <<<EOF
<script type="text/javascript">
......@@ -557,14 +556,12 @@ var loginbox = $('loginbox');
document.cookie = '$cookiename=1;expires=0';
if (document.cookie) {
loginbox.innerHTML = '$form';
document.cookie = '$cookiename=1;expires=1/1/1990 00:00:00';
}
else {
appendChildNodes(loginbox, P(null, $strcookiesnotenabled));
replaceChildNodes(loginbox, P(null, '$strcookiesnotenabled'));
}
</script>
<noscript>
<p>$strjavascriptnotenabled</p>
</noscript>
EOF;
}
......@@ -580,14 +577,6 @@ function login_submit($values) {
log_debug('auth details supplied, attempting to log user in');
// Check if the cookie set to test cookies is actually set
if (!get_cookie('ctest')) {
set_cookie('ctest', '', time() - 3600);
log_debug('cookie for detecting cookies not set');
redirect(get_config('wwwroot'));
}
set_cookie('ctest', '', time() - 3600);
$username = $values['login_username'];
$password = $values['login_password'];
$institution = (isset($values['login_institution'])) ? $values['login_institution'] : 'mahara';
......
{include file="header.tpl"}
{if !$USER}
<div class="sidebar" id="loginbox"></div>
<div class="sidebar" id="loginbox">
<noscript><p>{str tag="javascriptnotenabled"}</p></noscript>
</div>
{$login_form}
{/if}
......
......@@ -2,7 +2,9 @@
<h2>{$loginmessage}</h2>
<div id="loginbox"></div>
<div id="loginbox">
<noscript><p>{str tag="javascriptnotenabled"}</p></noscript>
</div>
{$login_form}
{include file="footer.tpl"}
......@@ -2,14 +2,55 @@
#
# Bootstraps the mahara system, filling out all the relevant forms
#
use warnings;
# Author: Nigel McNie <nigel@catalyst.net.nz>
#
# TODO: Remove -u, just have the groups created for the randomly inserted users
#
use strict;
use warnings;
use FindBin;
use lib qq{$FindBin::Bin/lib/};
use Carp;
use Data::Dumper;
use Getopt::Declare;
use Mahara::Config;
use Mahara::RandomData;
use WWW::Mechanize;
my $args = Getopt::Declare->new(q(
[strict]
-c <config> The config file to use for the installation (defaults to 'config')
-nu <ucount> The number of random users to create (default 0)
{ reject $ucount !~ /^\d+$/; }
-ng <gcount> The number of random groups to create
{ reject $gcount !~ /^\d+$/; }
-u <user> User to create data for (required for type 'group')
-mc <configfile> What mahara config.php to use (defaults to ../htdocs/config.php)
-v Verbose (say what's going on at each step)
));
exit unless defined $args;
$args->{-nu} ||= 0;
$args->{-ng} ||= 0;
croak 'You must insert some users so groups can have members (specify with -nu)' if (!$args->{-nu} and $args->{-ng});
croak 'You must specify a user to create the groups for with -u' if (!defined $args->{-u} and $args->{-ng} > 0);
my $randomdata;
if ($args->{-nu} or $args->{-ng}) {
$args->{-mc} ||= qq{$FindBin::Bin/../htdocs/config.php};
my $config = Mahara::Config->new($args->{-mc});
$randomdata = Mahara::RandomData->new($config);
$randomdata->verbose($args->{-v});
}
# Read in configuration
my $configfile = shift || 'config';
my $CFG = do $configfile or die('could not read configuration file');
die('no URL set by your configuration file') unless ($CFG->{url});
my $configfile = $args->{-c} || 'config';
my $CFG = do $configfile or croak 'Could not read configuration file';
croak 'no URL set by your configuration file' unless ($CFG->{url});
$CFG->{url} .= '/' unless $CFG->{url} =~ m{/$};
# Go!
......@@ -17,7 +58,7 @@ my $m = WWW::Mechanize->new( autocheck => 1 );
# Check for a mahara install page, and run it if we find it
$m->get($CFG->{url});
die('This doesn\'t look like a mahara install page!') unless $m->content =~ m{admin/upgrade\.php">Agree</a>};
croak 'This doesn\'t look like a mahara install page!' unless $m->content =~ m{admin/upgrade\.php">Agree</a>};
# Agree to license
debug("Agreeing to license...");
......@@ -37,31 +78,29 @@ for my $thing (@things) {
debug("Installing core data...");
$m->get($CFG->{url} . 'admin/upgrade.json.php?install=1');
# Install done, move on to log in
debug("Going to login page...");
$m->get ($CFG->{url} . 'admin/');
# Log in
# Install done, now Log in
debug("Logging in...");
$m->submit_form(
form_name => 'login',
fields => {
login_username => 'root',
login_password => 'mahara',
}
$m->get($CFG->{url} . 'admin/');
$m->post($CFG->{url} . 'admin/',
{ login_username => 'root', login_password => 'mahara', 'form_login' => '' }
);
# Change password
debug("Changing root password...");
$m->submit_form(
form_name => 'change_password',
fields => {
password1 => $CFG->{password},
password2 => $CFG->{password}
}
fields => { password1 => $CFG->{password}, password2 => $CFG->{password} }
);
if ($m->content =~ /Your new password has been saved/) {
if ($args->{-nu}) {
debug('Inserting ' . $args->{-nu} . ' random users...');
$randomdata->insert_random_users($args->{-nu});
}
if ($args->{-ng}) {
debug('Inserting ' . $args->{-ng} . ' random groups...');
$randomdata->insert_random_groups($args->{-u}, $args->{-ng});
}
print "Done!\n";
}
else {
......
#
# Inserts random data into a mahara database, for testing purposes
#
# Author: Martyn Smith <martyn@catalyst.net.nz>
# Re-organised into a module by Nigel McNie <nigel@catalyst.net.nz>
#
package Mahara::RandomData;
use Carp;
use DBI;
use Data::RandomPerson;
use Data::Random::WordList;
#use Smart::Comments;
sub new {
my ($class,$config) = @_;
croak 'Need to spefify config object' unless defined $config;
my $self = {};
$self->{config} = $config;
$self->{verbose} = 0;
$self->{pretend} = 0;
my $connect_string = 'dbi:Pg:dbname=' . $config->get('dbname');
my $host = $config->get('host');
$connect_string .= 'host=' . $host if $host and $host =~ /\S/;
my $port = $config->get('port');
$connect_string .= 'port=' . $port if $port and $port =~ /\S/;
$self->{dbh} = DBI->connect($connect_string, $config->get('dbuser'), $config->get('dbpass'), { RaiseError => 1 });
bless $self, $class;
return $self;
}
sub verbose {
my ($self, $value) = @_;
$self->{verbose} = $value;
}
sub pretend {
my ($self, $value) = @_;
$self->{pretend} = $value;
}
sub insert_random_users {
my ($self, $count) = @_;
my $prefix = $self->{config}{dbprefix};
# get a list of existing usernames
my $existing_users = $self->{dbh}->selectall_hashref('SELECT id, username FROM ' . $prefix . 'usr', 'username');
$self->{dbh}->begin_work();
foreach ( 1 .. $count ) { ### [... ] (%)
my $userinfo = Data::RandomPerson->new()->create();
$userinfo->{username} = lc $userinfo->{firstname};
$userinfo->{email} = lc $userinfo->{firstname} . '.' . lc $userinfo->{lastname} . '@dollyfish.net.nz';
while ( exists $existing_users->{$userinfo->{username}} ) {
$userinfo->{username} =~ s{ (\d*) \z }{($1 or 0)+1}exms;
}
$existing_users->{$userinfo->{username}} = 1;
if ( $self->{verbose} or $self->{pretend} ) {
print 'INSERT INTO usr (username,password,institution,firstname,lastname,email) VALUES (';
print join(',',$userinfo->{username}, 'password', 'mahara', @{$userinfo}{qw(firstname lastname email)}), ")\n";
}
unless ( $self->{pretend} ) {
$self->{dbh}->do('INSERT INTO ' . $prefix . 'usr (username,password,institution,firstname,lastname,email) VALUES (?,?,?,?,?,?)', undef,
$userinfo->{username},
'password',
'mahara',
@{$userinfo}{qw(firstname lastname email)},
);
}
}
$self->{dbh}->commit();
}
sub insert_random_groups {
my ($self, $user, $count) = @_;
my $prefix = $self->{config}{dbprefix};
my $existing_users = $self->{dbh}->selectall_hashref('SELECT id, username FROM ' . $prefix . 'usr', 'username');
unless ( exists $existing_users->{$user} ) {
croak qq{User '$user' doesn't exist\n};
}
my $user_id = $existing_users->{$user}{id};
print qq{Adding groups for '$user' ($user_id)\n};
my $wl = new Data::Random::WordList( wordlist => '/usr/share/dict/words' );
$self->{dbh}->begin_work();
foreach ( 1 .. $count ) { ### [... ] (%)
my $groupname = join(' ', $wl->get_words(int(rand(5)) + 1));
my $groupdescription = join(' ', $wl->get_words(int(rand(50)) + 10));
if ( $self->{pretend} or $self->{verbose} ) {
print "Creating group '$groupname'\n";
print "INSERT INTO usr_group (name,owner,description,ctime,mtime) VALUES (?,?,?,?,?)\n";
my $members = {};
foreach (1..(int(rand(20)) + 5)) {
$members->{((keys %$existing_users)[int(rand(keys %$existing_users))])} = 1;
}
print "Members ... " . join(', ', keys %$members) . "\n";;
}
unless ( $self->{pretend} ) {
$self->{dbh}->do(
'INSERT INTO usr_group (name,owner,description,ctime,mtime) VALUES (?,?,?,current_timestamp,current_timestamp)',
undef,
$groupname,
$user_id,
$groupdescription,
);
my $members = {};
foreach (1..(int(rand(20)) + 5)) {
$members->{$existing_users->{((keys %$existing_users)[int(rand(keys %$existing_users))])}{id}} = 1;
}
foreach my $id (keys %$members) {
$self->{dbh}->do(
'INSERT INTO ' . $prefix . 'usr_group_member (grp,member,ctime) VALUES (currval(\'usr_group_id_seq\'),?,current_timestamp)',
undef,
$id,
);
}
}
}
$self->{dbh}->commit();
}
1;
......@@ -2,15 +2,15 @@
use strict;
use warnings;
use Carp;
use FindBin;
use lib qq{$FindBin::Bin/lib/};
use Carp;
use Data::Dumper;
use DBI;
use Data::RandomPerson;
use Data::Random::WordList;
use Getopt::Declare;
use Perl6::Slurp;
#use Smart::Comments;
use Mahara::Config;
use Mahara::RandomData;
my $args = Getopt::Declare->new(q(
[strict]
......@@ -28,118 +28,19 @@ exit unless defined $args;
$args->{-n} ||= 1;
$args->{-c} ||= qq{$FindBin::Bin/../htdocs/config.php};
my $config = read_config($args->{-c});
my $connect_string = qq{dbi:Pg:dbname=$config->{dbname}};
$connect_string .= qq{host=$config->{host}} if ( defined $config->{host} and $config->{host} =~ /\S/ );
$connect_string .= qq{port=$config->{port}} if ( defined $config->{port} and $config->{port} =~ /\S/ );
my $dbh = DBI->connect($connect_string, $config->{dbuser}, $config->{dbpass}, { RaiseError => 1 });
$config->{dbprefix} = '' unless defined $config->{dbprefix};
my $config = Mahara::Config->new($args->{-c});
my $randomdata = Mahara::RandomData->new($config);
$randomdata->verbose($args->{-v});
$randomdata->pretend($args->{-p});
if ( $args->{-t} eq 'user' ) {
# get a list of existing usernames
my $existing_users = $dbh->selectall_hashref('SELECT id, username FROM ' . $config->{dbprefix} . 'usr', 'username');
$dbh->begin_work();
foreach ( 1 .. $args->{-n} ) { ### [... ] (%)
my $userinfo = Data::RandomPerson->new()->create();
$userinfo->{username} = lc $userinfo->{firstname};
$userinfo->{email} = lc $userinfo->{firstname} . '.' . lc $userinfo->{lastname} . '@dollyfish.net.nz';
while ( exists $existing_users->{$userinfo->{username}} ) {
$userinfo->{username} =~ s{ (\d*) \z }{($1 or 0)+1}exms;
}
$existing_users->{$userinfo->{username}} = 1;
if ( $args->{-p} or $args->{-v} ) {
print 'INSERT INTO usr (username,password,institution,firstname,lastname,email) VALUES (';
print join(',',$userinfo->{username}, 'password', 'mahara', @{$userinfo}{qw(firstname lastname email)}), ")\n";
}
unless ( $args->{-p} ) {
$dbh->do('INSERT INTO ' . $config->{dbprefix} . 'usr (username,password,institution,firstname,lastname,email) VALUES (?,?,?,?,?,?)', undef,
$userinfo->{username},
'password',
'mahara',
@{$userinfo}{qw(firstname lastname email)},
);
}
}
$dbh->commit();
$randomdata->insert_random_users($args->{-n});
}
if ( $args->{-t} eq 'group' ) {
unless ( defined $args->{-u} ) {
croak 'Need to specify a user with -u';
}
my $existing_users = $dbh->selectall_hashref('SELECT id, username FROM ' . $config->{dbprefix} . 'usr', 'username');
unless ( exists $existing_users->{$args->{-u}} ) {
croak qq{User '$args->{-u}' doesn't exist\n};
}
my $user_id = $existing_users->{$args->{-u}}{id};
print qq{Adding groups for '$args->{-u}' ($user_id)\n};
my $wl = new Data::Random::WordList( wordlist => '/usr/share/dict/words' );
$dbh->begin_work();
foreach ( 1 .. $args->{-n} ) { ### [... ] (%)
my $groupname = join(' ', $wl->get_words(int(rand(5)) + 1));
my $groupdescription = join(' ', $wl->get_words(int(rand(50)) + 10));
if ( $args->{-p} or $args->{-v} ) {
print "Creating group '$groupname'\n";
print "INSERT INTO usr_group (name,owner,description,ctime,mtime) VALUES (?,?,?,?,?)\n";
my $members = {};
foreach (1..(int(rand(20)) + 5)) {
$members->{((keys %$existing_users)[int(rand(keys %$existing_users))])} = 1;
}
print "Members ... " . join(', ', keys %$members) . "\n";;
}
unless ( $args->{-p} ) {
$dbh->do(
'INSERT INTO usr_group (name,owner,description,ctime,mtime) VALUES (?,?,?,current_timestamp,current_timestamp)',
undef,
$groupname,
$user_id,
$groupdescription,
);
my $members = {};
foreach (1..(int(rand(20)) + 5)) {
$members->{$existing_users->{((keys %$existing_users)[int(rand(keys %$existing_users))])}{id}} = 1;
}
foreach my $id (keys %$members) {
$dbh->do(
q{INSERT INTO usr_group_member (grp,member,ctime) VALUES (currval('usr_group_id_seq'),?,current_timestamp)},
undef,
$id,
);
}
}
}
$dbh->commit();
$randomdata->insert_random_groups($args->{-u}, $args->{-n});
}
sub read_config {
my ($file) = @_;
my $config = {};
my $data = slurp($file);
while ( $data =~ m{ \$cfg-> ( [^=\s]+ ) \s* = \s* ([^;]+) }gxms ) {
my ($key, $value) = ($1, $2);
$value =~ s{ \A ' ( .*? ) ' \z }{$1}xms;
$config->{$key} = $value;
}
return $config;
}
Supports Markdown
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