1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
35 /***
36 * Map on authorized schemas, key of the map : schema name.
37 */
38 private Map restrictions = new HashMap();
39
40
41
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
76 SchemaRestrictions schemaRestriction = (SchemaRestrictions) restrictions.get(nomSchema);
77 if (schemaRestriction != null) {
78 return !schemaRestriction.isTableRestricted(aTableName);
79 } else {
80 return true;
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
93 SchemaRestrictions schemaRestriction = (SchemaRestrictions) restrictions.get(nomSchema);
94 if (schemaRestriction != null) {
95 return !schemaRestriction.isRestrictionTotale();
96
97 } else {
98 return true;
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 }