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