public PrincipalAttributesPanel(String id)
{
super(id);
Form userAttrsForm = new Form("userAttrsForm");
add(new FeedbackPanel("feedback"));
userAttrsForm.add(new Label("attrNameLabel",new ResourceModel("common.name")));
userAttrsForm.add(new Label("attrValueLabel",new ResourceModel("common.value")));
add(userAttrsForm);
PageableListView usersList = new PageableListView(
"attributeEntries", new PropertyModel(this,
"userAttributes"), 10)
{
protected void populateItem(ListItem item)
{
final Map<String, SecurityAttribute> attributes = (Map<String, SecurityAttribute>) item
.getModelObject();
final SecurityAttribute attrib = attributes.get("value");
item.add(new TextField("name", new Model(attrib.getName()))
{
@Override
public boolean isEnabled()
{
return !attrib.isReadOnly();
}
});
item.add(new TextField("value", new PropertyModel<String>(attrib,"stringValue"))
{
@Override
public boolean isEnabled()
{
return !attrib.isReadOnly();
}
});
if (!attrib.isReadOnly())
{
Link deleteLink = new Link("link", item.getModel())
{
@Override
public void onClick()
{
try
{
getPrincipal().getSecurityAttributes()
.removeAttribute(attrib.getName());
getManager()
.updatePrincipal(getPrincipal());
}
catch (SecurityException e)
{
log.error("Failed to update principal.", e);
}
setPrincipal(getPrincipal());
refreshData();
}
};
deleteLink.add(new Label("deleteLabel",
new ResourceModel("common.delete")));
item.add(deleteLink);
}
}
};
userAttrsForm.add(usersList);
userAttrsForm.add(new PagingNavigator("navigator", usersList));
Button updateAttrButton = new Button("updateAttr",
new ResourceModel("common.update"))
{
public void onSubmit()
{
Map<String, SecurityAttribute> attribs = getPrincipal().getSecurityAttributes().getAttributeMap() ;
for (Iterator it = userAttributes.iterator(); it.hasNext();)
{
Map userAttrMap = (Map) it.next();
String userAttrName = (String) userAttrMap.get("name");
String userAttrValue = ((SecurityAttribute) userAttrMap
.get("value")).getStringValue();
String oldUserAttrValue = attribs.get(userAttrName).getStringValue();
Map<String,SecurityAttribute> userAttributes = getPrincipal().getSecurityAttributes().getAttributeMap();
try
{
getPrincipal().getSecurityAttributes().getAttribute(userAttrName).setStringValue(userAttrValue);
}
catch (SecurityException e)
{
log.error("Failed to update security attribute of principal.", e);
}
getServiceLocator()
.getAuditActivity()
.logAdminAttributeActivity(
getPrincipal().getName(),
getIPAddress(),
getPrincipal().getName(),
AuditActivity.USER_UPDATE_ATTRIBUTE,
userAttrName, oldUserAttrValue,
userAttrValue,
AdminPortletWebPage.USER_ADMINISTRATION);
}
try
{
getManager().updatePrincipal(getPrincipal());
}
catch (SecurityException e)
{
error(e.getMessage());
}
refreshData();
}
};
userAttrsForm.add(updateAttrButton);
Form addAttrForm = new Form("addAttrForm")
{
protected void onSubmit()
{
String userAttrName = getUserAttrName();
String userAttrValue = getUserAttrValue();
if (userAttrName != null
&& userAttrName.trim().length() > 0)
{
// Preferences prefs = user.getUserAttributes();
// prefs.put(userAttrName, userAttrValue);
try
{
getPrincipal().getSecurityAttributes()
.getAttribute(userAttrName, true)
.setStringValue(userAttrValue);
getManager().updatePrincipal(getPrincipal());
getServiceLocator()
.getAuditActivity()
.logAdminAttributeActivity(
getPrincipal().getName(),
getIPAddress(),
getPrincipal().getName(),
AuditActivity.USER_ADD_ATTRIBUTE,
userAttrName,
"",
userAttrValue,
AdminPortletWebPage.USER_ADMINISTRATION);
}
catch (SecurityException e)
{
log.error("Failed to update security attribute of principal.", e);
}
}
setPrincipal(getPrincipal());
refreshData();
}
};
add(addAttrForm);
addAttrForm.add(new Label("nameLabel", new ResourceModel(
"common.name")));
TextField userAttrNameField = new RequiredTextField("userAttrName",
new PropertyModel(this, "userAttrName"));
addAttrForm.add(userAttrNameField);
addAttrForm.add(new Label("valueLabel", new ResourceModel(
"common.value")));
TextField userAttrValueField = new RequiredTextField(
"userAttrValue", new PropertyModel(this, "userAttrValue"));
addAttrForm.add(userAttrValueField);
addAttrForm.add(new Button("addAttr", new ResourceModel(
"common.attribute.add")));
}