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 jdbcadmin.core.access.IConfigurableAuthorizationAccess} with declaration of the
29   * forbiden schemas and tables.
30   * @author Thomas Recloux (trecloux@norsys.fr)
31   */
32  public class RestrictiveAccessImpl implements IConfigurableAuthorizationAccess {
33  
34      /* ----------------------------------------- Attributs */
35      /***
36       * Map on authorized schemas, key of the map : schema name.
37       */
38      private Map restrictions = new HashMap();
39  
40  
41      /* ----------------------------------------- methodes publiques */
42      /*** {@inheritDoc} */
43      public void addTable (String aTableName, String aSchemaName) {
44          String nomSchema;
45          if (aSchemaName == null) {
46              nomSchema = "";
47          } else {
48              nomSchema = aSchemaName;
49          }
50          SchemaRestrictions schemaRestriction = (SchemaRestrictions) restrictions.get(nomSchema);
51          schemaRestriction.addTable(aTableName);
52      }
53  
54      /*** {@inheritDoc} */
55      public void addSchema (String aSchemaName, boolean aRestrictionTotale) {
56          String nomSchema;
57          if (aSchemaName == null) {
58              nomSchema = "";
59          } else {
60              nomSchema = aSchemaName;
61          }
62          SchemaRestrictions schema = new SchemaRestrictions();
63          schema.setRestrictionTotale(aRestrictionTotale);
64          restrictions.put(nomSchema, schema);
65      }
66  
67      /*** {@inheritDoc} */
68      public boolean isTableAuthorized(String aTableName, String aSchemaName) {
69          String nomSchema;
70          if (aSchemaName == null) {
71              nomSchema = "";
72          } else {
73              nomSchema = aSchemaName;
74          }
75          // On teste si le schema a été restrint
76          SchemaRestrictions schemaRestriction = (SchemaRestrictions) restrictions.get(nomSchema);
77          if (schemaRestriction != null) {
78              return !schemaRestriction.isTableRestricted(aTableName);
79          } else {
80              return true; // Le schema n'est pas restreint, il est autorisé
81          }
82      }
83  
84      /*** {@inheritDoc} */
85      public boolean isSchemaAuthorized(String aSchemaName) {
86          String nomSchema;
87          if (aSchemaName == null) {
88              nomSchema = "";
89          } else {
90              nomSchema = aSchemaName;
91          }
92          // On teste si le schema a été restrint
93          SchemaRestrictions schemaRestriction = (SchemaRestrictions) restrictions.get(nomSchema);
94          if (schemaRestriction != null) {
95              return !schemaRestriction.isRestrictionTotale();
96              // Le schema est declaré, on teste si la retriction est totale;
97          } else {
98              return true; // Le schema n'est pas restreint, il est autorisé
99          }
100     }
101 
102     /***
103      * Represents schema restrictions
104      * @author Thomas Recloux (trecloux@norsys.fr)
105      */
106     private class SchemaRestrictions {
107         /*** restricted table list */
108         private List tables = new ArrayList();
109         /*** Indicates whether or not the schema is completely restricted */
110         private boolean restrictionTotale = false;
111 
112         /***
113          * Adds restiction on the specified table.
114          * @param aTableName table name
115          */
116         public void addTable(String aTableName) {
117             tables.add(aTableName);
118         }
119         /***
120          * Indicates whether or not the specified table is restricted.
121          * @param aTableName table name
122          * @return true if restricted, false otherwise
123          */
124         public boolean isTableRestricted (String aTableName) {
125             if (restrictionTotale) {
126                 return true;
127             } else {
128                 return tables.contains(aTableName);
129             }
130         }
131 
132         /***@return Returns the restrictionTotale. */
133         public boolean isRestrictionTotale() {
134             return restrictionTotale;
135         }
136         /*** @param aRestrictionTotale The restrictionTotale to set. */
137         public void setRestrictionTotale(boolean aRestrictionTotale) {
138             this.restrictionTotale = aRestrictionTotale;
139         }
140     }
141 }