Title: Messaggio
Thanks again Matt. I'll work on this examples and let you know. I agree that this should be a common issue deserving some more documentation. If I can I'll help.
 
Bye, Fabio.
-----Messaggio originale-----
Da: Matt Raible [mailto:[EMAIL PROTECTED]
Inviato: mercoledì 7 gennaio 2004 13:41
A: [EMAIL PROTECTED]
Oggetto: RE: [struts-menu] Disable/enable menu items

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);
 
        MenuComponent menu = repository.getMenu("JobPostingMenu");
        List menusToEnable = new ArrayList();
        boolean enableMenu = false;
 
        List items = menu.getComponents();
        List menus = new ArrayList();
 
        String currentURL = request.getRequestURI();
 
        if (log.isDebugEnabled()) {
            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();
 
                // 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());
 
                if (log.isDebugEnabled()) {
                    log.debug("comparing itemURL: " + pattern);
                }
 
                boolean matchFound =
                    Pattern.compile(pattern).matcher(currentURL).find();
 
                //Pattern.matches(desc, currentURL);
                if (matchFound) {
                    enableMenu = true;
 
                    break;
                }
            }
        }
 
        if (log.isDebugEnabled()) {
            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());
 
            // 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));
            }
        }
 
        if (log.isDebugEnabled()) {
            log.debug("allowed menus: " + menusToEnable);
        }
 
        PermissionsAdapter permissions =
            new BreadcrumbsMenuAdapter(menusToEnable);
        request.setAttribute("breadcrumbsAdapter", permissions);
My BreadcrumbsMenuAdapter.java has the following code:
 
public class BreadcrumbsMenuAdapter implements PermissionsAdapter {
    //~ Instance fields ========================================================
 
    private Log log = LogFactory.getLog(BreadcrumbsMenuAdapter.class);
    private List menuNames;
 
    //~ Constructors ===========================================================
 
    /**
     * 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));
            }
        }
    }
 
    //~ 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());
    }
}
 
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 items

Thanx 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.
-----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 items

Your 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 items

Hi 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.
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.
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.

Reply via email to