1 package org
.argeo
.osgi
.useradmin
;
3 import java
.util
.ArrayList
;
6 import javax
.naming
.InvalidNameException
;
7 import javax
.naming
.NamingEnumeration
;
8 import javax
.naming
.NamingException
;
9 import javax
.naming
.directory
.Attribute
;
10 import javax
.naming
.directory
.Attributes
;
11 import javax
.naming
.ldap
.LdapName
;
13 import org
.osgi
.service
.useradmin
.Role
;
15 /** Directory group implementation */
16 abstract class LdifGroup
extends LdifUser
implements DirectoryGroup
{
17 private final String memberAttributeId
;
19 LdifGroup(AbstractUserDirectory userAdmin
, LdapName dn
, Attributes attributes
) {
20 super(userAdmin
, dn
, attributes
);
21 memberAttributeId
= userAdmin
.getMemberAttributeId();
25 public boolean addMember(Role role
) {
27 Role foundRole
= findRole(new LdapName(role
.getName()));
28 if (foundRole
== null)
29 throw new UnsupportedOperationException(
30 "Adding role " + role
.getName() + " is unsupported within this context.");
31 } catch (InvalidNameException e
) {
32 throw new IllegalArgumentException("Role name" + role
.getName() + " is badly formatted");
35 getUserAdmin().checkEdit();
39 Attribute member
= getAttributes().get(memberAttributeId
);
41 if (member
.contains(role
.getName()))
44 member
.add(role
.getName());
46 getAttributes().put(memberAttributeId
, role
.getName());
51 public boolean addRequiredMember(Role role
) {
52 throw new UnsupportedOperationException();
56 public boolean removeMember(Role role
) {
57 getUserAdmin().checkEdit();
61 Attribute member
= getAttributes().get(memberAttributeId
);
63 if (!member
.contains(role
.getName()))
65 member
.remove(role
.getName());
72 public Role
[] getMembers() {
73 List
<Role
> directMembers
= new ArrayList
<Role
>();
74 for (LdapName ldapName
: getMemberNames()) {
75 Role role
= findRole(ldapName
);
77 throw new IllegalStateException("Role " + ldapName
+ " not found.");
79 directMembers
.add(role
);
81 return directMembers
.toArray(new Role
[directMembers
.size()]);
85 * Whether a role with this name can be found from this context.
87 * @return The related {@link Role} or <code>null</code>.
89 protected Role
findRole(LdapName ldapName
) {
90 Role role
= getUserAdmin().getRole(ldapName
.toString());
92 if (getUserAdmin().getExternalRoles() != null)
93 role
= getUserAdmin().getExternalRoles().getRole(ldapName
.toString());
99 public List
<LdapName
> getMemberNames() {
100 Attribute memberAttribute
= getAttributes().get(memberAttributeId
);
101 if (memberAttribute
== null)
102 return new ArrayList
<LdapName
>();
104 List
<LdapName
> roles
= new ArrayList
<LdapName
>();
105 NamingEnumeration
<?
> values
= memberAttribute
.getAll();
106 while (values
.hasMore()) {
107 LdapName dn
= new LdapName(values
.next().toString());
111 } catch (NamingException e
) {
112 throw new IllegalStateException("Cannot get members", e
);
117 public Role
[] getRequiredMembers() {
118 throw new UnsupportedOperationException();
122 public int getType() {
129 static class LdifFunctionalGroup
extends LdifGroup
implements FunctionalGroup
{
131 public LdifFunctionalGroup(AbstractUserDirectory userAdmin
, LdapName dn
, Attributes attributes
) {
132 super(userAdmin
, dn
, attributes
);
137 static class LdifOrganization
extends LdifGroup
implements Organization
{
139 public LdifOrganization(AbstractUserDirectory userAdmin
, LdapName dn
, Attributes attributes
) {
140 super(userAdmin
, dn
, attributes
);
145 static class LdifSystemPermissions
extends LdifGroup
implements SystemPermissions
{
147 public LdifSystemPermissions(AbstractUserDirectory userAdmin
, LdapName dn
, Attributes attributes
) {
148 super(userAdmin
, dn
, attributes
);