optimize logic

这个提交包含在:
WaitSpring 2024-04-17 17:50:10 +08:00
父节点 cda91d6d83
当前提交 92187a26ac
共有 1 个文件被更改,包括 79 次插入115 次删除

查看文件

@ -66,12 +66,12 @@ class PermissionsHook implements
$rnrsverifytime = $this->userOptionsLookup->getOption($user, 'rnrsverifytime');
// Mode 1: user ID + verify time + salt
$rnrsverified_mode1 = (
$rnrsHashVerified = (
$rnrsverifyhash === hash('sha3-256', $localuserid . $rnrsverifytime . hash('sha3-256', $rnrssalt)) ||
$rnrsverifyhash === hash('sha3-256', $centralid . $rnrsverifytime . hash('sha3-256', $rnrssalt))
);
if ($rnrsverified_mode1) {
if ($rnrsHashVerified) {
return true;
} else {
return false;
@ -83,12 +83,12 @@ class PermissionsHook implements
*
* @return bool
*/
private function checkUserRights(UserIdentity $user)
private function checkConfirmedGroup(UserIdentity $user)
{
// Mode2: confirmed by verification
$rnrsverified_mode2 = in_array($this->config->get('RNRSConfirmedGroup'), $this->userGroupManager->getUserGroups($user));
$rnrsInConfirmedGroup = in_array($this->config->get('RNRSConfirmedGroup'), $this->userGroupManager->getUserGroups($user));
if ($rnrsverified_mode2) {
if ($rnrsInConfirmedGroup) {
return true;
} else {
return false;
@ -100,16 +100,36 @@ class PermissionsHook implements
*
* @return bool
*/
private function checkUserGroup(UserIdentity $user)
private function checkExemptedGroup(UserIdentity $user)
{
// Mode3: in group(s) that exempted from verification
$rnrsverified_mode3 = (
$rnrsInExemptGroup = (
in_array($this->config->get('RNRSExemptGroup'), $this->userGroupManager->getUserGroups($user)) ||
in_array('rnrsverify-exempt', $this->permissionManager->getUserPermissions($user)) ||
in_array('bot', $this->permissionManager->getUserPermissions($user))
);
if ($rnrsverified_mode3) {
if ($rnrsInExemptGroup) {
return true;
} else {
return false;
}
}
/**
* @param UserIdentity $user
*
* @return bool
*/
private function checkOtherExemptedGroups(UserIdentity $user)
{
// Mode4: in group(s) that exempted from verification
$rnrsInExemptGroup = (
in_array('rnrsverify-exempt', $this->permissionManager->getUserPermissions($user)) ||
in_array('bot', $this->permissionManager->getUserPermissions($user))
) && !(in_array($this->config->get('RNRSExemptGroup'), $this->userGroupManager->getUserGroups($user)));
if ($rnrsInExemptGroup) {
return true;
} else {
return false;
@ -155,7 +175,7 @@ class PermissionsHook implements
// // Add Remove Log
// $oldGroups = $this->userGroupManager->getUserGroups($user); // previous groups
// $newGroups = array_intersect($oldGroups, [$removed_group]); // new groups
// if ($oldGroups !== $newGroups) {
// If ($oldGroups !== $newGroups) {
// $logEntry = new ManualLogEntry('rights', 'autopromote');
// $logEntry->setPerformer($user);
// $logEntry->setTarget($user->getUserPage());
@ -178,88 +198,67 @@ class PermissionsHook implements
*/
public function onGetUserPermissionsErrors($title, $user, $action, &$result)
{
$rnrsverified_mode1 = $this->checkUserVerifyHash($user);
$rnrsverified_mode2 = $this->checkUserRights($user);
$rnrsverified_mode3 = $this->checkUserGroup($user);
$rnrsverified_enabled_confirmed_group = in_array(
$rnrsEnabledConfirmedGroup = in_array(
$this->config->get('RNRSConfirmedGroup'),
array_merge(
$this->userGroupManager->listAllImplicitGroups(),
$this->userGroupManager->listAllGroups()
)
);
$rnrsverified_enabled_exempt_group = in_array(
$rnrsEnabledExemptGroup = in_array(
$this->config->get('RNRSExemptGroup'),
array_merge(
$this->userGroupManager->listAllImplicitGroups(),
$this->userGroupManager->listAllGroups()
)
);
$rnrsHashVerified = $this->checkUserVerifyHash($user);
$rnrsInConfirmedGroup = $this->checkConfirmedGroup($user);
$rnrsInExemptGroup = $this->checkExemptedGroup($user);
$rnrsInOtherExemptedGroups = $this->checkOtherExemptedGroups($user);
// Adjust user groups
if ($rnrsHashVerified) {
// If hash verified, and not in the confirmed group, add to confirmed group
if ($rnrsEnabledConfirmedGroup && !$rnrsInConfirmedGroup) {
$this->addGroup($user, $this->config->get('RNRSConfirmedGroup'));
$rnrsInConfirmedGroup = true;
}
} else if ($rnrsInExemptGroup || $rnrsInOtherExemptedGroups) {
if ($rnrsEnabledConfirmedGroup && !$rnrsInConfirmedGroup) {
$this->removeGroup($user, $this->config->get('RNRSConfirmedGroup'));
$rnrsInConfirmedGroup = false;
}
}
if (
!$this->config->has('RNRSExclusiveRights') ||
!$this->config->has('RNRSSalt')
) {
// Case 1: not enabled
// return true
return true;
} else if (!$rnrsverified_mode1 && $rnrsverified_mode2 && $rnrsverified_mode3) {
// case 0: not verified but confirmed and exempted (bugfix)
// remove from confirmed group
if (
$rnrsverified_enabled_confirmed_group &&
$rnrsverified_enabled_exempt_group &&
in_array($this->config->get('RNRSConfirmedGroup'), $this->userGroupManager->getUserGroups($user)) &&
in_array($this->config->get('RNRSExemptGroup'), $this->userGroupManager->getUserGroups($user))
) {
$this->removeGroup($user, $this->config->get('RNRSConfirmedGroup'));
}
} else if ($rnrsverified_mode1 && $rnrsverified_mode2 && $rnrsverified_mode3) {
// case 1: verified and exempted (bugfix)
// remove from exempted group
if (
$rnrsverified_enabled_exempt_group &&
in_array($this->config->get('RNRSExemptGroup'), $this->userGroupManager->getUserGroups($user))
) {
$this->removeGroup($user, $this->config->get('RNRSExemptGroup'));
}
// add to confirmed group
if (
$rnrsverified_enabled_confirmed_group &&
!in_array($this->config->get('RNRSConfirmedGroup'), $this->userGroupManager->getUserGroups($user))
) {
$this->addGroup($user, $this->config->get('RNRSConfirmedGroup'));
}
} else if ($rnrsInConfirmedGroup) {
// Case 2: in confirmed group
return true;
} else if (($rnrsverified_mode1 || $rnrsverified_mode2) && !$rnrsverified_mode3) {
// case 2: verified but not exempted
if (
$rnrsverified_enabled_confirmed_group &&
!in_array($this->config->get('RNRSConfirmedGroup'), $this->userGroupManager->getUserGroups($user))
) {
// add to confirmed group
$this->addGroup($user, $this->config->get('RNRSConfirmedGroup'));
}
} else if ($rnrsHashVerified) {
// Case 3: hash verified
return true;
} else if ($rnrsInExemptGroup) {
// Case 4: in exempted groups
return true;
} else if ($rnrsInOtherExemptedGroups) {
// Case 5: with exempt permissions
return true;
} else if (
!($rnrsverified_mode1 || $rnrsverified_mode2) && $rnrsverified_mode3
) {
// case 3: exempted but not verified
if (
$rnrsverified_enabled_confirmed_group &&
in_array($this->config->get('RNRSConfirmedGroup'), $this->userGroupManager->getUserGroups($user))
) {
// remove from confirmed group
$this->removeGroup($user, $this->config->get('RNRSConfirmedGroup'));
}
return true;
} else if (
!($rnrsverified_mode1 || $rnrsverified_mode2) && !$rnrsverified_mode3 &&
!$rnrsHashVerified && !$rnrsInConfirmedGroup && !$rnrsInExemptGroup && !$rnrsInOtherExemptedGroups &&
in_array($action, $this->config->get('RNRSExclusiveRights'))
) {
// case 4: not exempted and not verified
// Case 0: not verified, not in confirmed group, not in exempted groups, actions in restrict list
// return false
$result = 'rnrshook-action-restricted';
return false;
};
}
}
/**
@ -271,71 +270,36 @@ class PermissionsHook implements
*/
public function onSpecialContributionsBeforeMainOutput($userId, $user, $special)
{
$rnrsverified_mode1 = $this->checkUserVerifyHash($user);
$rnrsverified_mode2 = $this->checkUserRights($user);
$rnrsverified_mode3 = $this->checkUserGroup($user);
$rnrsverified_enabled_confirmed_group = in_array(
$rnrsEnabledConfirmedGroup = in_array(
$this->config->get('RNRSConfirmedGroup'),
array_merge(
$this->userGroupManager->listAllImplicitGroups(),
$this->userGroupManager->listAllGroups()
)
);
$rnrsverified_enabled_exempt_group = in_array(
$rnrsEnabledExemptGroup = in_array(
$this->config->get('RNRSExemptGroup'),
array_merge(
$this->userGroupManager->listAllImplicitGroups(),
$this->userGroupManager->listAllGroups()
)
);
$rnrsHashVerified = $this->checkUserVerifyHash($user);
$rnrsInConfirmedGroup = $this->checkConfirmedGroup($user);
$rnrsInExemptGroup = $this->checkExemptedGroup($user);
$rnrsInOtherExemptedGroups = $this->checkOtherExemptedGroups($user);
if (!$rnrsverified_mode1 && $rnrsverified_mode2 && $rnrsverified_mode3) {
// case 0: not verified but confirmed and exempted (bugfix)
// remove from confirmed group
if (
$rnrsverified_enabled_confirmed_group &&
$rnrsverified_enabled_exempt_group &&
in_array($this->config->get('RNRSConfirmedGroup'), $this->userGroupManager->getUserGroups($user)) &&
in_array($this->config->get('RNRSExemptGroup'), $this->userGroupManager->getUserGroups($user))
) {
$this->removeGroup($user, $this->config->get('RNRSConfirmedGroup'));
}
} else if ($rnrsverified_mode1 && $rnrsverified_mode2 && $rnrsverified_mode3) {
// case 1: verified and exempted (bugfix)
// remove from exempted group
if (
$rnrsverified_enabled_exempt_group &&
in_array($this->config->get('RNRSExemptGroup'), $this->userGroupManager->getUserGroups($user))
) {
$this->removeGroup($user, $this->config->get('RNRSExemptGroup'));
}
// add to confirmed group
if (
$rnrsverified_enabled_confirmed_group &&
!in_array($this->config->get('RNRSConfirmedGroup'), $this->userGroupManager->getUserGroups($user))
) {
// Adjust user groups
if ($rnrsHashVerified) {
// If hash verified, and not in the confirmed group, add to confirmed group
if ($rnrsEnabledConfirmedGroup && !$rnrsInConfirmedGroup) {
$this->addGroup($user, $this->config->get('RNRSConfirmedGroup'));
$rnrsInConfirmedGroup = true;
}
} else if (($rnrsverified_mode1 || $rnrsverified_mode2) && !$rnrsverified_mode3) {
// case 2: verified but not exempted
if (
$rnrsverified_enabled_confirmed_group &&
!in_array($this->config->get('RNRSConfirmedGroup'), $this->userGroupManager->getUserGroups($user))
) {
// add to confirmed group
$this->addGroup($user, $this->config->get('RNRSConfirmedGroup'));
}
} else if (
!($rnrsverified_mode1 || $rnrsverified_mode2) && $rnrsverified_mode3
) {
// case 3: exempted but not verified
if (
$rnrsverified_enabled_confirmed_group &&
in_array($this->config->get('RNRSConfirmedGroup'), $this->userGroupManager->getUserGroups($user))
) {
// remove from confirmed group
} else if ($rnrsInExemptGroup || $rnrsInOtherExemptedGroups) {
if ($rnrsEnabledConfirmedGroup && !$rnrsInConfirmedGroup) {
$this->removeGroup($user, $this->config->get('RNRSConfirmedGroup'));
$rnrsInConfirmedGroup = false;
}
}