Skip to content

Commit 68d437d

Browse files
authored
Improve permission checking in DemographicsSources (#26)
1 parent c99da09 commit 68d437d

File tree

3 files changed

+69
-13
lines changed

3 files changed

+69
-13
lines changed

laboratory/src/org/labkey/laboratory/DemographicsSource.java

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.labkey.api.query.QueryService;
1717
import org.labkey.api.query.UserSchema;
1818
import org.labkey.api.security.User;
19+
import org.labkey.api.security.permissions.ReadPermission;
1920
import org.labkey.api.study.Dataset;
2021
import org.labkey.api.study.DatasetTable;
2122

@@ -40,15 +41,21 @@ public DemographicsSource(String label, String containerId, String schemaName, S
4041

4142
public static DemographicsSource getFromParts(Container c, User u, String label, String containerId, String schemaName, String queryName, String targetColumn) throws IllegalArgumentException
4243
{
43-
DemographicsSource.validateKey(c, u, containerId, schemaName, queryName, targetColumn, label);
44+
if (!isValidSource(c, u, containerId, schemaName, queryName, targetColumn, label))
45+
{
46+
return null;
47+
}
48+
4449
return new DemographicsSource(label, containerId, schemaName, queryName, targetColumn);
4550
}
4651

4752
public static DemographicsSource getFromPropertyManager(Container c, User u, String key, String value) throws IllegalArgumentException
4853
{
4954
if (value == null)
55+
{
5056
return null;
51-
57+
}
58+
5259
try
5360
{
5461
JSONObject json = new JSONObject(value);
@@ -58,7 +65,10 @@ public static DemographicsSource getFromPropertyManager(Container c, User u, Str
5865
String label = json.getString("label");
5966
String targetColumn = json.getString("targetColumn");
6067

61-
validateKey(c, u, containerId, schemaName, queryName, targetColumn, label);
68+
if (!isValidSource(c, u, containerId, schemaName, queryName, targetColumn, label))
69+
{
70+
return null;
71+
}
6272

6373
return new DemographicsSource(label, containerId, schemaName, queryName, targetColumn);
6474
}
@@ -101,23 +111,39 @@ public JSONObject toJSON(Container c, User u, boolean includeTotals)
101111
return json;
102112
}
103113

104-
public static boolean validateKey(Container defaultContainer, User u, @Nullable String containerId, String schemaName, String queryName, String targetColumn, String label) throws IllegalArgumentException
114+
private static boolean isValidSource(Container defaultContainer, User u, @Nullable String containerId, String schemaName, String queryName, String targetColumn, String label) throws IllegalArgumentException
105115
{
106116
Container target;
107117
if (containerId == null)
118+
{
108119
target = defaultContainer;
120+
}
109121
else
122+
{
110123
target = ContainerManager.getForId(containerId);
124+
}
111125

112126
if (target == null)
127+
{
113128
target = defaultContainer;
129+
}
130+
131+
if (!target.hasPermission(u, ReadPermission.class))
132+
{
133+
return false;
134+
}
114135

115136
UserSchema us = QueryService.get().getUserSchema(u, target, schemaName);
116-
if (target == null)
137+
if (us == null)
117138
{
118139
throw new IllegalArgumentException("Unknown schema in saved data source: " + schemaName);
119140
}
120141

142+
if (!us.canReadSchema())
143+
{
144+
return false;
145+
}
146+
121147
QueryDefinition qd = us.getQueryDefForTable(queryName);
122148
if (qd == null)
123149
{
@@ -129,19 +155,28 @@ public static boolean validateKey(Container defaultContainer, User u, @Nullable
129155
throw new IllegalArgumentException("Missing targetColumn");
130156
}
131157

132-
List<QueryException> errors = new ArrayList<QueryException>();
158+
List<QueryException> errors = new ArrayList<>();
133159
TableInfo ti = qd.getTable(errors, true);
134-
if (errors.size() != 0 || ti == null)
160+
161+
if (!errors.isEmpty())
135162
{
136163
_log.error("Unable to create TableInfo for query: " + queryName + ". there were " + errors.size() + " errors");
137164
for (QueryException e : errors)
138165
{
139166
_log.error(e.getMessage());
140167
}
141-
if (errors.size() > 0)
142-
throw new IllegalArgumentException("Unable to create table for query: " + queryName, errors.get(0));
143-
else
144-
throw new IllegalArgumentException("Unable to create table for query: " + queryName);
168+
169+
throw new IllegalArgumentException("Unable to create table for query: " + queryName, errors.get(0));
170+
}
171+
172+
if (ti == null)
173+
{
174+
throw new IllegalArgumentException("Unable to create table for query: " + queryName);
175+
}
176+
177+
if (!ti.hasPermission(u, ReadPermission.class))
178+
{
179+
return false;
145180
}
146181

147182
ColumnInfo col = ti.getColumn(targetColumn);
@@ -158,6 +193,11 @@ public static boolean validateKey(Container defaultContainer, User u, @Nullable
158193
if (ti instanceof DatasetTable)
159194
{
160195
Dataset ds = ((DatasetTable)ti).getDataset();
196+
if (!ds.hasPermission(u, ReadPermission.class))
197+
{
198+
return false;
199+
}
200+
161201
if (!(ds.isDemographicData() && ds.getStudy().getSubjectColumnName().equalsIgnoreCase(col.getName())))
162202
{
163203
throw new IllegalArgumentException("Target column is not a key field: " + targetColumn);
@@ -175,7 +215,9 @@ public static boolean validateKey(Container defaultContainer, User u, @Nullable
175215
}
176216

177217
if (StringUtils.trimToNull(label) == null)
218+
{
178219
throw new IllegalArgumentException("Label must not be blank");
220+
}
179221

180222
return true;
181223
}

laboratory/src/org/labkey/laboratory/LaboratoryController.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1308,7 +1308,11 @@ public ApiResponse execute(SetDataSourcesForm form, BindException errors)
13081308
return null;
13091309
}
13101310

1311-
sources.add(DemographicsSource.getFromParts(getContainer(), getUser(), label, containerId, schemaName, queryName, targetColumn));
1311+
DemographicsSource s = DemographicsSource.getFromParts(getContainer(), getUser(), label, containerId, schemaName, queryName, targetColumn);
1312+
if (s != null)
1313+
{
1314+
sources.add(s);
1315+
}
13121316
}
13131317

13141318
try

laboratory/src/org/labkey/laboratory/LaboratoryServiceImpl.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,9 @@ public Set<DemographicsSource> getDemographicsSources(Container c, User u) throw
383383
{
384384
DemographicsSource source = DemographicsSource.getFromPropertyManager(target, u, key, properties.get(key));
385385
if (source != null)
386+
{
386387
qds.add(source);
388+
}
387389
}
388390
catch (IllegalArgumentException e)
389391
{
@@ -450,18 +452,26 @@ public Map<Container, Set<DemographicsSource>> getAllDemographicsSources(User u)
450452
{
451453
Container c = ContainerManager.getForId(entry.getObjectId());
452454
if (c == null || !c.hasPermission(u, ReadPermission.class))
455+
{
453456
continue;
457+
}
454458

455459
Set<DemographicsSource> set = map.get(c);
456460
if (set == null)
461+
{
457462
set = new HashSet<>();
463+
}
458464

459465
DemographicsSource source = DemographicsSource.getFromPropertyManager(c, u, entry.getKey(), entry.getValue());
460466
if (source != null)
467+
{
461468
set.add(source);
469+
}
462470

463-
if (set.size() > 0)
471+
if (!set.isEmpty())
472+
{
464473
map.put(c, set);
474+
}
465475
}
466476

467477
return Collections.unmodifiableMap(map);

0 commit comments

Comments
 (0)