1 package org
.argeo
.cms
.e4
.addons
;
3 import java
.security
.AccessController
;
4 import java
.util
.Iterator
;
6 import javax
.annotation
.PostConstruct
;
7 import javax
.security
.auth
.Subject
;
8 import javax
.servlet
.http
.HttpServletRequest
;
10 import org
.apache
.commons
.logging
.Log
;
11 import org
.apache
.commons
.logging
.LogFactory
;
12 import org
.argeo
.cms
.CmsException
;
13 import org
.argeo
.cms
.auth
.CurrentUser
;
14 import org
.eclipse
.e4
.ui
.model
.application
.MApplication
;
15 import org
.eclipse
.e4
.ui
.model
.application
.ui
.MElementContainer
;
16 import org
.eclipse
.e4
.ui
.model
.application
.ui
.MUIElement
;
17 import org
.eclipse
.e4
.ui
.model
.application
.ui
.basic
.MTrimBar
;
18 import org
.eclipse
.e4
.ui
.model
.application
.ui
.basic
.MTrimmedWindow
;
19 import org
.eclipse
.e4
.ui
.model
.application
.ui
.basic
.MWindow
;
21 public class AuthAddon
{
22 private final static Log log
= LogFactory
.getLog(AuthAddon
.class);
24 public final static String AUTH
= "auth.";
27 void init(MApplication application
) {
28 Iterator
<MWindow
> windows
= application
.getChildren().iterator();
29 boolean atLeastOneTopLevelWindowVisible
= false;
30 windows
: while (windows
.hasNext()) {
31 MWindow window
= windows
.next();
33 boolean windowVisible
= process(window
);
38 atLeastOneTopLevelWindowVisible
= true;
40 if (window
instanceof MTrimmedWindow
) {
41 Iterator
<MTrimBar
> trimBars
= ((MTrimmedWindow
) window
).getTrimBars().iterator();
42 while (trimBars
.hasNext()) {
43 MTrimBar trimBar
= trimBars
.next();
44 if (!process(trimBar
)) {
51 if (!atLeastOneTopLevelWindowVisible
) {
52 log
.warn("No top-level window is authorized for user " + CurrentUser
.getUsername() + ", logging out..");
57 protected boolean process(MUIElement element
) {
58 for (String tag
: element
.getTags()) {
59 if (tag
.startsWith(AUTH
)) {
60 String role
= tag
.substring(AUTH
.length(), tag
.length());
61 if (!CurrentUser
.isInRole(role
)) {
62 element
.setVisible(false);
63 element
.setToBeRendered(false);
70 if (element
instanceof MElementContainer
) {
71 @SuppressWarnings("unchecked")
72 MElementContainer
<?
extends MUIElement
> container
= (MElementContainer
<?
extends MUIElement
>) element
;
73 Iterator
<?
extends MUIElement
> children
= container
.getChildren().iterator();
74 while (children
.hasNext()) {
75 MUIElement child
= children
.next();
76 boolean visible
= process(child
);
81 for (Object child
: container
.getChildren()) {
82 if (child
instanceof MUIElement
) {
83 boolean visible
= process((MUIElement
) child
);
85 container
.getChildren().remove(child
);
93 protected void logout() {
94 Subject subject
= Subject
.getSubject(AccessController
.getContext());
96 CurrentUser
.logoutCmsSession(subject
);
97 } catch (Exception e
) {
98 throw new CmsException("Cannot log out", e
);
100 HttpServletRequest request
= org
.argeo
.eclipse
.ui
.specific
.UiContext
.getHttpRequest();
102 request
.getSession().setMaxInactiveInterval(0);