PL-7473 UserEditor should not define private methods

This commit is contained in:
Yuriy Artamonov 2016-07-01 15:57:39 +04:00
parent 22847bce3d
commit 15f71cb6b8

View File

@ -16,6 +16,7 @@
*/ */
package com.haulmont.cuba.gui.app.security.user.edit; package com.haulmont.cuba.gui.app.security.user.edit;
import com.haulmont.bali.util.ParamsMap;
import com.haulmont.chile.core.model.MetaClass; import com.haulmont.chile.core.model.MetaClass;
import com.haulmont.cuba.client.ClientConfig; import com.haulmont.cuba.client.ClientConfig;
import com.haulmont.cuba.core.entity.Entity; import com.haulmont.cuba.core.entity.Entity;
@ -61,10 +62,10 @@ public class UserEditor extends AbstractEditor<User> {
protected CollectionDatasource<UserSubstitution, UUID> substitutionsDs; protected CollectionDatasource<UserSubstitution, UUID> substitutionsDs;
@Inject @Inject
protected Table rolesTable; protected Table<UserRole> rolesTable;
@Inject @Inject
protected Table substTable; protected Table<UserSubstitution> substTable;
@Inject @Inject
protected FieldGroup fieldGroupLeft; protected FieldGroup fieldGroupLeft;
@ -113,16 +114,24 @@ public class UserEditor extends AbstractEditor<User> {
public void init(Map<String, Object> params) { public void init(Map<String, Object> params) {
userDs.addItemPropertyChangeListener(new NameBuilderListener<>(userDs)); userDs.addItemPropertyChangeListener(new NameBuilderListener<>(userDs));
userDs.addItemPropertyChangeListener(e -> { userDs.addItemPropertyChangeListener(e -> {
if (e.getProperty().equals("timeZoneAuto")) { if ("timeZoneAuto".equals(e.getProperty())) {
timeZoneLookup.setEnabled(!Boolean.TRUE.equals(e.getValue())); timeZoneLookup.setEnabled(!Boolean.TRUE.equals(e.getValue()));
} }
}); });
rolesTable.addAction(new AddRoleAction()); AddRoleAction addRoleAction = new AddRoleAction();
addRoleAction.setEnabled(security.isEntityOpPermitted(metadata.getClass(UserRole.class), EntityOp.CREATE));
rolesTable.addAction(addRoleAction);
rolesTable.addAction(new EditRoleAction()); rolesTable.addAction(new EditRoleAction());
rolesTable.addAction(new RemoveRoleAction(rolesTable, false));
substTable.addAction(new AddSubstitutedAction()); RemoveRoleAction removeRoleAction = new RemoveRoleAction(rolesTable, false);
removeRoleAction.setEnabled(security.isEntityOpPermitted(metadata.getClass(UserRole.class), EntityOp.DELETE));
rolesTable.addAction(removeRoleAction);
AddSubstitutedAction addSubstitutedAction = new AddSubstitutedAction();
addSubstitutedAction.setEnabled(security.isEntityOpPermitted(metadata.getClass(UserSubstitution.class), EntityOp.CREATE));
substTable.addAction(addSubstitutedAction);
substTable.addAction(new EditSubstitutedAction()); substTable.addAction(new EditSubstitutedAction());
substTable.addAction(new RemoveAction(substTable, false)); substTable.addAction(new RemoveAction(substTable, false));
@ -158,7 +167,7 @@ public class UserEditor extends AbstractEditor<User> {
timeZoneLookup.setEnabled(!Boolean.TRUE.equals(getItem().getTimeZoneAuto())); timeZoneLookup.setEnabled(!Boolean.TRUE.equals(getItem().getTimeZoneAuto()));
// Do not show roles which are not allowed by security constraints // Do not show roles which are not allowed by security constraints
LoadContext lc = new LoadContext(Role.class); LoadContext<Role> lc = new LoadContext<>(Role.class);
lc.setQueryString("select r from sec$Role r"); lc.setQueryString("select r from sec$Role r");
lc.setView(View.MINIMAL); lc.setView(View.MINIMAL);
List<Role> allowedRoles = dataSupplier.loadList(lc); List<Role> allowedRoles = dataSupplier.loadList(lc);
@ -187,7 +196,7 @@ public class UserEditor extends AbstractEditor<User> {
} }
protected void initUserGroup(User user) { protected void initUserGroup(User user) {
LoadContext ctx = new LoadContext(Group.class); LoadContext<Group> ctx = new LoadContext<>(Group.class);
ctx.setQueryString("select g from sec$Group g"); ctx.setQueryString("select g from sec$Group g");
ctx.setView(View.MINIMAL); ctx.setView(View.MINIMAL);
List<Group> groups = dataSupplier.loadList(ctx); List<Group> groups = dataSupplier.loadList(ctx);
@ -196,8 +205,8 @@ public class UserEditor extends AbstractEditor<User> {
} }
} }
private void addDefaultRoles(User user) { protected void addDefaultRoles(User user) {
LoadContext ctx = new LoadContext(Role.class); LoadContext<Role> ctx = new LoadContext<>(Role.class);
ctx.setQueryString("select r from sec$Role r where r.defaultRole = true"); ctx.setQueryString("select r from sec$Role r where r.defaultRole = true");
List<Role> defaultRoles = dataSupplier.loadList(ctx); List<Role> defaultRoles = dataSupplier.loadList(ctx);
@ -206,7 +215,7 @@ public class UserEditor extends AbstractEditor<User> {
newRoles.addAll(user.getUserRoles()); newRoles.addAll(user.getUserRoles());
} }
final MetaClass metaClass = rolesDs.getMetaClass(); MetaClass metaClass = rolesDs.getMetaClass();
for (Role role : defaultRoles) { for (Role role : defaultRoles) {
UserRole userRole = dataSupplier.newInstance(metaClass); UserRole userRole = dataSupplier.newInstance(metaClass);
userRole.setRole(role); userRole.setRole(role);
@ -217,121 +226,110 @@ public class UserEditor extends AbstractEditor<User> {
user.setUserRoles(newRoles); user.setUserRoles(newRoles);
} }
private void initCustomFields(final boolean isNew) { protected void initCustomFields(final boolean isNew) {
fieldGroupLeft.addCustomField("passw", new FieldGroup.CustomFieldGenerator() { fieldGroupLeft.addCustomField("passw", (datasource, propertyId) -> {
@Override passwField = factory.createComponent(PasswordField.class);
public Component generateField(Datasource datasource, String propertyId) { if (isNew) {
passwField = factory.createComponent(PasswordField.class); passwField.setRequiredMessage(getMessage("passwMsg"));
if (isNew) {
passwField.setRequiredMessage(getMessage("passwMsg"));
Companion companion = getCompanion(); Companion companion = getCompanion();
if (companion != null) { if (companion != null) {
companion.initPasswordField(passwField); companion.initPasswordField(passwField);
} else {
passwField.setRequired(true);
}
passwField.addValueChangeListener(e -> ((DatasourceImplementation) userDs).setModified(true));
} else { } else {
passwField.setVisible(false); passwField.setRequired(true);
} }
return passwField; passwField.addValueChangeListener(e ->
((DatasourceImplementation) userDs).setModified(true)
);
} else {
passwField.setVisible(false);
} }
return passwField;
}); });
fieldGroupLeft.addCustomField("confirmPassw", new FieldGroup.CustomFieldGenerator() { fieldGroupLeft.addCustomField("confirmPassw", (datasource, propertyId) -> {
@Override confirmPasswField = factory.createComponent(PasswordField.class);
public Component generateField(Datasource datasource, String propertyId) { if (isNew) {
confirmPasswField = factory.createComponent(PasswordField.class); confirmPasswField.setRequiredMessage(getMessage("confirmPasswMsg"));
if (isNew) {
confirmPasswField.setRequiredMessage(getMessage("confirmPasswMsg"));
Companion companion = getCompanion(); Companion companion = getCompanion();
if (companion != null) { if (companion != null) {
companion.initPasswordField(confirmPasswField); companion.initPasswordField(confirmPasswField);
} else {
confirmPasswField.setRequired(true);
}
confirmPasswField.addValueChangeListener(e -> ((DatasourceImplementation) userDs).setModified(true));
} else { } else {
confirmPasswField.setVisible(false); confirmPasswField.setRequired(true);
} }
return confirmPasswField; confirmPasswField.addValueChangeListener(e ->
((DatasourceImplementation) userDs).setModified(true)
);
} else {
confirmPasswField.setVisible(false);
} }
return confirmPasswField;
}); });
fieldGroupRight.addCustomField("language", new FieldGroup.CustomFieldGenerator() { fieldGroupRight.addCustomField("language", (datasource, propertyId) -> {
@Override languageLookup = factory.createComponent(LookupField.class);
public Component generateField(Datasource datasource, String propertyId) {
languageLookup = factory.createComponent(LookupField.class);
languageLookup.setDatasource(datasource, propertyId); languageLookup.setDatasource(datasource, propertyId);
languageLookup.setRequired(false); languageLookup.setRequired(false);
Map<String, Locale> locales = configuration.getConfig(GlobalConfig.class).getAvailableLocales(); Map<String, Locale> locales = configuration.getConfig(GlobalConfig.class).getAvailableLocales();
TreeMap<String, Object> options = new TreeMap<>(); Map<String, Object> options = new TreeMap<>();
for (Map.Entry<String, Locale> entry : locales.entrySet()) { for (Map.Entry<String, Locale> entry : locales.entrySet()) {
options.put(entry.getKey(), messages.getTools().localeToString(entry.getValue())); options.put(entry.getKey(), messages.getTools().localeToString(entry.getValue()));
}
languageLookup.setOptionsMap(options);
return languageLookup;
} }
languageLookup.setOptionsMap(options);
return languageLookup;
}); });
fieldGroupRight.addCustomField("timeZone", new FieldGroup.CustomFieldGenerator() { fieldGroupRight.addCustomField("timeZone", (datasource, propertyId) -> {
@Override HBoxLayout hbox = factory.createComponent(HBoxLayout.class);
public Component generateField(Datasource datasource, String propertyId) { hbox.setSpacing(true);
HBoxLayout hbox = factory.createComponent(HBoxLayout.class);
hbox.setSpacing(true);
timeZoneLookup = factory.createComponent(LookupField.class); timeZoneLookup = factory.createComponent(LookupField.class);
timeZoneLookup.setDatasource(datasource, propertyId); timeZoneLookup.setDatasource(datasource, propertyId);
timeZoneLookup.setRequired(false); timeZoneLookup.setRequired(false);
MetaClass userMetaClass = userDs.getMetaClass(); MetaClass userMetaClass = userDs.getMetaClass();
timeZoneLookup.setEditable(fieldGroupRight.isEditable() timeZoneLookup.setEditable(fieldGroupRight.isEditable()
&& security.isEntityAttrUpdatePermitted(userMetaClass, propertyId)); && security.isEntityAttrUpdatePermitted(userMetaClass, propertyId));
Map<String, Object> options = new TreeMap<>(); Map<String, Object> options = new TreeMap<>();
for (String id : TimeZone.getAvailableIDs()) { for (String id : TimeZone.getAvailableIDs()) {
TimeZone timeZone = TimeZone.getTimeZone(id); TimeZone timeZone = TimeZone.getTimeZone(id);
options.put(timeZones.getDisplayNameLong(timeZone), id); options.put(timeZones.getDisplayNameLong(timeZone), id);
}
timeZoneLookup.setOptionsMap(options);
hbox.add(timeZoneLookup);
CheckBox autoDetectField = factory.createComponent(CheckBox.class);
autoDetectField.setDatasource(datasource, "timeZoneAuto");
autoDetectField.setCaption(messages.getMainMessage("timeZone.auto"));
autoDetectField.setDescription(messages.getMainMessage("timeZone.auto.descr"));
autoDetectField.setAlignment(Alignment.MIDDLE_RIGHT);
autoDetectField.setEditable(fieldGroupRight.isEditable()
&& security.isEntityAttrUpdatePermitted(userMetaClass, "timeZoneAuto"));
hbox.add(autoDetectField);
hbox.expand(timeZoneLookup);
return hbox;
} }
timeZoneLookup.setOptionsMap(options);
hbox.add(timeZoneLookup);
CheckBox autoDetectField = factory.createComponent(CheckBox.class);
autoDetectField.setDatasource(datasource, "timeZoneAuto");
autoDetectField.setCaption(messages.getMainMessage("timeZone.auto"));
autoDetectField.setDescription(messages.getMainMessage("timeZone.auto.descr"));
autoDetectField.setAlignment(Alignment.MIDDLE_RIGHT);
autoDetectField.setEditable(fieldGroupRight.isEditable()
&& security.isEntityAttrUpdatePermitted(userMetaClass, "timeZoneAuto"));
hbox.add(autoDetectField);
hbox.expand(timeZoneLookup);
return hbox;
}); });
fieldGroupRight.addCustomField("group", new FieldGroup.CustomFieldGenerator() { fieldGroupRight.addCustomField("group", (datasource, propertyId) -> {
@Override PickerField pickerField = factory.createComponent(PickerField.class);
public Component generateField(Datasource datasource, String propertyId) { pickerField.setDatasource(datasource, propertyId);
PickerField pickerField = factory.createComponent(PickerField.class); pickerField.setRequired(true);
pickerField.setDatasource(datasource, propertyId); pickerField.setRequiredMessage(getMessage("groupMsg"));
pickerField.setRequired(true);
pickerField.setRequiredMessage(getMessage("groupMsg"));
PickerField.LookupAction action = pickerField.addLookupAction(); PickerField.LookupAction action = pickerField.addLookupAction();
action.setLookupScreenOpenType(OpenType.DIALOG); action.setLookupScreenOpenType(OpenType.DIALOG);
return pickerField; return pickerField;
}
}); });
} }
@ -397,55 +395,54 @@ public class UserEditor extends AbstractEditor<User> {
} }
protected class AddRoleAction extends AbstractAction { protected class AddRoleAction extends AbstractAction {
public AddRoleAction() { public AddRoleAction() {
super("add"); super("add");
icon = themeConstants.get("actions.Add.icon"); icon = themeConstants.get("actions.Add.icon");
setCaption(getMessage("actions.Add"));
ClientConfig clientConfig = configuration.getConfig(ClientConfig.class); ClientConfig clientConfig = configuration.getConfig(ClientConfig.class);
setShortcut(clientConfig.getTableAddShortcut()); setShortcut(clientConfig.getTableAddShortcut());
} }
protected Collection<String> getExistingRoleNames() {
User user = userDs.getItem();
Collection<String> existingRoleNames = new HashSet<>();
if (user.getUserRoles() != null) {
for (UserRole userRole : user.getUserRoles()) {
if (userRole.getRole() != null)
existingRoleNames.add(userRole.getRole().getName());
}
}
return existingRoleNames;
}
@Override @Override
public void actionPerform(Component component) { public void actionPerform(Component component) {
Map<String, Object> lookupParams = Collections.singletonMap("windowOpener", "sec$User.edit"); Lookup roleLookupWindow = openLookup(Role.class, items -> {
Lookup roleLookupWindow = openLookup("sec$Role.lookup", new Lookup.Handler() { Collection<String> existingRoleNames = getExistingRoleNames();
@Override rolesDs.suspendListeners();
public void handleLookup(Collection items) { try {
Collection<String> existingRoleNames = getExistingRoleNames(); for (Object item : items) {
rolesDs.suspendListeners(); Role role = (Role) item;
try {
for (Object item : items) {
Role role = (Role) item;
if (existingRoleNames.contains(role.getName())) continue;
final MetaClass metaClass = rolesDs.getMetaClass(); if (existingRoleNames.contains(role.getName())) {
UserRole userRole = dataSupplier.newInstance(metaClass); continue;
userRole.setRole(role);
userRole.setUser(userDs.getItem());
rolesDs.addItem(userRole);
existingRoleNames.add(role.getName());
} }
} finally {
rolesDs.resumeListeners();
}
}
private Collection<String> getExistingRoleNames() { MetaClass metaClass = rolesDs.getMetaClass();
User user = userDs.getItem(); UserRole userRole = dataSupplier.newInstance(metaClass);
Collection<String> existingRoleNames = new HashSet<>(); userRole.setRole(role);
if (user.getUserRoles() != null) { userRole.setUser(userDs.getItem());
for (UserRole userRole : user.getUserRoles()) {
if (userRole.getRole() != null)
existingRoleNames.add(userRole.getRole().getName());
}
}
return existingRoleNames;
}
}, OpenType.THIS_TAB, lookupParams); rolesDs.addItem(userRole);
existingRoleNames.add(role.getName());
}
} finally {
rolesDs.resumeListeners();
}
}, OpenType.THIS_TAB, ParamsMap.of("windowOpener", "sec$User.edit"));
roleLookupWindow.addCloseListener(actionId -> { roleLookupWindow.addCloseListener(actionId -> {
rolesTable.requestFocus(); rolesTable.requestFocus();
@ -456,32 +453,22 @@ public class UserEditor extends AbstractEditor<User> {
((Table) lookupComponent).setMultiSelect(true); ((Table) lookupComponent).setMultiSelect(true);
} }
} }
@Override
public boolean isEnabled() {
return super.isEnabled() &&
security.isEntityOpPermitted(
metadata.getSession().getClass(UserRole.class), EntityOp.CREATE);
}
@Override
public String getCaption() {
return getMessage("actions.Add");
}
} }
protected class EditRoleAction extends ItemTrackingAction { protected class EditRoleAction extends ItemTrackingAction {
public EditRoleAction() { public EditRoleAction() {
super("edit"); super("edit");
icon = themeConstants.get("actions.Edit.icon"); icon = themeConstants.get("actions.Edit.icon");
setCaption(getMessage("actions.Edit"));
} }
@Override @Override
public void actionPerform(Component component) { public void actionPerform(Component component) {
if (rolesDs.getItem() == null) if (rolesDs.getItem() == null)
return; return;
Window window = openEditor("sec$Role.edit", rolesDs.getItem().getRole(), OpenType.THIS_TAB); Window window = openEditor("sec$Role.edit", rolesDs.getItem().getRole(), OpenType.THIS_TAB);
window.addCloseListener(actionId -> { window.addCloseListener(actionId -> {
if (Window.COMMIT_ACTION_ID.equals(actionId)) { if (Window.COMMIT_ACTION_ID.equals(actionId)) {
@ -490,11 +477,6 @@ public class UserEditor extends AbstractEditor<User> {
rolesTable.requestFocus(); rolesTable.requestFocus();
}); });
} }
@Override
public String getCaption() {
return getMessage("actions.Edit");
}
} }
protected class RemoveRoleAction extends RemoveAction { protected class RemoveRoleAction extends RemoveAction {
@ -508,6 +490,7 @@ public class UserEditor extends AbstractEditor<User> {
@Override @Override
protected void confirmAndRemove(Set selected) { protected void confirmAndRemove(Set selected) {
hasDefaultRole = hasDefaultRole(selected); hasDefaultRole = hasDefaultRole(selected);
super.confirmAndRemove(selected); super.confirmAndRemove(selected);
} }
@ -527,17 +510,9 @@ public class UserEditor extends AbstractEditor<User> {
} }
return false; return false;
} }
@Override
public boolean isEnabled() {
return super.isEnabled() &&
security.isEntityOpPermitted(
metadata.getSession().getClass(UserRole.class), EntityOp.DELETE);
}
} }
protected class AddSubstitutedAction extends AbstractAction { protected class AddSubstitutedAction extends AbstractAction {
public AddSubstitutedAction() { public AddSubstitutedAction() {
super("add"); super("add");
@ -549,27 +524,17 @@ public class UserEditor extends AbstractEditor<User> {
@Override @Override
public void actionPerform(Component component) { public void actionPerform(Component component) {
final UserSubstitution substitution = metadata.create(UserSubstitution.class); UserSubstitution substitution = metadata.create(UserSubstitution.class);
substitution.setUser(userDs.getItem()); substitution.setUser(userDs.getItem());
int dialogWidth = themeConstants.getInt("cuba.gui.UserEditor.substitutionEditor.width"); Window editor = openEditor(substitution, OpenType.DIALOG, ParamsMap.empty(), substitutionsDs);
Window substitutionEditor = openEditor("sec$UserSubstitution.edit", substitution, editor.addCloseListener(actionId -> {
OpenType.DIALOG.width(dialogWidth), substitutionsDs);
substitutionEditor.addCloseListener(actionId -> {
substTable.requestFocus(); substTable.requestFocus();
}); });
} }
@Override
public boolean isEnabled() {
return super.isEnabled() &&
security.isEntityOpPermitted(
metadata.getSession().getClass(UserSubstitution.class), EntityOp.CREATE);
}
} }
protected class EditSubstitutedAction extends ItemTrackingAction { protected class EditSubstitutedAction extends ItemTrackingAction {
public EditSubstitutedAction() { public EditSubstitutedAction() {
super("edit"); super("edit");
@ -578,12 +543,9 @@ public class UserEditor extends AbstractEditor<User> {
@Override @Override
public void actionPerform(Component component) { public void actionPerform(Component component) {
getDialogOptions().setWidth(themeConstants.getInt("cuba.gui.UserEditor.substitutionEditor.width"));
if (substitutionsDs.getItem() != null) { if (substitutionsDs.getItem() != null) {
Window substitutionEditor = openEditor("sec$UserSubstitution.edit", substitutionsDs.getItem(), Window editor = openEditor(substitutionsDs.getItem(), OpenType.DIALOG, ParamsMap.empty(), substitutionsDs);
OpenType.DIALOG, substitutionsDs); editor.addCloseListener(actionId -> {
substitutionEditor.addCloseListener(actionId -> {
substTable.requestFocus(); substTable.requestFocus();
}); });
} }