]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/internal/auth/ImpliedByPrincipal.java
Use runtime namespace context as default.
[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 static org.argeo.api.acr.RuntimeNamespaceContext.getNamespaceContext;
4
5 import java.security.Principal;
6 import java.util.HashSet;
7 import java.util.Set;
8
9 import javax.xml.namespace.QName;
10
11 import org.argeo.api.acr.CrName;
12 import org.argeo.api.acr.NamespaceUtils;
13 import org.argeo.cms.auth.RoleNameUtils;
14 import org.osgi.service.useradmin.Authorization;
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 {
25 private final String name;
26 private Set<Principal> causes = new HashSet<Principal>();
27
28 private QName roleName;
29 // private int type = Role.ROLE;
30
31 private boolean systemRole = false;
32 private String context;
33
34 public ImpliedByPrincipal(String name, Principal userPrincipal) {
35 this.name = name;
36 String cn = RoleNameUtils.getLastRdnValue(name);
37 roleName = NamespaceUtils.parsePrefixedName(getNamespaceContext(), cn);
38 if (roleName.getNamespaceURI().equals(CrName.ROLE_NAMESPACE_URI)) {
39 systemRole = true;
40 }
41 context = RoleNameUtils.getContext(name);
42 // try {
43 // this.name = new LdapName(name);
44 // } catch (InvalidNameException e) {
45 // throw new IllegalArgumentException("Badly formatted role name", e);
46 // }
47 if (userPrincipal != null)
48 causes.add(userPrincipal);
49 }
50
51 // public ImpliedByPrincipal(LdapName name, Principal userPrincipal) {
52 // this.name = name;
53 // if (userPrincipal != null)
54 // causes.add(userPrincipal);
55 // }
56
57 public String getName() {
58 return name;
59 }
60
61 /*
62 * USER ADMIN
63 */
64 // public boolean addMember(Principal user) {
65 // throw new UnsupportedOperationException();
66 // }
67 //
68 // public boolean removeMember(Principal user) {
69 // throw new UnsupportedOperationException();
70 // }
71 //
72 // public boolean isMember(Principal member) {
73 // return causes.contains(member);
74 // }
75 //
76 // public Enumeration<? extends Principal> members() {
77 // return Collections.enumeration(causes);
78 // }
79 //
80 //
81 // /** Type of {@link Role}, if known. */
82 // public int getType() {
83 // return type;
84 // }
85 //
86 // /** Not supported for the time being. */
87 // public Dictionary<String, Object> getProperties() {
88 // throw new UnsupportedOperationException();
89 // }
90
91 /*
92 * OBJECT
93 */
94
95 public QName getRoleName() {
96 return roleName;
97 }
98
99 public String getContext() {
100 return context;
101 }
102
103 public boolean isSystemRole() {
104 return systemRole;
105 }
106
107 @Override
108 public int hashCode() {
109 return name.hashCode();
110 }
111
112 @Override
113 public boolean equals(Object obj) {
114 // if (this == obj)
115 // return true;
116 if (obj instanceof ImpliedByPrincipal) {
117 ImpliedByPrincipal that = (ImpliedByPrincipal) obj;
118 // TODO check members too?
119 return name.equals(that.name);
120 }
121 return false;
122 }
123
124 @Override
125 public String toString() {
126 // return name.toString() + " implied by " + causes;
127 return name.toString();
128 }
129 }