Search in sources :

Example 1 with ChangePassword

use of cz.metacentrum.perun.webgui.json.usersManager.ChangePassword in project perun by CESNET.

the class PasswordResetFormPage method getContent.

/**
	 * Returns page content
	 *
	 * @return page content
	 */
public Widget getContent() {
    bodyContents.clear();
    final VerticalPanel vp = new VerticalPanel();
    vp.setSize("50%", "50%");
    vp.getElement().setAttribute("style", "margin: auto; position: relative; top: 50px;");
    bodyContents.setWidget(vp);
    // if using backup pwd-reset option, draw different content
    if (Location.getParameterMap().keySet().contains("m") && Location.getParameterMap().keySet().contains("i") && session.getRpcUrl().startsWith("/non/rpc")) {
        return drawNonAuthzPasswordReset(vp);
    }
    if (Location.getParameter("login-namespace") != null && !Location.getParameter("login-namespace").isEmpty()) {
        namespace = Location.getParameter("login-namespace");
    } else {
        namespace = "";
    }
    final Label headerLabel = new Label();
    final ExtendedPasswordBox passBox = new ExtendedPasswordBox();
    final ExtendedPasswordBox secondPassBox = new ExtendedPasswordBox();
    final ExtendedTextBox.TextBoxValidator validator;
    final ExtendedTextBox.TextBoxValidator validator2;
    final CustomButton resetPass = new CustomButton("Reset password", "Reset password in namespace: " + namespace, SmallIcons.INSTANCE.keyIcon());
    validator = new ExtendedTextBox.TextBoxValidator() {

        @Override
        public boolean validateTextBox() {
            if (passBox.getTextBox().getValue().trim().isEmpty()) {
                passBox.setError("Password can't be empty !");
                return false;
            } else if (!passBox.getTextBox().getValue().trim().equals(secondPassBox.getTextBox().getValue().trim())) {
                passBox.setError("Password in both textboxes must be the same !");
                return false;
            } else {
                passBox.setOk();
                secondPassBox.setOk();
                return true;
            }
        }
    };
    validator2 = new ExtendedTextBox.TextBoxValidator() {

        @Override
        public boolean validateTextBox() {
            if (secondPassBox.getTextBox().getValue().trim().isEmpty()) {
                secondPassBox.setError("Password can't be empty !");
                return false;
            } else if (!secondPassBox.getTextBox().getValue().trim().equals(passBox.getTextBox().getValue().trim())) {
                secondPassBox.setError("Password in both textboxes must be the same !");
                return false;
            } else {
                secondPassBox.setOk();
                passBox.setOk();
                return true;
            }
        }
    };
    passBox.setValidator(validator);
    secondPassBox.setValidator(validator2);
    FlexTable ft = new FlexTable();
    ft.setSize("300px", "100px");
    ft.addStyleName("inputFormFlexTable");
    ft.getElement().setAttribute("style", "margin: auto;");
    ft.setHTML(1, 0, "New password:");
    ft.getFlexCellFormatter().setStyleName(1, 0, "itemName");
    ft.setWidget(1, 1, passBox);
    ft.setHTML(2, 0, "Retype new password:");
    ft.getFlexCellFormatter().setStyleName(2, 0, "itemName");
    ft.setWidget(2, 1, secondPassBox);
    final FlexTable header = new FlexTable();
    header.setWidget(0, 0, new AjaxLoaderImage());
    header.setWidget(0, 1, headerLabel);
    GetLogins loginsCall = new GetLogins(session.getUser().getId(), new JsonCallbackEvents() {

        @Override
        public void onError(PerunError error) {
            bodyContents.clear();
            FlexTable ft = new FlexTable();
            ft.setSize("100%", "300px");
            ft.setHTML(0, 0, new Image(LargeIcons.INSTANCE.errorIcon()) + "<h2>Error occurred when getting your login from Perun. Please, reload page to retry.</h2><p>" + error.getErrorId() + ": " + error.getErrorInfo() + "</p>");
            ft.getFlexCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
            ft.getFlexCellFormatter().setVerticalAlignment(0, 0, HasVerticalAlignment.ALIGN_MIDDLE);
            bodyContents.setWidget(ft);
        }

        @Override
        public void onFinished(JavaScriptObject jso) {
            header.setWidget(0, 0, new Image(LargeIcons.INSTANCE.keyIcon()));
            logins = JsonUtils.jsoAsList(jso);
            if (logins != null && !logins.isEmpty()) {
                for (Attribute a : logins) {
                    // if have login in namespace
                    if (a.getFriendlyNameParameter().equals(namespace)) {
                        boolean found = false;
                        for (String name : Utils.getSupportedPasswordNamespaces()) {
                            if (a.getFriendlyNameParameter().equalsIgnoreCase(name)) {
                                found = true;
                            }
                        }
                        if (found) {
                            // HAVE LOGIN AND SUPPORTED
                            headerLabel.setText("Password reset for " + a.getValue() + "@" + namespace);
                            return;
                        } else {
                            // NOT SUPPORTED
                            bodyContents.clear();
                            FlexTable ft = new FlexTable();
                            ft.setSize("100%", "300px");
                            ft.setHTML(0, 0, new Image(LargeIcons.INSTANCE.errorIcon()) + "<h2>Password reset in selected namespace is not supported !</h2>");
                            ft.getFlexCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
                            ft.getFlexCellFormatter().setVerticalAlignment(0, 0, HasVerticalAlignment.ALIGN_MIDDLE);
                            bodyContents.setWidget(ft);
                            return;
                        }
                    }
                }
            }
            // DO NOT HAVE LOGIN IN NAMESPACE
            bodyContents.clear();
            FlexTable ft = new FlexTable();
            ft.setSize("100%", "300px");
            if (namespace != null && !namespace.isEmpty()) {
                ft.setHTML(0, 0, new Image(LargeIcons.INSTANCE.errorIcon()) + "<h2>You don't have login in selected namespace !</h2>");
            } else {
                ft.setHTML(0, 0, new Image(LargeIcons.INSTANCE.errorIcon()) + "<h2>You must specify login-namespace in URL !</h2>");
            }
            ft.getFlexCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
            ft.getFlexCellFormatter().setVerticalAlignment(0, 0, HasVerticalAlignment.ALIGN_MIDDLE);
            bodyContents.setWidget(ft);
        }
    });
    loginsCall.retrieveData();
    resetPass.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
            ChangePassword changepw = new ChangePassword(JsonCallbackEvents.disableButtonEvents(resetPass, new JsonCallbackEvents() {

                @Override
                public void onFinished(JavaScriptObject jso) {
                    bodyContents.clear();
                    FlexTable ft = new FlexTable();
                    ft.setSize("100%", "300px");
                    ft.setHTML(0, 0, new Image(LargeIcons.INSTANCE.acceptIcon()) + "<h2>Password successfully changed!</h2>");
                    ft.getFlexCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
                    ft.getFlexCellFormatter().setVerticalAlignment(0, 0, HasVerticalAlignment.ALIGN_MIDDLE);
                    ft.setHTML(1, 0, "<h2>New password will work on all resources in approx. 15 minutes after reset.</h2>");
                    ft.getFlexCellFormatter().setHorizontalAlignment(1, 0, HasHorizontalAlignment.ALIGN_CENTER);
                    ft.getFlexCellFormatter().setVerticalAlignment(1, 0, HasVerticalAlignment.ALIGN_MIDDLE);
                    bodyContents.setWidget(ft);
                }
            }), false);
            if (validator.validateTextBox() && validator2.validateTextBox()) {
                changepw.changePassword(session.getUser(), namespace, "", passBox.getTextBox().getText().trim());
            }
        }
    });
    headerLabel.setText("Password reset for ");
    headerLabel.setStyleName("now-managing");
    vp.add(header);
    vp.add(ft);
    TabMenu menu = new TabMenu();
    menu.addWidget(resetPass);
    vp.add(menu);
    vp.setCellHorizontalAlignment(menu, HasHorizontalAlignment.ALIGN_RIGHT);
    return bodyContents;
}
Also used : JsonCallbackEvents(cz.metacentrum.perun.webgui.json.JsonCallbackEvents) ChangePassword(cz.metacentrum.perun.webgui.json.usersManager.ChangePassword) GetLogins(cz.metacentrum.perun.webgui.json.attributesManager.GetLogins) Attribute(cz.metacentrum.perun.webgui.model.Attribute) ClickEvent(com.google.gwt.event.dom.client.ClickEvent) ClickHandler(com.google.gwt.event.dom.client.ClickHandler) JavaScriptObject(com.google.gwt.core.client.JavaScriptObject) CustomButton(cz.metacentrum.perun.webgui.widgets.CustomButton) PerunError(cz.metacentrum.perun.webgui.model.PerunError)

Example 2 with ChangePassword

use of cz.metacentrum.perun.webgui.json.usersManager.ChangePassword in project perun by CESNET.

the class SelfPasswordTabItem method draw.

public Widget draw() {
    String actionText = "Change";
    if (action.equals(Actions.CREATE)) {
        actionText = "Create";
    } else if (action.equals(Actions.DELETE)) {
        actionText = "Delete";
    }
    // set tab name
    this.titleWidget.setText(Utils.getStrippedStringWithEllipsis(user.getFullNameWithTitles().trim()) + ": " + actionText + " password");
    // main panel
    final VerticalPanel vp = new VerticalPanel();
    vp.setSize("100%", "100%");
    TabMenu menu = new TabMenu();
    // widgets
    final Label namespaceLabel = new Label();
    namespaceLabel.setText(namespace);
    final Label loginLabel = new Label();
    loginLabel.setText(login);
    final ExtendedPasswordBox newPass = new ExtendedPasswordBox();
    final ExtendedPasswordBox confPass = new ExtendedPasswordBox();
    final ExtendedPasswordBox oldPass = new ExtendedPasswordBox();
    final CustomButton changeButton = new CustomButton("Change password", "Changes your password in selected namespace", SmallIcons.INSTANCE.keyIcon());
    final CustomButton createButton = new CustomButton("Create password", "Creates your password in selected namespace", SmallIcons.INSTANCE.keyAddIcon());
    final CustomButton deleteButton = new CustomButton("Delete password", "Deletes your password in selected namespace", SmallIcons.INSTANCE.keyDeleteIcon());
    final TabItem tab = this;
    final ExtendedTextBox.TextBoxValidator validator = new ExtendedTextBox.TextBoxValidator() {

        @Override
        public boolean validateTextBox() {
            if (newPass.getTextBox().getValue().trim().equals("")) {
                newPass.setError("Password can't be empty!");
                return false;
            } else if (!newPass.getTextBox().getValue().trim().equals(confPass.getTextBox().getValue().trim())) {
                newPass.setError("Password in both textboxes must be the same!");
                return false;
            } else {
                newPass.setOk();
                confPass.setOk();
                return true;
            }
        }
    };
    final ExtendedTextBox.TextBoxValidator validator2 = new ExtendedTextBox.TextBoxValidator() {

        @Override
        public boolean validateTextBox() {
            if (confPass.getTextBox().getValue().trim().equals("")) {
                confPass.setError("Password can't be empty!");
                return false;
            } else if (!confPass.getTextBox().getValue().trim().equals(newPass.getTextBox().getValue().trim())) {
                confPass.setError("Password in both textboxes must be the same!");
                return false;
            } else {
                confPass.setOk();
                newPass.setOk();
                return true;
            }
        }
    };
    final ExtendedTextBox.TextBoxValidator oldValidator = new ExtendedTextBox.TextBoxValidator() {

        @Override
        public boolean validateTextBox() {
            if (oldPass.getTextBox().getValue().trim().equals("")) {
                oldPass.setError("Password can't be empty!");
                return false;
            } else {
                oldPass.setOk();
                return true;
            }
        }
    };
    newPass.setValidator(validator);
    confPass.setValidator(validator2);
    oldPass.setValidator(oldValidator);
    // save changes
    changeButton.addClickHandler(new ClickHandler() {

        public void onClick(ClickEvent event) {
            if ("mu".equals(namespace) && !JsonUtils.checkParseInt(login)) {
                UiElements.generateAlert("Operation not supported", "Password change/reset is not supported for non-numeric logins (UČO).");
            }
            if (session.isPerunAdmin() || user.isServiceUser()) {
                if (!validator.validateTextBox() && !validator2.validateTextBox())
                    return;
                ChangePassword changepw = new ChangePassword(JsonCallbackEvents.closeTabDisableButtonEvents(changeButton, tab), false);
                changepw.changePassword(user, namespace, oldPass.getTextBox().getValue().trim(), newPass.getTextBox().getValue().trim());
            } else {
                if (!validator.validateTextBox() && !validator2.validateTextBox() && !oldValidator.validateTextBox())
                    return;
                ChangePassword changepw = new ChangePassword(JsonCallbackEvents.closeTabDisableButtonEvents(changeButton, tab), true);
                changepw.changePassword(user, namespace, oldPass.getTextBox().getValue().trim(), newPass.getTextBox().getValue().trim());
            }
        }
    });
    if (user.isServiceUser()) {
        // for service users it's reset since they don't provide old password
        changeButton.setText("Reset password…");
    }
    createButton.addClickHandler(new ClickHandler() {

        public void onClick(ClickEvent clickEvent) {
            if (validator.validateTextBox() && validator2.validateTextBox()) {
                if ("mu".equals(namespace)) {
                    final GenerateAccount generateAccount = new GenerateAccount(JsonCallbackEvents.disableButtonEvents(createButton, new JsonCallbackEvents() {

                        @Override
                        public void onFinished(JavaScriptObject jso) {
                            BasicOverlayType basic = jso.cast();
                            final String login = basic.getCustomProperty("urn:perun:user:attribute-def:def:login-namespace:mu");
                            SetLogin setLogin = new SetLogin(JsonCallbackEvents.disableButtonEvents(createButton, new JsonCallbackEvents() {

                                @Override
                                public void onFinished(JavaScriptObject jso) {
                                    UiElements.generateInfo("Assigned login", "You were assigned with login <b>" + login + "</b> in namespace MU.");
                                    // VALIDATE PASSWORD - SET EXT SOURCES
                                    CreatePassword req = new CreatePassword(JsonCallbackEvents.closeTabDisableButtonEvents(createButton, tab));
                                    req.validateAndSetUserExtSources(user.getId(), login, namespace);
                                }
                            }));
                            setLogin.setLogin(user.getId(), "mu", login);
                        }
                    }));
                    final Map<String, String> params = new HashMap<String, String>();
                    GetEntityById get = new GetEntityById(PerunEntity.RICH_USER_WITH_ATTRS, user.getId(), JsonCallbackEvents.disableButtonEvents(createButton, new JsonCallbackEvents() {

                        @Override
                        public void onFinished(JavaScriptObject jso) {
                            User usr = jso.cast();
                            params.put("urn:perun:user:attribute-def:core:firstName", usr.getFirstName());
                            params.put("urn:perun:user:attribute-def:core:lastName", usr.getLastName());
                            params.put("urn:perun:member:attribute-def:def:mail", usr.getAttribute("urn:perun:user:attribute-def:def:preferredMail").getValue());
                            generateAccount.generateAccount(namespace, newPass.getTextBox().getValue().trim(), params);
                        }
                    }));
                    get.retrieveData();
                } else {
                    // NORMAL PWD LOGIC
                    CreatePassword create = new CreatePassword(JsonCallbackEvents.closeTabDisableButtonEvents(createButton, tab));
                    create.createPassword(userId, login, namespace, newPass.getTextBox().getValue().trim());
                }
            }
        }
    });
    deleteButton.addClickHandler(new ClickHandler() {

        public void onClick(ClickEvent clickEvent) {
            UiElements.generateAlert("Not yet implemented", "Not yet implemented");
        }
    });
    FlexTable layout = new FlexTable();
    layout.setStyleName("inputFormFlexTable");
    // change layout
    if (action.equals(Actions.CHANGE)) {
        int row = 0;
        layout.setHTML(row, 0, "Namespace:");
        layout.setWidget(row, 1, namespaceLabel);
        row++;
        layout.setHTML(row, 0, "Login:");
        layout.setWidget(row, 1, loginLabel);
        row++;
        // mu namespace can change without knowing old
        if (!session.isPerunAdmin()) {
            if (!user.isServiceUser()) {
                layout.setHTML(row, 0, "Old password: ");
                layout.setWidget(row, 1, oldPass);
                row++;
            }
        }
        layout.setHTML(row, 0, "New password:");
        layout.setWidget(row, 1, newPass);
        row++;
        layout.setHTML(row, 0, "Retype new pass:");
        layout.setWidget(row, 1, confPass);
        menu.addWidget(changeButton);
        vp.add(layout);
    } else if (action.equals(Actions.CREATE)) {
        layout.setHTML(0, 0, "Namespace:");
        layout.setWidget(0, 1, namespaceLabel);
        if ("mu".equals(namespace)) {
            loginLabel.setText("Will be generated...");
            loginLabel.addStyleName("inputFormInlineComment");
        }
        layout.setHTML(1, 0, "Login:");
        layout.setWidget(1, 1, loginLabel);
        layout.setHTML(2, 0, "New password:");
        layout.setWidget(2, 1, newPass);
        layout.setHTML(3, 0, "Retype new pass:");
        layout.setWidget(3, 1, confPass);
        final CustomButton skip = new CustomButton("Skip", "Will set random/empty password", SmallIcons.INSTANCE.arrowRightIcon());
        skip.addClickHandler(new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                if ("mu".equals(namespace)) {
                    final GenerateAccount generateAccount = new GenerateAccount(JsonCallbackEvents.disableButtonEvents(createButton, new JsonCallbackEvents() {

                        @Override
                        public void onFinished(JavaScriptObject jso) {
                            BasicOverlayType basic = jso.cast();
                            final String login = basic.getCustomProperty("urn:perun:user:attribute-def:def:login-namespace:mu");
                            SetLogin setLogin = new SetLogin(JsonCallbackEvents.disableButtonEvents(createButton, new JsonCallbackEvents() {

                                @Override
                                public void onFinished(JavaScriptObject jso) {
                                    UiElements.generateInfo("Assigned login", "You were assigned with login <b>" + login + "</b> in namespace MU.");
                                    // VALIDATE PASSWORD - SET EXT SOURCES
                                    CreatePassword req = new CreatePassword(JsonCallbackEvents.closeTabDisableButtonEvents(createButton, tab));
                                    req.validateAndSetUserExtSources(user.getId(), login, namespace);
                                }
                            }));
                            setLogin.setLogin(user.getId(), "mu", login);
                        }
                    }));
                    final Map<String, String> params = new HashMap<String, String>();
                    GetEntityById get = new GetEntityById(PerunEntity.RICH_USER_WITH_ATTRS, user.getId(), JsonCallbackEvents.disableButtonEvents(createButton, new JsonCallbackEvents() {

                        @Override
                        public void onFinished(JavaScriptObject jso) {
                            User usr = jso.cast();
                            params.put("urn:perun:user:attribute-def:core:firstName", usr.getFirstName());
                            params.put("urn:perun:user:attribute-def:core:lastName", usr.getLastName());
                            params.put("urn:perun:member:attribute-def:def:mail", usr.getAttribute("urn:perun:user:attribute-def:def:preferredMail").getValue());
                            generateAccount.generateAccount(namespace, newPass.getTextBox().getValue().trim(), params);
                        }
                    }));
                    get.retrieveData();
                } else {
                    CreatePassword create = new CreatePassword(JsonCallbackEvents.closeTabDisableButtonEvents(skip, tab));
                    create.createRandomPassword(userId, login, namespace);
                }
            }
        });
        menu.addWidget(skip);
        menu.addWidget(createButton);
        vp.add(layout);
    } else if (action.equals(Actions.DELETE)) {
        layout.setHTML(0, 0, "Namespace:");
        layout.setWidget(0, 1, namespaceLabel);
        layout.setHTML(1, 0, "Login:");
        layout.setWidget(1, 1, loginLabel);
        menu.addWidget(deleteButton);
        vp.add(layout);
    }
    for (int i = 0; i < layout.getRowCount(); i++) {
        layout.getFlexCellFormatter().setStyleName(i, 0, "itemName");
    }
    if (!action.equals(Actions.CREATE)) {
        menu.addWidget(TabMenu.getPredefinedButton(ButtonType.CANCEL, "", new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                session.getTabManager().closeTab(tab, false);
            }
        }));
    }
    vp.add(menu);
    vp.setCellHorizontalAlignment(menu, HasHorizontalAlignment.ALIGN_RIGHT);
    this.contentWidget.setWidget(vp);
    return getWidget();
}
Also used : ChangePassword(cz.metacentrum.perun.webgui.json.usersManager.ChangePassword) JsonCallbackEvents(cz.metacentrum.perun.webgui.json.JsonCallbackEvents) User(cz.metacentrum.perun.webgui.model.User) HashMap(java.util.HashMap) ClickEvent(com.google.gwt.event.dom.client.ClickEvent) BasicOverlayType(cz.metacentrum.perun.webgui.model.BasicOverlayType) GenerateAccount(cz.metacentrum.perun.webgui.json.usersManager.GenerateAccount) CustomButton(cz.metacentrum.perun.webgui.widgets.CustomButton) GetEntityById(cz.metacentrum.perun.webgui.json.GetEntityById) CreatePassword(cz.metacentrum.perun.webgui.json.usersManager.CreatePassword) ExtendedTextBox(cz.metacentrum.perun.webgui.widgets.ExtendedTextBox) SetLogin(cz.metacentrum.perun.webgui.json.usersManager.SetLogin) TabMenu(cz.metacentrum.perun.webgui.widgets.TabMenu) TabItem(cz.metacentrum.perun.webgui.tabs.TabItem) ClickHandler(com.google.gwt.event.dom.client.ClickHandler) JavaScriptObject(com.google.gwt.core.client.JavaScriptObject) HashMap(java.util.HashMap) Map(java.util.Map) ExtendedPasswordBox(cz.metacentrum.perun.webgui.widgets.ExtendedPasswordBox)

Aggregations

JavaScriptObject (com.google.gwt.core.client.JavaScriptObject)2 ClickEvent (com.google.gwt.event.dom.client.ClickEvent)2 ClickHandler (com.google.gwt.event.dom.client.ClickHandler)2 JsonCallbackEvents (cz.metacentrum.perun.webgui.json.JsonCallbackEvents)2 ChangePassword (cz.metacentrum.perun.webgui.json.usersManager.ChangePassword)2 CustomButton (cz.metacentrum.perun.webgui.widgets.CustomButton)2 GetEntityById (cz.metacentrum.perun.webgui.json.GetEntityById)1 GetLogins (cz.metacentrum.perun.webgui.json.attributesManager.GetLogins)1 CreatePassword (cz.metacentrum.perun.webgui.json.usersManager.CreatePassword)1 GenerateAccount (cz.metacentrum.perun.webgui.json.usersManager.GenerateAccount)1 SetLogin (cz.metacentrum.perun.webgui.json.usersManager.SetLogin)1 Attribute (cz.metacentrum.perun.webgui.model.Attribute)1 BasicOverlayType (cz.metacentrum.perun.webgui.model.BasicOverlayType)1 PerunError (cz.metacentrum.perun.webgui.model.PerunError)1 User (cz.metacentrum.perun.webgui.model.User)1 TabItem (cz.metacentrum.perun.webgui.tabs.TabItem)1 ExtendedPasswordBox (cz.metacentrum.perun.webgui.widgets.ExtendedPasswordBox)1 ExtendedTextBox (cz.metacentrum.perun.webgui.widgets.ExtendedTextBox)1 TabMenu (cz.metacentrum.perun.webgui.widgets.TabMenu)1 HashMap (java.util.HashMap)1