Search in sources :

Example 1 with SelectUserDialog

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;
}
Also used : Project(com.intellij.openapi.project.Project) SelectUserDialog(com.google.cloud.tools.intellij.resources.SelectUserDialog) AuthData(com.intellij.util.AuthData) CredentialedUser(com.google.cloud.tools.intellij.login.CredentialedUser) IOException(java.io.IOException) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

CredentialedUser (com.google.cloud.tools.intellij.login.CredentialedUser)1 SelectUserDialog (com.google.cloud.tools.intellij.resources.SelectUserDialog)1 Project (com.intellij.openapi.project.Project)1 AuthData (com.intellij.util.AuthData)1 IOException (java.io.IOException)1 Nullable (org.jetbrains.annotations.Nullable)1