0

First I make an API call to return all emails in a domain. I won't bother posting the API call as it is functional. But this is what I do with the response:

    $resJson = json_decode($response, TRUE);

    $exclusions = DB::table('exclusion')->get();
    foreach($exclusions as $exclusion) {
      $exc[]=$exclusion->email;
    }

I json decode it, then import a list of excluded emails from the database, In the first if statement below, I check it against array of exclusions for emails I do not want displayed.

Then I echo the following.

  <div class="table-responsive">
      <table class="table">
          <thead class="text-primary">
              <th><strong>Users</strong></th><th><strong>Creation Date</strong></th><th><strong>Last Login</strong></th><th><strong>Status</strong></th><th><strong>Suspension Reason</strong></th><th style="text-align: center;"><strong>Action</strong></th>
          </thead>
          <tbody>
            @php
            foreach($resJson["users"] as $user) {
              if(!in_array($user['primaryEmail'], $exc))
              {
                if($user['suspended'] == false)
                {
                  $susEnable = '<form name="suspend" style="text-align: center;" action="/customer/apps/gsuite/suspend" method="POST"><input type="hidden" name="_token" value="'.csrf_token().'"/><input type="hidden" name="email" value="'.$user['primaryEmail'].'"/><button class="btn btn-danger">Suspend Account</button></form>';
                  $status = 'Active';
                  $reason = '';
                }
                elseif($user['suspended'] == true)
                {
                  $susEnable = '<form name="enable" style="text-align: center;" action="/customer/apps/gsuite/restore" method="POST"><input type="hidden" name="_token" value="'.csrf_token().'"/><input type="hidden" name="email2" value="'.$user['primaryEmail'].'"/><button class="btn btn-fill btn-success">Restore Account</button></form>';
                  $status = 'Suspended';
                  $reason = $user['suspensionReason'];
                }
                if($user['lastLoginTime']=="1970-01-01T00:00:00.000Z")
                {
                  $lastLogin = 'Never';
                } else {
                  $lastLogin = str_replace('T',' // ', $user['lastLoginTime']);
                  $lastLogin = str_replace('Z','', $lastLogin);
                }
                $creationTime = str_replace('T',' // ', $user['creationTime']);
                $creationTime = str_replace('Z','', $creationTime);
                    echo '<tr><td style="line-height: 5; color: white;">' . $user['primaryEmail'] . '</td><td style="line-height: 5; color: white;">' . $creationTime . '</td><td style="line-height: 5; color: white;">' . $lastLogin . '</td><td style="color: white;">'.$status.'</td><td style="color: white;">'.$reason.'</td><td>'.$susEnable.'</td></tr>';
                    
              }
            }
            @endphp
          </tbody>
      </table>
  </div>

the code above is pulling from a Google ADMIN SDK JSON Response. A sample JSON response can be seen below:

{
   "kind":"admin#directory#user",
   "id":"115906813143010867543",
   "etag":"",
   "primaryEmail":"amr@domain.com",
   "name":{
      "givenName":"amr",
      "familyName":"h",
      "fullName":"amr h"
   },
   "isAdmin":false,
   "isDelegatedAdmin":false,
   "lastLoginTime":"1970-01-01T00:00:00.000Z",
   "creationTime":"2021-01-11T20:46:25.000Z",
   "agreedToTerms":false,
   "suspended":true,
   "suspensionReason":"ADMIN",
   "archived":false,
   "changePasswordAtNextLogin":true,
   "ipWhitelisted":false,
   "emails":[
      {
         "address":"amr@domain.com",
         "primary":true
      },
      {
         "address":"amr@domain.com.test-google-a.com"
      }
   ],
   "nonEditableAliases":[
      "amr@domain.com.test-google-a.com"
   ],
   "customerId":"C04357r1m",
   "orgUnitPath":"/",
   "isMailboxSetup":true,
   "includeInGlobalAddressList":true,
   "recoveryEmail":""
}

The thing is, sometimes the if statements will work and the correct form is outputted, sometimes it fails. I see that when the correct button does show up, it does exactly what it's supposed to as per my controller, so I won't bother posting the code for that here as it is simply an API call.

Let's take an example email:

first@domain.com

I see in the JSON response that this email is suspended. Therefore, based on the if statements in the code, it should show "SUSPENDED" as well as suspension reason, as well as a btn-success that says "Restore Account." However, it shows the exact opposite of that in HTML. I thought it might be a cache issue, but it wasn't. Any thoughts?

Amr H
  • 58
  • 1
  • 9
  • Unable to reproduce on a [simplified example](https://3v4l.org/erIcg). You're going to have to further investigate on this "sometimes" aspect. – El_Vanja Jan 28 '21 at 19:47
  • Yep. This isn't strictly a PHP problem, the if statements are fine. It seems to be more of a Laravel "bug." Just now I found this: if I refresh the page a few times, it renders the correct button. But that's not how it's supposed to function, so it must be some sort of cacheing problem--not a browser cache issue but rather a Laravel cache issue – Amr H Jan 28 '21 at 19:50
  • When you get a wrong result, do you inspect the html? Does the wrong button have the correct data? Meaning it's referring to the correct email. – El_Vanja Jan 28 '21 at 19:54
  • I inspected, it has the incorrect data. I guess it makes sense because every time you make a change to a view you have to run php artisan optimize, whereas here I am changing the output dynamically based on the JSON response. But that's no solution to realtime dynamic applications. I guess I have to find a workaround for this. – Amr H Jan 28 '21 at 19:55
  • Do you have this issue in production or just in dev? – El_Vanja Jan 28 '21 at 19:57
  • Just dev for now, i haven't tested production – Amr H Jan 28 '21 at 19:57
  • 1
    See if manually clearing cache between each attempt would still produce the error (though I doubt it). I suspect it won't happen in prod. – El_Vanja Jan 28 '21 at 20:00

1 Answers1

0

After spending a few hours on this, I figured out why the if statement wasn't working. The issue is actually completely unrelated to PHP or laravel, but rather a problem in the Google API itself.

Suspension/restoring are the same API call with different parameters, while pulling user data in a domain is actually another API call. While the suspend and restore functionalities in the API are instantaneous, it takes the second API a minute or two to actually update the status of the users.

Removing the Laravel/PHP tags and adding the Google tag to this question in case anyone else faces a similar issue.

Amr H
  • 58
  • 1
  • 9