1 package org
.argeo
.osgi
.useradmin
;
3 import java
.util
.ArrayList
;
6 import javax
.naming
.InvalidNameException
;
7 import javax
.naming
.directory
.Attribute
;
8 import javax
.naming
.directory
.Attributes
;
9 import javax
.naming
.ldap
.LdapName
;
11 import org
.argeo
.util
.directory
.FunctionalGroup
;
12 import org
.argeo
.util
.directory
.Organization
;
13 import org
.argeo
.util
.directory
.SystemPermissions
;
14 import org
.argeo
.util
.directory
.ldap
.AbstractLdapDirectory
;
15 import org
.osgi
.service
.useradmin
.Role
;
17 /** Directory group implementation */
18 abstract class LdifGroup
extends LdifUser
implements DirectoryGroup
{
19 private final String memberAttributeId
;
21 LdifGroup(AbstractLdapDirectory userAdmin
, LdapName dn
, Attributes attributes
) {
22 super(userAdmin
, dn
, attributes
);
23 memberAttributeId
= userAdmin
.getMemberAttributeId();
27 public boolean addMember(Role role
) {
29 Role foundRole
= findRole(new LdapName(role
.getName()));
30 if (foundRole
== null)
31 throw new UnsupportedOperationException(
32 "Adding role " + role
.getName() + " is unsupported within this context.");
33 } catch (InvalidNameException e
) {
34 throw new IllegalArgumentException("Role name" + role
.getName() + " is badly formatted");
37 getUserAdmin().checkEdit();
41 Attribute member
= getAttributes().get(memberAttributeId
);
43 if (member
.contains(role
.getName()))
46 member
.add(role
.getName());
48 getAttributes().put(memberAttributeId
, role
.getName());
53 public boolean addRequiredMember(Role role
) {
54 throw new UnsupportedOperationException();
58 public boolean removeMember(Role role
) {
59 getUserAdmin().checkEdit();
63 Attribute member
= getAttributes().get(memberAttributeId
);
65 if (!member
.contains(role
.getName()))
67 member
.remove(role
.getName());
74 public Role
[] getMembers() {
75 List
<Role
> directMembers
= new ArrayList
<Role
>();
76 for (LdapName ldapName
: getReferences(memberAttributeId
)) {
77 Role role
= findRole(ldapName
);
79 throw new IllegalStateException("Role " + ldapName
+ " not found.");
81 directMembers
.add(role
);
83 return directMembers
.toArray(new Role
[directMembers
.size()]);
87 * Whether a role with this name can be found from this context.
89 * @return The related {@link Role} or <code>null</code>.
91 protected Role
findRole(LdapName ldapName
) {
92 Role role
= getUserAdmin().getRole(ldapName
.toString());
94 if (getUserAdmin().getExternalRoles() != null)
95 role
= getUserAdmin().getExternalRoles().getRole(ldapName
.toString());
101 // public List<LdapName> getMemberNames() {
102 // Attribute memberAttribute = getAttributes().get(memberAttributeId);
103 // if (memberAttribute == null)
104 // return new ArrayList<LdapName>();
106 // List<LdapName> roles = new ArrayList<LdapName>();
107 // NamingEnumeration<?> values = memberAttribute.getAll();
108 // while (values.hasMore()) {
109 // LdapName dn = new LdapName(values.next().toString());
113 // } catch (NamingException e) {
114 // throw new IllegalStateException("Cannot get members", e);
119 public Role
[] getRequiredMembers() {
120 throw new UnsupportedOperationException();
124 public int getType() {
131 static class LdifFunctionalGroup
extends LdifGroup
implements FunctionalGroup
{
133 public LdifFunctionalGroup(DirectoryUserAdmin userAdmin
, LdapName dn
, Attributes attributes
) {
134 super(userAdmin
, dn
, attributes
);
139 static class LdifOrganization
extends LdifGroup
implements Organization
{
141 public LdifOrganization(DirectoryUserAdmin userAdmin
, LdapName dn
, Attributes attributes
) {
142 super(userAdmin
, dn
, attributes
);
147 static class LdifSystemPermissions
extends LdifGroup
implements SystemPermissions
{
149 public LdifSystemPermissions(DirectoryUserAdmin userAdmin
, LdapName dn
, Attributes attributes
) {
150 super(userAdmin
, dn
, attributes
);