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,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 }
0 commit comments