Skip to content

Commit

Permalink
addressing @josenavas comments
Browse files Browse the repository at this point in the history
  • Loading branch information
antgonza committed Jan 27, 2017
1 parent 7f97f2a commit 23104d7
Showing 1 changed file with 70 additions and 44 deletions.
114 changes: 70 additions & 44 deletions qiita_db/meta_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,7 @@ def _get_data_fpids(constructor, object_id):


def validate_filepath_access_by_user(user, filepath_id):
"""Gets all filepaths that this user should have access to
This gets all raw, preprocessed, and processed filepaths, for studies
that the user has access to, as well as all the mapping files and biom
tables associated with the analyses that the user has access to.
"""Validates if the user has access to the filepath_id
Parameters
----------
Expand All @@ -70,60 +66,90 @@ def validate_filepath_access_by_user(user, filepath_id):
-----
Admins have access to all files so True is always returned
"""
with qdb.sql_connection.TRN:
TRN = qdb.sql_connection.TRN
with TRN:
if user.level == "admin":
return True
# admins have access all files

access = False

# check the public artifacts
for artifact in qdb.artifact.Artifact.iter_public():
for fid, _, _ in artifact.filepaths:
if fid == filepath_id:
sql = """SELECT
(SELECT count(*) FROM qiita.artifact_filepath
WHERE filepath_id = {0}) AS artifact,
(SELECT count(*) FROM qiita.sample_template_filepath
WHERE filepath_id = {0}) AS sample_info,
(SELECT count(*) FROM qiita.prep_template_filepath
WHERE filepath_id = {0}) AS prep_info,
(SELECT count(*) FROM qiita.job_results_filepath
WHERE filepath_id = {0}) AS job_results,
(SELECT count(*) FROM qiita.analysis_filepath
WHERE filepath_id = {0}) AS analysis""".format(filepath_id)
TRN.add(sql)

arid, sid, pid, jid, anid = TRN.execute_fetchflatten()

# artifacts
if arid:
# check the public artifacts
public_artifacts = qdb.artifact.Artifact.iter_public()
for artifact in public_artifacts:
if filepath_id in [fid for fid, _, _ in artifact.filepaths]:
access = True
break
# prep templates
# if not found check the user artifacts from their studies
if not access:
for pt in artifact.prep_templates:
for fid, _ in pt.get_filepaths():
if fid == filepath_id:
access = True
user_studies = user.user_studies | user.shared_studies
for s in user_studies:
if s.sample_template:
for a in s.artifacts():
if filepath_id in [fid[0] for fid in a.filepaths]:
access = True
break
# just avoiding extra loops if found
if access:
break
# sample template
if not access:
for fid, _ in artifact.study.sample_template.get_filepaths():
if fid == filepath_id:
# sample info files
elif sid:
# check private and shared studies with the user
user_studies = user.user_studies | user.shared_studies
for s in user_studies:
st = s.sample_template
if st is not None:
# sample info files
if filepath_id in [fid for fid, _ in st.get_filepaths()]:
access = True
break

# check private and shared studies with the user
if not access:
studies = user.user_studies | user.shared_studies
for study in studies:
if study.sample_template:
# if that didn't work let's check the public sample info files
if not access:
public_studies = qdb.study.Study.get_by_status('public')
for s in public_studies:
st = s.sample_template
if st is not None:
if filepath_id in [fid[0] for fid in
st.get_filepaths()]:
access = True
break
# prep info files
elif pid:
# check private and shared studies with the user
user_studies = user.user_studies | user.shared_studies
for s in user_studies:
for pt in s.prep_templates():
# sample info files
for fid, _ in study.sample_template.get_filepaths():
if fid == filepath_id:
if filepath_id in [fid for fid, _ in pt.get_filepaths()]:
access = True
break
# if that didn't work let's check the public prep info files
if not access:
public_studies = qdb.study.Study.get_by_status('public')
for s in public_studies:
for pt in s.prep_templates():
if filepath_id in [fid[0]
for fid in pt.get_filepaths()]:
access = True
break
# prep info files
if not access:
for pt in study.prep_templates():
for fid, _ in pt.get_filepaths():
if fid == filepath_id:
access = True
break
# artifacts
if not access:
for artifact in study.artifacts():
for fid, _, _ in artifact.filepaths:
if fid == filepath_id:
access = True
break

# next analyses
if not access:
elif anid or jid:
analyses = qdb.analysis.Analysis.get_by_status('public') | \
user.private_analyses | user.shared_analyses
for analysis in analyses:
Expand Down

0 comments on commit 23104d7

Please sign in to comment.