Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
120 views
in Technique[技术] by (71.8m points)

google api - Why are my if statements not working properly in Laravel?

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":"[email protected]",
   "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":"[email protected]",
         "primary":true
      },
      {
         "address":"[email protected]"
      }
   ],
   "nonEditableAliases":[
      "[email protected]"
   ],
   "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:

[email protected]

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?

question from:https://stackoverflow.com/questions/65944049/why-are-my-if-statements-not-working-properly-in-laravel

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...