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")
    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 post(self):

        logger.debug("Attempt to login")

        data = json_decode(self.request.body)
        username = data["username"]
        password = data["password"]
        print(username)

        splitter = username.find('@')
        print(splitter)

        if splitter == -1:
            self.set_status(401)

            response = {"status": "error", "reason": "Invalid username"}
        else:
            owner_name = username[:splitter]
            owner_domain = username[splitter + 1:]

            if OwnerService.validate_password(owner_name, owner_domain,
                                              password):
                # Generate a session
                session = secure.create_session(
                    OwnerService.get(owner_name, owner_domain).id)

                self.set_status(200)

                response = {
                    "status": "success",
                    "session_id": session.id,
                    "username": username
                }

            else:
                self.set_status(401)

                response = {
                    "status": "error",
                    "reason": "Username and password do not match"
                }

        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 validate_plugin(plugin_id, owner_id, token):
        plugin = PluginService.get_plugin(plugin_id)
        owner = OwnerService.get_by_id(owner_id)

        if plugin and owner is not None:
            if plugin.owner_id == owner.id:
                test = secure.encrypt_password(token, plugin.salt).decode()
                return test == plugin.secret_token

        return False
    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()
Esempio n. 11
0
    def post(self):

        logger.debug("Attempting to verify a session")

        data = json_decode(self.request.body)

        username = data["username"]
        session_id = data["session_id"]

        splitter = username.find('@')

        if splitter == -1:
            self.set_status(401)

            response = {"status": "error", "reason": "Invalid username"}
        else:
            owner_name = username[:splitter]
            owner_domain = username[splitter + 1:]

            if secure.session_valid(
                    OwnerService.get(owner_name, owner_domain).id, session_id):
                self.set_status(200)

                response = {"status": "success"}
            else:
                self.set_status(401)

                response = {
                    "status": "error",
                    "reason": "Invalid or out of date session"
                }

        response_json = json.dumps(response)
        logger.debug(response_json)
        self.write(response_json)
        self.flush()
    def post(self):
        logger.debug("Adding new enrollment")

        name = self.get_argument("name")
        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"]:
            if open_date is not None:
                open_date = parser.parse(open_date)

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

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

            owner = OwnerService.get_by_id(auth_response["owner_id"])
            enrollment = EnrollmentService.add_enrollment(
                name, owner.id, open_date, close_date, expiry_date)
            response = {"status": "success", "enrollment_id": enrollment.id}
            self.set_status(200)
        else:
            response = {
                "status": "error",
                "message": "Do not have authorization to make this request"
            }
            self.set_status(401)

        response_json = json.dumps(response)
        logger.debug(response_json)
        self.write(response_json)
        self.flush()
Esempio n. 13
0
from smsurvey.core.model.model import Model

from smsurvey import config
from smsurvey.core.services.instance_service import InstanceService
from smsurvey.core.services.owner_service import OwnerService
from smsurvey.core.services.enrollment_service import EnrollmentService
from smsurvey.core.services.survey_service import SurveyService
from smsurvey.core.services.protocol_service import ProtocolService

Model.from_database(config.DAO)

owner = OwnerService.get("sam", "mhealth")

enrollment = EnrollmentService.get_by_owner(owner.id)[0]
survey = SurveyService.create_survey(owner.id,
                                     ProtocolService.get_all_protocols()[0].id,
                                     enrollment.id, 1, 20, 1)

instances = InstanceService.create_instances(survey.id)
Esempio n. 14
0
if __name__ == "__main__":

    print("Loading models")
    Model.from_database(config.dao)

    create_question_store.create(True)
    create_response_store.create(True)
    create_time_rule_store.create(True)

    question_service = QuestionService()

    survey_id = "1"

    print("Creating Owner")
    owner = OwnerService.create_owner('ayanagrawal', 'mhealth', 'password')
    print("Owner created")

    print("Generating questions")
    one = get_one(survey_id)
    two = get_two(survey_id)
    three = get_three(survey_id)
    four = get_four(survey_id)
    five = get_five(survey_id)
    six = get_six(survey_id)
    seven = get_seven(survey_id)
    eight = get_eight(survey_id)
    ten = get_ten(survey_id)
    eleven = get_eleven(survey_id)
    twelve = get_twelve(survey_id)
    thirteen = get_thirteen(survey_id)