usermanagement.php
来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· PHP 代码 · 共 763 行 · 第 1/3 页
PHP
763 行
$mobile_number = KTUtil::arrayGet($_REQUEST, 'mobile_number');
$max_sessions = $this->oValidator->validateInteger(
KTUtil::arrayGet($_REQUEST, 'max_sessions'),
KTUtil::meldOptions($aErrorOptions, array('message' => _kt("You must specify a numeric value for maximum sessions.")))
);
$password = KTUtil::arrayGet($_REQUEST, 'new_password');
$confirm_password = KTUtil::arrayGet($_REQUEST, 'confirm_password');
$KTConfig =& KTConfig::getSingleton();
$minLength = ((int) $KTConfig->get('user_prefs/passwordLength', 6));
$restrictAdmin = ((bool) $KTConfig->get('user_prefs/restrictAdminPasswords', false));
if ($restrictAdmin && (strlen($password) < $minLength)) {
$this->errorRedirectTo('addUser', sprintf(_kt("The password must be at least %d characters long."), $minLength), sprintf("old_search=%s&do_search=1", $old_search));
} else if (empty($password)) {
$this->errorRedirectTo('addUser', _kt("You must specify a password for the user."), sprintf("old_search=%s&do_search=1", $old_search));
} else if ($password !== $confirm_password) {
$this->errorRedirectTo('addUser', _kt("The passwords you specified do not match."), sprintf("old_search=%s&do_search=1", $old_search));
}
if(preg_match('/[\!\$\#\%\^\&\*]/', $username)){
$this->errorRedirectTo('addUser', _kt("You have entered an invalid character in your username."));
}
if(preg_match('/[\!\$\#\%\^\&\*]/', $name)){
$this->errorRedirectTo('addUser', _kt("You have entered an invalid character in your name."));
}
$dupUser =& User::getByUserName($username);
if(!PEAR::isError($dupUser)) {
$this->errorRedirectTo('addUser', _kt("A user with that username already exists"));
}
$oUser =& User::createFromArray(array(
"sUsername" => $username,
"sName" => $name,
"sPassword" => md5($password),
"iQuotaMax" => 0,
"iQuotaCurrent" => 0,
"sEmail" => $email_address,
"bEmailNotification" => $email_notifications,
"sMobile" => $mobile_number,
"bSmsNotification" => false, // FIXME do we auto-act if the user has a mobile?
"iMaxSessions" => $max_sessions,
));
if (PEAR::isError($oUser) || ($oUser == false)) {
$this->errorRedirectToMain(_kt("failed to create user."), sprintf("old_search=%s&do_search=1", $old_search));
exit(0);
}
$this->successRedirectToMain(_kt('Created new user') . ': ' . $oUser->getUsername(), 'name=' . $oUser->getUsername(), sprintf("old_search=%s&do_search=1", $old_search));
}
function do_deleteUser() {
$old_search = KTUtil::arrayGet($_REQUEST, 'old_search');
$user_id = KTUtil::arrayGet($_REQUEST, 'user_id');
$oUser = User::get($user_id);
if ((PEAR::isError($oUser)) || ($oUser === false)) {
$this->errorRedirectToMain(_kt('Please select a user first.'));
}
$res = $oUser->delete();
if (PEAR::isError($res)) {
$this->errorRedirectToMain(sprintf(_kt('Unable to delete user - the user may still be referred by documents.'), $res->getMessage()), sprintf("old_search=%s&do_search=1", $old_search));
}
$this->successRedirectToMain(_kt('User deleted') . ': ' . $oUser->getName(), sprintf("old_search=%s&do_search=1", $old_search));
}
function do_updateGroups() {
$old_search = KTUtil::arrayGet($_REQUEST, 'old_search');
$user_id = KTUtil::arrayGet($_REQUEST, 'user_id');
$oUser = User::get($user_id);
if ((PEAR::isError($oUser)) || ($oUser === false)) {
$this->errorRedirectToMain(_kt('Please select a user first.'), sprintf("old_search=%s&do_search=1", $old_search));
}
$groupAdded = KTUtil::arrayGet($_REQUEST, 'groups_items_added','');
$groupRemoved = KTUtil::arrayGet($_REQUEST, 'groups_items_removed','');
$aGroupToAddIDs = explode(",", $groupAdded);
$aGroupToRemoveIDs = explode(",", $groupRemoved);
// FIXME we need to ensure that only groups which are allocatable by the admin are added here.
// FIXME what groups are _allocatable_?
$this->startTransaction();
$groupsAdded = array();
$groupsRemoved = array();
$addWarnings = array();
$removeWarnings = array();
foreach ($aGroupToAddIDs as $iGroupID ) {
if ($iGroupID > 0) {
$oGroup = Group::get($iGroupID);
$memberReason = GroupUtil::getMembershipReason($oUser, $oGroup);
//var_dump($memberReason);
if (!(PEAR::isError($memberReason) || is_null($memberReason))) {
$addWarnings[] = $memberReason;
}
$res = $oGroup->addMember($oUser);
if (PEAR::isError($res) || $res == false) {
$this->errorRedirectToMain(sprintf(_kt('Unable to add user to group "%s"'), $oGroup->getName()), sprintf("old_search=%s&do_search=1", $old_search));
} else {
$groupsAdded[] = $oGroup->getName();
}
}
}
// Remove groups
foreach ($aGroupToRemoveIDs as $iGroupID ) {
if ($iGroupID > 0) {
$oGroup = Group::get($iGroupID);
$res = $oGroup->removeMember($oUser);
if (PEAR::isError($res) || $res == false) {
$this->errorRedirectToMain(sprintf(_kt('Unable to remove user from group "%s"'), $oGroup->getName()), sprintf("old_search=%s&do_search=1", $old_search));
} else {
$groupsRemoved[] = $oGroup->getName();
$memberReason = GroupUtil::getMembershipReason($oUser, $oGroup);
//var_dump($memberReason);
if (!(PEAR::isError($memberReason) || is_null($memberReason))) {
$removeWarnings[] = $memberReason;
}
}
}
}
if (!empty($addWarnings)) {
$sWarnStr = _kt('Warning: the user was already a member of some subgroups') . ' — ';
$sWarnStr .= implode(', ', $addWarnings);
$_SESSION['KTInfoMessage'][] = $sWarnStr;
}
if (!empty($removeWarnings)) {
$sWarnStr = _kt('Warning: the user is still a member of some subgroups') . ' — ';
$sWarnStr .= implode(', ', $removeWarnings);
$_SESSION['KTInfoMessage'][] = $sWarnStr;
}
$msg = '';
if (!empty($groupsAdded)) { $msg .= ' ' . _kt('Added to groups') . ': ' . implode(', ', $groupsAdded) . '.'; }
if (!empty($groupsRemoved)) { $msg .= ' ' . _kt('Removed from groups') . ': ' . implode(', ',$groupsRemoved) . '.'; }
if (!Permission::userIsSystemAdministrator($_SESSION['userID'])) {
$this->rollbackTransaction();
$this->errorRedirectTo('editgroups', _kt('For security purposes, you cannot remove your own administration priviledges.'), sprintf('user_id=%d&do_search=1&old_search=%s', $oUser->getId(), $old_search));
exit(0);
}
$this->commitTransaction();
$this->successRedirectToMain($msg, sprintf("old_search=%s&do_search=1", $old_search));
}
function getGroupStringForUser($oUser) {
$aGroupNames = array();
$aGroups = GroupUtil::listGroupsForUser($oUser);
$MAX_GROUPS = 6;
$add_elipsis = false;
if (count($aGroups) == 0) { return _kt('User is currently not a member of any groups.'); }
if (count($aGroups) > $MAX_GROUPS) {
$aGroups = array_slice($aGroups, 0, $MAX_GROUPS);
$add_elipsis = true;
}
foreach ($aGroups as $oGroup) {
$aGroupNames[] = $oGroup->getName();
}
if ($add_elipsis) {
$aGroupNames[] = '…';
}
return implode(', ', $aGroupNames);
}
// change enabled / disabled status of users
function do_change_enabled() {
$this->startTransaction();
$iLicenses = 0;
$bRequireLicenses = false;
if (KTPluginUtil::pluginIsActive('ktdms.wintools')) {
require_once(KT_DIR . '/plugins/wintools/baobabkeyutil.inc.php');
$iLicenses = BaobabKeyUtil::getLicenseCount();
$bRequireLicenses = true;
}
// admin and anonymous are automatically ignored here.
$iEnabledUsers = User::getNumberEnabledUsers();
if($_REQUEST['update_value'] == 'enable')
{
foreach(KTUtil::arrayGet($_REQUEST, 'edit_user', array()) as $sUserId => $v) {
// check that we haven't hit max user limit
if($bRequireLicenses && $iEnabledUsers >= $iLicenses) {
// if so, add to error messages, but commit transaction (break this loop)
$_SESSION['KTErrorMessage'][] = _kt('You may only have ') . $iLicenses . _kt(' users enabled at one time.');
break;
}
// else enable user
$oUser = User::get((int)$sUserId);
if(PEAR::isError($oUser)) { $this->errorRedirectToMain(_kt('Error getting user object')); }
$oUser->enable();
$res = $oUser->update();
if(PEAR::isError($res)) { $this->errorRedirectToMain(_kt('Error updating user')); }
$iEnabledUsers++;
}
}
if($_REQUEST['update_value'] == 'disable')
{
//echo 'got into disable';
//exit;
foreach(KTUtil::arrayGet($_REQUEST, 'edit_user', array()) as $sUserId => $v) {
$oUser = User::get((int)$sUserId);
if(PEAR::isError($oUser)) { $this->errorRedirectToMain(_kt('Error getting user object')); }
$oUser->disable();
$res = $oUser->update();
if(PEAR::isError($res)) { $this->errorRedirectToMain(_kt('Error updating user')); }
$iEnabledUsers--;
}
}
if($_REQUEST['update_value'] == 'delete')
{
//echo 'Delete called';
foreach(KTUtil::arrayGet($_REQUEST, 'edit_user', array()) as $sUserId => $v) {
$oUser = User::get((int)$sUserId);
if(PEAR::isError($oUser)) { $this->errorRedirectToMain(_kt('Error getting user object')); }
$oUser->delete();
$res = $oUser->update();
if(PEAR::isError($res)) { $this->errorRedirectToMain(_kt('Error updating user')); }
$iEnabledUsers--;
}
}
$this->commitTransaction();
$this->successRedirectToMain(_kt('Users updated'));
}
}
?>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?