View Javadoc
1   package org.argeo.cms.e4.maintenance;
2   
3   import java.net.URI;
4   
5   import org.argeo.cms.ui.util.CmsUiUtils;
6   import org.eclipse.swt.SWT;
7   import org.eclipse.swt.layout.GridData;
8   import org.eclipse.swt.widgets.Composite;
9   import org.eclipse.swt.widgets.Label;
10  import org.osgi.framework.InvalidSyntaxException;
11  import org.osgi.framework.ServiceReference;
12  import org.osgi.service.useradmin.Role;
13  import org.osgi.service.useradmin.UserAdmin;
14  
15  class SecurityDeploymentUi extends AbstractOsgiComposite {
16  	private static final long serialVersionUID = 590221539553514693L;
17  
18  	public SecurityDeploymentUi(Composite parent, int style) {
19  		super(parent, style);
20  	}
21  
22  	@Override
23  	protected void initUi(int style) {
24  		if (isDeployed()) {
25  			initCurrentUi(this);
26  		} else {
27  			initNewUi(this);
28  		}
29  	}
30  
31  	private void initNewUi(Composite parent) {
32  		new Label(parent, SWT.NONE).setText("Security is not configured");
33  	}
34  
35  	private void initCurrentUi(Composite parent) {
36  		ServiceReference<UserAdmin> userAdminRef = bc.getServiceReference(UserAdmin.class);
37  		UserAdmin userAdmin = bc.getService(userAdminRef);
38  		StringBuffer text = new StringBuffer();
39  		text.append("<span style='font-variant: small-caps;'>Domains</span><br/>");
40  		domains: for (String key : userAdminRef.getPropertyKeys()) {
41  			if (!key.startsWith("/"))
42  				continue domains;
43  			URI uri;
44  			try {
45  				uri = new URI(key);
46  			} catch (Exception e) {
47  				// ignore non URI keys
48  				continue domains;
49  			}
50  
51  			String rootDn = uri.getPath().substring(1, uri.getPath().length());
52  			// FIXME make reading query options more robust, using utils
53  			boolean readOnly = uri.getQuery().equals("readOnly=true");
54  			if (readOnly)
55  				text.append("<span style='font-weight:bold;font-style: italic'>");
56  			else
57  				text.append("<span style='font-weight:bold'>");
58  
59  			text.append(rootDn);
60  			text.append("</span><br/>");
61  			try {
62  				Role[] roles = userAdmin.getRoles("(dn=*," + rootDn + ")");
63  				long userCount = 0;
64  				long groupCount = 0;
65  				for (Role role : roles) {
66  					if (role.getType() == Role.USER)
67  						userCount++;
68  					else
69  						groupCount++;
70  				}
71  				text.append(" " + userCount + " users, " + groupCount +" groups.<br/>");
72  			} catch (InvalidSyntaxException e) {
73  				log.error("Invalid syntax", e);
74  			}
75  		}
76  		Label label = new Label(parent, SWT.NONE);
77  		label.setData(new GridData(SWT.FILL, SWT.FILL, false, false));
78  		CmsUiUtils.markup(label);
79  		label.setText(text.toString());
80  	}
81  
82  	protected boolean isDeployed() {
83  		return bc.getServiceReference(UserAdmin.class) != null;
84  	}
85  }