1 package org
.argeo
.security
.ui
.admin
.internal
;
3 import java
.security
.AccessController
;
5 import javax
.naming
.InvalidNameException
;
6 import javax
.naming
.ldap
.LdapName
;
7 import javax
.security
.auth
.Subject
;
8 import javax
.security
.auth
.x500
.X500Principal
;
9 import javax
.transaction
.Status
;
10 import javax
.transaction
.UserTransaction
;
12 import org
.argeo
.ArgeoException
;
13 import org
.eclipse
.ui
.IWorkbenchWindow
;
14 import org
.eclipse
.ui
.PlatformUI
;
15 import org
.eclipse
.ui
.services
.ISourceProviderService
;
16 import org
.osgi
.service
.useradmin
.Role
;
17 import org
.osgi
.service
.useradmin
.User
;
18 import org
.osgi
.service
.useradmin
.UserAdmin
;
20 /** First effort to centralize back end methods used by the user admin UI */
21 public class UiAdminUtils
{
23 /** returns the local name of the current connected user */
24 public final static String
getUsername(UserAdmin userAdmin
) {
25 LdapName dn
= getLdapName();
26 return getUsername(getUser(userAdmin
, dn
));
29 public final static boolean isCurrentUser(User user
) {
30 String userName
= UiAdminUtils
.getProperty(user
,
31 UserAdminConstants
.KEY_DN
);
33 LdapName selfUserName
= UiAdminUtils
.getLdapName();
34 LdapName userLdapName
= new LdapName(userName
);
35 if (userLdapName
.equals(selfUserName
))
39 } catch (InvalidNameException e
) {
40 throw new ArgeoException("User " + user
+ " has an unvalid dn: "
45 public final static LdapName
getLdapName() {
46 Subject subject
= Subject
.getSubject(AccessController
.getContext());
47 String name
= subject
.getPrincipals(X500Principal
.class).iterator()
51 dn
= new LdapName(name
);
52 } catch (InvalidNameException e
) {
53 throw new ArgeoException("Invalid user dn " + name
, e
);
58 public final static User
getUser(UserAdmin userAdmin
, LdapName dn
) {
59 User user
= userAdmin
.getUser(UserAdminConstants
.KEY_DN
, dn
.toString());
63 public final static String
getUsername(User user
) {
64 String cn
= getProperty(user
, UserAdminConstants
.KEY_CN
);
66 cn
= getProperty(user
, UserAdminConstants
.KEY_UID
);
70 public final static String
getProperty(Role role
, String key
) {
71 Object obj
= role
.getProperties().get(key
);
78 public final static String
getDefaultCn(String firstName
, String lastName
) {
79 return (firstName
.trim() + " " + lastName
.trim() + " ").trim();
83 * INTERNAL METHODS: Below methods are meant to stay here and are not part
84 * of a potential generic backend to manage the useradmin
86 public final static boolean notNull(String string
) {
90 return !"".equals(string
.trim());
93 public final static boolean isEmpty(String string
) {
97 return "".equals(string
.trim());
100 /** Must be called from the UI Thread. */
101 public final static void beginTransactionIfNeeded(
102 UserTransaction userTransaction
) {
104 if (userTransaction
.getStatus() == Status
.STATUS_NO_TRANSACTION
) {
105 userTransaction
.begin();
106 notifyTransactionStateChange(userTransaction
);
108 } catch (Exception e
) {
109 throw new ArgeoException("Unable to begin transaction", e
);
113 /** Easily notify the ActiveWindow that the transaction had a state change */
114 public final static void notifyTransactionStateChange(
115 UserTransaction userTransaction
) {
117 IWorkbenchWindow aww
= PlatformUI
.getWorkbench()
118 .getActiveWorkbenchWindow();
119 ISourceProviderService sourceProviderService
= (ISourceProviderService
) aww
120 .getService(ISourceProviderService
.class);
121 UserTransactionProvider esp
= (UserTransactionProvider
) sourceProviderService
122 .getSourceProvider(UserTransactionProvider
.TRANSACTION_STATE
);
123 esp
.fireTransactionStateChange();
124 } catch (Exception e
) {
125 throw new ArgeoException("Unable to begin transaction", e
);