source: java/net/deterlab/abac/Role.java @ 5129e3e

abac0-leakabac0-meicompt_changesgec13mei-idmei-rt0-nmei_rt0mei_rt2mei_rt2_fix_1meiyap-rt1meiyap1rt2tvf-new-xml
Last change on this file since 5129e3e was 5129e3e, checked in by Ted Faber <faber@…>, 13 years ago

Scoping and remove matches.

  • Property mode set to 100644
File size: 5.1 KB
Line 
1package net.deterlab.abac;
2
3import java.util.*;
4
5import org.bouncycastle.asn1.*;
6
7/**
8 * Represents a role, which is a vertex in a Credential graph.
9 */
10public class Role implements Comparable {
11    protected String m_string;
12    protected String[] m_parts;
13    protected String m_A_r1;
14    protected String m_r2;
15    protected String m_prefix;
16    protected Role[] m_prereqs;
17
18    /**
19     * Create a role from a string. A single role must be of the format "A",
20     * "A.r1", or "A.r1.r2", where A is a principal and r1 and r2 are role
21     * names. This constructor also supports intersection roles: a sequence of
22     * two or more roles separated by "&amp;". The whitespace surrounding &amp;
23     * is arbitrary.
24     *
25     * If the string does not have this format, the constructor throws a
26     * RuntimeException.
27     */
28    public Role(String s) throws RuntimeException {
29        m_string = s;
30
31        // intersection roles have at least two roles separated by "&"
32        String[] isect_roles = s.split("&");
33
34        // ordinary role
35        if (isect_roles.length == 1)
36            single_role();
37
38        // intersection role: make a list of prereqs
39        else {
40            m_prereqs = new Role[isect_roles.length];
41
42            // trim() handles arbitrary whitespace
43            for (int i = 0; i < isect_roles.length; ++i)
44                m_prereqs[i] = new Role(isect_roles[i].trim());
45
46            // this make is_principal etc. work properly
47            m_parts = new String[0];
48        }
49    }
50
51    public Role(String s, Context c) {
52        this(c.expandKeyID(s));
53    }
54
55    public Role(Role r) {
56        m_string = r.m_string;
57        m_A_r1 = r.m_A_r1;
58        m_r2 = r.m_r2;
59        m_prefix = r.m_prefix;
60        m_parts = new String[r.m_parts.length];
61        for (int i = 0; i < r.m_parts.length; i++) 
62            m_parts[i] = r.m_parts[i];
63        m_prereqs = new Role[m_prereqs.length];
64        for (int i = 0; i < r.m_prereqs.length; i++) 
65            m_prereqs[i] = new Role(r.m_prereqs[i]);
66    }
67
68    /**
69     * Initialize a single non-intersection role. See constructor for details of
70     * role format. Will throw RuntimeException if the role is invalid.
71     */
72    private void single_role() throws RuntimeException {
73        m_parts = m_string.split("\\.");
74        if (m_parts.length > 3)
75            throw new RuntimeException("Not a valid role: " + m_string);
76
77        // linking role: prefix is A.r1 from A.r1.r2
78        if (is_linking()) {
79            m_A_r1 = m_parts[0] + "." + m_parts[1];
80            m_r2 = m_parts[2];
81            m_prefix = m_A_r1;
82        }
83
84        // role: prefix is A from A.r1
85        else if (is_role())
86            m_prefix = m_parts[0];
87
88        // principal: prefix is the whole thing
89        else
90            m_prefix = m_string;
91    }
92
93    /**
94     * Returns true iff the role is a principal.
95     */
96    public boolean is_principal() { return m_parts.length == 1; }
97
98    /**
99     * Returns true iff the role is a role (i.e., A.r1).
100     */
101    public boolean is_role() { return m_parts.length == 2; }
102
103    /**
104     * Returns true iff the role is a linking role (i.e., A.r1.r2).
105     */
106    public boolean is_linking() { return m_parts.length == 3; }
107
108    /**
109     * Returns true iff the role represents an intersection role.
110     */
111    public boolean is_intersection() { return m_prereqs != null; }
112
113    /**
114     * Returns the first two elements of a linking role's name. This typically
115     * refers to another role in the graph. This will throw a runtime
116     * exception if the node is not a linking role.
117     */
118    String A_r1() throws RuntimeException {
119        if (!is_linking())
120            throw new RuntimeException("Not a linking role");
121        return m_A_r1;
122    }
123
124    /**
125     * Return the last element of a linking role's name. This will throw a
126     * runtime exception if the node is not a linking role.
127     */
128    String r2() throws RuntimeException {
129        if (!is_linking())
130            throw new RuntimeException("Not a linking role");
131        return m_r2;
132    }
133
134    /**
135     * Returns the principal part of a role or principal. This is everything
136     * except the last element of the name.
137     */
138    public String principal_part() {
139        return m_prefix;
140    }
141
142    public String principal() { return m_parts.length > 0 ? m_parts[0] : null; }
143    public String role_name() { return m_parts.length > 1 ? m_parts[1] : null; }
144    public String linked_role() { return A_r1(); }
145
146    /**
147     * Get the roles that form the prerequisites to this intersection. Throws
148     * a runtime exception if this is not an intersection role.
149     */
150    Role[] prereqs() throws RuntimeException {
151        if (!is_intersection())
152            throw new RuntimeException("Not an intersection role.");
153
154        return m_prereqs;
155    }
156
157    public String toString() {
158        return m_string;
159    }
160
161    public String simpleString(Context c) {
162        return c.expandNickname(m_string);
163    }
164
165    public boolean equals(Object v2) {
166        if (v2 instanceof Role)
167            return m_string.equals(((Role)v2).m_string);
168        return false;
169    }
170
171    public int compareTo(Object o) {
172        if (o instanceof Role) 
173            return m_string.compareTo(((Role)o).m_string);
174        else return 1;
175    }
176
177    public int hashCode() {
178        return m_string.hashCode();
179    }
180
181}
Note: See TracBrowser for help on using the repository browser.