Merge pull request #9090 from nextcloud/fix/noid/ldap-format-strict-param

parameter provided to L10N::n() could have been a string
This commit is contained in:
Morris Jobke 2018-04-09 13:00:25 +02:00 committed by GitHub
commit 21dd28e6fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 26 additions and 17 deletions

View File

@ -89,11 +89,11 @@ class Wizard extends LDAPUtility {
* *
* @param string $filter the LDAP search filter * @param string $filter the LDAP search filter
* @param string $type a string being either 'users' or 'groups'; * @param string $type a string being either 'users' or 'groups';
* @return bool|int * @return int
* @throws \Exception * @throws \Exception
*/ */
public function countEntries($filter, $type) { public function countEntries(string $filter, string $type): int {
$reqs = array('ldapHost', 'ldapPort', 'ldapBase'); $reqs = ['ldapHost', 'ldapPort', 'ldapBase'];
if($type === 'users') { if($type === 'users') {
$reqs[] = 'ldapUserFilter'; $reqs[] = 'ldapUserFilter';
} }
@ -101,7 +101,7 @@ class Wizard extends LDAPUtility {
throw new \Exception('Requirements not met', 400); throw new \Exception('Requirements not met', 400);
} }
$attr = array('dn'); // default $attr = ['dn']; // default
$limit = 1001; $limit = 1001;
if($type === 'groups') { if($type === 'groups') {
$result = $this->access->countGroups($filter, $attr, $limit); $result = $this->access->countGroups($filter, $attr, $limit);
@ -113,22 +113,21 @@ class Wizard extends LDAPUtility {
throw new \Exception('Internal error: Invalid object type', 500); throw new \Exception('Internal error: Invalid object type', 500);
} }
return $result; return (int)$result;
} }
/** /**
* formats the return value of a count operation to the string to be * formats the return value of a count operation to the string to be
* inserted. * inserted.
* *
* @param bool|int $count * @param int $count
* @return int|string * @return string
*/ */
private function formatCountResult($count) { private function formatCountResult(int $count): string {
$formatted = ($count !== false) ? $count : 0; if($count > 1000) {
if($formatted > 1000) { return '> 1000';
$formatted = '> 1000';
} }
return $formatted; return (string)$count;
} }
public function countGroups() { public function countGroups() {
@ -141,7 +140,7 @@ class Wizard extends LDAPUtility {
} }
try { try {
$groupsTotal = $this->formatCountResult($this->countEntries($filter, 'groups')); $groupsTotal = $this->countEntries($filter, 'groups');
} catch (\Exception $e) { } catch (\Exception $e) {
//400 can be ignored, 500 is forwarded //400 can be ignored, 500 is forwarded
if($e->getCode() === 500) { if($e->getCode() === 500) {
@ -149,7 +148,12 @@ class Wizard extends LDAPUtility {
} }
return false; return false;
} }
$output = self::$l->n('%s group found', '%s groups found', $groupsTotal, array($groupsTotal)); $output = self::$l->n(
'%s group found',
'%s groups found',
$groupsTotal,
[$this->formatCountResult($groupsTotal)]
);
$this->result->addChange('ldap_group_count', $output); $this->result->addChange('ldap_group_count', $output);
return $this->result; return $this->result;
} }
@ -161,8 +165,13 @@ class Wizard extends LDAPUtility {
public function countUsers() { public function countUsers() {
$filter = $this->access->getFilterForUserCount(); $filter = $this->access->getFilterForUserCount();
$usersTotal = $this->formatCountResult($this->countEntries($filter, 'users')); $usersTotal = $this->countEntries($filter, 'users');
$output = self::$l->n('%s user found', '%s users found', $usersTotal, array($usersTotal)); $output = self::$l->n(
'%s user found',
'%s users found',
$usersTotal,
[$this->formatCountResult($usersTotal)]
);
$this->result->addChange('ldap_user_count', $output); $this->result->addChange('ldap_user_count', $output);
return $this->result; return $this->result;
} }
@ -175,7 +184,7 @@ class Wizard extends LDAPUtility {
*/ */
public function countInBaseDN() { public function countInBaseDN() {
// we don't need to provide a filter in this case // we don't need to provide a filter in this case
$total = $this->countEntries(null, 'objects'); $total = $this->countEntries('', 'objects');
if($total === false) { if($total === false) {
throw new \Exception('invalid results received'); throw new \Exception('invalid results received');
} }