问题描述
首先,我调用 API 以返回域中的所有电子邮件。我不会费心发布 API 调用,因为它是功能性的。但这就是我对响应所做的:
$resJson = json_decode($response,TRUE);
$exclusions = DB::table('exclusion')->get();
foreach($exclusions as $exclusion) {
$exc[]=$exclusion->email;
}
我对它进行 json 解码,然后从数据库中导入排除的电子邮件列表,在下面的第一个 if 语句中,我根据我不想显示的电子邮件的排除数组进行检查。 >
然后我回应以下内容。
<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>
上面的代码是从 Google ADMIN SDK JSON 响应中提取的。 JSON 响应示例如下所示:
{
"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":""
}
问题是,有时 if 语句会起作用并且输出正确的形式,有时会失败。我看到当正确的按钮确实出现时,它完全按照我的控制器执行了它应该做的事情,所以我不会费心在这里发布代码,因为它只是一个 API 调用。
让我们以电子邮件为例:
我在 JSON 响应中看到此电子邮件已暂停。因此,根据代码中的 if 语句,它应该显示“SUSPENDED”以及暂停原因,以及显示“Restore Account”的 btn-success。但是,它显示的与 HTML 中的完全相反。我认为这可能是缓存问题,但事实并非如此。有什么想法吗?
解决方法
在这上面花了几个小时后,我弄清楚了 if 语句不起作用的原因。该问题实际上与 PHP 或 Laravel 完全无关,而是 Google API 本身的问题。
暂停/恢复是同一个API调用,参数不同,而拉取一个域中的用户数据实际上是另一个API调用。虽然 API 中的暂停和恢复功能是即时的,但第二个 API 需要一两分钟才能真正更新用户的状态。
删除 Laravel/PHP 标签并在此问题中添加 Google 标签,以防其他人遇到类似问题。