* Use true/false to return booleans

* Misc fixes
This commit is contained in:
Jellyfrog
2021-04-01 17:35:18 +02:00
committed by GitHub
parent 6616727a95
commit 777b78cca1
16 changed files with 58 additions and 59 deletions

View File

@@ -11,8 +11,8 @@ class ADAuthorizationAuthorizer extends MysqlAuthorizer
use LdapSessionCache;
use ActiveDirectoryCommon;
protected static $AUTH_IS_EXTERNAL = 1;
protected static $CAN_UPDATE_PASSWORDS = 0;
protected static $AUTH_IS_EXTERNAL = true;
protected static $CAN_UPDATE_PASSWORDS = false;
protected $ldap_connection;
@@ -68,7 +68,7 @@ class ADAuthorizationAuthorizer extends MysqlAuthorizer
public function userExists($username, $throw_exception = false)
{
if ($this->authLdapSessionCacheGet('user_exists')) {
return 1;
return true;
}
$search = ldap_search(
@@ -86,10 +86,10 @@ class ADAuthorizationAuthorizer extends MysqlAuthorizer
*/
$this->authLdapSessionCacheSet('user_exists', 1);
return 1;
return true;
}
return 0;
return false;
}
public function getUserlevel($username)

View File

@@ -14,7 +14,7 @@ class ActiveDirectoryAuthorizer extends AuthorizerBase
{
use ActiveDirectoryCommon;
protected static $CAN_UPDATE_PASSWORDS = 0;
protected static $CAN_UPDATE_PASSWORDS = false;
protected $ldap_connection;
protected $is_bound = false; // this variable tracks if bind has been called so we don't call it multiple times

View File

@@ -28,10 +28,10 @@ use LibreNMS\Interfaces\Authentication\Authorizer;
abstract class AuthorizerBase implements Authorizer
{
protected static $HAS_AUTH_USERMANAGEMENT = 0;
protected static $CAN_UPDATE_USER = 0;
protected static $CAN_UPDATE_PASSWORDS = 0;
protected static $AUTH_IS_EXTERNAL = 0;
protected static $HAS_AUTH_USERMANAGEMENT = false;
protected static $CAN_UPDATE_USER = false;
protected static $CAN_UPDATE_PASSWORDS = false;
protected static $AUTH_IS_EXTERNAL = false;
public function canUpdatePasswords($username = '')
{
@@ -41,7 +41,7 @@ abstract class AuthorizerBase implements Authorizer
public function changePassword($username, $newpassword)
{
//not supported by default
return 0;
return false;
}
public function canManageUsers()
@@ -52,13 +52,13 @@ abstract class AuthorizerBase implements Authorizer
public function addUser($username, $password, $level = 0, $email = '', $realname = '', $can_modify_passwd = 0, $description = '')
{
//not supported by default
return 0;
return false;
}
public function deleteUser($user_id)
{
//not supported by default
return 0;
return false;
}
public function canUpdateUsers()
@@ -69,7 +69,7 @@ abstract class AuthorizerBase implements Authorizer
public function updateUser($user_id, $realname, $level, $can_modify_passwd, $email)
{
//not supported by default
return 0;
return false;
}
public function authIsExternal()

View File

@@ -7,10 +7,10 @@ use LibreNMS\Exceptions\AuthenticationException;
class HttpAuthAuthorizer extends MysqlAuthorizer
{
protected static $HAS_AUTH_USERMANAGEMENT = 1;
protected static $CAN_UPDATE_USER = 1;
protected static $CAN_UPDATE_PASSWORDS = 0;
protected static $AUTH_IS_EXTERNAL = 1;
protected static $HAS_AUTH_USERMANAGEMENT = true;
protected static $CAN_UPDATE_USER = true;
protected static $CAN_UPDATE_PASSWORDS = false;
protected static $AUTH_IS_EXTERNAL = true;
public function authenticate($credentials)
{

View File

@@ -33,7 +33,7 @@ class LdapAuthorizationAuthorizer extends AuthorizerBase
use LdapSessionCache;
protected $ldap_connection;
protected static $AUTH_IS_EXTERNAL = 1;
protected static $AUTH_IS_EXTERNAL = true;
public function __construct()
{
@@ -77,7 +77,7 @@ class LdapAuthorizationAuthorizer extends AuthorizerBase
public function userExists($username, $throw_exception = false)
{
if ($this->authLdapSessionCacheGet('user_exists')) {
return 1;
return true;
}
$filter = '(' . Config::get('auth_ldap_prefix') . $username . ')';
@@ -90,7 +90,7 @@ class LdapAuthorizationAuthorizer extends AuthorizerBase
*/
$this->authLdapSessionCacheSet('user_exists', 1);
return 1;
return true;
}
/*
@@ -98,7 +98,7 @@ class LdapAuthorizationAuthorizer extends AuthorizerBase
* on some end and the user will be happy if it "just works" after the user
* has been added to LDAP.
*/
return 0;
return false;
}
public function getUserlevel($username)
@@ -209,7 +209,7 @@ class LdapAuthorizationAuthorizer extends AuthorizerBase
}
}
return 0;
return false;
}
protected function getMembername($username)

View File

@@ -76,7 +76,7 @@ class LdapAuthorizer extends AuthorizerBase
$search = ldap_search($connection, trim(Config::get('auth_ldap_suffix'), ','), $filter);
$entries = ldap_get_entries($connection, $search);
if ($entries['count']) {
return 1;
return true;
}
} catch (AuthenticationException $e) {
if ($throw_exception) {
@@ -92,7 +92,7 @@ class LdapAuthorizer extends AuthorizerBase
}
}
return 0;
return false;
}
public function getUserlevel($username)
@@ -234,7 +234,7 @@ class LdapAuthorizer extends AuthorizerBase
return $user;
}
return 0;
return false;
}
protected function getMembername($username)

View File

@@ -9,9 +9,9 @@ use LibreNMS\Exceptions\AuthenticationException;
class MysqlAuthorizer extends AuthorizerBase
{
protected static $HAS_AUTH_USERMANAGEMENT = 1;
protected static $CAN_UPDATE_USER = 1;
protected static $CAN_UPDATE_PASSWORDS = 1;
protected static $HAS_AUTH_USERMANAGEMENT = true;
protected static $CAN_UPDATE_USER = true;
protected static $CAN_UPDATE_PASSWORDS = true;
public function authenticate($credentials)
{
@@ -45,9 +45,9 @@ class MysqlAuthorizer extends AuthorizerBase
*/
if (! static::$CAN_UPDATE_PASSWORDS) {
return 0;
return false;
} elseif (empty($username) || ! $this->userExists($username)) {
return 1;
return true;
} else {
return User::thisAuth()->where('username', $username)->value('can_modify_passwd');
}
@@ -57,7 +57,7 @@ class MysqlAuthorizer extends AuthorizerBase
{
// check if updating passwords is allowed (mostly for classes that extend this)
if (! static::$CAN_UPDATE_PASSWORDS) {
return 0;
return false;
}
/** @var User $user */
@@ -129,7 +129,7 @@ class MysqlAuthorizer extends AuthorizerBase
Eloquent::DB()->table('ports_perms')->where('user_id', $user_id)->delete();
Eloquent::DB()->table('users_prefs')->where('user_id', $user_id)->delete();
return User::destroy($user_id);
return (bool) User::destroy($user_id);
}
public function getUserlist()
@@ -144,7 +144,7 @@ class MysqlAuthorizer extends AuthorizerBase
return $user->toArray();
}
return null;
return false;
}
public function updateUser($user_id, $realname, $level, $can_modify_passwd, $email)
@@ -156,6 +156,6 @@ class MysqlAuthorizer extends AuthorizerBase
$user->can_modify_passwd = (int) $can_modify_passwd;
$user->email = $email;
$user->save();
return $user->save();
}
}

View File

@@ -8,9 +8,9 @@ use LibreNMS\Exceptions\AuthenticationException;
class RadiusAuthorizer extends MysqlAuthorizer
{
protected static $HAS_AUTH_USERMANAGEMENT = 1;
protected static $CAN_UPDATE_USER = 1;
protected static $CAN_UPDATE_PASSWORDS = 0;
protected static $HAS_AUTH_USERMANAGEMENT = true;
protected static $CAN_UPDATE_USER = true;
protected static $CAN_UPDATE_PASSWORDS = false;
/** @var Radius */
protected $radius;

View File

@@ -34,10 +34,10 @@ use LibreNMS\Util\IP;
*/
class SSOAuthorizer extends MysqlAuthorizer
{
protected static $HAS_AUTH_USERMANAGEMENT = 1;
protected static $CAN_UPDATE_USER = 1;
protected static $CAN_UPDATE_PASSWORDS = 0;
protected static $AUTH_IS_EXTERNAL = 1;
protected static $HAS_AUTH_USERMANAGEMENT = true;
protected static $CAN_UPDATE_USER = true;
protected static $CAN_UPDATE_PASSWORDS = false;
protected static $AUTH_IS_EXTERNAL = true;
public function authenticate($credentials)
{

View File

@@ -54,7 +54,7 @@ interface Authorizer
* can_modify_passwd
*
* @param int $user_id
* @return array|bool
* @return array|false
*/
public function getUser($user_id);

View File

@@ -73,7 +73,7 @@ class Airos extends OS implements
$location = parent::fetchLocation();
// fix longitude having an extra - in the middle after the decimal point
$location->lng = preg_replace('/(-?\d+)\.-?(\d+)/', '$1.$2', $location->getAttributes()['lng']);
$location->lng = (float) preg_replace('/(-?\d+)\.-?(\d+)/', '$1.$2', $location->getAttributes()['lng']);
return $location;
}

View File

@@ -43,7 +43,7 @@ class Zxdsl extends \LibreNMS\OS
$variant = ' ';
$parts = explode('.', trim($oid, '.'));
foreach ($parts as $part) {
$variant .= chr(64 + $part);
$variant .= chr(64 + (int) $part);
}
return trim($variant);

View File

@@ -39,7 +39,6 @@ class MigrateToUtf8mb4 extends Migration
// Get the list of all tables
$tableNames = DB::table('information_schema.tables')
->where('table_schema', $databaseName)
->get(['TABLE_NAME'])
->pluck('TABLE_NAME');
// Iterate through the list and alter each table

View File

@@ -55,11 +55,11 @@ class AuthHTTPTest extends TestCase
{
$a = LegacyAuth::reset();
$this->assertTrue($a->canUpdatePasswords() === 0);
$this->assertTrue($a->changePassword(null, null) === 0);
$this->assertTrue($a->canManageUsers() === 1);
$this->assertTrue($a->canUpdateUsers() === 1);
$this->assertTrue($a->authIsExternal() === 1);
$this->assertFalse($a->canUpdatePasswords());
$this->assertFalse($a->changePassword(null, null));
$this->assertTrue($a->canManageUsers());
$this->assertTrue($a->canUpdateUsers());
$this->assertTrue($a->authIsExternal());
}
public function testOldBehaviourAgainstCurrent()

View File

@@ -118,7 +118,7 @@ class AuthSSOTest extends DBTestCase
// Retrieve it and validate
$dbuser = $a->getUser($a->getUserid($user));
$this->assertNull($dbuser);
$this->assertFalse($dbuser);
}
// Excercise general auth flow with creation enabled
@@ -225,11 +225,11 @@ class AuthSSOTest extends DBTestCase
{
$a = LegacyAuth::reset();
$this->assertTrue($a->canUpdatePasswords() === 0);
$this->assertTrue($a->changePassword(null, null) === 0);
$this->assertTrue($a->canManageUsers() === 1);
$this->assertTrue($a->canUpdateUsers() === 1);
$this->assertTrue($a->authIsExternal() === 1);
$this->assertFalse($a->canUpdatePasswords());
$this->assertFalse($a->changePassword(null, null));
$this->assertTrue($a->canManageUsers());
$this->assertTrue($a->canUpdateUsers());
$this->assertTrue($a->authIsExternal());
}
/* Everything from here comprises of targeted tests to excercise single methods */

View File

@@ -24,10 +24,10 @@
namespace LibreNMS\Tests;
use app\Console\Commands\SmokepingGenerateCommand;
use App\Console\Commands\SmokepingGenerateCommand;
use App\Models\Device;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\arr;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
class SmokepingCliTest extends DBTestCase