def register_participant(enrollment_id, plugin_id, plugin_scratch,
                             owner_name, owner_domain):
        # Checking that owner exists, is using a valid password, and the plugin is registered to that owner
        if OwnerService.does_owner_exist(owner_name, owner_domain):
            if PluginService.is_plugin_registered(plugin_id):
                owner = OwnerService.get(owner_name, owner_domain)
                enrollment = EnrollmentService.get(enrollment_id)
                plugin = PluginService.get_plugin(plugin_id)

                if owner.id == enrollment.owner_id == plugin.owner_id:
                    if EnrollmentService.is_enrollment_open(enrollment_id):
                        participants = Model.repository.participants
                        participant = participants.create()

                        participant.enrollment_id = enrollment_id
                        participant.plugin_id = plugin_id
                        participant.plugin_scratch = plugin_scratch

                        return participant.save()
                    else:
                        raise secure.SecurityException("Enrollment not open")
                else:
                    raise secure.SecurityException(
                        "Owner is not valid for enrollment or plugin")
            else:
                raise secure.SecurityException("Plugin is not valid")
예제 #2
0
    def get(self):
        logger.debug("Querying for tasks")
        auth = authenticate(self, [Permissions.READ_TASK])

        if auth["valid"]:
            surveys = SurveyService.get_surveys_by_owner(auth["owner_id"])

            surveys_tasks = {}

            for survey in surveys:
                surveys_tasks[survey] = TaskService.get_tasks_by_survey_id(
                    survey.id)

            tasks = []

            for survey, task_list in surveys_tasks.items():
                for task in task_list:
                    tasks.append({
                        "id":
                        task.id,
                        "name":
                        task.name,
                        "protocol_name":
                        ProtocolService.get_protocol(survey.protocol_id).name,
                        "enrollment_name":
                        EnrollmentService.get(survey.enrollment_id).name
                    })

            response = {"status": "success", "tasks": tasks}
            self.set_status(200)

            response_json = json.dumps(response)
            logger.debug(response_json)
            self.write(response_json)
            self.flush()
    def get(self, enrollment_id):
        logger.debug("Getting list of enrolled participants")
        auth_response = authenticate(
            self, [Permissions.READ_ENROLLMENT, Permissions.READ_PARTICIPANT])

        if auth_response["valid"]:
            owner = OwnerService.get_by_id(auth_response["owner_id"])
            enrollment = EnrollmentService.get(enrollment_id)

            if owner.id == enrollment.owner_id:
                participants = ParticipantService.get_participants_in_enrollment(
                    enrollment.id)

                response = {
                    "status":
                    "success",
                    "participant_ids":
                    [participant.id for participant in participants]
                }
                self.set_status(200)
            else:
                response = {
                    "status":
                    "error",
                    "message":
                    "Owner does not have authorization to administer this enrollment"
                }
                self.set_status(401)

            response_json = json.dumps(response)
            logger.debug(response_json)
            self.write(response_json)
            self.flush()
    def delete(self, enrollment_id):
        logger.debug("Removing an enrollment")
        auth_response = authenticate(self, [Permissions.WRITE_ENROLLMENT])

        if auth_response['valid']:
            owner = OwnerService.get_by_id(auth_response["owner_id"])
            enrollment = EnrollmentService.get(enrollment_id)

            if enrollment is None or owner.id == enrollment.owner_id:
                if enrollment is not None:  # DELETE is idempotent
                    EnrollmentService.delete_enrollment(enrollment_id)

                self.set_status(200)
                response = {"status": "success"}
            else:
                self.set_status(401)
                response = {
                    "status":
                    "error",
                    "message":
                    "Owner does not have authorization to administer this enrollment"
                }

            response_json = json.dumps(response)
            logger.debug(response_json)
            self.write(response_json)
            self.flush()
    def get(self, enrollment_id):
        logger.debug("Getting metadata about an enrollment")
        auth_response = authenticate(self, [Permissions.READ_ENROLLMENT])

        if auth_response["valid"]:
            owner = OwnerService.get_by_id(auth_response["owner_id"])
            enrollment = EnrollmentService.get(enrollment_id)

            if enrollment.owner_id == owner.id:

                enrollment_dict = {
                    "id": enrollment.id,
                    "open_date": enrollment.open_date.strftime('%Y-%m-%d'),
                    "close_date": enrollment.close_date.strftime('%Y-%m-%d'),
                    "expiry_date": enrollment.expiry_date.strftime('%Y-%m-%d')
                }

                response = {"status": "success", "enrollment": enrollment_dict}
                self.set_status(200)
            else:
                response = {
                    "status":
                    "error",
                    "message":
                    "Owner does not have authorization to administer this enrollment"
                }
                self.set_status(401)

            response_json = json.dumps(response)
            logger.debug(response_json)
            self.write(response_json)
            self.flush()
    def get(self, enrollment_id, participant_id):
        logger.debug("Retrieving participant info")
        auth_response = authenticate(
            self,
            [[Permissions.READ_ENROLLMENT, Permissions.READ_PARTICIPANT]])

        if auth_response["valid"]:
            owner = OwnerService.get_by_id(auth_response["owner_id"])
            enrollment = EnrollmentService.get(enrollment_id)

            if owner.id == enrollment.owner_id:
                try:
                    participant = ParticipantService.get_participant(
                        participant_id)
                except SecurityException as e:
                    response = {"status": "error", "message": e.message}
                    self.set_status(410)
                else:
                    if participant is None:
                        response = {
                            "status": "error",
                            "message": "Participant does not exist"
                        }

                        self.set_status(410)
                    elif participant.enrollment_id == enrollment_id:
                        participant_dict = {
                            "participant_id": participant.id,
                            "plugin_id": participant.plugin_id,
                            "plugin_scratch": participant.plugin_scratch,
                            "enrollment_id": participant.enrollment_id
                        }

                        response = {
                            "status": "success",
                            "participant": participant_dict
                        }

                        self.set_status(200)
                    else:
                        response = {
                            "status": "error",
                            "message":
                            "Participant does not belong to enrollment"
                        }
                        self.set_status(400)
            else:
                response = {
                    "status":
                    "error",
                    "message":
                    "Owner does not have authorization to administer enrollment"
                }

            response_json = json.dumps(response)
            logger.debug(response_json)
            self.write(response_json)
            self.flush()
    def post(self, enrollment_id):
        logger.debug("Adding participant to enrollment")
        plugin_id = self.get_argument("plugin_id")
        plugin_name = self.get_argument("plugin_name")
        plugin_scratch = self.get_argument("plugin_scratch")
        ParticipantDetails.participantEnrollment.append(enrollment_id)
        auth_response = authenticate(
            self, [Permissions.READ_ENROLLMENT, Permissions.WRITE_PARTICIPANT])

        if auth_response["valid"]:
            owner = OwnerService.get_by_id(auth_response["owner_id"])

            if PluginService.is_owned_by(plugin_id, owner.id):
                enrollment = EnrollmentService.get(int(enrollment_id))

                if owner.id == enrollment.owner_id:
                    try:
                        ParticipantService.register_participant(
                            enrollment.id, plugin_id, plugin_scratch,
                            owner.name, owner.domain)

                        participants = ParticipantService.get_participants_in_enrollment(
                            enrollment_id)
                        for participant in participants:
                            lastparticipantid = participant.id
                        ParticipantDetails.participantEnrollment.append(
                            lastparticipantid)
                        ParticipantDetails.participantEnrollment.append(
                            plugin_name)
                        ParticipantDetails.participantEnrollment.append(
                            plugin_scratch)
                        ParticipantDetails.get_enrollment()
                    except SecurityException as e:
                        response = {"status": "error", "message": e.message}
                    else:
                        response = {"status": "success"}
                else:
                    response = {
                        "status":
                        "error",
                        "message":
                        "Owner does not have authorization to administer this enrollment"
                    }
            else:
                response = {
                    "status": "error",
                    "message":
                    "Participant's plugin is not registered with owner"
                }

                self.set_status(401)

            response_json = json.dumps(response)
            logger.debug(response_json)
            self.write(response_json)
            self.flush()
    def post(self, enrollment_id):
        logger.debug("Updating metadata about an enrollment")

        name = self.get_argument("name", None)
        open_date = self.get_argument("open_date", None)
        close_date = self.get_argument("close_date", None)
        expiry_date = self.get_argument("expiry_date", None)

        auth_response = authenticate(self, [Permissions.WRITE_ENROLLMENT])

        if auth_response['valid']:
            owner = OwnerService.get_by_id(auth_response["owner_id"])
            enrollment = EnrollmentService.get(enrollment_id)

            if enrollment.owner_id == owner.id:

                if name is not None:
                    enrollment.name = name

                if open_date is not None:
                    enrollment.open_date = parser.parse(open_date)

                if close_date is not None:
                    enrollment.close_date = parser.parse(close_date)

                if expiry_date is not None:
                    enrollment.expiry_date = parser.parse(expiry_date)

                enrollment.save()

                response = {"status": "success"}
                self.set_status(200)
            else:
                response = {
                    "status":
                    "error",
                    "message":
                    "Owner does not have authorization to administer this enrollment"
                }
                self.set_status(401)

            response_json = json.dumps(response)
            logger.debug(response_json)
            self.write(response_json)
            self.flush()
    def delete(self, enrollment_id, participant_id):
        logger.debug("Removing participant from enrollment")
        auth_response = authenticate(
            self, [Permissions.READ_ENROLLMENT, Permissions.WRITE_PARTICIPANT])

        if auth_response["valid"]:
            owner = OwnerService.get_by_id(auth_response["owner_id"])
            enrollment = EnrollmentService.get(enrollment_id)

            if owner.id == enrollment.owner_id:
                participant = ParticipantService.get_participant(
                    participant_id)

                if participant is None or participant.enrollment_id == enrollment.id:
                    if participant is not None:
                        ParticipantService.delete_participant(participant_id)

                    response = {
                        "status": "success",
                    }
                    self.set_status(200)
                else:
                    response = {
                        "status": "error",
                        "message": "Participant does not belong to enrollment"
                    }
            else:
                response = {
                    "status":
                    "error",
                    "message":
                    "Owner does not have authorization to administer enrollment"
                }
                self.set_status(401)

            response_json = json.dumps(response)
            logger.debug(response_json)
            self.write(response_json)
            self.flush()