Skip to content

Commit

Permalink
Handle unusual OUCU formats - 'csv-import' and 'index' PHP [iet:8839587]
Browse files Browse the repository at this point in the history
* [iet:8772481]
  • Loading branch information
nfreear committed Apr 27, 2017
1 parent 7c1747e commit 92b0c84
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
4 changes: 4 additions & 0 deletions bin/csv-import.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
cli_write('.');
});


cli_writeln("\nWarnings:");
print_r(OuUser::getWarnings());

cli_writeln("\nUsers inserted: $count");


Expand Down
23 changes: 16 additions & 7 deletions db/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ class User
const CSV_TEAM = 4;

const PREFIX = 'ouop_';
const UNDEF_INSTRUMENT = 'kd';
const UNDEF_INSTRUMENT = 'tpt'; //'kd';
const INSTRUMENT_REGEX = '@^(kd|tpt)@';

const OUCU_REGEX = '@^[a-z]{2,4}\d{1,7}$@';
const OPENID_URL_REGEX = '@^http:\/\/openid\.open\.ac\.uk\/oucu\/(?P<oucu>\w+)$@';
const USERNAME_REGEX = '@^httpopenidopenacukoucu(?P<oucu>\w+)$@';
const USERNAME_REPLACE = '@^(httpopenidopenacukoucu)?@';

protected static $warnings = [];

/** Get plugin DB record for given username.
* @return object
*/
Expand Down Expand Up @@ -103,9 +105,9 @@ public static function insertFromCsv($filename = '../example.csv', $ignore_headi
}

$user_record = (object) [
'oucu' => self::validateOucu($row[ self::CSV_OUCU ]), # 0,
'oucu' => self::validateOucu($row[ self::CSV_OUCU ], $count), # 0,
'course_presentation' => $row[ self::CSV_OUCU + 1 ], # 1,
'teslainstrument' => self::validateInstrument(self::row($row, self::CSV_OUCU + 2)),
'teslainstrument' => self::validateInstrument(self::row($row, self::CSV_OUCU + 2), $count, $row[ self::CSV_OUCU ]),
'notes' => self::row($row, self::CSV_OUCU + 3), # 3,
'is_team' => self::row($row, self::CSV_TEAM), # 4,
'firstname' => self::row($row, self::CSV_TEAM + 1), # 5,
Expand All @@ -125,18 +127,20 @@ public static function insertFromCsv($filename = '../example.csv', $ignore_headi
return $count;
}

protected static function validateOucu($oucu)
protected static function validateOucu($oucu, $row)
{
if (!preg_match(self::OUCU_REGEX, $oucu)) {
throw new Exception('Unexpected OUCU format: ' . $oucu);
self::$warnings[] = [ 'row' => $row, 'msg' => 'Unexpected OUCU', 'oucu' => $oucu ];
echo 'W';
//throw new Exception('Unexpected OUCU format: ' . $oucu);
}
return $oucu;
}

protected static function validateInstrument($instrument)
protected static function validateInstrument($instrument, $row, $ref)
{
if (!preg_match(self::INSTRUMENT_REGEX, $instrument)) {
throw new Exception('Unexpected TeSLA instrument code: '. $instrument);
throw new Exception(sprintf('Unexpected TeSLA instrument code, %d, %s: "%s"', $row, $ref, $instrument));
}
return $instrument;
}
Expand Down Expand Up @@ -236,6 +240,11 @@ public static function getRoles()
return (object) [ 'is_admin' => is_siteadmin(), 'roles' => $roles, 'is_loggedin' => isloggedin() ];
}

public static function getWarnings()
{
return self::$warnings;
}

// ====================================================================

/** Get URL relating to the TeSLA instrument assigned to the user (in the DB) [ MOVE ] ?
Expand Down
5 changes: 3 additions & 2 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
class Ou_Open_Id_Form {

const ACTION = '/login/index.php';
const OUCU_REGEX = '[a-z]{2,4}\d{1,7}';
const OUCU_REGEX = '[a-z]{1,6}\d{1,7}'; // Was: '[a-z]{2,4}\d{1,7}'.
const OUCU_MIN = 2; // Was: 3.
const OPEN_ID_URL = 'http://openid.open.ac.uk/oucu/';
const JQUERY_URL = 'https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js';

Expand Down Expand Up @@ -59,7 +60,7 @@ public static function printOucu() {
?>
<input
id="oucu" value="<?php Ou_Open_Id_Form::printOucu() ?>"
required="required" aria-required="true" pattern="[a-z]{2,4}\d{1,7}" minlength="3" maxlength="9"
required="required" aria-required="true" pattern="<?php echo Ou_Open_Id_Form::OUCU_REGEX ?>" minlength="2" maxlength="9"
title="<?php print_string( 'login_field_help', OUOP_STRING ) ?>" /></label>

<input type="hidden" id="openid_base_url" value="<?php echo Ou_Open_Id_Form::OPEN_ID_URL ?>" />
Expand Down

0 comments on commit 92b0c84

Please sign in to comment.