]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/internal/auth/ImpliedByPrincipal.java
Work on SNAPSHOTs containers.
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / internal / auth / ImpliedByPrincipal.java
1 package org.argeo.cms.internal.auth;
2
3 import java.security.Principal;
4 import java.util.Collections;
5 import java.util.Enumeration;
6 import java.util.HashSet;
7 import java.util.Set;
8
9 import javax.naming.InvalidNameException;
10 import javax.naming.ldap.LdapName;
11
12 import org.argeo.cms.CmsException;
13 import org.osgi.service.useradmin.Authorization;
14
15 /**
16 * A {@link Principal} which has been implied by an {@link Authorization}. If it
17 * is empty it means this is an additional identity, otherwise it lists the
18 * users (typically the logged in user but possibly empty
19 * {@link ImpliedByPrincipal}s) which have implied it. When an additional
20 * identity is removed, the related {@link ImpliedByPrincipal}s can thus be
21 * removed.
22 */
23 public final class ImpliedByPrincipal implements Principal {
24 private final LdapName name;
25 private Set<Principal> causes = new HashSet<Principal>();
26
27 public ImpliedByPrincipal(String name, Principal userPrincipal) {
28 try {
29 this.name = new LdapName(name);
30 } catch (InvalidNameException e) {
31 throw new CmsException("Badly formatted role name", e);
32 }
33 if (userPrincipal != null)
34 causes.add(userPrincipal);
35 }
36
37 public ImpliedByPrincipal(LdapName name, Principal userPrincipal) {
38 this.name = name;
39 if (userPrincipal != null)
40 causes.add(userPrincipal);
41 }
42
43 public String getName() {
44 return name.toString();
45 }
46
47 public boolean addMember(Principal user) {
48 throw new UnsupportedOperationException();
49 }
50
51 public boolean removeMember(Principal user) {
52 throw new UnsupportedOperationException();
53 }
54
55 public boolean isMember(Principal member) {
56 return causes.contains(member);
57 }
58
59 public Enumeration<? extends Principal> members() {
60 return Collections.enumeration(causes);
61 }
62
63 @Override
64 public int hashCode() {
65 return name.hashCode();
66 }
67
68 @Override
69 public boolean equals(Object obj) {
70 // if (this == obj)
71 // return true;
72 if (obj instanceof ImpliedByPrincipal) {
73 ImpliedByPrincipal that = (ImpliedByPrincipal) obj;
74 // TODO check members too?
75 return name.equals(that.name);
76 }
77 return false;
78 }
79
80 @Override
81 public String toString() {
82 // return name.toString() + " implied by " + causes;
83 return name.toString();
84 }
85 }