0

Post Global Command From Listener

asked 2015-03-16 03:49:03 +0800

denilugito gravatar image denilugito
1 1

updated 2015-03-16 03:50:04 +0800

Hi Guys,

I'm new to zk, i would like to ask whther is it possible to post global command from event interceptor listener? I have implementation of event interceptor that will post a global command after a certain condition is met. Bellow is my snippet code:

public class EventInterceptors extends AbstractUtil implements EventInterceptor { AuthenticationService authService; UserDAOImpl userDAOInst; UserCredential userCre; User userCheckViewAccess;

public Event beforeSendEvent(Event event) {
    // TODO Auto-generated method stub
    return event;
}

public Event beforePostEvent(Event event) {
    // TODO Auto-generated method stub      
    return event;
}

public void afterProcessEvent(Event event) {
    // TODO Auto-generated method stub
    // implement event interceptor for page view access             
    userCre = authService.getUserCredential();              

    if (userCre != null) {
        if (userCre.getCurrentPageID() != null && !userCre.getRoles().equals(AbcConstantsUtil.HARDCODEDROLE.SUPERUSER)) {
            String currentPageID = userCre.getCurrentPageID();                  
            String currentUserID = userCre.getaccount();

            System.out.println("User Session ID: " + currentUserID);

            // check whether the current user ID has access to the current view_ID
            userCheckViewAccess = new User();
            userCheckViewAccess = userDAOInst.getJoinView(currentUserID, currentPageID);

            // if he / she doesn't have the access redirect it to the unauthorized page  
            if (userCheckViewAccess == null) {
                event.stopPropagation();    
                Clients.showNotification("Access Violation Ocurred");                                       
                if (!event.isPropagatable()) {
                    postSideBarNavGlobalCommand(PageNavigationConstants.pageRedirection.unAuthPage.VIEWID, 
                            PageNavigationConstants.pageRedirection.unAuthPage.VIEWLABEL, 
                            PageNavigationConstants.pageRedirection.unAuthPage.VIEWTYPE,
                            PageNavigationConstants.pageRedirection.unAuthPage.VIEWURI);
                }                                       
            }
        }
    }
}

}

Currently, when i'm doing this, when the access violation occured (triggered by any event intercepted in accordance to the conditional statement), the page just keeps loading and it stuck in an infinite loop (keeps printing "User Session ID: " + currentUserID in syso).. Has anybody experienced this before?

delete flag offensive retag edit

Comments

this is the live update for security roles correct?

chillworld ( 2015-03-16 05:22:40 +0800 )edit

Yes, I have tried the global command with "Application" Scope, however it affectted all of users (including the user with admin role will also be redirected into un - authorized page). Is it possible to post global command only to user with certain session id ? (only for the unauth user)?

denilugito ( 2015-03-16 06:12:15 +0800 )edit

Therefore, i'm trying a different approach which involved event interceptor (without knowing whether is it possible or not)..

denilugito ( 2015-03-16 06:23:20 +0800 )edit

your way with the global command in the application scope is correct, you just have to make your case correct for who you want to be redirected. if it's only for unauthenticated users, you can ask the security context if the current user is authenticated

chillworld ( 2015-03-16 06:36:24 +0800 )edit

I'll post you some code with the eventqueue

chillworld ( 2015-03-16 06:37:19 +0800 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2015-03-16 06:55:08 +0800

chillworld gravatar image chillworld flag of Belgium
5322 4 9
https://github.com/chillw...

updated 2015-03-16 12:10:05 +0800

AbstractVM

package be.chillworld;

import org.zkoss.bind.annotation.Init;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.EventQueues;
import org.zkoss.zk.ui.util.Clients;

/**
 *
 * @author chillworld
 */
public abstract class AbstractVM {

    @Init
    public void abstractInit() {
        EventQueues.lookup("changeRoles", EventQueues.APPLICATION, true).subscribe(new EventListener<Event>() {

            public void onEvent(Event event) throws Exception {
                if ("onChangeRoles".equals(event.getName()) && authService.getUserCredential().getaccount().equals((String)event.getData()) {
                    // This will be triggered for that specific user.
                    // you can redirect if you want of do other stuff. 
                }
            }
        });
    }
}

So now every Viewmodel have to extend the AbstractVM but there is 1 part even so important then just extending it, namely making a method who has @Init(superclass=true) annotation, even with no code.
If we don't do this, the init method from the abstractVM will never be triggered!

Your command for triggering the whole thing

@Command
public void changeRoles () {
    String userAccount;
    // Fill in userAccount for the targeted user.
    // Put the code for changing the roles.
    EventQueues.lookup("changeRoles", EventQueues.APPLICATION, true).publish(new Event("onChangeRoles", null, userAccount));
    }
}

If you have MVC classes, create a BasicComposer :

package be.chillworld;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.EventQueues;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.util.Clients;

/**
 *
 * @author chillworld
 */
public class BasicComposer extends SelectorComposer<Component> {

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        EventQueues.lookup("changeRoles", EventQueues.APPLICATION, true).subscribe(new EventListener<Event>() {

                   public void onEvent(Event event) throws Exception {
                       if ("onChangeRoles".equals(event.getName()) && authService.getUserCredential().getaccount().equals((String)event.getData()) {
                           // This will be triggered for that specific user.
                           // you can redirect if you want of do other stuff. 
                       }
                   }
               });
    }

}

Greetz chill.

link publish delete flag offensive edit

Comments

+1 my friend

cyiannoulis ( 2015-03-16 11:57:52 +0800 )edit

thx cyiannoulis, edit was a little mistake in the name of the eventqueue who wasn't consistent

chillworld ( 2015-03-16 12:09:36 +0800 )edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2015-03-16 03:49:03 +0800

Seen: 43 times

Last updated: Mar 16 '15

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More