2009-09-07 11:07:59 +00:00
< ? php
2008-03-09 22:49:53 +00:00
2019-12-30 12:11:26 +01:00
use App\Models\DeviceGroup ;
2018-10-29 21:41:50 +01:00
use App\Models\User ;
2017-11-18 11:33:03 +01:00
2015-07-13 20:10:26 +02:00
$no_refresh = true ;
2015-03-21 21:30:55 +00:00
2019-04-11 23:26:42 -05:00
require 'includes/html/javascript-interfacepicker.inc.php' ;
2010-06-21 04:18:06 +00:00
2015-07-13 20:10:26 +02:00
echo " <div style='margin: 10px;'> " ;
2008-03-09 22:49:53 +00:00
2015-07-13 20:10:26 +02:00
$pagetitle [] = 'Edit user' ;
2011-10-18 14:41:19 +00:00
2018-10-29 21:41:50 +01:00
if ( ! Auth :: user () -> hasGlobalAdmin ()) {
2019-04-11 23:26:42 -05:00
include 'includes/html/error-no-perm.inc.php' ;
2016-08-18 20:28:22 -05:00
} else {
2020-09-21 15:40:17 +02:00
if ( $vars [ 'user_id' ] && ! $vars [ 'edit' ]) {
2018-10-29 21:41:50 +01:00
/** @var User $user */
$user = User :: find ( $vars [ 'user_id' ]);
$user_data = $user -> toArray (); // for compatibility with current code
2020-09-21 15:59:34 +02:00
echo '<p><h2>' . $user_data [ 'realname' ] . '</h2></p>' ;
2015-07-13 20:10:26 +02:00
// Perform actions if requested
if ( $vars [ 'action' ] == 'deldevperm' ) {
2020-09-21 15:40:17 +02:00
if ( dbFetchCell ( 'SELECT COUNT(*) FROM devices_perms WHERE `device_id` = ? AND `user_id` = ?' , [ $vars [ 'device_id' ], $user_data [ 'user_id' ]])) {
dbDelete ( 'devices_perms' , '`device_id` = ? AND `user_id` = ?' , [ $vars [ 'device_id' ], $user_data [ 'user_id' ]]);
2015-07-13 20:10:26 +02:00
}
}
2011-03-17 00:09:20 +00:00
2015-07-13 20:10:26 +02:00
if ( $vars [ 'action' ] == 'adddevperm' ) {
2020-09-21 15:40:17 +02:00
if ( ! dbFetchCell ( 'SELECT COUNT(*) FROM devices_perms WHERE `device_id` = ? AND `user_id` = ?' , [ $vars [ 'device_id' ], $user_data [ 'user_id' ]])) {
dbInsert ([ 'device_id' => $vars [ 'device_id' ], 'user_id' => $user_data [ 'user_id' ]], 'devices_perms' );
2015-07-13 20:10:26 +02:00
}
}
2008-03-09 22:49:53 +00:00
2019-12-30 12:11:26 +01:00
if ( $vars [ 'action' ] == 'deldevgroupperm' ) {
$user -> deviceGroups () -> detach ( $vars [ 'device_group_id' ]);
}
if ( $vars [ 'action' ] == 'adddevgroupperm' ) {
$user -> deviceGroups () -> syncWithoutDetaching ( $vars [ 'device_group_id' ]);
}
2015-07-13 20:10:26 +02:00
if ( $vars [ 'action' ] == 'delifperm' ) {
2020-09-21 15:40:17 +02:00
if ( dbFetchCell ( 'SELECT COUNT(*) FROM ports_perms WHERE `port_id` = ? AND `user_id` = ?' , [ $vars [ 'port_id' ], $user_data [ 'user_id' ]])) {
dbDelete ( 'ports_perms' , '`port_id` = ? AND `user_id` = ?' , [ $vars [ 'port_id' ], $user_data [ 'user_id' ]]);
2015-07-13 20:10:26 +02:00
}
}
2008-03-09 22:49:53 +00:00
2015-07-13 20:10:26 +02:00
if ( $vars [ 'action' ] == 'addifperm' ) {
2020-09-21 15:40:17 +02:00
if ( ! dbFetchCell ( 'SELECT COUNT(*) FROM ports_perms WHERE `port_id` = ? AND `user_id` = ?' , [ $vars [ 'port_id' ], $user_data [ 'user_id' ]])) {
dbInsert ([ 'port_id' => $vars [ 'port_id' ], 'user_id' => $user_data [ 'user_id' ]], 'ports_perms' );
2015-07-13 20:10:26 +02:00
}
}
2008-03-09 22:49:53 +00:00
2015-07-13 20:10:26 +02:00
if ( $vars [ 'action' ] == 'delbillperm' ) {
2020-09-21 15:40:17 +02:00
if ( dbFetchCell ( 'SELECT COUNT(*) FROM bill_perms WHERE `bill_id` = ? AND `user_id` = ?' , [ $vars [ 'bill_id' ], $user_data [ 'user_id' ]])) {
dbDelete ( 'bill_perms' , '`bill_id` = ? AND `user_id` = ?' , [ $vars [ 'bill_id' ], $user_data [ 'user_id' ]]);
2015-07-13 20:10:26 +02:00
}
}
2008-03-09 22:49:53 +00:00
2015-07-13 20:10:26 +02:00
if ( $vars [ 'action' ] == 'addbillperm' ) {
2020-09-21 15:40:17 +02:00
if ( ! dbFetchCell ( 'SELECT COUNT(*) FROM bill_perms WHERE `bill_id` = ? AND `user_id` = ?' , [ $vars [ 'bill_id' ], $user_data [ 'user_id' ]])) {
dbInsert ([ 'bill_id' => $vars [ 'bill_id' ], 'user_id' => $user_data [ 'user_id' ]], 'bill_perms' );
2015-07-13 20:10:26 +02:00
}
}
echo '<div class="row">
<div class="col-md-4">' ;
// Display devices this users has access to
echo '<h3>Device Access</h3>' ;
echo " <div class='panel panel-default panel-condensed'>
2014-03-10 23:50:16 +00:00
<table class='table table-hover table-condensed table-striped'>
<tr>
<th>Device</th>
<th>Action</th>
2015-07-13 20:10:26 +02:00
</tr> " ;
2008-03-09 22:49:53 +00:00
2020-09-21 15:40:17 +02:00
$device_perms = dbFetchRows ( 'SELECT * from devices_perms as P, devices as D WHERE `user_id` = ? AND D.device_id = P.device_id' , [ $user_data [ 'user_id' ]]);
2015-07-13 20:10:26 +02:00
foreach ( $device_perms as $device_perm ) {
2020-09-21 15:40:17 +02:00
echo '<tr><td><strong>' . format_hostname ( $device_perm ) . " </td><td> <a href='edituser/action=deldevperm/user_id= " . $vars [ 'user_id' ] . '/device_id=' . $device_perm [ 'device_id' ] . " '><i class='fa fa-trash fa-lg icon-theme' aria-hidden='true'></i></a></strong></td></tr> " ;
2015-07-13 20:10:26 +02:00
$access_list [] = $device_perm [ 'device_id' ];
2020-09-21 15:40:17 +02:00
$permdone = 'yes' ;
2015-07-13 20:10:26 +02:00
}
2008-03-09 22:49:53 +00:00
2015-07-13 20:10:26 +02:00
echo '</table>
</div>' ;
2014-03-10 23:50:16 +00:00
2020-09-21 15:40:17 +02:00
if ( ! $permdone ) {
2015-07-13 20:10:26 +02:00
echo 'None Configured' ;
}
2011-03-17 00:09:20 +00:00
2015-07-13 20:10:26 +02:00
// Display devices this user doesn't have access to
echo '<h4>Grant access to new device</h4>' ;
echo " <form class='form-inline' role='form' method='post' action=''>
2019-07-17 07:20:26 -05:00
" . csrf_field () . "
2020-09-21 15:40:17 +02:00
<input type='hidden' value=' " . $user_data [ 'user_id' ] . " ' name='user_id'>
2011-03-17 00:09:20 +00:00
<input type='hidden' value='edituser' name='page'>
<input type='hidden' value='adddevperm' name='action'>
2014-03-10 23:50:16 +00:00
<div class='form-group'>
<label class='sr-only' for='device_id'>Device</label>
2015-07-13 20:10:26 +02:00
<select name='device_id' id='device_id' class='form-control'> " ;
$devices = dbFetchRows ( 'SELECT * FROM `devices` ORDER BY hostname' );
foreach ( $devices as $device ) {
unset ( $done );
foreach ( $access_list as $ac ) {
if ( $ac == $device [ 'device_id' ]) {
$done = 1 ;
}
}
2008-03-09 22:49:53 +00:00
2020-09-21 15:40:17 +02:00
if ( ! $done ) {
echo " <option value=' " . $device [ 'device_id' ] . " '> " . format_hostname ( $device , $device [ 'hostname' ]) . '</option>' ;
2015-07-13 20:10:26 +02:00
}
}
2008-03-09 22:49:53 +00:00
2015-07-13 20:10:26 +02:00
echo " </select>
2014-03-10 23:50:16 +00:00
</div>
2015-07-13 20:10:26 +02:00
<button type='submit' class='btn btn-default' name='Submit'>Add</button></form> " ;
2008-03-09 22:49:53 +00:00
2019-12-30 12:11:26 +01:00
echo '</div>
<div class="col-md-4">' ;
// Display devices this users has access to
echo '<h3>Device access via Device Group (beta)</h3>' ;
echo " <div class='panel panel-default panel-condensed'>
<table class='table table-hover table-condensed table-striped'>
<tr>
<th>Device Group</th>
<th>Action</th>
</tr> " ;
foreach ( $user -> deviceGroups as $device_group_perm ) {
2020-09-21 15:40:17 +02:00
echo '<tr><td><strong>' . $device_group_perm -> name . " </td><td> <a href='edituser/action=deldevgroupperm/user_id= " . $user -> user_id . '/device_group_id=' . $device_group_perm -> id . " '><i class='fa fa-trash fa-lg icon-theme' aria-hidden='true'></i></a></strong></td></tr> " ;
2019-12-30 12:11:26 +01:00
}
echo '</table>
</div>' ;
if ( $user -> deviceGroups -> isEmpty ()) {
echo 'None Configured' ;
}
// Display device groups this user doesn't have access to
echo '<h4>Grant access to new Device Group</h4>' ;
$allow_dynamic = \LibreNMS\Config :: get ( 'permission.device_group.allow_dynamic' );
2020-09-21 15:40:17 +02:00
if ( ! $allow_dynamic ) {
2020-09-21 15:59:34 +02:00
echo '<i>Dynamic groups are disabled, set permission.device_group.allow_dynamic to enable.</i>' ;
2019-12-30 12:11:26 +01:00
}
echo " <form class='form-inline' role='form' method='post' action=''>
" . csrf_field () . "
2020-09-21 15:40:17 +02:00
<input type='hidden' value=' " . $user_data [ 'user_id' ] . " ' name='user_id'>
2019-12-30 12:11:26 +01:00
<input type='hidden' value='edituser' name='page'>
<input type='hidden' value='adddevgroupperm' name='action'>
<div class='form-group'>
<label class='sr-only' for='device_group_id'>Device</label>
<select name='device_group_id' id='device_group_id' class='form-control'> " ;
$device_groups = DeviceGroup :: query ()
-> whereNotIn ( 'id' , $user -> deviceGroups -> pluck ( 'id' ))
2020-09-21 15:40:17 +02:00
-> when ( ! $allow_dynamic , function ( $query ) {
2019-12-30 12:11:26 +01:00
return $query -> where ( 'type' , 'static' );
})
-> orderBy ( 'name' )
-> get ([ 'id' , 'name' ]);
foreach ( $device_groups as $group ) {
2020-09-21 15:40:17 +02:00
echo '<option value="' . $group -> id . '">' . $group -> name . '</option>' ;
2019-12-30 12:11:26 +01:00
}
echo " </select>
</div>
<button type='submit' class='btn btn-default' name='Submit'>Add</button></form> " ;
echo " </div></div>
<div class='row'>
2015-07-13 20:10:26 +02:00
<div class='col-md-4'> " ;
echo '<h3>Interface Access</h3>' ;
2008-03-09 22:49:53 +00:00
2020-09-21 15:40:17 +02:00
$interface_perms = dbFetchRows ( 'SELECT * from ports_perms as P, ports as I, devices as D WHERE `user_id` = ? AND I.port_id = P.port_id AND D.device_id = I.device_id' , [ $user_data [ 'user_id' ]]);
2008-03-09 22:49:53 +00:00
2015-07-13 20:10:26 +02:00
echo " <div class='panel panel-default panel-condensed'>
2014-03-10 23:50:16 +00:00
<table class='table table-hover table-condensed table-striped'>
<tr>
<th>Interface name</th>
<th>Action</th>
2015-07-13 20:10:26 +02:00
</tr> " ;
foreach ( $interface_perms as $interface_perm ) {
echo '<tr>
2014-03-10 23:50:16 +00:00
<td>
2021-03-28 17:25:30 -05:00
<strong>' . $interface_perm [ 'hostname' ] . ' - ' . $interface_perm [ 'ifDescr' ] . '</strong>' . '' . \LibreNMS\Util\Clean :: html ( $interface_perm [ 'ifAlias' ], []) . "
2014-03-10 23:50:16 +00:00
</td>
<td>
2020-09-21 15:40:17 +02:00
<a href='edituser/action=delifperm/user_id= " . $user_data [ 'user_id' ] . '/port_id=' . $interface_perm [ 'port_id' ] . " '><i class='fa fa-trash fa-lg icon-theme' aria-hidden='true'></i></a>
2014-03-10 23:50:16 +00:00
</td>
2015-07-13 20:10:26 +02:00
</tr> " ;
$ipermdone = 'yes' ;
}
echo '</table>
</div>' ;
2008-03-09 22:49:53 +00:00
2020-09-21 15:40:17 +02:00
if ( ! $ipermdone ) {
2015-07-13 20:10:26 +02:00
echo 'None Configured' ;
}
2011-03-17 00:09:20 +00:00
2019-12-30 12:11:26 +01:00
// Display interfaces this user doesn't have access to
2015-07-13 20:10:26 +02:00
echo '<h4>Grant access to new interface</h4>' ;
2011-03-17 00:09:20 +00:00
2015-07-13 20:10:26 +02:00
echo " <form action='' method='post' class='form-horizontal' role='form'>
2019-07-17 07:20:26 -05:00
" . csrf_field () . "
2020-09-21 15:40:17 +02:00
<input type='hidden' value=' " . $user_data [ 'user_id' ] . " ' name='user_id'>
2011-03-17 00:09:20 +00:00
<input type='hidden' value='edituser' name='page'>
<input type='hidden' value='addifperm' name='action'>
2014-03-10 23:50:16 +00:00
<div class='form-group'>
<label for='device' class='col-sm-2 control-label'>Device: </label>
<div class='col-sm-10'>
<select id='device' class='form-control' name='device' onchange='getInterfaceList(this)'>
2015-07-13 20:10:26 +02:00
<option value=''>Select a device</option> " ;
foreach ( $devices as $device ) {
unset ( $done );
foreach ( $access_list as $ac ) {
if ( $ac == $device [ 'device_id' ]) {
$done = 1 ;
}
}
2011-03-17 00:09:20 +00:00
2020-09-21 15:40:17 +02:00
if ( ! $done ) {
echo " <option value=' " . $device [ 'device_id' ] . " '> " . format_hostname ( $device , $device [ 'hostname' ]) . '</option>' ;
2015-07-13 20:10:26 +02:00
}
}
2008-03-09 22:49:53 +00:00
2015-07-13 20:10:26 +02:00
echo " </select>
2014-03-10 23:50:16 +00:00
</div>
</div>
<div class='form-group'>
<label for='port_id' class='col-sm-2 control-label'>Interface: </label>
<div class='col-sm-10'>
<select class='form-control' id='port_id' name='port_id'>
</select>
</div>
</div>
<div class='form-group'>
<div class='col-sm-12'>
2014-08-27 20:09:29 +10:00
<button type='submit' class='btn btn-default' name='Submit' value='Add'>Add</button>
2014-03-10 23:50:16 +00:00
</div>
</div>
2015-07-13 20:10:26 +02:00
</form> " ;
2014-03-10 23:50:16 +00:00
2015-07-13 20:10:26 +02:00
echo " </div>
<div class='col-md-4'> " ;
echo '<h3>Bill Access</h3>' ;
2008-03-09 22:49:53 +00:00
2020-09-21 15:40:17 +02:00
$bill_perms = dbFetchRows ( 'SELECT * from bills AS B, bill_perms AS P WHERE P.user_id = ? AND P.bill_id = B.bill_id' , [ $user_data [ 'user_id' ]]);
2008-03-09 22:49:53 +00:00
2015-07-13 20:10:26 +02:00
echo " <div class='panel panel-default panel-condensed'>
2014-03-10 23:50:16 +00:00
<table class='table table-hover table-condensed table-striped'>
<tr>
<th>Bill name</th>
<th>Action</th>
2015-07-13 20:10:26 +02:00
</tr> " ;
2014-03-10 23:50:16 +00:00
2015-07-13 20:10:26 +02:00
foreach ( $bill_perms as $bill_perm ) {
echo '<tr>
2014-03-10 23:50:16 +00:00
<td>
2020-09-21 15:40:17 +02:00
<strong>' . $bill_perm [ 'bill_name' ] . " </strong></td><td width=50> <a href='edituser/action=delbillperm/user_id= " . $vars [ 'user_id' ] . '/bill_id=' . $bill_perm [ 'bill_id' ] . " '><i class='fa fa-trash fa-lg icon-theme' aria-hidden='true'></i></a>
2014-03-10 23:50:16 +00:00
</td>
2015-07-13 20:10:26 +02:00
</tr> " ;
$bill_access_list [] = $bill_perm [ 'bill_id' ];
2008-03-09 22:49:53 +00:00
2015-07-13 20:10:26 +02:00
$bpermdone = 'yes' ;
}
2008-03-09 22:49:53 +00:00
2015-07-13 20:10:26 +02:00
echo '</table>
</div>' ;
2014-03-10 23:50:16 +00:00
2020-09-21 15:40:17 +02:00
if ( ! $bpermdone ) {
2015-07-13 20:10:26 +02:00
echo 'None Configured' ;
}
2011-03-17 00:09:20 +00:00
2015-07-13 20:10:26 +02:00
// Display devices this user doesn't have access to
echo '<h4>Grant access to new bill</h4>' ;
echo " <form method='post' action='' class='form-inline' role='form'>
2019-07-17 07:20:26 -05:00
" . csrf_field () . "
2020-09-21 15:40:17 +02:00
<input type='hidden' value=' " . $user_data [ 'user_id' ] . " ' name='user_id'>
2011-03-17 00:09:20 +00:00
<input type='hidden' value='edituser' name='page'>
<input type='hidden' value='addbillperm' name='action'>
2014-03-10 23:50:16 +00:00
<div class='form-group'>
<label class='sr-only' for='bill_id'>Bill</label>
2015-07-13 20:10:26 +02:00
<select name='bill_id' class='form-control' id='bill_id'> " ;
$bills = dbFetchRows ( 'SELECT * FROM `bills` ORDER BY `bill_name`' );
foreach ( $bills as $bill ) {
unset ( $done );
foreach ( $bill_access_list as $ac ) {
if ( $ac == $bill [ 'bill_id' ]) {
$done = 1 ;
}
}
2008-03-09 22:49:53 +00:00
2020-09-21 15:40:17 +02:00
if ( ! $done ) {
echo " <option value=' " . $bill [ 'bill_id' ] . " '> " . $bill [ 'bill_name' ] . '</option>' ;
2015-07-13 20:10:26 +02:00
}
}
2008-03-09 22:49:53 +00:00
2015-07-13 20:10:26 +02:00
echo " </select>
2014-03-10 23:50:16 +00:00
</div>
2014-08-27 20:09:29 +10:00
<button type='submit' class='btn btn-default' name='Submit' value='Add'>Add</button>
2014-03-10 23:50:16 +00:00
</form>
2015-07-13 20:10:26 +02:00
</div> " ;
2016-08-18 20:28:22 -05:00
} else {
2019-04-22 19:01:39 -05:00
echo '<script>window.location.replace("' . url ( 'users' ) . '");</script>' ;
2015-07-13 20:10:26 +02:00
} //end if
} //end if
2008-03-09 22:49:53 +00:00
2015-07-13 20:10:26 +02:00
echo '</div>' ;