Commit c28ecc47 authored by Michael Felczak's avatar Michael Felczak
Browse files

PKPPS: Add tools/unenroll.php

parent 34d8cd2b
<?php
/**
* @file tools/unenroll.php
*
* Copyright (c) 2003-2017 John Willinsky
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* @class unEnrollTool
* @ingroup tools
*
* @brief CLI tool for unEnrolling users in a specified journal by a specified role
*/
require(dirname(__FILE__) . '/bootstrap.inc.php');
import('lib.pkp.classes.db.DBResultRange');
import('classes.journal.JournalDAO');
import('classes.security.RoleDAO');
import('classes.user.UserDAO');
class unEnrollTool extends CommandLineTool {
//@var $scriptName string
var $scriptName;
//@var $journalPath string
var $journalPath;
//@var $userInfoPath string
var $userInfoPath;
//@var $roleId string
var $roleId;
/**
* Constructor
* @param $argv inputted command line arguments
*/
function unEnrollTool($argv = array()){
parent::CommandLineTool($argv);
$this->scriptName = $argv[0];
if (!isset($argv[1]) || !isset($argv[2]) || !isset($argv[3])){
$this->usage();
exit(0);
}
else if (!($this->argumentVerification($argv))){
exit(1);
}
else {
$this->journalPath = $argv[1];
$this->userInfoPath = $argv[2];
$this->roleId= $argv[3];
}
}
/**
* Print command usage information
*/
function usage(){
echo "OJS role removal tool\n"
."Remove a role for specific journal users\n\n"
."Usage: {$this->scriptName} [journal path] [file path] [role]\n"
."journal path: Selected journal\n"
."file path: Full path to user input file; one username/email per line\n"
."role: The role from which specified users should be unenrolled\n";
}
/**
* Verification method to see if inputted argumnets are valid
* @param $argv string, inputted command line arguments
* @return boolean, returns true if arguments are valid, and false if otherwise
*/
function argumentVerification($argv = array()){
$roleConstants = array('ROLE_ID_SITE_ADMIN','ROLE_ID_JOURNAL_MANAGER','ROLE_ID_EDITOR',
'ROLE_ID_SECTION_EDITOR','ROLE_ID_LAYOUT_EDITOR','ROLE_ID_REVIEWER',
'ROLE_ID_COPYEDITOR','ROLE_ID_PROOFREADER','ROLE_ID_AUTHOR', 'ROLE_ID_READER',
'ROLE_ID_SUBSCRIPTION_MANAGER');
$journalDAO =& DAORegistry::getDAO('JournalDAO');
if (!($journalDAO->journalExistsByPath($argv[1]))){
echo 'Error: Invalid path or journal does not exist'."\n";
return false;
}
else if (!(file_exists($argv[2]))){
echo 'Error: Invalid user file path, file does not exist'."\n";
return false;
}
else if (!(in_array($argv[3], $roleConstants))){
echo 'Invalid role, see classes/security/Role.inc.php for role constants'."\n";
return false;
}
else {
return true;
}
}
/**
* Execute remove roles command
*/
function execute(){
$usersInfo = file($this->userInfoPath, FILE_IGNORE_NEW_LINES);
foreach($usersInfo as $userInfo){
$this->roleChange($userInfo);
}
}
/**
* This method queries the database in search of the email address or username
* Once found it will reduce that individual by whatever the fixed role is
* @param $userInfoKey string that is either the users' email address or username which will be used to get their id
*/
function roleChange($userInfoKey){
$journalDAO =& DAORegistry::getDAO('JournalDAO');
$journalId = $journalDAO->getJournalByPath($this->journalPath)->getId();
$userDAO =& DAORegistry::getDAO('UserDAO');
$roleDAO =& DAORegistry::getDAO('RoleDAO');
$dbResultRange = new DBResultRange(1,0);
$key = (strstr($userInfoKey, '@'))? array(USER_FIELD_EMAIL, 'email') : array(USER_FIELD_USERNAME, 'username');
$users =& $userDAO->getJournalUsersByField($key[0],'is', $userInfoKey,true, $journalId, $dbResultRange)->toArray();
foreach($users as $user){
if ($roleDAO->userHasRole($journalId, $user->getId(),constant($this->roleId))
&& $roleDAO->deleteRoleByUserId($user->getId(), $journalId, constant($this->roleId))){
echo "User with ".$key[1]." ".$userInfoKey." has had role ". $this->roleId . " removed"."\n";
return;
} else {
echo "Error (Invalid role): User with ".$key[1]." ".$userInfoKey." does not have role ". $this->roleId. "\n";
return;
}
}
echo "Error (Invalid User): User with userInfoKey ".$userInfoKey." is not a valid userkey\n";
return;
}
}
echo "Script is running...\n";
$tool = new unEnrollTool(isset($argv) ? $argv : array());
$tool->execute();
echo "Script complete...". "\n";
?>
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