]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.security.core/src/org/argeo/osgi/useradmin/LdifUserAdmin.java
1 package org
.argeo
.osgi
.useradmin
;
3 import java
.io
.InputStream
;
5 import java
.net
.URISyntaxException
;
6 import java
.util
.SortedMap
;
7 import java
.util
.TreeMap
;
9 import javax
.naming
.InvalidNameException
;
10 import javax
.naming
.NamingEnumeration
;
11 import javax
.naming
.directory
.Attributes
;
12 import javax
.naming
.ldap
.LdapName
;
14 import org
.osgi
.framework
.InvalidSyntaxException
;
15 import org
.osgi
.service
.useradmin
.Authorization
;
16 import org
.osgi
.service
.useradmin
.Role
;
17 import org
.osgi
.service
.useradmin
.User
;
18 import org
.osgi
.service
.useradmin
.UserAdmin
;
20 /** User admin implementation using LDIF file(s) as backend. */
21 public class LdifUserAdmin
implements UserAdmin
{
22 SortedMap
<LdapName
, LdifUser
> users
= new TreeMap
<LdapName
, LdifUser
>();
23 SortedMap
<LdapName
, LdifGroup
> groups
= new TreeMap
<LdapName
, LdifGroup
>();
25 private final boolean isReadOnly
;
26 private final URI uri
;
28 public LdifUserAdmin(String uri
) {
32 public LdifUserAdmin(String uri
, boolean isReadOnly
) {
33 this.isReadOnly
= isReadOnly
;
35 this.uri
= new URI(uri
);
36 } catch (URISyntaxException e
) {
37 throw new ArgeoUserAdminException("Invalid URI " + uri
, e
);
40 if (!isReadOnly
&& !this.uri
.getScheme().equals("file:"))
41 throw new UnsupportedOperationException(this.uri
.getScheme()
42 + "not supported read-write.");
45 load(this.uri
.toURL().openStream());
46 } catch (Exception e
) {
47 throw new ArgeoUserAdminException("Cannot open URL " + this.uri
, e
);
51 public LdifUserAdmin(InputStream in
) {
57 protected void load(InputStream in
) {
59 LdifParser ldifParser
= new LdifParser();
60 SortedMap
<LdapName
, Attributes
> allEntries
= ldifParser
.read(in
);
61 for (LdapName key
: allEntries
.keySet()) {
62 Attributes attributes
= allEntries
.get(key
);
63 NamingEnumeration
<?
> objectClasses
= attributes
.get(
64 "objectClass").getAll();
65 objectClasses
: while (objectClasses
.hasMore()) {
66 String objectClass
= objectClasses
.next().toString();
67 if (objectClass
.equals("inetOrgPerson")) {
68 users
.put(key
, new LdifUser(key
, attributes
));
70 } else if (objectClass
.equals("groupOfNames")) {
71 groups
.put(key
, new LdifGroup(key
, attributes
));
78 for (LdifGroup group
: groups
.values()) {
79 group
.loadMembers(this);
81 } catch (Exception e
) {
82 throw new ArgeoUserAdminException(
83 "Cannot load user admin service from LDIF", e
);
87 public void destroy() {
95 public Role
getRole(String name
) {
98 key
= new LdapName(name
);
99 } catch (InvalidNameException e
) {
100 // TODO implements default base DN
101 throw new IllegalArgumentException("Badly formatted role name: "
105 if (groups
.containsKey(key
))
106 return groups
.get(key
);
107 if (users
.containsKey(key
))
108 return users
.get(key
);
113 public Authorization
getAuthorization(User user
) {
114 return new LdifAuthorization((LdifUser
) user
);
118 public Role
createRole(String name
, int type
) {
119 throw new UnsupportedOperationException();
123 public boolean removeRole(String name
) {
124 throw new UnsupportedOperationException();
128 public Role
[] getRoles(String filter
) throws InvalidSyntaxException
{
129 throw new UnsupportedOperationException();
133 public User
getUser(String key
, String value
) {
134 throw new UnsupportedOperationException();
137 public boolean getIsReadOnly() {