KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aspects > security > SecurityContext


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.aspects.security;
23
24 import org.jboss.security.RealmMapping;
25 import org.jboss.security.RunAsIdentity;
26 import org.jboss.security.SimplePrincipal;
27
28 import java.security.Principal JavaDoc;
29 import java.util.HashSet JavaDoc;
30
31 /**
32  * Obtain security information based on the current security domain.
33  *
34  *
35  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
36  * @version $Revision: 54985 $
37  */

38 public class SecurityContext
39 {
40    protected static ThreadLocal JavaDoc currentDomain = new ThreadLocal JavaDoc();
41    
42    public static ThreadLocal JavaDoc getCurrentDomain()
43    {
44       return currentDomain;
45    }
46
47    /**
48     * Get the caller's principal
49     * @return
50     */

51    public static Principal getCallerPrincipal()
52    {
53       return SecurityActions.getCallerPrincipal();
54    }
55
56    /**
57     * Get the current principal. Could be run-as, or propagated
58     * @return
59     */

60    public static Principal getCurrentPrincipal()
61    {
62      return SecurityActions.getPrincipal();
63    }
64
65    /**
66     * Is the caller's security identity within the role
67     *
68     * Does not include current run-as
69     * @param roleName
70     * @return
71     */

72    public static boolean isCallerInRole(String JavaDoc roleName)
73    {
74       return isInRole(getCallerPrincipal(), roleName);
75    }
76
77    /**
78     * Checks current identity is within roleName
79     *
80     * Does include current run-as
81     *
82     * @param roleName
83     * @return
84     */

85    public static boolean isCurrentInRole(String JavaDoc roleName)
86    {
87       return isInRole(getCurrentPrincipal(), roleName);
88    }
89
90    private static boolean isInRole(Principal principal, String JavaDoc roleName)
91    {
92       RealmMapping rm = (RealmMapping)currentDomain.get();
93       if (rm == null) return false;
94
95       HashSet JavaDoc set = new HashSet JavaDoc();
96       set.add(new SimplePrincipal(roleName));
97
98       if (principal instanceof RunAsIdentity)
99       {
100          return ((RunAsIdentity)principal).doesUserHaveRole(set);
101       }
102       else
103       {
104          return rm.doesUserHaveRole(principal, set);
105       }
106    }
107
108
109 }
110
Popular Tags