2017-01-12 19:40:20 -08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use App\Http\Controllers\Controller;
|
2017-07-07 23:44:48 -07:00
|
|
|
use App\Models\Ldap;
|
2017-10-16 07:07:21 -07:00
|
|
|
use Validator;
|
|
|
|
use App\Models\Setting;
|
2017-01-12 19:40:20 -08:00
|
|
|
|
|
|
|
class SettingsController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v4.0]
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v4.0]
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function store(Request $request)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the specified resource.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function show($id)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v4.0]
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function update(Request $request, $id)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v4.0]
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function destroy($id)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
2017-07-07 23:44:48 -07:00
|
|
|
|
2017-10-16 05:52:18 -07:00
|
|
|
public function ldaptest()
|
2017-07-07 23:44:48 -07:00
|
|
|
{
|
2017-10-16 05:54:33 -07:00
|
|
|
|
2017-10-16 07:07:21 -07:00
|
|
|
if (Setting::getSettings()->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
|
|
|
\Log::debug('Preparing to test LDAP connection');
|
|
|
|
|
|
|
|
try {
|
|
|
|
$connection = Ldap::connectToLdap();
|
|
|
|
try {
|
|
|
|
\Log::debug('attempting to bind to LDAP for LDAP test');
|
|
|
|
Ldap::bindAdminToLdap($connection);
|
|
|
|
return response()->json(['message' => 'It worked!'], 200);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
\Log::debug('Bind failed');
|
|
|
|
return response()->json(['message' => $e->getMessage()], 400);
|
|
|
|
//return response()->json(['message' => $e->getMessage()], 500);
|
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
\Log::debug('Connection failed');
|
|
|
|
return response()->json(['message' => $e->getMessage()], 600);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-10-16 06:34:04 -07:00
|
|
|
public function ldaptestlogin(Request $request)
|
|
|
|
{
|
|
|
|
|
2017-10-16 07:07:21 -07:00
|
|
|
if (Setting::getSettings()->ldap_enabled!='1') {
|
|
|
|
\Log::debug('LDAP is not enabled. Cannot test.');
|
|
|
|
return response()->json(['message' => 'LDAP is not enabled, cannot test.'], 400);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$rules = array(
|
|
|
|
'ldaptest_user' => 'required',
|
|
|
|
'ldaptest_password' => 'required'
|
|
|
|
);
|
2017-10-16 06:34:04 -07:00
|
|
|
|
2017-10-16 07:07:21 -07:00
|
|
|
$validator = Validator::make($request->all(), $rules);
|
|
|
|
if ($validator->fails()) {
|
|
|
|
\Log::debug('LDAP Validation test failed.');
|
2017-10-16 09:00:51 -07:00
|
|
|
$validation_errors = implode(' ',$validator->errors()->all());
|
2017-10-16 07:07:21 -07:00
|
|
|
return response()->json(['message' => $validator->errors()->all()], 400);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
\Log::debug('Preparing to test LDAP login');
|
2017-10-16 06:34:04 -07:00
|
|
|
try {
|
|
|
|
$connection = Ldap::connectToLdap();
|
|
|
|
try {
|
|
|
|
Ldap::bindAdminToLdap($connection);
|
2017-10-16 07:07:21 -07:00
|
|
|
\Log::debug('Attempting to bind to LDAP for LDAP test');
|
2017-10-16 06:34:04 -07:00
|
|
|
try {
|
|
|
|
$ldap_user = Ldap::findAndBindUserLdap($request->input('ldaptest_user'), $request->input('ldaptest_password'));
|
2017-10-16 06:39:36 -07:00
|
|
|
if ($ldap_user) {
|
2017-10-16 06:46:33 -07:00
|
|
|
\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);
|
2017-10-16 06:39:36 -07:00
|
|
|
}
|
2017-10-16 07:07:21 -07:00
|
|
|
return response()->json(['message' => 'Login Failed. '. $request->input('ldaptest_user').' did not successfully bind to LDAP.'], 400);
|
2017-10-16 06:39:36 -07:00
|
|
|
|
2017-10-16 06:34:04 -07:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
\Log::debug('LDAP login failed');
|
|
|
|
return response()->json(['message' => $e->getMessage()], 400);
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
\Log::debug('Bind failed');
|
|
|
|
return response()->json(['message' => $e->getMessage()], 400);
|
|
|
|
//return response()->json(['message' => $e->getMessage()], 500);
|
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
\Log::debug('Connection failed');
|
2017-10-16 09:00:51 -07:00
|
|
|
return response()->json(['message' => $e->getMessage()], 500);
|
2017-10-16 06:34:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-01-12 19:40:20 -08:00
|
|
|
}
|