diff --git a/test/functional/WorkerUnityCourseTest.php b/test/functional/WorkerUnityCourseTest.php index f0d522d25..fce752fa4 100644 --- a/test/functional/WorkerUnityCourseTest.php +++ b/test/functional/WorkerUnityCourseTest.php @@ -2,9 +2,11 @@ class WorkerUnityCourseTest extends UnityWebPortalTestCase { + private static string $course_id = "cs124"; + private static string $course_org = "org1_test"; private static string $course_owner_uid = "cs124_org1_test"; private static string $course_gid = "pi_cs124_org1_test"; - private static array $course_owner_name = ["cs124", "Fall 2025"]; + private static string $course_semester = "Fall 2025"; private static string $test1_manager_uid = "user2_org1_test"; private static array $test2_manager_uids = ["user2_org1_test", "user1_org1_test"]; private static string $test2_manager_uids_str = " user2_org1_test , user1_org1_test , ,,, user2_org1_test "; @@ -23,9 +25,9 @@ public function testCreateCourse() $this->assertFalse($pi_group_entry->exists()); $this->assertFalse($owner_user_entry->exists()); $stdin_file = writeLinesToTmpFile([ - self::$course_owner_name[0], - self::$course_owner_name[1], - self::$course_owner_uid, + self::$course_id, + self::$course_semester, + self::$course_org, self::$test1_manager_uid, ]); $stdin_file_path = getPathFromFileHandle($stdin_file); @@ -62,9 +64,9 @@ public function testCreateCourseMultipleManagers() $this->assertFalse($pi_group_entry->exists()); $this->assertFalse($owner_user_entry->exists()); $stdin_file = writeLinesToTmpFile([ - self::$course_owner_name[0], - self::$course_owner_name[1], - self::$course_owner_uid, + self::$course_id, + self::$course_semester, + self::$course_org, self::$test2_manager_uids_str, ]); $stdin_file_path = getPathFromFileHandle($stdin_file); diff --git a/workers/unity-course.php b/workers/unity-course.php index 19653d630..d2157310e 100755 --- a/workers/unity-course.php +++ b/workers/unity-course.php @@ -6,14 +6,6 @@ use UnityWebPortal\lib\UnityOrg; use UnityWebPortal\lib\UserFlag; -function cn2org($cn) -{ - $matches = []; - _preg_match("/.*_([^_]+_[^_]+)$/", $cn, $matches); - assert(count($matches) == 2, "failed to extract org from cn: '$cn'"); - return $matches[1]; -} - // if array is length 1 then replace it with its one element function flatten_attributes(array $attributes): array { @@ -32,17 +24,21 @@ function parse_comma_delimited_list(string $input): array } $givenName = trim(readline("Enter the course ID (example: CS123): ")); +if (!_preg_match("/^[a-zA-Z0-9_-]+$/", $givenName)) { + _die("error: course ID '$givenName' contains invalid characters", 1); +} $sn = trim(readline("Enter the year and semester of the course (example: Fall 2025): ")); -$cn = strtolower( - trim(readline("Please enter the cn to be used for the course (example: cs123_umass_edu): ")), -); +$org_gid = strtolower(trim(readline("Please enter the organization (example: umass_edu): "))); +if (!_preg_match("/^[a-z0-9_]+$/", $org_gid)) { + _die("error: organization '$org_gid' contains invalid characters", 1); +} +$cn = implode("_", [strtolower($givenName), $org_gid]); $manager_uids = parse_comma_delimited_list( readline("Enter the UID(s) of the group manager(s) (example: simonleary_umass_edu,bryank_uri_edu): ") ); if (count($manager_uids) === 0) { _die("at least one group manager UID is required", 1); } -$org_gid = cn2org($cn); $managers = []; foreach ($manager_uids as $manager_uid) {