Search in sources :

Example 1 with GenerateAccount

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

the class CreateServiceMemberInVoTabItem method draw.

public Widget draw() {
    titleWidget.setText("Create service member");
    final TabItem tab = this;
    // draw the main tab
    final VerticalPanel mainTab = new VerticalPanel();
    mainTab.setSize("100%", "100%");
    final ExtendedTextBox serviceUserName = new ExtendedTextBox();
    final ExtendedTextBox serviceUserEmail = new ExtendedTextBox();
    final ExtendedTextBox serviceUserLogin = new ExtendedTextBox();
    final ExtendedPasswordBox serviceUserPassword = new ExtendedPasswordBox();
    final ExtendedPasswordBox serviceUserPassword2 = new ExtendedPasswordBox();
    final ListBox namespace = new ListBox();
    final ExtendedTextBox certDN = new ExtendedTextBox();
    final ExtendedTextBox cacertDN = new ExtendedTextBox();
    final ListBox userType = new ListBox();
    userType.addItem("Service", "SERVICE");
    userType.addItem("Sponsored", "SPONSORED");
    serviceUserPassword.getTextBox().setWidth("200px");
    serviceUserPassword2.getTextBox().setWidth("200px");
    final ExtendedTextBox.TextBoxValidator nameValidator = new ExtendedTextBox.TextBoxValidator() {

        @Override
        public boolean validateTextBox() {
            if (serviceUserName.getTextBox().getValue().trim().isEmpty()) {
                serviceUserName.setError("Name can't be empty!");
                return false;
            }
            serviceUserName.setOk();
            return true;
        }
    };
    serviceUserName.setValidator(nameValidator);
    final ExtendedTextBox.TextBoxValidator loginValidator = new ExtendedTextBox.TextBoxValidator() {

        @Override
        public boolean validateTextBox() {
            if (namespace.getSelectedIndex() == 0) {
                // do not validate if namespace is not selected
                serviceUserLogin.getTextBox().setValue(null);
                serviceUserLogin.setOk();
                return true;
            }
            if (serviceUserLogin.getTextBox().getValue().trim().isEmpty()) {
                serviceUserLogin.setError("Login can't be empty!");
                return false;
            }
            RegExp regExp = RegExp.compile(Utils.LOGIN_VALUE_MATCHER);
            boolean match = regExp.test(serviceUserLogin.getTextBox().getValue().trim());
            if (!match) {
                serviceUserLogin.setError("Invalid format!");
                return false;
            }
            if (serviceUserLogin.isProcessing() || serviceUserLogin.isHardError()) {
                // keep original message
                return false;
            }
            serviceUserLogin.setOk();
            return true;
        }
    };
    serviceUserLogin.setValidator(loginValidator);
    final ExtendedTextBox.TextBoxValidator emailValidator = new ExtendedTextBox.TextBoxValidator() {

        @Override
        public boolean validateTextBox() {
            if (!JsonUtils.isValidEmail(serviceUserEmail.getTextBox().getValue().trim())) {
                serviceUserEmail.setError("Wrong email format!");
                return false;
            }
            serviceUserEmail.setOk();
            return true;
        }
    };
    serviceUserEmail.setValidator(emailValidator);
    final ExtendedTextBox.TextBoxValidator validator = new ExtendedTextBox.TextBoxValidator() {

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

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

        @Override
        public boolean validateTextBox() {
            if ((certDN.getTextBox().getValue().trim().isEmpty() && cacertDN.getTextBox().getValue().trim().isEmpty()) || (!certDN.getTextBox().getValue().trim().isEmpty() && !cacertDN.getTextBox().getValue().trim().isEmpty())) {
                certDN.setOk();
                cacertDN.setOk();
                return true;
            } else {
                if (certDN.getTextBox().getValue().trim().isEmpty()) {
                    certDN.setError("Value can't be empty!");
                }
                if (cacertDN.getTextBox().getValue().trim().isEmpty()) {
                    cacertDN.setError("Value can't be empty!");
                }
            }
            return false;
        }
    };
    certDN.setValidator(certDNValidator);
    cacertDN.setValidator(certDNValidator);
    // make value empty
    namespace.addItem("Not selected", "");
    for (String name : Utils.getSupportedPasswordNamespaces()) {
        namespace.addItem(name.toUpperCase(), name);
    }
    final FlexTable layout = new FlexTable();
    layout.setCellPadding(5);
    layout.setHTML(0, 0, "<h3>1. Create service identity</h3>");
    layout.getFlexCellFormatter().setColSpan(0, 0, 3);
    layout.setHTML(1, 0, "<strong>Member's name: </strong>");
    layout.setWidget(1, 1, serviceUserName);
    layout.setHTML(2, 0, "<strong>Member's email: </strong>");
    layout.setWidget(2, 1, serviceUserEmail);
    layout.setHTML(3, 0, "<strong>Namespace: </strong>");
    layout.setWidget(3, 1, namespace);
    layout.setHTML(4, 0, "<strong>Login: </strong>");
    layout.setWidget(4, 1, serviceUserLogin);
    final Label label = new Label("You will be assigned with available login");
    label.setVisible(false);
    layout.setWidget(5, 0, label);
    layout.getFlexCellFormatter().setStyleName(5, 0, "inputFormInlineComment");
    layout.getFlexCellFormatter().setColSpan(5, 0, 2);
    layout.setHTML(6, 0, "<strong>Subject DN: </strong>");
    layout.setWidget(6, 1, certDN);
    layout.setHTML(7, 0, "<strong>Issuer DN: </strong>");
    layout.setWidget(7, 1, cacertDN);
    if (session.isPerunAdmin()) {
        layout.setHTML(8, 0, "<strong>User type: </strong>");
        layout.setWidget(8, 1, userType);
    }
    final FlexTable firstTabLayout = new FlexTable();
    firstTabLayout.setSize("100%", "100%");
    firstTabLayout.setVisible(false);
    final AddRemoveItemsTable<User> itemsTable = new AddRemoveItemsTable<User>(true);
    itemsTable.addItem(session.getUser());
    final VerticalPanel secondTabPanel = new VerticalPanel();
    secondTabPanel.setSize("100%", "100%");
    secondTabPanel.setVisible(false);
    mainTab.add(layout);
    mainTab.add(firstTabLayout);
    mainTab.add(secondTabPanel);
    // disable login by default (first option)
    serviceUserLogin.getTextBox().setEnabled(false);
    final CustomButton cb = new CustomButton("Continue", SmallIcons.INSTANCE.addIcon(), new ClickHandler() {

        public void onClick(ClickEvent clickEvent) {
            // check
            if (!namespace.getSelectedValue().equals("mu") && namespace.getSelectedIndex() != 0 && !loginValidator.validateTextBox())
                return;
            if (!nameValidator.validateTextBox())
                return;
            if (!emailValidator.validateTextBox())
                return;
            if (!certDNValidator.validateTextBox())
                return;
            // change to lager tab
            session.getTabManager().changeStyleOfInnerTab(true);
            // first tab panel
            firstTabLayout.setHTML(0, 0, "<h3>2. Associate real users</h3>");
            firstTabLayout.getFlexCellFormatter().setColSpan(0, 0, 2);
            layout.setVisible(false);
            firstTabLayout.setVisible(true);
            final FindUsersInVo callback = new FindUsersInVo();
            // Service users can't own another Service or Guest (Sponsored) account.
            callback.hideService(true);
            if (userType.getSelectedValue().equals("SPONSORED")) {
                // Sponsored account can't sponsor another !
                callback.setHideSponsored(true);
            }
            // HORIZONTAL MENU
            TabMenu tabMenu = new TabMenu();
            // get the table
            final CellTable<User> table = callback.getTable();
            // search textbox
            ExtendedTextBox searchBox = tabMenu.addSearchWidget(new PerunSearchEvent() {

                @Override
                public void searchFor(String text) {
                    callback.searchFor(text, voId);
                    searchString = text;
                }
            }, ButtonTranslation.INSTANCE.searchUsers());
            final CustomButton cb = new CustomButton("Continue", SmallIcons.INSTANCE.arrowRightIcon());
            cb.addClickHandler(new ClickHandler() {

                public void onClick(ClickEvent clickEvent) {
                    // check
                    if (itemsTable.getList().isEmpty()) {
                        new Confirm("No user associated", new HTML("You must associate at least one real user to service member."), true).show();
                        return;
                    }
                    // create member + user
                    CreateSpecificMember request = new CreateSpecificMember(JsonCallbackEvents.disableButtonEvents(cb, new JsonCallbackEvents() {

                        public void onFinished(JavaScriptObject jso) {
                            final Member member = jso.cast();
                            for (User u : itemsTable.getList()) {
                                if (u.getId() == session.getUser().getId()) {
                                    // set local authz if one of associated users is us
                                    session.addEditableUser(member.getUserId());
                                }
                            }
                            if (namespace.getSelectedIndex() == 0) {
                                // we didn't set login, hence skip password setting
                                session.getTabManager().closeTab(tab, true);
                                return;
                            }
                            // change to small tab
                            session.getTabManager().changeStyleOfInnerTab(false);
                            secondTabPanel.add(new HTML("<h3>3.Set password for: " + serviceUserLogin.getTextBox().getValue().trim() + "</h3>"));
                            final CustomButton button = new CustomButton("Set password", SmallIcons.INSTANCE.keyIcon());
                            button.addClickHandler(new ClickHandler() {

                                public void onClick(ClickEvent clickEvent) {
                                    if (!validator.validateTextBox() && !validator2.validateTextBox()) {
                                        return;
                                    }
                                    final String namespaceValue = namespace.getSelectedValue();
                                    if ("mu".equals(namespaceValue)) {
                                        final GenerateAccount req = new GenerateAccount(JsonCallbackEvents.disableButtonEvents(button, new JsonCallbackEvents() {

                                            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(button, new JsonCallbackEvents() {

                                                    @Override
                                                    public void onFinished(JavaScriptObject jso) {
                                                        // VALIDATE PASSWORD - SET EXT SOURCES AND VALIDATE MEMBER
                                                        CreatePassword req = new CreatePassword(JsonCallbackEvents.disableButtonEvents(button, new JsonCallbackEvents() {

                                                            @Override
                                                            public void onFinished(JavaScriptObject jso) {
                                                                // validate member when all kerberos logins are set
                                                                ValidateMemberAsync req2 = new ValidateMemberAsync(JsonCallbackEvents.closeTabDisableButtonEvents(button, tab));
                                                                req2.validateMemberAsync(member);
                                                            }
                                                        }));
                                                        // validate login returned from account generation
                                                        req.validateAndSetUserExtSources(member.getUserId(), login, namespaceValue);
                                                        // show assigned login
                                                        UiElements.generateInfo("Assigned login", "You were assigned with login <b>" + login + "</b> in namespace MU.");
                                                    }

                                                    @Override
                                                    public void onError(PerunError error) {
                                                        UiElements.generateError(error, "Saving login failed", "You were assigned with login <b>" + login + "</b> in namespace MU but saving it to user failed. <b>Please copy your login and contact support at <a href=\"mailto:" + Utils.perunReportEmailAddress() + "\">" + Utils.perunReportEmailAddress() + "</a>.</b>");
                                                        // validate member when all logins are set
                                                        ValidateMemberAsync req2 = new ValidateMemberAsync(JsonCallbackEvents.closeTabDisableButtonEvents(button, tab));
                                                        req2.validateMemberAsync(member);
                                                    }
                                                }));
                                                setLogin.setLogin(member.getUserId(), "mu", login);
                                            }
                                        }));
                                        final Map<String, String> params = new HashMap<String, String>();
                                        GetEntityById get = new GetEntityById(PerunEntity.RICH_MEMBER, member.getId(), JsonCallbackEvents.disableButtonEvents(button, new JsonCallbackEvents() {

                                            @Override
                                            public void onFinished(JavaScriptObject jso) {
                                                RichMember rm = jso.cast();
                                                params.put("urn:perun:user:attribute-def:core:firstName", rm.getUser().getFirstName());
                                                params.put("urn:perun:user:attribute-def:core:lastName", rm.getUser().getLastName());
                                                params.put("urn:perun:member:attribute-def:def:mail", serviceUserEmail.getTextBox().getValue().trim());
                                                req.generateAccount(namespaceValue, serviceUserPassword.getTextBox().getValue().trim(), params);
                                            }
                                        }));
                                        get.retrieveData();
                                    } else {
                                        // create password which sets also user ext sources
                                        CreatePassword req = new CreatePassword(JsonCallbackEvents.disableButtonEvents(button, new JsonCallbackEvents() {

                                            public void onFinished(JavaScriptObject jso) {
                                                // validate member when all kerberos logins are set
                                                ValidateMemberAsync req2 = new ValidateMemberAsync(JsonCallbackEvents.closeTabDisableButtonEvents(button, tab));
                                                req2.validateMemberAsync(member);
                                            }
                                        }));
                                        req.createPassword(member.getUserId(), serviceUserLogin.getTextBox().getValue().trim(), namespaceValue, serviceUserPassword.getTextBox().getValue().trim());
                                    }
                                }
                            });
                            final CustomButton skipButton = new CustomButton("Skip", SmallIcons.INSTANCE.arrowRightIcon());
                            skipButton.addClickHandler(new ClickHandler() {

                                public void onClick(ClickEvent clickEvent) {
                                    final String namespaceValue = namespace.getSelectedValue();
                                    if ("mu".equals(namespaceValue)) {
                                        final GenerateAccount req = new GenerateAccount(JsonCallbackEvents.disableButtonEvents(button, 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(button, new JsonCallbackEvents() {

                                                    @Override
                                                    public void onFinished(JavaScriptObject jso) {
                                                        // VALIDATE PASSWORD - SET EXT SOURCES AND VALIDATE MEMBER
                                                        CreatePassword req = new CreatePassword(JsonCallbackEvents.disableButtonEvents(button, new JsonCallbackEvents() {

                                                            @Override
                                                            public void onFinished(JavaScriptObject jso) {
                                                                // validate member when all kerberos logins are set
                                                                ValidateMemberAsync req2 = new ValidateMemberAsync(JsonCallbackEvents.closeTabDisableButtonEvents(button, tab));
                                                                req2.validateMemberAsync(member);
                                                            }
                                                        }));
                                                        // validate login returned from account generation
                                                        req.validateAndSetUserExtSources(member.getUserId(), login, namespaceValue);
                                                        // show assigned login
                                                        UiElements.generateInfo("Assigned login", "You were assigned with login <b>" + login + "</b> in namespace MU.");
                                                    }

                                                    @Override
                                                    public void onError(PerunError error) {
                                                        // validate member when all logins are set
                                                        ValidateMemberAsync req2 = new ValidateMemberAsync(JsonCallbackEvents.closeTabDisableButtonEvents(button, tab));
                                                        req2.validateMemberAsync(member);
                                                    }
                                                }));
                                                setLogin.setLogin(member.getUserId(), "mu", login);
                                            }
                                        }));
                                        final Map<String, String> params = new HashMap<String, String>();
                                        GetEntityById get = new GetEntityById(PerunEntity.RICH_MEMBER, member.getId(), JsonCallbackEvents.disableButtonEvents(button, new JsonCallbackEvents() {

                                            @Override
                                            public void onFinished(JavaScriptObject jso) {
                                                RichMember rm = jso.cast();
                                                params.put("urn:perun:user:attribute-def:core:firstName", rm.getUser().getFirstName());
                                                params.put("urn:perun:user:attribute-def:core:lastName", rm.getUser().getLastName());
                                                params.put("urn:perun:member:attribute-def:def:mail", serviceUserEmail.getTextBox().getValue().trim());
                                                req.generateAccount(namespaceValue, null, params);
                                            }
                                        }));
                                        get.retrieveData();
                                    } else {
                                        CreatePassword req = new CreatePassword(JsonCallbackEvents.disableButtonEvents(skipButton, new JsonCallbackEvents() {

                                            public void onFinished(JavaScriptObject jso) {
                                                // validate member when all kerberos logins are set
                                                ValidateMemberAsync req2 = new ValidateMemberAsync(JsonCallbackEvents.closeTabDisableButtonEvents(skipButton, tab));
                                                req2.validateMemberAsync(member);
                                            }
                                        }));
                                        // set empty password for service member if "skipped"
                                        req.createRandomPassword(member.getUserId(), serviceUserLogin.getTextBox().getValue().trim(), namespaceValue);
                                    }
                                }
                            });
                            FlexTable ft = new FlexTable();
                            ft.setStyleName("inputFormFlexTable");
                            ft.setWidth("400px");
                            ft.setHTML(0, 0, "Password:");
                            ft.setWidget(0, 1, serviceUserPassword);
                            ft.getFlexCellFormatter().setStyleName(0, 0, "itemName");
                            ft.setHTML(1, 0, "Re-type password:");
                            ft.setWidget(1, 1, serviceUserPassword2);
                            ft.getFlexCellFormatter().setStyleName(1, 0, "itemName");
                            ft.setWidget(2, 1, skipButton);
                            ft.getFlexCellFormatter().setHorizontalAlignment(2, 1, HasHorizontalAlignment.ALIGN_RIGHT);
                            ft.setWidget(3, 1, button);
                            ft.getFlexCellFormatter().setHorizontalAlignment(3, 1, HasHorizontalAlignment.ALIGN_RIGHT);
                            secondTabPanel.add(ft);
                            // hide 2nd panel
                            firstTabLayout.setVisible(false);
                            // show 3rd panel
                            secondTabPanel.setVisible(true);
                        }

                        ;
                    }));
                    request.createMember(voId, serviceUserName.getTextBox().getValue().trim(), serviceUserEmail.getTextBox().getValue().trim(), itemsTable.getList(), namespace.getValue(namespace.getSelectedIndex()), serviceUserLogin.getTextBox().getValue().trim(), certDN.getTextBox().getValue().trim(), cacertDN.getTextBox().getValue().trim(), userType.getSelectedValue());
                }
            });
            // we have ourselves already assigned
            //cb.setEnabled(false);
            CustomButton button = TabMenu.getPredefinedButton(ButtonType.ADD, "Add selected users to service member");
            button.addClickHandler(new ClickHandler() {

                public void onClick(ClickEvent clickEvent) {
                    ArrayList<User> list = callback.getTableSelectedList();
                    if (UiElements.cantSaveEmptyListDialogBox(list)) {
                        // skip self
                        ArrayList<User> list2 = new ArrayList<User>();
                        for (User user : list) {
                            if (user != null && user.getId() != session.getUser().getId()) {
                                list2.add(user);
                            }
                        }
                        itemsTable.addItems(list2);
                        cb.setEnabled(true);
                        callback.clearTableSelectedSet();
                    }
                }
            });
            button.setEnabled(false);
            tabMenu.addWidget(button);
            JsonUtils.addTableManagedButton(callback, table, button);
            // add finish button to menu
            tabMenu.addWidget(cb);
            // if some text has been searched before
            if (!searchString.equals("")) {
                searchBox.getTextBox().setText(searchString);
                callback.searchFor(searchString, voId);
            }
            final ScrollPanel sp = new ScrollPanel(table);
            table.addStyleName("perun-table");
            sp.addStyleName("perun-tableScrollPanel");
            session.getUiElements().resizeSmallTabPanel(sp, 350, tab);
            firstTabLayout.setWidget(1, 0, tabMenu);
            firstTabLayout.setWidget(2, 0, sp);
            firstTabLayout.setHTML(1, 1, "<h3>To be associated:</h3>");
            firstTabLayout.setWidget(2, 1, itemsTable);
            firstTabLayout.getFlexCellFormatter().setWidth(2, 0, "75%");
            firstTabLayout.getFlexCellFormatter().setWidth(2, 1, "25%");
            firstTabLayout.getFlexCellFormatter().setVerticalAlignment(2, 1, HasVerticalAlignment.ALIGN_TOP);
            firstTabLayout.getFlexCellFormatter().setVerticalAlignment(2, 0, HasVerticalAlignment.ALIGN_TOP);
            // actions when added items or removed items
            itemsTable.setEvents(new AddRemoveItemsTable.HandleItemsAction<User>() {

                @Override
                public void onAdd(User object) {
                    cb.setEnabled(true);
                }

                @Override
                public void onRemove(User object) {
                    if (object.equals(session.getUser())) {
                        itemsTable.addItem(object);
                        UiElements.generateInfo("Can't remove yourself", "<p>You can't remove yourself yet. You wouldn't be able to finish service member configuration. Please remove yourself afterwards.");
                    }
                    if (itemsTable.getList().isEmpty()) {
                        cb.setEnabled(false);
                    }
                }
            });
        }
    });
    // check login availability
    serviceUserLogin.getTextBox().addKeyUpHandler(new KeyUpHandler() {

        @Override
        public void onKeyUp(KeyUpEvent keyUpEvent) {
            if (keyUpEvent.isDownArrow() || keyUpEvent.isUpArrow() || keyUpEvent.isLeftArrow() || keyUpEvent.isRightArrow()) {
                // do not trigger when no text input
                return;
            }
            final String login = serviceUserLogin.getTextBox().getValue().trim();
            final String loginNamespace = namespace.getValue(namespace.getSelectedIndex());
            // trigger new validation on checked input or if previously was hard error
            if ((!login.isEmpty() && RegExp.compile(Utils.LOGIN_VALUE_MATCHER).test(login)) || serviceUserLogin.isHardError()) {
                new IsLoginAvailable(loginNamespace, login, new JsonCallbackEvents() {

                    @Override
                    public void onFinished(JavaScriptObject jso) {
                        // UPDATE RESULT ONLY IF CONTENT OF LOGIN BOX IS SAME AS ON CALLBACK START
                        if (serviceUserLogin.getTextBox().getValue().trim().equals(login)) {
                            BasicOverlayType bo = jso.cast();
                            serviceUserLogin.setProcessing(false);
                            if (!bo.getBoolean()) {
                                serviceUserLogin.setHardError("Login is already in use!");
                            } else {
                                serviceUserLogin.removeHardError();
                                loginValidator.validateTextBox();
                            }
                        }
                    }

                    @Override
                    public void onLoadingStart() {
                        if (serviceUserLogin.getTextBox().getValue().trim().equals(login)) {
                            serviceUserLogin.removeHardError();
                            serviceUserLogin.setProcessing(true);
                        }
                    }

                    @Override
                    public void onError(PerunError error) {
                        if (serviceUserLogin.getTextBox().getValue().trim().equals(login)) {
                            serviceUserLogin.setProcessing(false);
                            serviceUserLogin.setHardError("Unable to check if login is available!");
                        }
                    }
                }).retrieveData();
            }
        }
    });
    namespace.addChangeHandler(new ChangeHandler() {

        @Override
        public void onChange(ChangeEvent changeEvent) {
            if (namespace.getSelectedIndex() == 0) {
                // do not set login
                serviceUserLogin.getTextBox().setEnabled(false);
                serviceUserLogin.getTextBox().setValue(null);
                label.setVisible(false);
            } else if (namespace.getSelectedValue().equals("mu")) {
                serviceUserLogin.getTextBox().setEnabled(false);
                label.setVisible(true);
            } else {
                serviceUserLogin.getTextBox().setEnabled(true);
                label.setVisible(false);
            }
            final String login = serviceUserLogin.getTextBox().getValue().trim();
            final String loginNamespace = namespace.getValue(namespace.getSelectedIndex());
            if ((!login.isEmpty() && RegExp.compile(Utils.LOGIN_VALUE_MATCHER).test(login)) || serviceUserLogin.isHardError()) {
                new IsLoginAvailable(loginNamespace, login, new JsonCallbackEvents() {

                    @Override
                    public void onFinished(JavaScriptObject jso) {
                        // UPDATE RESULT ONLY IF CONTENT OF LOGIN BOX IS SAME AS ON CALLBACK START
                        if (serviceUserLogin.getTextBox().getValue().trim().equals(login)) {
                            serviceUserLogin.setProcessing(false);
                            BasicOverlayType bo = jso.cast();
                            if (!bo.getBoolean()) {
                                serviceUserLogin.setHardError("Login is already in use!");
                            } else {
                                serviceUserLogin.removeHardError();
                                loginValidator.validateTextBox();
                            }
                        }
                    }

                    @Override
                    public void onLoadingStart() {
                        if (serviceUserLogin.getTextBox().getValue().trim().equals(login)) {
                            serviceUserLogin.removeHardError();
                            serviceUserLogin.setProcessing(true);
                            loginValidator.validateTextBox();
                        }
                    }

                    @Override
                    public void onError(PerunError error) {
                        if (serviceUserLogin.getTextBox().getValue().trim().equals(login)) {
                            serviceUserLogin.setProcessing(false);
                            serviceUserLogin.setHardError("Unable to check if login is available!");
                        }
                    }
                }).retrieveData();
            }
        }
    });
    if (session.isPerunAdmin()) {
        layout.setWidget(9, 0, cb);
        layout.getFlexCellFormatter().setHorizontalAlignment(9, 0, HasHorizontalAlignment.ALIGN_RIGHT);
        layout.getFlexCellFormatter().setColSpan(9, 0, 2);
    } else {
        layout.setWidget(8, 0, cb);
        layout.getFlexCellFormatter().setHorizontalAlignment(8, 0, HasHorizontalAlignment.ALIGN_RIGHT);
        layout.getFlexCellFormatter().setColSpan(8, 0, 2);
    }
    this.contentWidget.setWidget(mainTab);
    return getWidget();
}
Also used : JsonCallbackEvents(cz.metacentrum.perun.webgui.json.JsonCallbackEvents) ArrayList(java.util.ArrayList) IsLoginAvailable(cz.metacentrum.perun.webgui.json.usersManager.IsLoginAvailable) CellTable(com.google.gwt.user.cellview.client.CellTable) GenerateAccount(cz.metacentrum.perun.webgui.json.usersManager.GenerateAccount) CustomButton(cz.metacentrum.perun.webgui.widgets.CustomButton) FindUsersInVo(cz.metacentrum.perun.webgui.json.membersManager.FindUsersInVo) GetEntityById(cz.metacentrum.perun.webgui.json.GetEntityById) CreatePassword(cz.metacentrum.perun.webgui.json.usersManager.CreatePassword) ValidateMemberAsync(cz.metacentrum.perun.webgui.json.membersManager.ValidateMemberAsync) CreateSpecificMember(cz.metacentrum.perun.webgui.json.membersManager.CreateSpecificMember) RegExp(com.google.gwt.regexp.shared.RegExp) CreateSpecificMember(cz.metacentrum.perun.webgui.json.membersManager.CreateSpecificMember) SetLogin(cz.metacentrum.perun.webgui.json.usersManager.SetLogin) TabItem(cz.metacentrum.perun.webgui.tabs.TabItem) JavaScriptObject(com.google.gwt.core.client.JavaScriptObject) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with GenerateAccount

use of cz.metacentrum.perun.webgui.json.usersManager.GenerateAccount 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 GetEntityById (cz.metacentrum.perun.webgui.json.GetEntityById)2 JsonCallbackEvents (cz.metacentrum.perun.webgui.json.JsonCallbackEvents)2 CreatePassword (cz.metacentrum.perun.webgui.json.usersManager.CreatePassword)2 GenerateAccount (cz.metacentrum.perun.webgui.json.usersManager.GenerateAccount)2 SetLogin (cz.metacentrum.perun.webgui.json.usersManager.SetLogin)2 TabItem (cz.metacentrum.perun.webgui.tabs.TabItem)2 CustomButton (cz.metacentrum.perun.webgui.widgets.CustomButton)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ClickEvent (com.google.gwt.event.dom.client.ClickEvent)1 ClickHandler (com.google.gwt.event.dom.client.ClickHandler)1 RegExp (com.google.gwt.regexp.shared.RegExp)1 CellTable (com.google.gwt.user.cellview.client.CellTable)1 CreateSpecificMember (cz.metacentrum.perun.webgui.json.membersManager.CreateSpecificMember)1 FindUsersInVo (cz.metacentrum.perun.webgui.json.membersManager.FindUsersInVo)1 ValidateMemberAsync (cz.metacentrum.perun.webgui.json.membersManager.ValidateMemberAsync)1 ChangePassword (cz.metacentrum.perun.webgui.json.usersManager.ChangePassword)1 IsLoginAvailable (cz.metacentrum.perun.webgui.json.usersManager.IsLoginAvailable)1 BasicOverlayType (cz.metacentrum.perun.webgui.model.BasicOverlayType)1