def post(self):
    # raise Exception(self.request)
    now = datetime.datetime.now()
    form = GetOrganizationForm(self.request.POST)
    if not form.validate():
      self.redirect('/authentication')
    event = None
    for e in event_db.Event.gql(
    "WHERE name = :name LIMIT 1", name = form.event.data):
        event = e

    # check org and incident match
    org = None
    selected_org_name = self.request.get("name")
    if selected_org_name == "Other":
      selected_org_name = self.request.get("existing-organization")
    if selected_org_name == "Admin":
      # admin user
      for x in organization.Organization.gql(
    "WHERE name = :name LIMIT 1", name=selected_org_name
      ):
        org = x
    else:
      # regular user
      for x in organization.Organization.gql(
    "WHERE name = :name AND incidents = :incident LIMIT 1",
          name=selected_org_name,
          incident=event.key()
      ):
        org = x
      if org is None:
          # try legacy incident field
          for x in organization.Organization.gql(
              "WHERE name = :name and incident = :incident LIMIT 1",
              name=selected_org_name,
              incident=event.key()
          ):
              org = x

    # handle verified+active existing org joining new incident
    if not org and selected_org_name == 'Other':
        existing_org_name = self.request.get("existing-organization")
        for x in organization.Organization.gql(
            "WHERE name = :name LIMIT 1", name=existing_org_name):
            org = x

    # hash here, test if event and org and password_hash(form.password.data) in org.password_hash_list
    if event and org and generate_hash.recursive_hash(form.password.data) in org._password_hash_list and audit_db.login(org_name = org.name, ip=self.request.remote_addr, org = org, password_hash = generate_hash.recursive_hash(form.password.data), event_name = event.name, email=self.request.get("email")):
    # if event and org and org.password == form.password.data:
      # login was successful
      # (temp) force migration of org.incident -> org.incidents
      unicode(org.incidents)

      # add org to incident if not already allowed
      if not org.may_access(event):
          org.join(event)
          logging.info(
            u"authentication_handler: "
            u"Existing organization %s has joined incident %s." % (
                org.name, event.name
            )
          )

          # email administrators
          review_url = "%s://%s/admin-single-organization?organization=%s" % (
              urlparse(self.request.url).scheme,
              urlparse(self.request.url).netloc,
              org.key().id()
          )
          organization_form = organization.OrganizationForm(None, org)
          email_administrators_using_templates(
            event=event,
            subject_template_name='organization_joins_incident.to_admins.subject.txt',
            body_template_name='organization_joins_incident.to_admins.body.txt',
            organization=org,
            review_url=review_url,
            organization_form=organization_form,
          )
          org.save()

      # timestamp login
      now = datetime.datetime.utcnow()
      org.timestamp_login = now
      org.save()
      event.timestamp_last_login = now
      event.save()

      # create login key
      keys = key.Key.all()
      keys.order("date")
      selected_key = None
      for k in keys:
        age = now - k.date
        # Only use keys created in about the last day,
        # and garbage collect keys older than 2 days.
        if age.days > 14:
          k.delete()
        elif age.days <= 1:
          selected_key = k
      if not selected_key:
        selected_key = key.Key(
            secret_key = ''.join(random.choice(
                string.ascii_uppercase + string.digits)
                                  for x in range(20)))
        selected_key.put()

      # set cookie of org and event
      self.response.headers.add_header("Set-Cookie",
                                       selected_key.getCookie(org, event))
      self.redirect(urllib.unquote(self.request.get('destination', default_value='/').encode('ascii')))
    else:
      audit_db.bad_login(ip=self.request.remote_addr)
      self.redirect(self.request.url + "?error_message=Incorrect Organization and Passcode Combination")
示例#2
0
    def post(self):
        # create inactive, unverified org with a random password
        org = organization.Organization(
            name=self.request.get("name"),
            is_active=False,
            org_verified=False,
            voad_referral=self.request.get("voad_referral"),
            password=random_password.generate_password())

        # set non-contact org attributes by type
        for k, v in self.request.POST.iteritems():
            if not any(
                    k.startswith(prop)
                    for prop in self.CONTACT_PROPERTIES_LIST):
                if k == "choose_event":
                    chosen_event = event_db.Event.get_by_id(int(v))
                    setattr(org, "incidents", [chosen_event.key()])
                elif k in self.BOOLEAN_PROPERTIES_LIST:
                    setattr(org, k, bool(int(v)))
                else:
                    # clean away known pre-supplied strings
                    if v.strip() in ('http://', '@'):
                        v = None
                    setattr(org, k, v)

        # create contacts
        def get_contact_field(field_name, contact_num):
            field_value = self.request.get(field_name + '_%d' % i)
            return field_value.strip() if field_value else None

        new_contacts = []

        for i in range(10):
            first_name = get_contact_field('first_name', i)
            last_name = get_contact_field('last_name', i)
            title = get_contact_field('title', i)
            email = get_contact_field('personal_email', i)
            phone = get_contact_field('personal_phone', i)

            if first_name and last_name and email and phone:  # required fields
                new_contacts.append(
                    primary_contact_db.Contact(
                        first_name=first_name,
                        last_name=last_name,
                        title=title,
                        email=email,
                        phone=phone,
                        is_primary=(i == 0
                                    )  # the first contact is the primary one
                    ))

        # save
        organization.PutAndCacheOrganizationAndContact(org, new_contacts)

        # email primary contacts & administrators
        organization_form = organization.OrganizationForm(None, org)
        contact_forms = [
            primary_contact_db.ContactFormFull(None, contact)
            for contact in new_contacts
        ]
        messaging.email_contacts_using_templates(
            event=chosen_event,
            contacts=[c for c in new_contacts if c.is_primary],
            subject_template_name='new_organization.to_contacts.subject.txt',
            body_template_name='new_organization.to_contacts.body.txt',
            new_organization=org,
            primary_contact=new_contacts[0],
            application_id=messaging.get_application_id(),
            organization_form=organization_form,
            contact_forms=contact_forms,
        )
        approval_url = "%s://%s/admin-new-organization?new_organization=%d" % (
            urlparse(self.request.url).scheme, urlparse(
                self.request.url).netloc, org.key().id())
        messaging.email_administrators_using_templates(
            event=chosen_event,
            subject_template_name='new_organization.to_admins.subject.txt',
            body_template_name='new_organization.to_admins.body.txt',
            new_organization=org,
            primary_contact=new_contacts[0],
            application_id=messaging.get_application_id(),
            approval_url=approval_url,
            organization_form=organization_form,
            contact_forms=contact_forms,
        )

        self.redirect("/welcome")
    def post(self):
        # create inactive, unverified org with a random password
        org = organization.Organization(
            name=self.request.get("name"),
            is_active=False,
            org_verified=False,
            voad_referral=self.request.get("voad_referral"),
            password=random_password.generate_password()
        )

        # set non-contact org attributes by type
	for k, v in self.request.POST.iteritems():
          if not any(k.startswith(prop) for prop in self.CONTACT_PROPERTIES_LIST):
	    if k == "choose_event":
	      chosen_event = event_db.Event.get_by_id(int(v))
	      setattr(org, "incidents", [chosen_event.key()])
	    elif k in self.BOOLEAN_PROPERTIES_LIST:
	      setattr(org, k, bool(int(v)))
	    else:
              # clean away known pre-supplied strings
              if v.strip() in ('http://', '@'):
                  v = None
	      setattr(org, k, v)

        # create contacts
        def get_contact_field(field_name, contact_num):
            field_value = self.request.get(field_name + '_%d' % i)
            return field_value.strip() if field_value else None

        new_contacts = []

        for i in range(10):
            first_name = get_contact_field('first_name', i)
            last_name = get_contact_field('last_name', i)
            title = get_contact_field('title', i)
            email = get_contact_field('personal_email', i)
            phone = get_contact_field('personal_phone', i)

            if first_name and last_name and email and phone:  # required fields
                new_contacts.append(primary_contact_db.Contact(
                    first_name=first_name,
                    last_name=last_name,
                    title=title,
                    email=email,
                    phone=phone,
                    is_primary=(i == 0)  # the first contact is the primary one
                ))

        # save
	organization.PutAndCacheOrganizationAndContact(org, new_contacts)

        # email primary contacts & administrators
        organization_form = organization.OrganizationForm(None, org)
        contact_forms = [
            primary_contact_db.ContactFormFull(None, contact)
            for contact in new_contacts
        ]
        messaging.email_contacts_using_templates(
            event=chosen_event,
            contacts=[c for c in new_contacts if c.is_primary],
            subject_template_name='new_organization.to_contacts.subject.txt',
            body_template_name='new_organization.to_contacts.body.txt',
            new_organization=org,
            primary_contact=new_contacts[0],
            application_id=messaging.get_application_id(),
            organization_form=organization_form,
            contact_forms=contact_forms,
        )
        approval_url = "%s://%s/admin-new-organization?new_organization=%d" % (
            urlparse(self.request.url).scheme,
            urlparse(self.request.url).netloc,
            org.key().id()
        )
        messaging.email_administrators_using_templates(
            event=chosen_event,
            subject_template_name='new_organization.to_admins.subject.txt',
            body_template_name='new_organization.to_admins.body.txt',
            new_organization=org,
            primary_contact=new_contacts[0],
            application_id=messaging.get_application_id(),
            approval_url=approval_url,
            organization_form=organization_form,
            contact_forms=contact_forms,
        )
			    
	self.redirect("/welcome")
  def post(self):
    now = datetime.datetime.now()
    form = GetOrganizationForm(self.request.POST)
    if not form.validate():
      self.redirect('/authentication')
    event = None
    for e in event_db.Event.gql(
    "WHERE name = :name LIMIT 1", name = form.event.data):
        event = e

    # check org and incident match
    org = None
    selected_org_name = self.request.get("name")
    if selected_org_name == "Admin":
      # admin user
      for x in organization.Organization.gql(
	  "WHERE name = :name LIMIT 1", name=selected_org_name
      ):
	org = x
    else:
      # regular user
      for x in organization.Organization.gql(
	  "WHERE name = :name AND incidents = :incident LIMIT 1",
          name=selected_org_name,
          incident=event.key()
      ):
	org = x
      if org is None:
          # try legacy incident field
          for x in organization.Organization.gql(
              "WHERE name = :name and incident = :incident LIMIT 1",
              name=selected_org_name,
              incident=event.key()
          ):
              org = x

    # handle verified+active existing org joining new incident
    if not org and selected_org_name == 'Other':
        existing_org_name = self.request.get("existing-organization")
        for x in organization.Organization.gql(
            "WHERE name = :name LIMIT 1", name=existing_org_name):
            org = x

    if event and org and org.password == form.password.data:
      # login was successful

      # (temp) force migration of org.incident -> org.incidents
      unicode(org.incidents)

      # add org to incident if not already allowed
      if not org.may_access(event):
          org.join(event)
          logging.info(
            u"authentication_handler: "
            u"Existing organization %s has joined incident %s." % (
                org.name, event.name
            )
          )

          # email administrators
          review_url = "%s://%s/admin-single-organization?organization=%s" % (
              urlparse(self.request.url).scheme,
              urlparse(self.request.url).netloc,
              org.key().id()
          )
          organization_form = organization.OrganizationForm(None, org)
          email_administrators_using_templates(
            event=event,
            subject_template_name='organization_joins_incident.to_admins.subject.txt',
            body_template_name='organization_joins_incident.to_admins.body.txt',
            organization=org,
            review_url=review_url,
            organization_form=organization_form,
          )
          org.save()

      # timestamp login
      now = datetime.datetime.utcnow()
      org.timestamp_login = now
      org.save()
      event.timestamp_last_login = now
      event.save()

      # create login key
      keys = key.Key.all()
      keys.order("date")
      selected_key = None
      for k in keys:
        age = now - k.date
        # Only use keys created in about the last day,
        # and garbage collect keys older than 2 days.
        if age.days > 14:
          k.delete()
        elif age.days <= 1:
          selected_key = k
      if not selected_key:
        selected_key = key.Key(
            secret_key = ''.join(random.choice(
                string.ascii_uppercase + string.digits)
                                  for x in range(20)))
        selected_key.put()

      # set cookie of org and event
      self.response.headers.add_header("Set-Cookie",
                                       selected_key.getCookie(org, event))
      self.redirect(urllib.unquote(self.request.get('destination', default_value='/').encode('ascii')))
    else:
      self.redirect(self.request.url + "?error_message=Incorrect Organization and Passcode Combination")