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
.argeo
.util
.directory
.FunctionalGroup
;
14 import org
.argeo
.util
.directory
.Organization
;
15 import org
.argeo
.util
.directory
.SystemPermissions
;
16 import org
.osgi
.service
.useradmin
.Role
;
18 /** Directory group implementation */
19 abstract class LdifGroup
extends LdifUser
implements DirectoryGroup
{
20 private final String memberAttributeId
;
22 LdifGroup(AbstractUserDirectory userAdmin
, LdapName dn
, Attributes attributes
) {
23 super(userAdmin
, dn
, attributes
);
24 memberAttributeId
= userAdmin
.getMemberAttributeId();
28 public boolean addMember(Role role
) {
30 Role foundRole
= findRole(new LdapName(role
.getName()));
31 if (foundRole
== null)
32 throw new UnsupportedOperationException(
33 "Adding role " + role
.getName() + " is unsupported within this context.");
34 } catch (InvalidNameException e
) {
35 throw new IllegalArgumentException("Role name" + role
.getName() + " is badly formatted");
38 getUserAdmin().checkEdit();
42 Attribute member
= getAttributes().get(memberAttributeId
);
44 if (member
.contains(role
.getName()))
47 member
.add(role
.getName());
49 getAttributes().put(memberAttributeId
, role
.getName());
54 public boolean addRequiredMember(Role role
) {
55 throw new UnsupportedOperationException();
59 public boolean removeMember(Role role
) {
60 getUserAdmin().checkEdit();
64 Attribute member
= getAttributes().get(memberAttributeId
);
66 if (!member
.contains(role
.getName()))
68 member
.remove(role
.getName());
75 public Role
[] getMembers() {
76 List
<Role
> directMembers
= new ArrayList
<Role
>();
77 for (LdapName ldapName
: getMemberNames()) {
78 Role role
= findRole(ldapName
);
80 throw new IllegalStateException("Role " + ldapName
+ " not found.");
82 directMembers
.add(role
);
84 return directMembers
.toArray(new Role
[directMembers
.size()]);
88 * Whether a role with this name can be found from this context.
90 * @return The related {@link Role} or <code>null</code>.
92 protected Role
findRole(LdapName ldapName
) {
93 Role role
= getUserAdmin().getRole(ldapName
.toString());
95 if (getUserAdmin().getExternalRoles() != null)
96 role
= getUserAdmin().getExternalRoles().getRole(ldapName
.toString());
102 public List
<LdapName
> getMemberNames() {
103 Attribute memberAttribute
= getAttributes().get(memberAttributeId
);
104 if (memberAttribute
== null)
105 return new ArrayList
<LdapName
>();
107 List
<LdapName
> roles
= new ArrayList
<LdapName
>();
108 NamingEnumeration
<?
> values
= memberAttribute
.getAll();
109 while (values
.hasMore()) {
110 LdapName dn
= new LdapName(values
.next().toString());
114 } catch (NamingException e
) {
115 throw new IllegalStateException("Cannot get members", e
);
120 public Role
[] getRequiredMembers() {
121 throw new UnsupportedOperationException();
125 public int getType() {
132 static class LdifFunctionalGroup
extends LdifGroup
implements FunctionalGroup
{
134 public LdifFunctionalGroup(AbstractUserDirectory userAdmin
, LdapName dn
, Attributes attributes
) {
135 super(userAdmin
, dn
, attributes
);
140 static class LdifOrganization
extends LdifGroup
implements Organization
{
142 public LdifOrganization(AbstractUserDirectory userAdmin
, LdapName dn
, Attributes attributes
) {
143 super(userAdmin
, dn
, attributes
);
148 static class LdifSystemPermissions
extends LdifGroup
implements SystemPermissions
{
150 public LdifSystemPermissions(AbstractUserDirectory userAdmin
, LdapName dn
, Attributes attributes
) {
151 super(userAdmin
, dn
, attributes
);