]> git.argeo.org Git - lgpl/argeo-commons.git/blob - LdifGroup.java
7aad15a8c4d404453079b64e2da0dbc03d9816c1
[lgpl/argeo-commons.git] / LdifGroup.java
1 package org.argeo.osgi.useradmin;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import javax.naming.InvalidNameException;
7 import javax.naming.directory.Attribute;
8 import javax.naming.directory.Attributes;
9 import javax.naming.ldap.LdapName;
10
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;
16
17 /** Directory group implementation */
18 abstract class LdifGroup extends LdifUser implements DirectoryGroup {
19 private final String memberAttributeId;
20
21 LdifGroup(AbstractLdapDirectory userAdmin, LdapName dn, Attributes attributes) {
22 super(userAdmin, dn, attributes);
23 memberAttributeId = userAdmin.getMemberAttributeId();
24 }
25
26 @Override
27 public boolean addMember(Role role) {
28 try {
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");
35 }
36
37 getUserAdmin().checkEdit();
38 if (!isEditing())
39 startEditing();
40
41 Attribute member = getAttributes().get(memberAttributeId);
42 if (member != null) {
43 if (member.contains(role.getName()))
44 return false;
45 else
46 member.add(role.getName());
47 } else
48 getAttributes().put(memberAttributeId, role.getName());
49 return true;
50 }
51
52 @Override
53 public boolean addRequiredMember(Role role) {
54 throw new UnsupportedOperationException();
55 }
56
57 @Override
58 public boolean removeMember(Role role) {
59 getUserAdmin().checkEdit();
60 if (!isEditing())
61 startEditing();
62
63 Attribute member = getAttributes().get(memberAttributeId);
64 if (member != null) {
65 if (!member.contains(role.getName()))
66 return false;
67 member.remove(role.getName());
68 return true;
69 } else
70 return false;
71 }
72
73 @Override
74 public Role[] getMembers() {
75 List<Role> directMembers = new ArrayList<Role>();
76 for (LdapName ldapName : getReferences(memberAttributeId)) {
77 Role role = findRole(ldapName);
78 if (role == null) {
79 throw new IllegalStateException("Role " + ldapName + " not found.");
80 }
81 directMembers.add(role);
82 }
83 return directMembers.toArray(new Role[directMembers.size()]);
84 }
85
86 /**
87 * Whether a role with this name can be found from this context.
88 *
89 * @return The related {@link Role} or <code>null</code>.
90 */
91 protected Role findRole(LdapName ldapName) {
92 Role role = getUserAdmin().getRole(ldapName.toString());
93 if (role == null) {
94 if (getUserAdmin().getExternalRoles() != null)
95 role = getUserAdmin().getExternalRoles().getRole(ldapName.toString());
96 }
97 return role;
98 }
99
100 // @Override
101 // public List<LdapName> getMemberNames() {
102 // Attribute memberAttribute = getAttributes().get(memberAttributeId);
103 // if (memberAttribute == null)
104 // return new ArrayList<LdapName>();
105 // try {
106 // List<LdapName> roles = new ArrayList<LdapName>();
107 // NamingEnumeration<?> values = memberAttribute.getAll();
108 // while (values.hasMore()) {
109 // LdapName dn = new LdapName(values.next().toString());
110 // roles.add(dn);
111 // }
112 // return roles;
113 // } catch (NamingException e) {
114 // throw new IllegalStateException("Cannot get members", e);
115 // }
116 // }
117
118 @Override
119 public Role[] getRequiredMembers() {
120 throw new UnsupportedOperationException();
121 }
122
123 @Override
124 public int getType() {
125 return GROUP;
126 }
127
128 /*
129 * KIND
130 */
131 static class LdifFunctionalGroup extends LdifGroup implements FunctionalGroup {
132
133 public LdifFunctionalGroup(DirectoryUserAdmin userAdmin, LdapName dn, Attributes attributes) {
134 super(userAdmin, dn, attributes);
135 }
136
137 }
138
139 static class LdifOrganization extends LdifGroup implements Organization {
140
141 public LdifOrganization(DirectoryUserAdmin userAdmin, LdapName dn, Attributes attributes) {
142 super(userAdmin, dn, attributes);
143 }
144
145 }
146
147 static class LdifSystemPermissions extends LdifGroup implements SystemPermissions {
148
149 public LdifSystemPermissions(DirectoryUserAdmin userAdmin, LdapName dn, Attributes attributes) {
150 super(userAdmin, dn, attributes);
151 }
152
153 }
154 }