]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.security.ui.admin/src/org/argeo/security/ui/admin/internal/UiAdminUtils.java
136c41540957913db11dc7666be5deea1d489163
[lgpl/argeo-commons.git] / org.argeo.security.ui.admin / src / org / argeo / security / ui / admin / internal / UiAdminUtils.java
1 package org.argeo.security.ui.admin.internal;
2
3 import java.security.AccessController;
4
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;
11
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;
19
20 /** First effort to centralize back end methods used by the user admin UI */
21 public class UiAdminUtils {
22
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));
27 }
28
29 public final static boolean isCurrentUser(User user) {
30 String userName = UiAdminUtils.getProperty(user,
31 UserAdminConstants.KEY_DN);
32 try {
33 LdapName selfUserName = UiAdminUtils.getLdapName();
34 LdapName userLdapName = new LdapName(userName);
35 if (userLdapName.equals(selfUserName))
36 return true;
37 else
38 return false;
39 } catch (InvalidNameException e) {
40 throw new ArgeoException("User " + user + " has an unvalid dn: "
41 + userName, e);
42 }
43 }
44
45 public final static LdapName getLdapName() {
46 Subject subject = Subject.getSubject(AccessController.getContext());
47 String name = subject.getPrincipals(X500Principal.class).iterator()
48 .next().toString();
49 LdapName dn;
50 try {
51 dn = new LdapName(name);
52 } catch (InvalidNameException e) {
53 throw new ArgeoException("Invalid user dn " + name, e);
54 }
55 return dn;
56 }
57
58 public final static User getUser(UserAdmin userAdmin, LdapName dn) {
59 User user = userAdmin.getUser(UserAdminConstants.KEY_DN, dn.toString());
60 return user;
61 }
62
63 public final static String getUsername(User user) {
64 String cn = getProperty(user, UserAdminConstants.KEY_CN);
65 if (isEmpty(cn))
66 cn = getProperty(user, UserAdminConstants.KEY_UID);
67 return cn;
68 }
69
70 public final static String getProperty(Role role, String key) {
71 Object obj = role.getProperties().get(key);
72 if (obj != null)
73 return (String) obj;
74 else
75 return "";
76 }
77
78 public final static String getDefaultCn(String firstName, String lastName) {
79 return (firstName.trim() + " " + lastName.trim() + " ").trim();
80 }
81
82 /*
83 * INTERNAL METHODS: Below methods are meant to stay here and are not part
84 * of a potential generic backend to manage the useradmin
85 */
86 public final static boolean notNull(String string) {
87 if (string == null)
88 return false;
89 else
90 return !"".equals(string.trim());
91 }
92
93 public final static boolean isEmpty(String string) {
94 if (string == null)
95 return true;
96 else
97 return "".equals(string.trim());
98 }
99
100 /** Must be called from the UI Thread. */
101 public final static void beginTransactionIfNeeded(
102 UserTransaction userTransaction) {
103 try {
104 if (userTransaction.getStatus() == Status.STATUS_NO_TRANSACTION) {
105 userTransaction.begin();
106 notifyTransactionStateChange(userTransaction);
107 }
108 } catch (Exception e) {
109 throw new ArgeoException("Unable to begin transaction", e);
110 }
111 }
112
113 /** Easily notify the ActiveWindow that the transaction had a state change */
114 public final static void notifyTransactionStateChange(
115 UserTransaction userTransaction) {
116 try {
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);
126 }
127 }
128 }