2018-07-24 19:35:26 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Users;
|
|
|
|
|
2019-03-13 20:12:03 -07:00
|
|
|
use App\Http\Controllers\Controller;
|
2019-01-10 13:20:43 -08:00
|
|
|
use App\Services\LdapAd;
|
2018-07-24 19:35:26 -07:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Artisan;
|
|
|
|
|
|
|
|
class LDAPImportController extends Controller
|
|
|
|
{
|
2018-12-06 14:05:43 -08:00
|
|
|
/**
|
|
|
|
* An Ldap instance.
|
|
|
|
*
|
|
|
|
* @var LdapAd
|
|
|
|
*/
|
|
|
|
protected $ldap;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* __construct.
|
|
|
|
*
|
|
|
|
* @param LdapAd $ldap
|
|
|
|
*/
|
|
|
|
public function __construct(LdapAd $ldap)
|
|
|
|
{
|
2019-01-10 13:20:43 -08:00
|
|
|
parent::__construct();
|
2018-12-06 14:05:43 -08:00
|
|
|
$this->ldap = $ldap;
|
2019-01-24 16:15:44 -08:00
|
|
|
$this->ldap->init();
|
2018-12-06 14:05:43 -08:00
|
|
|
}
|
2018-07-24 19:35:26 -07:00
|
|
|
|
|
|
|
/**
|
2018-12-06 14:05:43 -08:00
|
|
|
* Return view for LDAP import.
|
2018-07-24 19:35:26 -07:00
|
|
|
*
|
|
|
|
* @author Aladin Alaily
|
2018-12-06 14:05:43 -08:00
|
|
|
* @author Wes Hulette <jwhulette@gmail.com>
|
|
|
|
*
|
|
|
|
* @since 5.0.0
|
|
|
|
*
|
2018-07-24 19:35:26 -07:00
|
|
|
* @return \Illuminate\Contracts\View\View
|
2018-12-06 14:05:43 -08:00
|
|
|
*
|
2018-07-24 19:35:26 -07:00
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
|
|
|
public function create()
|
|
|
|
{
|
|
|
|
$this->authorize('update', User::class);
|
|
|
|
try {
|
2020-04-23 22:45:44 -07:00
|
|
|
$this->ldap->connect();
|
2018-07-24 19:35:26 -07:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
return redirect()->route('users.index')->with('error', $e->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
return view('users/ldap');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* LDAP form processing.
|
|
|
|
*
|
|
|
|
* @author Aladin Alaily
|
2020-04-23 22:45:44 -07:00
|
|
|
* @author A. Gianotto <snipe@snipe.net>
|
2018-12-06 14:05:43 -08:00
|
|
|
* @author Wes Hulette <jwhulette@gmail.com>
|
|
|
|
*
|
|
|
|
* @since 5.0.0
|
|
|
|
*
|
2018-07-24 19:35:26 -07:00
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
|
|
|
public function store(Request $request)
|
|
|
|
{
|
|
|
|
// Call Artisan LDAP import command.
|
|
|
|
$location_id = $request->input('location_id');
|
2018-12-11 15:18:30 -08:00
|
|
|
Artisan::call('snipeit:ldap-sync', ['--location_id' => $location_id, '--json_summary' => true]);
|
2018-07-24 19:35:26 -07:00
|
|
|
|
|
|
|
// Collect and parse JSON summary.
|
|
|
|
$ldap_results_json = Artisan::output();
|
|
|
|
$ldap_results = json_decode($ldap_results_json, true);
|
|
|
|
|
|
|
|
// Direct user to appropriate status page.
|
|
|
|
if ($ldap_results['error']) {
|
|
|
|
return redirect()->back()->withInput()->with('error', $ldap_results['error_message']);
|
|
|
|
}
|
2018-12-06 14:05:43 -08:00
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
return redirect()->route('ldap/user')
|
2018-12-06 14:05:43 -08:00
|
|
|
->with('success', 'LDAP Import successful.')
|
2018-07-24 19:35:26 -07:00
|
|
|
->with('summary', $ldap_results['summary']);
|
|
|
|
}
|
|
|
|
}
|