KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sessionsystem > AuthorizationIdentifier


1 package com.daffodilwoods.daffodildb.server.sessionsystem;
2
3 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator._Iterator;
4
5
6 import java.util.*;
7 import com.daffodilwoods.database.resource.*;
8 import com.daffodilwoods.daffodildb.server.datadictionarysystem.*;
9 import com.daffodilwoods.database.general.*;
10 import com.daffodilwoods.daffodildb.utils.*;
11 import com.daffodilwoods.daffodildb.server.serversystem.*;
12 import com.daffodilwoods.daffodildb.utils.field.FieldBase;
13
14 /**
15  *
16  * <p>Title: AuthorizationIdentifier</p>
17  * <p>Description: </p>
18  * Class for representing the authorization Identifier as used in the
19  * com.daffodilwoods.database.sql-session.
20  * Authorization Identifier is a pair of user-identifier and role.
21  * Either the user-identifier is set or role is set.
22  * At a given instance of time, either user-identifier or role is null,
23  * but not both.
24  * Current Authorization Identifier refers to the non-null value in the
25  * ser-identifier and rolename.
26  *
27  */

28 public class AuthorizationIdentifier
29 {
30   String JavaDoc user; // user Name
31
String JavaDoc role; // role name
32

33   ArrayList enabledAuthorizationIdentifiers;
34   ArrayList applicableRoles;
35
36   public AuthorizationIdentifier() throws DException
37   {}
38
39   /**
40    * Sets the User name.
41    * @param user
42    * @throws DException
43    */

44   public void setUser(String JavaDoc user) throws DException {
45     this.user = user;
46   }
47
48   /**
49    * Sets the role of the user.
50    * @param role
51    * @throws DException
52    */

53   public void setRole(String JavaDoc role) throws DException {
54     this.role = role;
55   }
56
57   /**
58    * Returns the user name.
59    * @return String
60    * @throws DException
61    */

62   public String JavaDoc getUser() throws DException {
63     return this.user;
64   }
65
66   /**
67    * Returns the role corresponding to a user.
68    * @return String
69    * @throws DException
70    */

71   public String JavaDoc getRole() throws DException {
72     return this.role;
73   }
74
75
76   /**
77    * Returns a user or role of the session.
78    * @return String
79    * @throws DException
80    */

81   public String JavaDoc getCurrentAuthorizationIdentifier() throws DException {
82      return user == null ? role : user ;
83   }
84
85   /**
86    * Returns a list of all authorized users of session.
87    * @param globalSession
88    * @return ArrayList
89    * @throws com.daffodilwoods.database.resource.DException
90    */

91   public ArrayList getEnabledAuthorizationIdentifiers(_ServerSession globalSession) throws com.daffodilwoods.database.resource.DException{
92     if( enabledAuthorizationIdentifiers != null )
93       return enabledAuthorizationIdentifiers;
94
95     enabledAuthorizationIdentifiers = new ArrayList(2);
96     enabledAuthorizationIdentifiers.add(user);
97     enabledAuthorizationIdentifiers.addAll( getEnabledRoles(globalSession) );
98     return enabledAuthorizationIdentifiers;
99   }
100
101   /**
102    * Enabled roles are defined as follows: -
103    * If the value of the current role name of the current SQL-session is the null value, then the
104    * empty set.
105    * Otherwise, the set of roles defined by the current role name of the current SQL-session together
106    * with its applicable roles.
107    * @param globalSession
108    * @return
109    * @throws com.daffodilwoods.database.resource.DException
110    */

111   public ArrayList getEnabledRoles(_ServerSession globalSession) throws com.daffodilwoods.database.resource.DException{
112     if( role == null )
113       return new ArrayList(1);
114
115     ArrayList enabledRoles = new ArrayList(5);
116     enabledRoles.add(role);
117     enabledRoles.addAll( getApplicableRoles(globalSession) );
118     return enabledRoles;
119   }
120
121   /**
122    * The set of applicable roles for an <authorization identifier> consists of
123    * all roles defined by the role authorization com.daffodilwoods.database.descriptors whose grantee
124    * is that <authorization identifier> or PUBLIC together with
125    * all other roles they contain.
126    * Algo:-
127    * get the applicable roles for the given role;
128    * get the applicable roles for all the roles retrieved above.
129    * @param globalSession
130    * @return
131    * @throws com.daffodilwoods.database.resource.DException
132    */

133   public ArrayList getApplicableRoles(_ServerSession globalSession) throws com.daffodilwoods.database.resource.DException{
134     if( applicableRoles != null ) // already cached, simply return
135
return applicableRoles;
136
137     applicableRoles = new ArrayList(4);
138     applicableRoles.add(role);
139     String JavaDoc query = "Select * from "+ SystemTables.role_authorization_TableName ;
140
141     _Iterator iterator = (_Iterator)((_ServerSession)globalSession).executeQuery( query,0 );
142
143     int pointer = 0;
144     while(pointer != applicableRoles.size()){
145       String JavaDoc currentRole = (String JavaDoc)applicableRoles.get(pointer);
146       /*
147          Navigating through all roles and checking if the grantee is equal to the
148          current Role or grantee is Public, if so adding the role to the applicable role.
149       */

150       if( iterator.first() ){
151          do{
152            com.daffodilwoods.daffodildb.server.datasystem.utility._Record record = iterator.getRecord();
153            String JavaDoc grantee = (String JavaDoc)((FieldBase)record.getObject("grantee")).getObject();
154            if( grantee.equalsIgnoreCase(currentRole) || grantee.equalsIgnoreCase("PUBLIC") )
155              applicableRoles.add( ((FieldBase) record.getObject("role_name")).getObject() );
156          }while( iterator.next() );
157       }
158       pointer++;
159     }
160     return applicableRoles;
161   }
162
163
164
165   /**
166    * @todo
167    * @param globalSession
168    * @return ArrayList
169    * @throws com.daffodilwoods.database.resource.DException
170    */

171   public ArrayList getRolePrivileges(_ServerSession globalSession) throws com.daffodilwoods.database.resource.DException{
172     ArrayList rolePrivileges = new ArrayList(5);
173     ArrayList applicableRoles = getApplicableRoles(globalSession);
174     String JavaDoc clause = getClauseForRolePrivileges(applicableRoles);
175
176
177     return rolePrivileges;
178   }
179
180   private String JavaDoc getClauseForRolePrivileges(ArrayList applicableRoles) throws DException {
181     StringBuffer JavaDoc clause = new StringBuffer JavaDoc(50);
182     clause.append("grantee == 'PUBLIC'");
183     for(int a=0,count = applicableRoles.size();a <count;a++){
184       clause.append("or grantee == '");
185       clause.append(applicableRoles.get(a));
186       clause.append('\'');
187     }
188     return clause.toString();
189   }
190 }
191
Popular Tags