There
is an example of a Permissions Adapter in the sample app. Using something
similar to the example, you can simply add a list of Menu Names to the Adapter
(as part of its constructor) in the Filter and then stuff it into the
request. Then in the taglib, you use permissions="scopedAttributeName" to
invoke isAllowed on each menu and item. Here's some sample code from my
Filter:
MenuRepository repository
=
(MenuRepository) request.getSession().getServletContext()
.getAttribute(MenuRepository.MENU_REPOSITORY_KEY);
(MenuRepository) request.getSession().getServletContext()
.getAttribute(MenuRepository.MENU_REPOSITORY_KEY);
MenuComponent menu =
repository.getMenu("JobPostingMenu");
List menusToEnable = new ArrayList();
boolean enableMenu = false;
List menusToEnable = new ArrayList();
boolean enableMenu = false;
List items =
menu.getComponents();
List menus = new ArrayList();
List menus = new ArrayList();
String currentURL =
request.getRequestURI();
if (log.isDebugEnabled())
{
log.debug("currentURL: " + currentURL);
}
log.debug("currentURL: " + currentURL);
}
// if the currentURL matches
any in the JobPostingMenu,
// perform logic to show/hide menus
if (items != null) {
for (Iterator it = items.iterator(); it.hasNext();) {
MenuComponent kid = (MenuComponent) it.next();
// perform logic to show/hide menus
if (items != null) {
for (Iterator it = items.iterator(); it.hasNext();) {
MenuComponent kid = (MenuComponent) it.next();
// using descriptions to match URLs since the
actions
// and forwards are determined in the taglib at runtime
String pattern = kid.getDescription().trim();
menus.add(kid.getName());
// and forwards are determined in the taglib at runtime
String pattern = kid.getDescription().trim();
menus.add(kid.getName());
if (log.isDebugEnabled())
{
log.debug("comparing itemURL: " + pattern);
}
log.debug("comparing itemURL: " + pattern);
}
boolean matchFound
=
Pattern.compile(pattern).matcher(currentURL).find();
Pattern.compile(pattern).matcher(currentURL).find();
//Pattern.matches(desc,
currentURL);
if (matchFound) {
enableMenu = true;
if (matchFound) {
enableMenu = true;
break;
}
}
}
}
}
}
if (log.isDebugEnabled())
{
log.debug(((enableMenu) ? "enabling" : "disabling") +
" JobPostingMenu");
}
log.debug(((enableMenu) ? "enabling" : "disabling") +
" JobPostingMenu");
}
if (enableMenu)
{
String positionId =
(request.getParameter("positionId") == null)
? request.getParameter("id") : request.getParameter("positionId");
request.getSession().setAttribute("positionId", positionId);
menusToEnable.add(menu.getName());
String positionId =
(request.getParameter("positionId") == null)
? request.getParameter("id") : request.getParameter("positionId");
request.getSession().setAttribute("positionId", positionId);
menusToEnable.add(menu.getName());
//
loop through, get the first one that matches, and then
enable
// all the ones up to that point
for (int i = 0; i < menus.size(); i++) {
menusToEnable.add((String) menus.get(i));
}
}
// all the ones up to that point
for (int i = 0; i < menus.size(); i++) {
menusToEnable.add((String) menus.get(i));
}
}
if (log.isDebugEnabled())
{
log.debug("allowed menus: " + menusToEnable);
}
log.debug("allowed menus: " + menusToEnable);
}
PermissionsAdapter permissions
=
new BreadcrumbsMenuAdapter(menusToEnable);
request.setAttribute("breadcrumbsAdapter", permissions);
new BreadcrumbsMenuAdapter(menusToEnable);
request.setAttribute("breadcrumbsAdapter", permissions);
My
BreadcrumbsMenuAdapter.java has the following code:
public
class BreadcrumbsMenuAdapter implements PermissionsAdapter
{
//~ Instance fields ========================================================
//~ Instance fields ========================================================
private Log log =
LogFactory.getLog(BreadcrumbsMenuAdapter.class);
private List menuNames;
private List menuNames;
//~ Constructors
===========================================================
/**
* Creates a new instance of MenuAdapter
*/
public BreadcrumbsMenuAdapter(List theMenuNames) {
menuNames = new ArrayList();
* Creates a new instance of MenuAdapter
*/
public BreadcrumbsMenuAdapter(List theMenuNames) {
menuNames = new ArrayList();
if (theMenuNames != null)
{
for (int i = 0; i < theMenuNames.size(); i++) {
menuNames.add((String) theMenuNames.get(i));
}
}
}
for (int i = 0; i < theMenuNames.size(); i++) {
menuNames.add((String) theMenuNames.get(i));
}
}
}
//~ Methods
================================================================
/**
* If the menu is allowed, this should return true.
*
* @return whether or not the menu is allowed.
*/
public boolean isAllowed(MenuComponent menu) {
return menuNames.contains(menu.getName());
}
}
* If the menu is allowed, this should return true.
*
* @return whether or not the menu is allowed.
*/
public boolean isAllowed(MenuComponent menu) {
return menuNames.contains(menu.getName());
}
}
I'll
try to write up some formal documentation for the website on this - it's
important stuff that should be there.
Matt
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Grassi Fabio
Sent: Wednesday, January 07, 2004 5:28 AM
To: [EMAIL PROTECTED]
Subject: R: [struts-menu] Disable/enable menu itemsThanx for the answer Matt, I'll have a deeper look at PermissionAdapter. By the way, how would a filter do the job? It should set some kind of attribute of TMenuItem, the problem is that I don't see what attribute to set. Are you suggesting tha a Filter shoul set some attribute that the PermissionAdapter should afterwards read when deciding if an item isAllowed?Ciao, Fabio.Ai sensi della Legge 675/96 si precisa che le informazioni contenute in questo messaggio sono riservate ed a uso esclusivo del destinatario. Qualora il messaggio in parola Le fosse pervenuto per errore, la preghiamo di eliminarlo senza copiarlo e di non inoltrarlo a terzi, dandocene gentilmente comunicazione. Grazie.-----Messaggio originale-----
Da: Matt Raible [mailto:[EMAIL PROTECTED]
Inviato: mercoledì 7 gennaio 2004 13:10
A: [EMAIL PROTECTED]
Oggetto: RE: [struts-menu] Disable/enable menu itemsYour best bet is the permissions adapter. http://struts-menu.sourceforge.net/security.html I usually use a Filter to enable/disable items based on parameters, regular expressions or URL patterns.HTH,Matt-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Grassi Fabio
Sent: Wednesday, January 07, 2004 2:56 AM
To: [EMAIL PROTECTED]
Subject: [struts-menu] Disable/enable menu itemsHi all, I would like to be able to enable / disable some of my menu items based on some scoped attribute or parameter. I was looking for a property which could accept expressions (possibly EL) evaluated at runtime. Is there already something similar built in struts-menu? I looked in the docs and the code and didn't find much.TIA, Fabio.Ai sensi della Legge 675/96 si precisa che le informazioni contenute in questo messaggio sono riservate ed a uso esclusivo del destinatario. Qualora il messaggio in parola Le fosse pervenuto per errore, la preghiamo di eliminarlo senza copiarlo e di non inoltrarlo a terzi, dandocene gentilmente comunicazione. Grazie.
This message for the law 675/96, may contain confidential and/or privileged information. If you are not the addressee or authorized to receive this for the addressee, you must not use, copy, disclose or take any action based on this message or any information herein. If you have received this message in error, please advise the sender immediately by reply e-mail and delete this message. Thank you for your cooperation.
This message for the law 675/96, may contain confidential and/or privileged information. If you are not the addressee or authorized to receive this for the addressee, you must not use, copy, disclose or take any action based on this message or any information herein. If you have received this message in error, please advise the sender immediately by reply e-mail and delete this message. Thank you for your cooperation.