Search in sources :

Example 1 with PasswordResetFormPage

use of cz.metacentrum.perun.webgui.client.passwordresetresources.PasswordResetFormPage in project perun by CESNET.

the class PasswordResetGui method loadPerunPrincipal.

/**
	 * Performs a login into the RPC, loads user and his roles into session and enables GUI.
	 */
private void loadPerunPrincipal() {
    // show loading box
    final PopupPanel loadingBox = session.getUiElements().perunLoadingBox();
    loadingBox.show();
    // events after getting PerunPrincipal from RPC
    final JsonCallbackEvents events = new JsonCallbackEvents() {

        @Override
        public void onFinished(JavaScriptObject jso) {
            // store perun principal into session for future use
            PerunPrincipal pp = (PerunPrincipal) jso;
            session.setPerunPrincipal(pp);
            // check if user exists
            if (session.getUser() != null && !pp.getRoles().hasAnyRole() && !session.getRpcUrl().startsWith("/non/rpc")) {
                // if not and no role, redraw page body
                RootLayoutPanel body = RootLayoutPanel.get();
                loadingBox.hide();
                body.clear();
                body.add(new NotUserOfPerunWidget());
                return;
            }
            if (session.getUser() != null && !pp.getRoles().hasAnyRole()) {
                // store users roles and editable entities into session
                session.setRoles(pp.getRoles());
                // display logged user
                session.getUiElements().setLoggedUserInfo(pp);
            }
            GetGuiConfiguration getConf = new GetGuiConfiguration(new JsonCallbackEvents() {

                @Override
                public void onFinished(JavaScriptObject jso) {
                    session.setConfiguration((BasicOverlayType) jso.cast());
                    // hides the loading box
                    loadingBox.hide();
                    if (session.getRpcUrl().startsWith("/non/rpc")) {
                        // CHALLENGE WITH CAPTCHA
                        FlexTable ft = new FlexTable();
                        ft.setSize("100%", "500px");
                        // captcha with public key
                        String key = Utils.getReCaptchaPublicKey();
                        if (key == null) {
                            PerunError error = new JSONObject().getJavaScriptObject().cast();
                            error.setErrorId("0");
                            error.setName("Missing public key");
                            error.setErrorInfo("Public key for Re-Captcha service is missing. Please add public key to GUIs configuration file.");
                            error.setRequestURL("");
                            UiElements.generateError(error, "Missing public key", "Public key for Re-Captcha service is missing.<br />Accessing password reset without authorization is not possible.");
                            loadingBox.hide();
                            return;
                        }
                        final RecaptchaWidget captcha = new RecaptchaWidget(key, LocaleInfo.getCurrentLocale().getLocaleName(), "clean");
                        final CustomButton cb = new CustomButton();
                        cb.setIcon(SmallIcons.INSTANCE.arrowRightIcon());
                        cb.setText(ApplicationMessages.INSTANCE.captchaSendButton());
                        cb.setImageAlign(true);
                        final TextBox response = new TextBox();
                        captcha.setOwnTextBox(response);
                        Scheduler.get().scheduleDeferred(new Command() {

                            @Override
                            public void execute() {
                                response.setFocus(true);
                            }
                        });
                        response.addKeyDownHandler(new KeyDownHandler() {

                            @Override
                            public void onKeyDown(KeyDownEvent event) {
                                if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
                                    cb.click();
                                }
                            }
                        });
                        cb.addClickHandler(new ClickHandler() {

                            @Override
                            public void onClick(ClickEvent clickEvent) {
                                VerifyCaptcha req = new VerifyCaptcha(captcha.getChallenge(), captcha.getResponse(), JsonCallbackEvents.disableButtonEvents(cb, new JsonCallbackEvents() {

                                    public void onFinished(JavaScriptObject jso) {
                                        BasicOverlayType bt = jso.cast();
                                        if (bt.getBoolean()) {
                                            // OK captcha answer - load GUI
                                            // add menu item and load content
                                            contentPanel.setWidget(new PasswordResetFormPage().getContent());
                                        //Anchor a = leftMenu.addMenuContents("Password reset", SmallIcons.INSTANCE.keyIcon(), new PasswordResetFormPage().getContent());
                                        //a.fireEvent(new ClickEvent(){});
                                        } else {
                                            // wrong captcha answer
                                            UiElements.generateAlert(ApplicationMessages.INSTANCE.captchaErrorHeader(), ApplicationMessages.INSTANCE.captchaErrorMessage());
                                        }
                                    }
                                }));
                                req.retrieveData();
                            }
                        });
                        // set layout
                        int row = 0;
                        ft.getFlexCellFormatter().setAlignment(row, 0, HasHorizontalAlignment.ALIGN_CENTER, HasVerticalAlignment.ALIGN_MIDDLE);
                        ft.setHTML(row, 0, "<h2>In order to continue to the password reset page, please, use CAPTCHA below.</h2>");
                        ft.setWidget(row + 1, 0, captcha);
                        ft.getFlexCellFormatter().setHorizontalAlignment(row + 1, 0, HasHorizontalAlignment.ALIGN_CENTER);
                        ft.getFlexCellFormatter().setVerticalAlignment(row + 1, 0, HasVerticalAlignment.ALIGN_BOTTOM);
                        FlexTable sendFt = new FlexTable();
                        sendFt.setStyleName("inputFormFlexTable");
                        sendFt.setWidget(0, 0, response);
                        sendFt.setWidget(0, 1, cb);
                        ft.setWidget(row + 2, 0, sendFt);
                        ft.getFlexCellFormatter().setHorizontalAlignment(row + 2, 0, HasHorizontalAlignment.ALIGN_CENTER);
                        ft.getFlexCellFormatter().setVerticalAlignment(row + 2, 0, HasVerticalAlignment.ALIGN_TOP);
                        ft.setHeight("100%");
                        ft.getFlexCellFormatter().setHeight(row, 0, "50%");
                        ft.getFlexCellFormatter().setHeight(row + 2, 0, "50%");
                        // finish loading GUI
                        loadingBox.hide();
                        contentPanel.setWidget(ft);
                    } else {
                        // add menu item and load content
                        contentPanel.setWidget(new PasswordResetFormPage().getContent());
                    //Anchor a = leftMenu.addMenuContents("Password reset", SmallIcons.INSTANCE.keyIcon(), new PasswordResetFormPage().getContent());
                    //a.fireEvent(new ClickEvent(){});
                    }
                }

                @Override
                public void onError(PerunError error) {
                    // hides the loading box
                    loadingBox.hide();
                    // shows error box
                    PopupPanel loadingFailedBox;
                    if (error == null) {
                        loadingFailedBox = session.getUiElements().perunLoadingFailedBox("Request timeout exceeded.");
                    } else {
                        if (error.getName().contains("UserNotExistsException")) {
                            loadingFailedBox = session.getUiElements().perunLoadingFailedBox("You are not registered to any Virtual Organization.</br></br>" + error.getErrorInfo());
                        } else {
                            loadingFailedBox = session.getUiElements().perunLoadingFailedBox(error.getErrorInfo());
                        }
                    }
                    loadingFailedBox.show();
                    leftMenu.addItem("Password reset", SmallIcons.INSTANCE.keyIcon(), null);
                }
            });
            getConf.retrieveData();
        }

        @Override
        public void onError(PerunError error) {
            // hides the loading box
            loadingBox.hide();
            // shows error box
            PopupPanel loadingFailedBox;
            if (error == null) {
                loadingFailedBox = session.getUiElements().perunLoadingFailedBox("Request timeout exceeded.");
            } else {
                if (error.getName().contains("UserNotExistsException")) {
                    loadingFailedBox = session.getUiElements().perunLoadingFailedBox("You are not registered to any Virtual Organization.</br></br>" + error.getErrorInfo());
                } else {
                    loadingFailedBox = session.getUiElements().perunLoadingFailedBox(error.getErrorInfo());
                }
            }
            loadingFailedBox.show();
            leftMenu.addItem("Password reset", SmallIcons.INSTANCE.keyIcon(), null);
        }
    };
    GetPerunPrincipal loggedUserRequest = new GetPerunPrincipal(events);
    loggedUserRequest.retrieveData();
}
Also used : JsonCallbackEvents(cz.metacentrum.perun.webgui.json.JsonCallbackEvents) BasicOverlayType(cz.metacentrum.perun.webgui.model.BasicOverlayType) VerifyCaptcha(cz.metacentrum.perun.webgui.json.registrarManager.VerifyCaptcha) JSONObject(com.google.gwt.json.client.JSONObject) Command(com.google.gwt.user.client.Command) CustomButton(cz.metacentrum.perun.webgui.widgets.CustomButton) RecaptchaWidget(cz.metacentrum.perun.webgui.widgets.recaptcha.RecaptchaWidget) GetPerunPrincipal(cz.metacentrum.perun.webgui.json.authzResolver.GetPerunPrincipal) PerunPrincipal(cz.metacentrum.perun.webgui.model.PerunPrincipal) GetPerunPrincipal(cz.metacentrum.perun.webgui.json.authzResolver.GetPerunPrincipal) PerunError(cz.metacentrum.perun.webgui.model.PerunError) PasswordResetFormPage(cz.metacentrum.perun.webgui.client.passwordresetresources.PasswordResetFormPage) GetGuiConfiguration(cz.metacentrum.perun.webgui.json.GetGuiConfiguration)

Aggregations

JSONObject (com.google.gwt.json.client.JSONObject)1 Command (com.google.gwt.user.client.Command)1 PasswordResetFormPage (cz.metacentrum.perun.webgui.client.passwordresetresources.PasswordResetFormPage)1 GetGuiConfiguration (cz.metacentrum.perun.webgui.json.GetGuiConfiguration)1 JsonCallbackEvents (cz.metacentrum.perun.webgui.json.JsonCallbackEvents)1 GetPerunPrincipal (cz.metacentrum.perun.webgui.json.authzResolver.GetPerunPrincipal)1 VerifyCaptcha (cz.metacentrum.perun.webgui.json.registrarManager.VerifyCaptcha)1 BasicOverlayType (cz.metacentrum.perun.webgui.model.BasicOverlayType)1 PerunError (cz.metacentrum.perun.webgui.model.PerunError)1 PerunPrincipal (cz.metacentrum.perun.webgui.model.PerunPrincipal)1 CustomButton (cz.metacentrum.perun.webgui.widgets.CustomButton)1 RecaptchaWidget (cz.metacentrum.perun.webgui.widgets.recaptcha.RecaptchaWidget)1