use of com.google.cloud.tools.intellij.resources.SelectUserDialog in project google-cloud-intellij by GoogleCloudPlatform.
the class GcpHttpAuthDataProvider method getAuthData.
@Nullable
@Override
public AuthData getAuthData(@NotNull String url) {
final Project currentProject = getCurrentProject();
if ((currentProject != null || Context.currentContext != null) && isGcpUrl(url)) {
Context currentContext = // always prefer context over project setting.
Context.currentContext;
String userEmail = currentContext != null ? currentContext.userName : null;
if (Strings.isNullOrEmpty(userEmail) && currentProject != null) {
userEmail = PropertiesComponent.getInstance(currentProject).getValue(GCP_USER, "");
}
CredentialedUser targetUser = getUserFromEmail(userEmail);
if (targetUser == null) {
// show a dialog allowing the user to select a login. (new project recognized)
ApplicationManager.getApplication().invokeAndWait(new Runnable() {
@Override
public void run() {
SelectUserDialog dialog = new SelectUserDialog(currentProject, GctBundle.getString("httpauthprovider.chooselogin"));
DialogManager.show(dialog);
chooseManualLogin = !dialog.isOK();
selectedUser = dialog.getSelectedUser();
}
}, ModalityState.defaultModalityState());
if (chooseManualLogin) {
return null;
}
userEmail = selectedUser;
targetUser = getUserFromEmail(userEmail);
if (targetUser != null && currentProject != null && Context.currentContext == null) {
PropertiesComponent.getInstance(currentProject).setValue(GCP_USER, userEmail);
}
}
if (targetUser != null) {
try {
return new AuthData(targetUser.getEmail(), targetUser.getGoogleLoginState().fetchAccessToken());
} catch (IOException ex) {
LOG.error("IOException creating authdata:" + ex.toString());
}
}
}
return null;
}
Aggregations