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-
FEATURED COMPONENTS
First time here? Check out the FAQ!
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?
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!
@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));
}
}
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.
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 )editAsked: 2015-03-16 03:49:03 +0800
Seen: 46 times
Last updated: Mar 16 '15
How to Call Child ViewModel Method from Parent Window? [closed]
@global-command, how to set queueName when invoke globalCommand?
how can i get data from another page
Form validation before global command
Equivalent to global command on MVC ?
Why does the order of NotifyChange may alter the result of the objects?
this is the live update for security roles correct?
chillworld ( 2015-03-16 05:22:40 +0800 )editYes, 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 )editTherefore, 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 )edityour 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 )editI'll post you some code with the eventqueue
chillworld ( 2015-03-16 06:37:19 +0800 )edit