View Javadoc

1   /* ==============================================================================
2    *   JDBCAdmin, data management software.
3    *   Copyright (C) 2005  Norsys S.A
4    *
5    *   This library is free software; you can redistribute it and/or
6    *   modify it under the terms of the GNU Lesser General Public
7    *   License as published by the Free Software Foundation; either
8    *   version 2.1 of the License, or (at your option) any later version.
9    *
10   *   This library is distributed in the hope that it will be useful,
11   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   *   Lesser General Public License for more details.
14   *
15   *   You should have received a copy of the GNU Lesser General Public
16   *   License along with this library; if not, write to the Free Software
17   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   * ==============================================================================
19   */
20  package jdbcadmin.core.access;
21  
22  import java.util.ArrayList;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  /***
28   * Implementation of {@link IConfigurableAuthorizationAccess} with declaration of authorized Tables ans Schemas.
29   * Iems not declared are not authorired.
30   * @author Thomas Recloux (trecloux@norsys.fr)
31   */
32  public class DeclarativeAccessImpl implements IConfigurableAuthorizationAccess {
33  
34      /* ----------------------------------------- Attributs */
35      /***
36       * Map on authorized schemas, key of the map : schema name.
37       */
38      private Map authorizations = new HashMap();
39  
40      /* ----------------------------------------- methodes publiques */
41      /*** {@inheritDoc} */
42      public void addTable(String aTableName, String aSchemaName) {
43          String nomSchema;
44          if (aSchemaName == null) {
45              nomSchema = "";
46          } else {
47              nomSchema = aSchemaName;
48          }
49          SchemaAuthorization schemaAuthorization = (SchemaAuthorization) authorizations.get(nomSchema);
50          schemaAuthorization.addTable(aTableName);
51      }
52  
53      /*** {@inheritDoc} */
54      public void addSchema(String aSchemaName, boolean aAutorisationTotale) {
55          String nomSchema;
56          if (aSchemaName == null) {
57              nomSchema = "";
58          } else {
59              nomSchema = aSchemaName;
60          }
61          SchemaAuthorization schema = new SchemaAuthorization();
62          schema.setCompletelyAuthorized(aAutorisationTotale);
63          authorizations.put(nomSchema, schema);
64      }
65  
66      /*** {@inheritDoc} */
67      public boolean isTableAuthorized(String aTableName, String aSchemaName) {
68          String nomSchema;
69          if (aSchemaName == null) {
70              nomSchema = "";
71          } else {
72              nomSchema = aSchemaName;
73          }
74          // on recupere la liste des tables autorisées pour ce schema
75          SchemaAuthorization schemaAuthorization = (SchemaAuthorization) authorizations.get(nomSchema);
76          if (schemaAuthorization != null) {
77              return schemaAuthorization.isTableAuthorized(aTableName);
78          } else {
79              return false;
80          }
81      }
82  
83      /*** {@inheritDoc} */
84      public boolean isSchemaAuthorized(String aSchemaName) {
85          String nomSchema;
86          if (aSchemaName == null) {
87              nomSchema = "";
88          } else {
89              nomSchema = aSchemaName;
90          }
91          // Est ce que le schema a été declaré
92          SchemaAuthorization schemaAuthorization = (SchemaAuthorization) authorizations.get(nomSchema);
93          // Si le schema est declaré, il est autorisé
94          return schemaAuthorization != null;
95      }
96  
97      /***
98       * Represents the schema authorization.
99       * @author Thomas Recloux (trecloux@norsys.fr)
100      */
101     private class SchemaAuthorization {
102         /*** Table list. */
103         private List tables = new ArrayList();
104 
105         /*** Indicate wether the schméa is completely authorized. */
106         private boolean completelyAuthorized = false;
107 
108         /***
109          * Add authorisation on a table.
110          * @param aTableName table name.
111          */
112         public void addTable(String aTableName) {
113             tables.add(aTableName);
114         }
115 
116         /***
117          * Indicate whether the table is authorized.
118          * @param aTableName table name
119          * @return <code>true</code> if authorized, <code>false</code> otherwise.
120          */
121         public boolean isTableAuthorized(String aTableName) {
122             if (completelyAuthorized) {
123                 return true;
124             } else {
125                 return tables.contains(aTableName);
126             }
127         }
128 
129         /***
130          * @return Returns the completelyAuthorized.
131          */
132         public boolean isCompletelyAuthorized() {
133             return completelyAuthorized;
134         }
135 
136         /***
137          * @param aCompletelyAuthorized The completelyAuthorized to set.
138          */
139         public void setCompletelyAuthorized(boolean aCompletelyAuthorized) {
140             this.completelyAuthorized = aCompletelyAuthorized;
141         }
142     }
143 }