/** * @param Request $request * @return \BladeView|bool|\Illuminate\View\View */ public function index(Request $request) { if ($request->input('search')) { $search = $request->get('search'); $users = DuoUser::where('realname', 'like', "%{$search}%")->orWhere('username', 'like', "%{$search}%")->whereNull('deleted_at')->orderBy('username', 'asc')->paginate(10); } else { $users = DB::table('duo_users')->whereNull('deleted_at')->orderBy('username', 'asc')->paginate(10); } return view('duo.index', compact('users')); }
/** * Execute the job. * * @return void */ public function handle() { set_time_limit(0); ini_set('memory_limit', '2048M'); //Create the Duo Admin Client and set the timeout higher than default $duoAdmin = new DuoAdmin(); $duoAdmin->setRequesterOption('timeout', '6000000'); // Set the log $count value to 1000 $count = 1000; $backoff = NULL; while ($count >= 1000) { \Log::debug('Start Log gathering', ['count' => $count, 'backoff' => $backoff]); //Query Duo REST API $response = $duoAdmin->logs($this->getMinTime()); if (isset($response['response']['code']) && $response['response']['code'] == '42901') { $backoff += 10; \Log::debug('Received backoff notice', ['response' => $response, 'set-backoff' => $backoff]); sleep($backoff); continue; } //Duo SDK puts results in nested array [response][response] $logs = $response['response']['response']; $backoff = NULL; \Log::debug('Received Duo Response Object. Adding new entries ', ['object-count' => count($logs), 'set-backoff' => $backoff]); // Loop each log to save foreach ($logs as $log) { // Get the DuoUser ID to create a relation $duoUserId = User::where('username', $log['username'])->first(); // Sometimes the 'username' from Duo doesn't exist locally.... if ($duoUserId) { $log['duo_user_id'] = $duoUserId->id; } else { $log['duo_user_id'] = NULL; } // Save the log Log::create($log); } // Set the count to number of logs returned in the last call. // If it's less than 1000, we've reached the end of the logs \Log::debug('Added new log entries. Setting count: ', ['count' => count($logs)]); $count = count($logs); } }
/** * @param $freshDuoUserList */ private function removeStaleAccounts($freshDuoUserList) { \Log::debug('Removing stale accounts from UC Insight (soft delete)'); //Move the fresh list of usernames from Duo API to an array $newDuoUsers = []; foreach ($freshDuoUserList as $user) { $newDuoUsers[] = $user['user_id']; } // If there's only 1 user, we're just doing an on-demand // user sync, so we shouldn't worry about stale accounts. if (count($newDuoUsers) == 1) { \Log::debug('We are syncing a single user, no need to remove stale accounts. Exiting function.'); return; } //Get a list of local Duo usernames $localDuoUsers = User::lists('user_id')->toArray(); \Log::debug('Got a list of local DuoUsers - ', [count($localDuoUsers)]); //Compare the two arrays $staleUsers = array_diff($localDuoUsers, $newDuoUsers); \Log::debug('Local DuoUser stale accounts - ', [count($staleUsers)]); //Remove the 'stale' user accounts from the local database foreach ($staleUsers as $dead) { \Log::debug('Removing stale local DuoUser account - ', [$dead]); User::where('user_id', $dead)->delete(); } }