Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Toolbox cleanup, add associated user #632

Merged
merged 7 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/build_image_from_dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ jobs:
uses: docker/build-push-action@v6
with:
context: .
ref: dev
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
1 change: 1 addition & 0 deletions config/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
PRODUCTION = (environ.get('PRODUCTION', default="false") == "true")
TEST_UID = environ.get('TEST_UID', default="dhf8r")
ADMIN_UIDS = re.split(r',\s*', environ.get('ADMIN_UIDS', default="dhf8r,kcm4zc,cah3us"))
SUPERUSER_UIDS = re.split(r',\s*', environ.get('SUPERUSER_UIDS', default="dhf8r,kcm4zc,cah3us"))
DEFAULT_UID = environ.get('DEFAULT_UID', default="dhf8r")

# Sentry flag
Expand Down
10 changes: 10 additions & 0 deletions crc/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ def is_admin(self):
# may change in the future.
return self.uid in app.config['ADMIN_UIDS']

def is_superuser(self):
# Currently superuser abilities are set in the configuration, but this
# may change in the future.
return self.uid in app.config['SUPERUSER_UIDS']

def encode_auth_token(self):
"""
Generates the Auth Token
Expand Down Expand Up @@ -60,13 +65,18 @@ class Meta:
include_relationships = True
uid = fields.String()
is_admin = fields.Method('get_is_admin', dump_only=True)
is_superuser = fields.Method('get_is_superuser', dump_only=True)
ldap_info = fields.Nested(LdapSchema)
impersonator = fields.Nested('self', many=False, allow_none=True)

@staticmethod
def get_is_admin(user):
return user.is_admin()

@staticmethod
def get_is_superuser(user):
return user.is_superuser()


class AdminSessionModel(db.Model):
__tablename__ = 'admin_session'
Expand Down
44 changes: 44 additions & 0 deletions crc/scripts/add_admin_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from crc import app, db
from crc.api.common import ApiError
from crc.models.study import StudyAssociated
from crc.scripts.script import Script
from crc.services.ldap_service import LdapService

from sqlalchemy import text


class AddAdminUser(Script):
scripts = {}

def get_description(self):
return """Add a user to the study_associated_user table (for all studies)"""

def do_task_validate_only(self, task, study_id, workflow_id, *args, **kwargs):
return self.do_task(task, study_id, workflow_id, *args, **kwargs)

def do_task(self, task, study_id, workflow_id, *args, **kwargs):
associated_user = kwargs.get('associated_user', None)
associated_group = kwargs.get('associated_group', None)
scripts = self.generate_augmented_list(task, study_id, workflow_id)
self.scripts = scripts
try:
ldap_user = self.scripts['ldap'](associated_user)
except ApiError as ae:
return {"error": str(ae), 'message': f"User {associated_user} not found in LDAP"}

if 'uid' in ldap_user and ldap_user['uid'] == associated_user:
# study_associated = StudyAssociated()

# using 'lje5u' because they are in both CTO and CTO Finance groups
# sql_string = f"select study_id, '{associated_user}', 'CTO', false, true from study_associated_user where uid='lje5u' and role='{associated_group}'"

sql_string = """insert into study_associated_user
(study_id, uid, role, send_email, access)
select study_id, '%s', 'CTO', false, true
from study_associated_user
where uid='lje5u' and role='%s'""" % (associated_user, associated_group)

sql = text(sql_string)
result = db.engine.execute(sql)
print(result.rowcount)
return {"message": f"User {associated_user} added to {associated_group} group"}
26 changes: 26 additions & 0 deletions crc/scripts/get_top_level_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from crc import session
from crc.api.common import ApiError
from crc.models.study import StudyModel
from crc.scripts.script import Script
from crc.services.workflow_processor import WorkflowProcessor
from crc.services.workflow_spec_service import WorkflowSpecService


class GetTopLevelData(Script):

def get_description(self):
return """This is my description"""

def do_task_validate_only(self, task, study_id, workflow_id, *args, **kwargs):
return self.do_task(task, study_id, workflow_id, *args, **kwargs)

def do_task(self, task, study_id, workflow_id, *args, **kwargs):
spec_service = WorkflowSpecService()
study_model = session.query(StudyModel).filter(StudyModel.id == study_id).first()

try:
master_workflow_results = WorkflowProcessor.run_master_spec(spec_service.master_spec, study_model)
except Exception as e:
raise ApiError("error_running_master_spec", f"Error running master spec: {str(e)}")

return master_workflow_results