KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > model > CompierePrincipal


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.model;
15
16 import java.security.*;
17 import java.util.Arrays JavaDoc;
18 import java.util.List JavaDoc;
19
20 /**
21  * Implementation of <strong>java.security.Principal</strong>
22  *
23  * @author Jorg Janke
24  * @version $Revision: 1.2 $ $Date: 2003/07/06 18:39:44 $
25  */

26 public class CompierePrincipal implements Principal
27 {
28     /**
29      * Construct a new Principal, associated with the specified Realm, for the
30      * specified username and password, with the specified role names
31      * (as Strings).
32      *
33      * @param name The username of the user represented by this Principal
34      * @param password Credentials used to authenticate this user
35      * @param roles List of roles (must be Strings) possessed by this user
36      */

37     public CompierePrincipal (String JavaDoc name, String JavaDoc password, List JavaDoc roles)
38     {
39         super();
40         m_name = name;
41         m_password = password;
42         if (roles != null)
43         {
44             m_roles = new String JavaDoc[roles.size()];
45             m_roles = (String JavaDoc[]) roles.toArray(m_roles);
46             if (m_roles.length > 0)
47                 Arrays.sort(m_roles);
48         }
49     } // CompierePrincipal
50

51
52     /** Username of the user represented by this Principal */
53     private String JavaDoc m_name = null;
54     /** Authentication credentials for the user represented by this Principal */
55     private String JavaDoc m_password = null;
56     /** Array of roles associated with this user */
57     private String JavaDoc m_roles[] = new String JavaDoc[0];
58
59
60     /**
61      * Get Name
62      * @return name
63      */

64     public String JavaDoc getName()
65     {
66         return m_name;
67     } // getName
68

69     /**
70      * Get Password
71      * @return password
72      */

73     String JavaDoc getPassword()
74     {
75         return m_password;
76     } // getPassword
77

78     /**
79      * Get Roles
80      * @return roles array
81      */

82     String JavaDoc[] getRoles()
83     {
84         return m_roles;
85     } // getRoles
86

87     /**
88      * Return a String representation of this object, which exposes only
89      * information that should be public.
90      * @return info
91      */

92     public String JavaDoc toString()
93     {
94         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("CompierePrincipal[");
95         sb.append(m_name).append(": ");
96         for (int i = 0; i < m_roles.length; i++)
97             sb.append(m_roles[i]).append(" ");
98         sb.append("]");
99         return (sb.toString());
100     } // toString
101

102     /**
103      * Does the user represented by this Principal possess the specified role?
104      *
105      * @param role Role to be tested
106      * @return true if has role
107      */

108     public boolean hasRole (String JavaDoc role)
109     {
110         if (role == null)
111             return false;
112         return (Arrays.binarySearch(m_roles, role) >= 0);
113     } // hasRole
114

115 } // CompierePrincipal
116
Popular Tags