use of cz.metacentrum.perun.webgui.json.usersManager.CreatePassword 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 String serviceType = "SERVICE";
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().equals("")) {
serviceUserPassword.setError("Password can't be empty!");
return false;
}
// einfra check
if ("einfra".equals(namespace.getSelectedValue())) {
RegExp regExp2 = RegExp.compile("^[\\x20-\\x7E]{1,}$");
if (regExp2.exec(serviceUserPassword.getTextBox().getValue()) == null) {
serviceUserPassword.setError("Password <b>can`t contain accented characters (diacritics)</b> or non-printing and control characters!");
return false;
}
// check on login in password if login is longer than 2 chars
// TODO - check for name/surname too
String pass = serviceUserPassword.getTextBox().getValue();
String login = serviceUserLogin.getTextBox().getValue();
if (login.length() > 2) {
if (Utils.normalizeString(pass).contains(Utils.normalizeString(login)) || Utils.normalizeString(pass).contains(Utils.normalizeString(Utils.reverseString((login))))) {
serviceUserPassword.setError("Password <b>can't contain login, name or surname</b>, not even backwards!");
return false;
}
}
// Check that password contains at least 3 of 4 character groups
RegExp regExpDigit = RegExp.compile("^.*[0-9].*$");
RegExp regExpLower = RegExp.compile("^.*[a-z].*$");
RegExp regExpUpper = RegExp.compile("^.*[A-Z].*$");
// FIXME - are those correct printable specific chars?
RegExp regExpSpec = RegExp.compile("^.*[\\x20-\\x2F\\x3A-\\x40\\x5B-\\x60\\x7B-\\x7E].*$");
int matchCounter = 0;
if (regExpDigit.exec(serviceUserPassword.getTextBox().getValue()) != null)
matchCounter++;
if (regExpLower.exec(serviceUserPassword.getTextBox().getValue()) != null)
matchCounter++;
if (regExpUpper.exec(serviceUserPassword.getTextBox().getValue()) != null)
matchCounter++;
if (regExpSpec.exec(serviceUserPassword.getTextBox().getValue()) != null)
matchCounter++;
if (matchCounter < 3) {
serviceUserPassword.setError("Password must consist of <b>at least 3 of 4</b> character groups<ul><li>lower-case letters</li><li>upper-case letters</li><li>digits</li><li>special characters</li></ul>");
return false;
}
// check length
if (serviceUserPassword.getTextBox().getValue().length() < 10) {
serviceUserPassword.setError("Password must be <b>at least 10 characters</b> long!");
return false;
}
}
if (!serviceUserPassword.getTextBox().getValue().equals(serviceUserPassword2.getTextBox().getValue())) {
serviceUserPassword.setOk();
serviceUserPassword2.setError("Password in both textboxes must be the same!");
return false;
}
serviceUserPassword.setOk();
return true;
}
};
serviceUserPassword.setValidator(validator);
final ExtendedTextBox.TextBoxValidator validator2 = new ExtendedTextBox.TextBoxValidator() {
@Override
public boolean validateTextBox() {
if (!serviceUserPassword2.getTextBox().getValue().equals(serviceUserPassword.getTextBox().getValue())) {
serviceUserPassword2.setError("Password in both textboxes must be the same!");
return false;
} else {
serviceUserPassword2.setOk();
return true;
}
}
};
serviceUserPassword2.setValidator(validator2);
/*
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().equals(serviceUserPassword2.getTextBox().getValue())) {
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().equals(serviceUserPassword.getTextBox().getValue())) {
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);
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);
// 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()) {
// one of input boxes for passwords is wrong
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, true));
req2.validateMemberAsync(member);
}
}));
// validate login returned from account generation
req.validatePassword(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, true));
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(), 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, true));
req2.validateMemberAsync(member);
}
}));
req.createPassword(member.getUserId(), serviceUserLogin.getTextBox().getValue().trim(), namespaceValue, serviceUserPassword.getTextBox().getValue());
}
}
});
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, true));
req2.validateMemberAsync(member);
}
}));
// validate login returned from account generation
req.validatePassword(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, true));
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, true));
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");
if ("einfra".equals(namespace.getSelectedValue())) {
ft.setHTML(2, 0, "Password must <ul><li>contain only printing (non-accented) characters<li>be at least 10 characters long<li>consist of at least 3 of 4 character groups<ul><li>lower-case letters<li>upper-case letters<li>digits<li>special characters</ul></ul>");
} else {
ft.setHTML(2, 0, "Please <b>avoid using accented characters</b>. It might not be supported by all backend components and services.");
}
ft.getFlexCellFormatter().setColSpan(2, 0, 2);
ft.getCellFormatter().setStyleName(2, 0, "inputFormInlineComment");
ft.setWidget(3, 1, skipButton);
ft.getFlexCellFormatter().addStyleName(3, 1, "align-right");
ft.setWidget(4, 1, button);
ft.getFlexCellFormatter().addStyleName(4, 1, "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(), serviceType);
}
});
// 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) {
// response is relevant to current value
if (serviceUserLogin.getTextBox().getValue().trim().equals(login)) {
if ("InvalidLoginException".equalsIgnoreCase(error.getName())) {
serviceUserLogin.setProcessing(false);
String text = error.getErrorInfo();
text = text.split(":", 2)[1];
text = (text == null || text.isEmpty()) ? error.getErrorInfo() : text;
serviceUserLogin.setHardError(text);
} else {
// generic error
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);
loginValidator.validateTextBox();
label.setVisible(false);
} else if (namespace.getSelectedValue().equals("mu")) {
serviceUserLogin.getTextBox().setEnabled(false);
label.setVisible(true);
} else {
serviceUserLogin.getTextBox().setEnabled(true);
label.setVisible(false);
}
if (namespace.getSelectedIndex() != 0) {
// do not check login if not desired to set
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) {
// response is relevant to current value
if (serviceUserLogin.getTextBox().getValue().trim().equals(login)) {
if ("InvalidLoginException".equalsIgnoreCase(error.getName())) {
serviceUserLogin.setProcessing(false);
String text = error.getErrorInfo();
text = text.split(":", 2)[1];
text = (text == null || text.isEmpty()) ? error.getErrorInfo() : text;
serviceUserLogin.setHardError(text);
} else {
// generic error
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();
}
use of cz.metacentrum.perun.webgui.json.usersManager.CreatePassword 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;
}
// einfra check
if ("einfra".equals(namespace)) {
RegExp regExp2 = RegExp.compile("^[\\x20-\\x7E]{1,}$");
if (regExp2.exec(newPass.getTextBox().getValue()) == null) {
newPass.setError("Password <b>can`t contain accented characters (diacritics)</b> or non-printing and control characters!");
return false;
}
// check on login in password if login is longer than 2 chars
if (login.length() > 2) {
String pass = newPass.getTextBox().getValue();
if (Utils.normalizeString(pass).contains(Utils.normalizeString(login)) || Utils.normalizeString(pass).contains(Utils.normalizeString(Utils.reverseString((login))))) {
newPass.setError("Password <b>can't contain login, name or surname</b>, not even backwards!");
return false;
}
}
// Check that password contains at least 3 of 4 character groups
RegExp regExpDigit = RegExp.compile("^.*[0-9].*$");
RegExp regExpLower = RegExp.compile("^.*[a-z].*$");
RegExp regExpUpper = RegExp.compile("^.*[A-Z].*$");
RegExp regExpSpec = RegExp.compile("^.*[\\x20-\\x2F\\x3A-\\x40\\x5B-\\x60\\x7B-\\x7E].*$");
int matchCounter = 0;
if (regExpDigit.exec(newPass.getTextBox().getValue()) != null)
matchCounter++;
if (regExpLower.exec(newPass.getTextBox().getValue()) != null)
matchCounter++;
if (regExpUpper.exec(newPass.getTextBox().getValue()) != null)
matchCounter++;
if (regExpSpec.exec(newPass.getTextBox().getValue()) != null)
matchCounter++;
if (matchCounter < 3) {
newPass.setError("Password must consist of <b>at least 3 of 4</b> character groups<ul><li>lower-case letters</li><li>upper-case letters</li><li>digits</li><li>special characters</li></ul>");
return false;
}
// check length
if (newPass.getTextBox().getValue().length() < 10) {
newPass.setError("Password must be <b>at least 10 characters</b> long!");
return false;
}
}
if (!newPass.getTextBox().getValue().equals(confPass.getTextBox().getValue())) {
newPass.setOk();
confPass.setError("Password in both textboxes must be the same!");
return false;
}
newPass.setOk();
return true;
}
};
final ExtendedTextBox.TextBoxValidator validator2 = new ExtendedTextBox.TextBoxValidator() {
@Override
public boolean validateTextBox() {
if (!confPass.getTextBox().getValue().equals(newPass.getTextBox().getValue())) {
confPass.setError("Password in both textboxes must be the same!");
return false;
} else {
confPass.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, true), false);
changepw.changePassword(user, namespace, oldPass.getTextBox().getValue(), newPass.getTextBox().getValue());
} else {
if (!validator.validateTextBox() && !validator2.validateTextBox() && !oldValidator.validateTextBox())
return;
ChangePassword changepw = new ChangePassword(JsonCallbackEvents.closeTabDisableButtonEvents(changeButton, tab, true), true);
changepw.changePassword(user, namespace, oldPass.getTextBox().getValue(), newPass.getTextBox().getValue());
}
}
});
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, true));
req.validatePassword(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(), params);
}
}));
get.retrieveData();
} else {
// NORMAL PWD LOGIC
CreatePassword create = new CreatePassword(JsonCallbackEvents.closeTabDisableButtonEvents(createButton, tab, true));
create.createPassword(userId, login, namespace, newPass.getTextBox().getValue());
}
}
}
});
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, true));
req.validatePassword(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(), params);
}
}));
get.retrieveData();
} else {
CreatePassword create = new CreatePassword(JsonCallbackEvents.closeTabDisableButtonEvents(skip, tab, true));
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");
}
int row = layout.getRowCount();
if ("einfra".equals(namespace)) {
layout.setHTML(row, 0, "Password must <ul><li>contain only printing (non-accented) characters<li>be at least 10 characters long<li>consist of at least 3 of 4 character groups<ul><li>lower-case letters<li>upper-case letters<li>digits<li>special characters</ul></ul>");
} else {
layout.setHTML(row, 0, "Please <b>avoid using accented characters</b>. It might not be supported by all backend components and services.");
}
layout.getFlexCellFormatter().setColSpan(row, 0, 2);
layout.getCellFormatter().setStyleName(row, 0, "inputFormInlineComment");
layout.setWidth("400px");
if (!action.equals(Actions.CREATE)) {
menu.addWidget(TabMenu.getPredefinedButton(ButtonType.CANCEL, "", new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
session.getTabManager().closeTab(tab, isRefreshParentOnClose());
}
}));
}
vp.add(menu);
vp.setCellHorizontalAlignment(menu, HasHorizontalAlignment.ALIGN_RIGHT);
this.contentWidget.setWidget(vp);
return getWidget();
}
Aggregations