Example #1
0
 public function migrateUser(Request $request, $id)
 {
     \Log::debug('Starting new DuoUser migration process with request from UC Insight User - ', [\Auth::user()->username]);
     // Get Form input
     $input = $request->all();
     //Create Duo Admin Client
     $duoAdmin = new DuoAdmin();
     //Get the local Duo User account ID
     $insightUser = DuoUser::findorFail($id);
     \Log::debug('Found local DuoUser account to migrate - ', [$insightUser]);
     //Get a fresh copy of the current User data before adding the new user.
     $this->dispatch(new FetchDuoUsers($insightUser->username));
     \Log::debug('Refreshed local DuoUser with Duo API - ', [$insightUser]);
     //Fetch the User details via Duo API
     $res = $duoAdmin->users($insightUser->username);
     //If we didn't get the user object back, error and redirect
     if (!count($res['response']['response'])) {
         \Log::debug('Source Duo User not found for migrate function', [$insightUser]);
         alert()->error("Not able to migrate {$insightUser->realname}.  Please contact the UC-Insight Admin")->persistent('Close');
         return redirect('duo/user/' . $id);
     }
     //Grab the user details
     $user = $res['response']['response'][0];
     \Log::debug('Got response for user details from Duo API - ', [$user]);
     // Check to see if a custom username was submitted
     if (isset($input['username'])) {
         //Make sure the new custom username does not have spaces
         if (preg_match('/\\s/', $input['username'])) {
             \Log::debug('The custom Duo User name has invalid spaces', [$input['username']]);
             alert()->error("The custom Duo User name has invalid spaces.")->persistent('Close');
             return redirect('duo/user/' . $id);
         }
         $user['username'] = $input['username'];
     } else {
         // No custom username supplied.
         // Implode the explode...  (Remove the space(s) from the username)
         $user['username'] = implode('', explode(' ', $user['username']));
         if ($user['username'] == $insightUser['username']) {
             // If the source and destination usernames are the same there's nothing to do.
             \Log::debug('The source and destination usernames are the same.  Nothing to do here.... - ', ['insightUser' => $insightUser['username'], 'New Username' => $user['username']]);
             alert()->error("The source and destination usernames are the same.  Nothing to do here....")->persistent('Close');
             return redirect('duo/user/' . $id);
         }
         \Log::debug('Setting new space-less username - ', [$user['username']]);
     }
     //Query the Duo API to see if the destination
     //user already exists in Duo
     $res = $duoAdmin->users($user['username']);
     //If we didn't get the user object back, let's create the new Duo user
     if (!count($res['response']['response'])) {
         \Log::debug('The new username does not currently exist in Duo.  Let\'s create the account - ', [$user['username']]);
         //Create the new Duo User
         $res = $duoAdmin->create_user($user['username'], $user['realname'], $user['email'], $user['status'], $user['notes']);
         //If the status is not OK, error and redirect
         if ($res['response']['stat'] != "OK") {
             \Log::debug('Error while creating new Duo User', [$insightUser, $user, $res]);
             alert()->error("Not able to migrate {$insightUser->realname}.  Please contact the UC-Insight Admin")->persistent('Close');
             return redirect('duo/user/' . $id);
         }
         //Our 'Add Duo User' call was successful.
         //Assign the new user to this variable
         $newDuoUser = $res['response']['response'];
         \Log::debug('Create new Duo User was successful', [$newDuoUser]);
     } else {
         // The new user account already exists in Duo.
         $newDuoUser = $res['response']['response'][0];
         \Log::debug('The new username does exist in Duo.  No need to create - ', [$newDuoUser]);
     }
     \Log::debug('Syncing Duo Phones with the new account');
     //Sync Phones to new Duo User account
     foreach ($insightUser->duoPhones()->lists('phone_id')->toArray() as $phone) {
         $res = $duoAdmin->user_associate_phone($newDuoUser['user_id'], $phone);
         //If the status is not OK, log the error
         if ($res['response']['stat'] != "OK") {
             \Log::debug('Error Associating Phone ' . $phone . ' with User ' . $newDuoUser['user_id'] . ' - ', [$res]);
             continue;
         }
         \Log::debug('Successfully associated Phone ' . $phone . ' with User ' . $newDuoUser['user_id'] . ' - ', [$res]);
     }
     //Sync Tokens to new Duo User account
     \Log::debug('Syncing Duo Tokens with the new account');
     foreach ($insightUser->duoTokens()->lists('token_id')->toArray() as $token) {
         $res = $duoAdmin->user_associate_token($newDuoUser['user_id'], $token);
         //If the status is not OK, error and redirect
         if ($res['response']['stat'] != "OK") {
             \Log::debug('Error Associating Token ' . $token . ' with User ' . $newDuoUser['user_id'] . ' - ', [$res]);
             //                alert()->error("Not able to migrate $insightUser->realname.  Please contact the UC-Insight Admin")->persistent('Close');
             continue;
         }
         \Log::debug('Successfully associated Token ' . $token . ' with User ' . $newDuoUser['user_id'] . ' - ', [$res]);
     }
     //Sync the new Duo User with UC Insight via Duo API
     $this->dispatch(new FetchDuoUsers($newDuoUser['username']));
     \Log::debug('Refreshed local DuoUser with Duo API - ', [$newDuoUser['username']]);
     alert()->success("Duo User Migration for " . $newDuoUser['realname'] . " completed");
     return redirect()->back();
 }