2017-01-12 19:40:20 -08:00
< ? php
namespace App\Http\Controllers\Api ;
2021-11-03 15:22:06 -07:00
use Illuminate\Http\Request ;
2017-01-12 19:40:20 -08:00
use App\Http\Controllers\Controller ;
2021-06-10 13:15:52 -07:00
use App\Models\Ldap ;
2017-10-16 07:07:21 -07:00
use App\Models\Setting ;
2021-11-03 15:22:06 -07:00
use Mail ;
use App\Notifications\SlackTest ;
2018-03-02 18:01:20 -08:00
use App\Notifications\MailTest ;
2021-06-10 13:15:52 -07:00
use GuzzleHttp\Client ;
2018-12-06 14:05:43 -08:00
use Illuminate\Http\JsonResponse ;
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-10-05 20:34:17 -07:00
use Illuminate\Support\Facades\Storage ;
2021-12-16 14:26:24 -08:00
use Illuminate\Support\Facades\Validator ;
2021-12-08 15:56:22 -08:00
use App\Http\Requests\SlackSettingsRequest ;
2022-05-13 16:46:13 -07:00
use App\Http\Transformers\LoginAttemptsTransformer ;
2020-11-30 17:11:44 -08:00
2017-01-12 19:40:20 -08:00
class SettingsController extends Controller
{
2017-10-16 07:07:21 -07:00
2017-07-07 23:44:48 -07:00
2021-11-03 15:22:06 -07:00
public function ldaptest ()
{
$settings = Setting :: getSettings ();
2019-12-04 14:54:36 -08:00
2021-11-03 15:22:06 -07:00
if ( $settings -> ldap_enabled != '1' ) {
\Log :: debug ( 'LDAP is not enabled cannot test.' );
return response () -> json ([ 'message' => 'LDAP is not enabled, cannot test.' ], 400 );
2017-07-07 23:44:48 -07:00
}
2021-11-03 15:22:06 -07:00
\Log :: debug ( 'Preparing to test LDAP connection' );
2017-10-16 07:07:21 -07:00
2021-11-03 15:22:06 -07:00
$message = []; //where we collect together test messages
2017-10-16 06:34:04 -07:00
try {
2021-11-03 15:22:06 -07:00
$connection = Ldap :: connectToLdap ();
try {
$message [ 'bind' ] = [ 'message' => 'Successfully bound to LDAP server.' ];
\Log :: debug ( 'attempting to bind to LDAP for LDAP test' );
Ldap :: bindAdminToLdap ( $connection );
$message [ 'login' ] = [
'message' => 'Successfully connected to LDAP server.' ,
2021-03-17 19:24:28 -07:00
];
2021-06-10 13:15:52 -07:00
2021-11-03 15:22:06 -07:00
$users = collect ( Ldap :: findLdapUsers ( null , 10 )) -> filter ( function ( $value , $key ) {
return is_int ( $key );
}) -> slice ( 0 , 10 ) -> map ( function ( $item ) use ( $settings ) {
return ( object ) [
'username' => $item [ $settings [ 'ldap_username_field' ]][ 0 ] ? ? null ,
'employee_number' => $item [ $settings [ 'ldap_emp_num' ]][ 0 ] ? ? null ,
'lastname' => $item [ $settings [ 'ldap_lname_field' ]][ 0 ] ? ? null ,
'firstname' => $item [ $settings [ 'ldap_fname_field' ]][ 0 ] ? ? null ,
'email' => $item [ $settings [ 'ldap_email' ]][ 0 ] ? ? null ,
];
});
if ( $users -> count () > 0 ) {
$message [ 'user_sync' ] = [
'users' => $users ,
];
} else {
$message [ 'user_sync' ] = [
'message' => 'Connection to LDAP was successful, however there were no users returned from your query. You should confirm the Base Bind DN above.' ,
];
return response () -> json ( $message , 400 );
}
2021-06-10 13:15:52 -07:00
2021-11-03 15:22:06 -07:00
return response () -> json ( $message , 200 );
} catch ( \Exception $e ) {
\Log :: debug ( 'Bind failed' );
\Log :: debug ( " Exception was: " . $e -> getMessage ());
return response () -> json ([ 'message' => $e -> getMessage ()], 400 );
//return response()->json(['message' => $e->getMessage()], 500);
}
} catch ( \Exception $e ) {
\Log :: debug ( 'Connection failed but we cannot debug it any further on our end.' );
return response () -> json ([ 'message' => $e -> getMessage ()], 500 );
2017-10-16 06:34:04 -07:00
}
2021-11-03 15:22:06 -07:00
2017-10-16 06:34:04 -07:00
}
2021-11-03 15:22:06 -07:00
public function ldaptestlogin ( Request $request )
2020-11-16 21:23:08 -08:00
{
2021-06-10 13:15:52 -07:00
if ( Setting :: getSettings () -> ldap_enabled != '1' ) {
2021-11-03 15:22:06 -07:00
\Log :: debug ( 'LDAP is not enabled. Cannot test.' );
2020-11-16 21:23:08 -08:00
return response () -> json ([ 'message' => 'LDAP is not enabled, cannot test.' ], 400 );
}
2021-11-03 15:22:06 -07:00
$rules = array (
2020-11-16 21:23:08 -08:00
'ldaptest_user' => 'required' ,
2021-11-03 15:22:06 -07:00
'ldaptest_password' => 'required'
);
2020-11-16 21:23:08 -08:00
$validator = Validator :: make ( $request -> all (), $rules );
if ( $validator -> fails ()) {
\Log :: debug ( 'LDAP Validation test failed.' );
2021-11-03 15:22:06 -07:00
$validation_errors = implode ( ' ' , $validator -> errors () -> all ());
2020-11-16 21:23:08 -08:00
return response () -> json ([ 'message' => $validator -> errors () -> all ()], 400 );
}
2021-11-03 15:22:06 -07:00
2020-11-16 21:23:08 -08:00
\Log :: debug ( 'Preparing to test LDAP login' );
try {
2021-11-03 15:22:06 -07:00
$connection = Ldap :: connectToLdap ();
try {
Ldap :: bindAdminToLdap ( $connection );
\Log :: debug ( 'Attempting to bind to LDAP for LDAP test' );
try {
$ldap_user = Ldap :: findAndBindUserLdap ( $request -> input ( 'ldaptest_user' ), $request -> input ( 'ldaptest_password' ));
if ( $ldap_user ) {
\Log :: debug ( 'It worked! ' . $request -> input ( 'ldaptest_user' ) . ' successfully binded to LDAP.' );
return response () -> json ([ 'message' => 'It worked! ' . $request -> input ( 'ldaptest_user' ) . ' successfully binded to LDAP.' ], 200 );
}
return response () -> json ([ 'message' => 'Login Failed. ' . $request -> input ( 'ldaptest_user' ) . ' did not successfully bind to LDAP.' ], 400 );
2020-11-16 21:23:08 -08:00
2021-11-03 15:22:06 -07:00
} catch ( \Exception $e ) {
\Log :: debug ( 'LDAP login failed' );
return response () -> json ([ 'message' => $e -> getMessage ()], 400 );
}
2020-11-16 21:23:08 -08:00
2021-11-03 15:22:06 -07:00
} catch ( \Exception $e ) {
\Log :: debug ( 'Bind failed' );
return response () -> json ([ 'message' => $e -> getMessage ()], 400 );
//return response()->json(['message' => $e->getMessage()], 500);
2020-11-16 21:23:08 -08:00
}
} catch ( \Exception $e ) {
\Log :: debug ( 'Connection failed' );
2021-11-03 15:22:06 -07:00
return response () -> json ([ 'message' => $e -> getMessage ()], 500 );
2020-11-16 21:23:08 -08:00
}
2021-11-03 15:22:06 -07:00
2020-11-16 21:23:08 -08:00
}
2021-12-16 14:38:04 -08:00
public function slacktest ( SlackSettingsRequest $request )
2018-02-22 16:35:34 -08:00
{
2021-12-06 11:40:24 -08:00
2021-12-08 17:58:46 -08:00
$validator = Validator :: make ( $request -> all (), [
2021-12-08 18:03:56 -08:00
'slack_endpoint' => 'url|required_with:slack_channel|starts_with:https://hooks.slack.com/|nullable' ,
2021-12-08 17:58:46 -08:00
'slack_channel' => 'required_with:slack_endpoint|starts_with:#|nullable' ,
2020-09-15 23:05:27 -07:00
]);
2021-12-08 17:58:46 -08:00
if ( $validator -> fails ()) {
return response () -> json ([ 'message' => 'Validation failed' , 'errors' => $validator -> errors ()], 422 );
}
2021-12-08 18:03:56 -08:00
// If validation passes, continue to the curl request
2021-12-06 11:40:24 -08:00
$slack = new Client ([
'base_url' => e ( $request -> input ( 'slack_endpoint' )),
'defaults' => [
'exceptions' => false ,
],
2020-09-15 23:05:27 -07:00
]);
2018-02-22 16:35:34 -08:00
2021-12-06 11:40:24 -08:00
$payload = json_encode (
[
'channel' => e ( $request -> input ( 'slack_channel' )),
'text' => trans ( 'general.slack_test_msg' ),
'username' => e ( $request -> input ( 'slack_botname' )),
'icon_emoji' => ':heart:' ,
]);
2021-06-10 13:15:52 -07:00
2021-12-06 11:40:24 -08:00
try {
$slack -> post ( $request -> input ( 'slack_endpoint' ), [ 'body' => $payload ]);
return response () -> json ([ 'message' => 'Success' ], 200 );
2018-02-22 16:35:34 -08:00
2021-12-06 11:40:24 -08:00
} catch ( \Exception $e ) {
2021-12-16 14:26:24 -08:00
return response () -> json ([ 'message' => 'Please check the channel name and webhook endpoint URL (' . e ( $request -> input ( 'slack_endpoint' )) . '). Slack responded with: ' . $e -> getMessage ()], 400 );
2021-12-06 11:40:24 -08:00
}
2018-02-22 16:35:34 -08:00
2021-12-08 15:56:22 -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
}
2021-11-03 15:22:06 -07: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 ()
{
2021-11-03 15:22:06 -07:00
if ( ! config ( 'app.lock_passwords' )) {
2017-10-19 08:33:46 -07:00
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
}
2021-06-10 13:15:52 -07:00
return response () -> json ([ 'message' => 'Mail would have been sent, but this application is in demo mode! ' ], 200 );
2021-11-03 15:22:06 -07:00
2017-10-19 08:18:56 -07:00
}
2021-11-03 15:22:06 -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
2021-06-10 13:15:52 -07:00
$file_parts = explode ( '.' , $file );
2020-10-05 20:34:17 -07:00
$extension = end ( $file_parts );
\Log :: debug ( $extension );
// Only generated barcodes would have a .png file extension
2021-06-10 13:15:52 -07:00
if ( $extension == 'png' ) {
2020-10-05 20:34:17 -07:00
\Log :: debug ( 'Deleting: ' . $file );
2021-06-10 13:15:52 -07:00
try {
2020-10-05 20:34:17 -07:00
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 )
{
2021-06-10 13:15:52 -07:00
$allowed_columns = [ 'id' , 'username' , 'remote_ip' , 'user_agent' , 'successful' , 'created_at' ];
2018-08-01 03:51:59 -07:00
2021-06-10 13:15:52 -07:00
$login_attempts = DB :: table ( 'login_attempts' );
2018-08-01 03:51:59 -07:00
$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 );
2021-06-10 13:15:52 -07:00
$login_attempt_results = $login_attempts -> skip ( request ( 'offset' , 0 )) -> take ( request ( 'limit' , 20 )) -> get ();
2018-08-01 03:51:59 -07:00
return ( new LoginAttemptsTransformer ) -> transformLoginAttempts ( $login_attempt_results , $total );
}
2021-11-03 15:22:06 -07:00
}