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