Skip to content

Commit

Permalink
Remove legacy auth usage of $_SESSION (librenms#10491)
Browse files Browse the repository at this point in the history
* Remove auth use of $_SESSION

Will break plugins that depend on $_SESSION, Weathermap was already fixed.
Port them to use Auth::check()/Auth::user()/Auth:id()

* revert accidental replacement
  • Loading branch information
murrant authored Aug 5, 2019
1 parent f2163ed commit e99f421
Show file tree
Hide file tree
Showing 185 changed files with 342 additions and 781 deletions.
7 changes: 2 additions & 5 deletions LibreNMS/Alert/AlertUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
namespace LibreNMS\Alert;

use App\Models\Device;
use LibreNMS\Authentication\LegacyAuth;
use App\Models\User;
use LibreNMS\Config;
use PHPMailer\PHPMailer\PHPMailer;

Expand Down Expand Up @@ -85,7 +85,7 @@ public static function getContacts($results)
$email = Config::get('alert.default_mail', Config::get('alerts.email.default'));
return $email ? [$email => ''] : [];
}
$users = LegacyAuth::get()->getUserlist();
$users = User::query()->thisAuth()->get();
$contacts = array();
$uids = array();
foreach ($results as $result) {
Expand Down Expand Up @@ -126,9 +126,6 @@ public static function getContacts($results)
if (empty($user['realname'])) {
$user['realname'] = $user['username'];
}
if (empty($user['level'])) {
$user['level'] = LegacyAuth::get()->getUserlevel($user['username']);
}
if (Config::get('alert.globals') && ( $user['level'] >= 5 && $user['level'] < 10 )) {
$contacts[$user['email']] = $user['realname'];
} elseif (Config::get('alert.admins') && $user['level'] == 10) {
Expand Down
42 changes: 1 addition & 41 deletions LibreNMS/Authentication/LegacyAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,50 +74,10 @@ public static function reset()
return static::get();
}

public static function check()
{
self::checkInitSession();
return isset($_SESSION['authenticated']) && $_SESSION['authenticated'];
}

public static function user()
{
self::checkInitSession();
return new UserProxy();
}

public static function id()
{
self::checkInitSession();
return isset($_SESSION['user_id']) ? $_SESSION['user_id'] : 0;
}

protected static function checkInitSession()
{
if (!isset($_SESSION)) {
@session_start();
session_write_close();
}
}

public static function setUpLegacySession()
{
if (Auth::check()) {
$user = Auth::user();

if (!isset($_SESSION)) {
@session_start();
$_SESSION['username'] = $user->username;

// set up legacy variables, but don't override existing ones (ad anonymous bind can only get user_id at login)
if (!isset($_SESSION['userlevel'])) {
$_SESSION['userlevel'] = $user->level;
}

if (!isset($_SESSION['user_id'])) {
$_SESSION['user_id'] = $user->user_id;
}

$_SESSION['authenticated'] = true;
session_write_close();
}
}
Expand Down
72 changes: 0 additions & 72 deletions LibreNMS/Authentication/UserProxy.php

This file was deleted.

8 changes: 0 additions & 8 deletions app/Listeners/AuthEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use DB;
use Illuminate\Auth\Events\Login;
use Illuminate\Auth\Events\Logout;
use LibreNMS\Authentication\LegacyAuth;
use Request;
use Toastr;

Expand Down Expand Up @@ -36,9 +35,6 @@ public function login(Login $event)
DB::table('authlog')->insert(['user' => $user->username ?: '', 'address' => Request::ip(), 'result' => 'Logged In']);

Toastr::info('Welcome ' . ($user->realname ?: $user->username));

// Authenticated, set up legacy session stuff. TODO Remove once ajax and graphs are ported to Laravel.
LegacyAuth::setUpLegacySession();
}

/**
Expand All @@ -53,9 +49,5 @@ public function logout(Logout $event)
$user = $event->user ?: (object)['username' => 'Not found'];

DB::table('authlog')->insert(['user' => $user->username ?: '', 'address' => Request::ip(), 'result' => 'Logged Out']);

@session_start();
unset($_SESSION['authenticated']);
session_destroy();
}
}
4 changes: 1 addition & 3 deletions html/ajax_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@
* the source code distribution for details.
*/

use LibreNMS\Authentication\LegacyAuth;

$init_modules = array('web', 'auth', 'alerts');
require realpath(__DIR__ . '/..') . '/includes/init.php';

if (!LegacyAuth::check()) {
if (!Auth::check()) {
die('Unauthorized');
}

Expand Down
4 changes: 1 addition & 3 deletions html/ajax_list.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@
* the source code distribution for details.
*/

use LibreNMS\Authentication\LegacyAuth;

$init_modules = array('web', 'auth');
require realpath(__DIR__ . '/..') . '/includes/init.php';

if (!LegacyAuth::check()) {
if (!Auth::check()) {
die('Unauthorized');
}

Expand Down
4 changes: 1 addition & 3 deletions html/ajax_listports.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@
* @copyright (C) 2006 - 2012 Adam Armstrong
*/

use LibreNMS\Authentication\LegacyAuth;

$init_modules = array('web', 'auth');
require realpath(__DIR__ . '/..') . '/includes/init.php';

if (!LegacyAuth::check()) {
if (!Auth::check()) {
die('Unauthorized');
}

Expand Down
4 changes: 1 addition & 3 deletions html/ajax_ossuggest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

use LibreNMS\Authentication\LegacyAuth;

$init_modules = array('web', 'auth');
require realpath(__DIR__ . '/..') . '/includes/init.php';

if (!LegacyAuth::check()) {
if (!Auth::check()) {
die('Unauthorized');
}

Expand Down
4 changes: 1 addition & 3 deletions html/ajax_output.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
* the source code distribution for details.
*/

use LibreNMS\Authentication\LegacyAuth;

session_start();
if (isset($_SESSION['stage']) && $_SESSION['stage'] == 2) {
$init_modules = array('web', 'nodb');
Expand All @@ -22,7 +20,7 @@
$init_modules = array('web', 'auth', 'alerts');
require realpath(__DIR__ . '/..') . '/includes/init.php';

if (!LegacyAuth::check()) {
if (!Auth::check()) {
die('Unauthorized');
}
}
Expand Down
Loading

0 comments on commit e99f421

Please sign in to comment.