/**
* Copyright 2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.bricket.web;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.request.target.basic.RedirectRequestTarget;
import org.bricket.plugin.role.service.RoleService.Roles;
import org.bricket.plugin.user.domain.User;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import brix.BrixNodeModel;
import brix.jcr.wrapper.BrixNode;
/**
* Basic bricket panel with common utility methods.
*
* @author Ingo Renner
* @author Henning Teek
*/
public abstract class BricketPanel extends Panel {
private BricketFeedbackPanel feedback;
private IModel<BrixNode> tileNode;
public BricketPanel(String id) {
super(id);
}
public BricketPanel(String id, IModel<BrixNode> tileNode) {
this(id);
this.tileNode = tileNode;
}
@Override
protected void onConfigure() {
this.feedback = getApplication() instanceof BricketApplication ? ((BricketApplication) getApplication())
.getBricketFeedbackPanel(getPage()) : null;
}
@Override
protected void onBeforeRender() {
if (!hasBeenRendered()) {
initGui();
}
super.onBeforeRender();
}
protected abstract void initGui();
/**
* @return the bricket feedback panel
*/
public BricketFeedbackPanel getFeedback() {
return feedback;
}
/**
* @return the logged in user
*/
public User getUser() {
return ((BricketWebSession) getSession()).getUser();
}
/**
* @return the tileNode
*/
public IModel<BrixNode> getTileNode() {
return tileNode;
}
/**
* @return true if a user is signed in. false otherwise
*/
public boolean isSignedIn() {
return ((BricketWebSession) getSession()).isSignedIn();
}
/**
* @param path
* The path to a page
* @return the i18n link for the path
*/
public String getI18nLink(String path) {
return getBricketWebSession().getI18nLink(path);
}
/**
* @param bnm
* The BrixNodeModel for the link
* @return the link to the given model
*/
public String getBrixLinkUrl(BrixNodeModel bnm) {
return getBricketApplication().getBrixLinkUrl(bnm, getRequestCycle());
}
/**
* @param path
* the path for the link
* @return the link to the path
*/
public String getBrixLinkUrl(String path) {
return getBrixLinkUrl(getBricketApplication().getBrixLink(path));
}
/**
* @param bnm
* the BrixNodeModel for the redirect
*/
public void redirectTo(BrixNodeModel bnm) {
getRequestCycle().setRequestTarget(new RedirectRequestTarget(getBrixLinkUrl(bnm)));
}
/**
* @param path
* the path for the redirect
*/
public void redirectTo(String path) {
getRequestCycle().setRequestTarget(new RedirectRequestTarget(getBrixLinkUrl(path)));
}
/**
* @return current authenticated wicket web session
*/
public BricketWebSession getBricketWebSession() {
return (BricketWebSession) BricketWebSession.get();
}
/**
* @return the bricket application
*/
public BricketApplication getBricketApplication() {
return (BricketApplication) this.getApplication();
}
public boolean isSuperuser() {
return hasRole(Roles.ROLE_SUPERUSER.name());
}
public boolean hasRole(String role) {
for (GrantedAuthority grantedAuthority : SecurityContextHolder.getContext().getAuthentication()
.getAuthorities()) {
if (grantedAuthority.getAuthority().equals(role)) {
return true;
}
}
return false;
}
}