01: package org.mockejb;
02:
03: import java.security.Principal;
04: import java.util.*;
05:
06: /**
07: * Provides simple implementation of java.security.Principal.
08: * Also stores the roles for the principal.
09: * The only purpose of this class is to support two security-related
10: * methods on EJBContext, so the implementation is very simple.
11: *
12: * @author Alexander Ananiev
13: */
14: public class MockUser implements Principal {
15:
16: public final static MockUser ANONYMOUS_USER = new MockUser(
17: "anonymous");
18:
19: private String name;
20: private List roles = new ArrayList();
21:
22: public MockUser(final String name) {
23: this .name = name;
24: }
25:
26: public MockUser(final String name, String role) {
27: this .name = name;
28: setRole(role);
29: }
30:
31: public MockUser(final String name, String[] roles) {
32: this .name = name;
33: setRoles(roles);
34: }
35:
36: /**
37: * Sets the roles that this user has
38: * @param roles role names
39: */
40: public void setRoles(String[] roles) {
41: this .roles = Arrays.asList(roles);
42: }
43:
44: /**
45: * Sets the role that this user has meaning that
46: * this user only has one role
47: *
48: * @param role name of the role
49: */
50: public void setRole(String role) {
51: roles = new ArrayList();
52: roles.add(role);
53: }
54:
55: /**
56: * Checks if the given role belongs to this user
57: *
58: * @param role role to check
59: * @return true if this user has the given role
60: */
61: public boolean hasRole(String role) {
62: return roles.contains(role);
63: }
64:
65: /**
66: * @see java.security.Principal#getName()
67: */
68: public String getName() {
69: return this .name;
70: }
71:
72: public String toString() {
73: return name;
74: }
75:
76: }
|