1616import org .labkey .api .query .QueryService ;
1717import org .labkey .api .query .UserSchema ;
1818import org .labkey .api .security .User ;
19+ import org .labkey .api .security .permissions .ReadPermission ;
1920import org .labkey .api .study .Dataset ;
2021import org .labkey .api .study .DatasetTable ;
2122
@@ -40,7 +41,11 @@ 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
@@ -60,7 +65,10 @@ public static DemographicsSource getFromParts(Container c, User u, String label,
6065 String label = json .getString ("label" );
6166 String targetColumn = json .getString ("targetColumn" );
6267
63- validateKey (c , u , containerId , schemaName , queryName , targetColumn , label );
68+ if (!isValidSource (c , u , containerId , schemaName , queryName , targetColumn , label ))
69+ {
70+ return null ;
71+ }
6472
6573 return new DemographicsSource (label , containerId , schemaName , queryName , targetColumn );
6674 }
@@ -103,23 +111,39 @@ public JSONObject toJSON(Container c, User u, boolean includeTotals)
103111 return json ;
104112 }
105113
106- 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
107115 {
108116 Container target ;
109117 if (containerId == null )
118+ {
110119 target = defaultContainer ;
120+ }
111121 else
122+ {
112123 target = ContainerManager .getForId (containerId );
124+ }
113125
114126 if (target == null )
127+ {
115128 target = defaultContainer ;
129+ }
130+
131+ if (!target .hasPermission (u , ReadPermission .class ))
132+ {
133+ return false ;
134+ }
116135
117136 UserSchema us = QueryService .get ().getUserSchema (u , target , schemaName );
118- if (target == null )
137+ if (us == null )
119138 {
120139 throw new IllegalArgumentException ("Unknown schema in saved data source: " + schemaName );
121140 }
122141
142+ if (!us .canReadSchema ())
143+ {
144+ return false ;
145+ }
146+
123147 QueryDefinition qd = us .getQueryDefForTable (queryName );
124148 if (qd == null )
125149 {
@@ -131,19 +155,28 @@ public static boolean validateKey(Container defaultContainer, User u, @Nullable
131155 throw new IllegalArgumentException ("Missing targetColumn" );
132156 }
133157
134- List <QueryException > errors = new ArrayList <QueryException >();
158+ List <QueryException > errors = new ArrayList <>();
135159 TableInfo ti = qd .getTable (errors , true );
136- if (errors .size () != 0 || ti == null )
160+
161+ if (!errors .isEmpty ())
137162 {
138163 _log .error ("Unable to create TableInfo for query: " + queryName + ". there were " + errors .size () + " errors" );
139164 for (QueryException e : errors )
140165 {
141166 _log .error (e .getMessage ());
142167 }
143- if (errors .size () > 0 )
144- throw new IllegalArgumentException ("Unable to create table for query: " + queryName , errors .get (0 ));
145- else
146- 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 ;
147180 }
148181
149182 ColumnInfo col = ti .getColumn (targetColumn );
@@ -160,6 +193,11 @@ public static boolean validateKey(Container defaultContainer, User u, @Nullable
160193 if (ti instanceof DatasetTable )
161194 {
162195 Dataset ds = ((DatasetTable )ti ).getDataset ();
196+ if (!ds .hasPermission (u , ReadPermission .class ))
197+ {
198+ return false ;
199+ }
200+
163201 if (!(ds .isDemographicData () && ds .getStudy ().getSubjectColumnName ().equalsIgnoreCase (col .getName ())))
164202 {
165203 throw new IllegalArgumentException ("Target column is not a key field: " + targetColumn );
@@ -177,7 +215,9 @@ public static boolean validateKey(Container defaultContainer, User u, @Nullable
177215 }
178216
179217 if (StringUtils .trimToNull (label ) == null )
218+ {
180219 throw new IllegalArgumentException ("Label must not be blank" );
220+ }
181221
182222 return true ;
183223 }
0 commit comments