]> git.argeo.org Git - lgpl/argeo-commons.git/blob - auth/ImpliedByPrincipal.java
Prepare next development cycle
[lgpl/argeo-commons.git] / 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.Dictionary;
6 import java.util.Enumeration;
7 import java.util.HashSet;
8 import java.util.Set;
9
10 import javax.naming.InvalidNameException;
11 import javax.naming.ldap.LdapName;
12
13 import org.argeo.cms.CmsException;
14 import org.osgi.service.useradmin.Authorization;
15 import org.osgi.service.useradmin.Role;
16
17 /**
18 * A {@link Principal} which has been implied by an {@link Authorization}. If it
19 * is empty it means this is an additional identity, otherwise it lists the
20 * users (typically the logged in user but possibly empty
21 * {@link ImpliedByPrincipal}s) which have implied it. When an additional
22 * identity is removed, the related {@link ImpliedByPrincipal}s can thus be
23 * removed.
24 */
25 public final class ImpliedByPrincipal implements Principal, Role {
26 private final LdapName name;
27 private Set<Principal> causes = new HashSet<Principal>();
28
29 private int type = Role.ROLE;
30
31 public ImpliedByPrincipal(String name, Principal userPrincipal) {
32 try {
33 this.name = new LdapName(name);
34 } catch (InvalidNameException e) {
35 throw new CmsException("Badly formatted role name", e);
36 }
37 if (userPrincipal != null)
38 causes.add(userPrincipal);
39 }
40
41 public ImpliedByPrincipal(LdapName name, Principal userPrincipal) {
42 this.name = name;
43 if (userPrincipal != null)
44 causes.add(userPrincipal);
45 }
46
47 public String getName() {
48 return name.toString();
49 }
50
51 public boolean addMember(Principal user) {
52 throw new UnsupportedOperationException();
53 }
54
55 public boolean removeMember(Principal user) {
56 throw new UnsupportedOperationException();
57 }
58
59 public boolean isMember(Principal member) {
60 return causes.contains(member);
61 }
62
63 public Enumeration<? extends Principal> members() {
64 return Collections.enumeration(causes);
65 }
66
67 /*
68 * USER ADMIN
69 */
70
71 @Override
72 /** Type of {@link Role}, if known. */
73 public int getType() {
74 return type;
75 }
76
77 @Override
78 /** Not supported for the time being. */
79 public Dictionary<String, Object> getProperties() {
80 throw new UnsupportedOperationException();
81 }
82
83 /*
84 * OBJECT
85 */
86
87 @Override
88 public int hashCode() {
89 return name.hashCode();
90 }
91
92 @Override
93 public boolean equals(Object obj) {
94 // if (this == obj)
95 // return true;
96 if (obj instanceof ImpliedByPrincipal) {
97 ImpliedByPrincipal that = (ImpliedByPrincipal) obj;
98 // TODO check members too?
99 return name.equals(that.name);
100 }
101 return false;
102 }
103
104 @Override
105 public String toString() {
106 // return name.toString() + " implied by " + causes;
107 return name.toString();
108 }
109 }