2017-01-12 19:40:20 -08:00
< ? php
namespace App\Http\Controllers\Api ;
use App\Http\Controllers\Controller ;
2019-03-13 20:12:03 -07:00
use App\Http\Transformers\LoginAttemptsTransformer ;
2017-10-16 07:07:21 -07:00
use App\Models\Setting ;
2018-03-02 18:01:20 -08:00
use App\Notifications\MailTest ;
2019-03-13 20:12:03 -07:00
use App\Services\LdapAd ;
2018-12-06 14:05:43 -08:00
use Illuminate\Http\JsonResponse ;
2019-03-13 20:12:03 -07:00
use Illuminate\Http\Request ;
2020-10-05 20:34:17 -07:00
use Illuminate\Http\Response ;
2019-03-13 20:12:03 -07:00
use Illuminate\Support\Facades\DB ;
2018-12-06 14:05:43 -08:00
use Illuminate\Support\Facades\Log ;
2019-03-13 20:12:03 -07:00
use Illuminate\Support\Facades\Notification ;
2020-09-15 23:05:27 -07:00
use GuzzleHttp\Client ;
2020-10-05 20:34:17 -07:00
use Illuminate\Support\Facades\Storage ;
2017-01-12 19:40:20 -08:00
class SettingsController extends Controller
{
2018-12-06 14:05:43 -08:00
/**
* Test the ldap settings
*
* @ author Wes Hulette < jwhulette @ gmail . com >
*
* @ since 5.0 . 0
*
* @ param App\Models\LdapAd $ldap
*
* @ return \Illuminate\Http\JsonResponse
*/
public function ldapAdSettingsTest ( LdapAd $ldap ) : JsonResponse
2017-07-07 23:44:48 -07:00
{
2019-01-10 13:20:43 -08:00
if ( ! $ldap -> init ()) {
2018-12-06 14:05:43 -08:00
Log :: info ( 'LDAP is not enabled cannot test.' );
2017-10-16 07:07:21 -07:00
return response () -> json ([ 'message' => 'LDAP is not enabled, cannot test.' ], 400 );
}
2018-12-06 14:05:43 -08:00
// The connect, bind and resulting users message
$message = [];
2017-07-07 23:44:48 -07:00
2018-12-06 14:05:43 -08:00
Log :: info ( 'Preparing to test LDAP user login' );
// Test user can connect to the LDAP server
2017-07-07 23:44:48 -07:00
try {
2018-12-06 14:05:43 -08:00
$ldap -> testLdapAdUserConnection ();
$message [ 'login' ] = [
'message' => 'Successfully connected to LDAP server.'
];
} catch ( \Exception $ex ) {
2020-04-09 09:55:44 -07:00
\Log :: debug ( 'LDAP connected but Bind failed. Please check your LDAP settings and try again.' );
2018-12-06 14:05:43 -08:00
return response () -> json ([
2019-12-04 14:54:36 -08:00
'message' => 'Error logging into LDAP server, error: ' . $ex -> getMessage () . ' - Verify your that your username and password are correct' ]);
2017-07-07 23:44:48 -07:00
} catch ( \Exception $e ) {
2020-04-09 09:55:44 -07:00
\Log :: info ( 'LDAP connection failed but we cannot debug it any further on our end.' );
return response () -> json ([ 'message' => 'The LDAP connection failed but we cannot debug it any further on our end. The error from the server is: ' . $e -> getMessage ()], 500 );
2017-07-07 23:44:48 -07:00
}
2018-12-06 14:05:43 -08:00
Log :: info ( 'Preparing to test LDAP bind connection' );
// Test user can bind to the LDAP server
try {
2020-04-23 22:46:02 -07:00
Log :: info ( 'Testing Bind' );
2018-12-06 14:05:43 -08:00
$ldap -> testLdapAdBindConnection ();
$message [ 'bind' ] = [
'message' => 'Successfully binded to LDAP server.'
];
} catch ( \Exception $ex ) {
2020-04-23 22:46:02 -07:00
Log :: info ( 'LDAP Bind failed' );
2018-12-06 14:05:43 -08:00
return response () -> json ([
'message' => 'Error binding to LDAP server, error: ' . $ex -> getMessage ()
], 400 );
2017-10-16 07:07:21 -07:00
}
2018-12-06 14:05:43 -08:00
Log :: info ( 'Preparing to get sample user set from LDAP directory' );
// Get a sample of 10 users so user can verify the data is correct
2017-10-16 06:34:04 -07:00
try {
2020-04-23 22:46:02 -07:00
Log :: info ( 'Testing LDAP sync' );
2020-09-09 14:18:05 -07:00
error_reporting ( E_ALL & ~ E_DEPRECATED ); // workaround for php7.4, which deprecates ldap_control_paged_result
2018-12-06 14:05:43 -08:00
$users = $ldap -> testUserImportSync ();
$message [ 'user_sync' ] = [
'users' => $users
];
} catch ( \Exception $ex ) {
2020-04-23 22:46:02 -07:00
Log :: info ( 'LDAP sync failed' );
2018-12-06 14:05:43 -08:00
$message [ 'user_sync' ] = [
'message' => 'Error getting users from LDAP directory, error: ' . $ex -> getMessage ()
];
return response () -> json ( $message , 400 );
2017-10-16 06:34:04 -07:00
}
2018-12-06 14:05:43 -08:00
return response () -> json ( $message , 200 );
2017-10-16 06:34:04 -07:00
}
2020-09-15 23:05:27 -07:00
public function slacktest ( Request $request )
2018-02-22 16:35:34 -08:00
{
2020-09-15 23:05:27 -07:00
$slack = new Client ([
2020-09-15 23:19:52 -07:00
'base_url' => e ( $request -> input ( 'slack_endpoint' )),
2020-09-15 23:05:27 -07:00
'defaults' => [
'exceptions' => false
]
]);
$payload = json_encode (
[
2020-09-15 23:19:52 -07:00
'channel' => e ( $request -> input ( 'slack_channel' )),
2020-09-15 23:05:27 -07:00
'text' => trans ( 'general.slack_test_msg' ),
2020-09-15 23:19:52 -07:00
'username' => e ( $request -> input ( 'slack_botname' )),
2020-09-15 23:05:27 -07:00
'icon_emoji' => ':heart:'
]);
2018-02-22 16:35:34 -08:00
try {
2020-09-15 23:05:27 -07:00
$slack -> post ( $request -> input ( 'slack_endpoint' ),[ 'body' => $payload ]);
2018-02-22 16:35:34 -08:00
return response () -> json ([ 'message' => 'Success' ], 200 );
} catch ( \Exception $e ) {
2020-09-15 23:05:27 -07:00
return response () -> json ([ 'message' => 'Oops! Please check the channel name and webhook endpoint URL. Slack responded with: ' . $e -> getMessage ()], 400 );
2018-02-22 16:35:34 -08:00
}
2020-09-15 23:05:27 -07:00
return response () -> json ([ 'message' => 'Something went wrong :( ' ], 400 );
2018-02-22 16:35:34 -08:00
}
2017-10-19 08:18:56 -07:00
/**
* Test the email configuration
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v3 . 0 ]
* @ return Redirect
*/
public function ajaxTestEmail ()
{
2017-10-19 08:33:46 -07:00
if ( ! config ( 'app.lock_passwords' )) {
try {
2018-03-02 18:01:20 -08:00
Notification :: send ( Setting :: first (), new MailTest ());
2017-11-03 14:58:49 -07:00
return response () -> json ([ 'message' => 'Mail sent to ' . config ( 'mail.reply_to.address' )], 200 );
2020-11-12 14:27:59 -08:00
} catch ( \Exception $e ) {
2017-10-19 08:33:46 -07:00
return response () -> json ([ 'message' => $e -> getMessage ()], 500 );
}
2017-10-19 08:18:56 -07:00
}
2017-10-19 08:33:46 -07:00
return response () -> json ([ 'message' => 'Mail would have been sent, but this application is in demo mode! ' ], 200 );
2017-10-19 08:18:56 -07:00
}
2020-10-05 20:34:17 -07:00
/**
* Delete server - cached barcodes
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v5 . 0.0 ]
* @ return Response
*/
public function purgeBarcodes ()
{
$file_count = 0 ;
$files = Storage :: disk ( 'public' ) -> files ( 'barcodes' );
foreach ( $files as $file ) { // iterate files
$file_parts = explode ( " . " , $file );
$extension = end ( $file_parts );
\Log :: debug ( $extension );
// Only generated barcodes would have a .png file extension
if ( $extension == 'png' ) {
\Log :: debug ( 'Deleting: ' . $file );
try {
Storage :: disk ( 'public' ) -> delete ( $file );
\Log :: debug ( 'Deleting: ' . $file );
$file_count ++ ;
} catch ( \Exception $e ) {
\Log :: debug ( $e );
}
}
}
return response () -> json ([ 'message' => 'Deleted ' . $file_count . ' barcodes' ], 200 );
}
2018-08-01 03:51:59 -07:00
/**
* Get a list of login attempts
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v5 . 0.0 ]
* @ param \Illuminate\Http\Request $request
* @ return array
*/
public function showLoginAttempts ( Request $request )
{
$allowed_columns = [ 'id' , 'username' , 'remote_ip' , 'user_agent' , 'successful' , 'created_at' ];
$login_attempts = DB :: table ( 'login_attempts' );
$order = $request -> input ( 'order' ) === 'asc' ? 'asc' : 'desc' ;
$sort = in_array ( $request -> get ( 'sort' ), $allowed_columns ) ? $request -> get ( 'sort' ) : 'created_at' ;
$total = $login_attempts -> count ();
$login_attempts -> orderBy ( $sort , $order );
$login_attempt_results = $login_attempts -> skip ( request ( 'offset' , 0 )) -> take ( request ( 'limit' , 20 )) -> get ();
return ( new LoginAttemptsTransformer ) -> transformLoginAttempts ( $login_attempt_results , $total );
}
2017-10-19 08:18:56 -07:00
2017-01-12 19:40:20 -08:00
}