Esempio n. 1
0
def add_user(
    first_name: str,
    last_name: str,
    email: str,
    agency_ein: str,
    middle_initial: str = None,
    is_admin: bool = False,
    is_active: bool = False,
):
    """
    Add an agency user into the database.
    """
    if not first_name:
        raise click.UsageError("First name is required")
    if not last_name:
        raise click.UsageError("Last name is required")
    if not email:
        raise click.UsageError("Email Address is required")
    if not agency_ein:
        raise click.UsageError("Agency EIN is required")

    user = Users(
        guid=generate_guid(),
        first_name=first_name,
        middle_initial=middle_initial,
        last_name=last_name,
        email=email,
        email_validated=False,
        is_nyc_employee=True,
        is_anonymous_requester=False,
    )
    db.session.add(user)

    agency_user = AgencyUsers(
        user_guid=user.guid,
        agency_ein=agency_ein,
        is_agency_active=is_active,
        is_agency_admin=is_admin,
        is_primary_agency=True,
    )

    db.session.add(agency_user)
    db.session.commit()
    if is_admin:
        redis_key = "{current_user_guid}-{update_user_guid}-{agency_ein}-{timestamp}".format(
            current_user_guid="openrecords_support",
            update_user_guid=user.guid,
            agency_ein=agency_ein,
            timestamp=datetime.now(),
        )
        make_user_admin.apply_async(args=(user.guid, "openrecords_support",
                                          agency_ein),
                                    task_id=redis_key)

    print(user)
Esempio n. 2
0
def create_user(
    first_name=None,
    last_name=None,
    email=None,
    ein=None,
    is_admin=False,
    is_active=False,
):
    """Create an agency user."""
    if first_name is None:
        raise InvalidCommand("First name is required")

    if last_name is None:
        raise InvalidCommand("Last name is required")

    if email is None:
        raise InvalidCommand("Email is required")

    if ein is None:
        raise InvalidCommand("Agency EIN is required")

    user = Users(
        guid=generate_guid(),
        auth_user_type=user_type_auth.AGENCY_LDAP_USER,
        email=email,
        first_name=first_name,
        last_name=last_name,
        title=None,
        organization=None,
        email_validated=True,
        terms_of_use_accepted=True,
        phone_number=None,
        fax_number=None,
        mailing_address=create_mailing_address(None, None, None, None),
    )
    db.session.add(user)

    agency_user = AgencyUsers(
        user_guid=user.guid,
        auth_user_type=user.auth_user_type,
        agency_ein=ein,
        is_agency_active=is_active,
        is_agency_admin=is_admin,
        is_primary_agency=True,
    )
    db.session.add(agency_user)
    db.session.commit()

    print(user)
Esempio n. 3
0
def migrate_to_agencyusers():
    """Migrate Users and Agencies to Users + Agencies + AgencyUsers."""
    users = Users.query.with_entities(
        Users.guid, Users.auth_user_type, Users.agency_ein,
        Users.is_agency_admin, Users.is_agency_active).filter(
            Users.auth_user_type == user_type_auth.AGENCY_LDAP_USER).all()

    for user in users:
        guid, user_type, ein, is_admin, is_active = user
        agency_user = AgencyUsers(user_guid=guid,
                                  auth_user_type=user_type,
                                  agency_ein=ein,
                                  is_agency_active=is_active,
                                  is_agency_admin=is_admin,
                                  is_primary_agency=True)
        db.session.add(agency_user)

    db.session.commit()
Esempio n. 4
0
def add_user():
    """Adds a user to the users and agency_users tables.

    Returns:
        Template with context.
    """
    form = AddAgencyUserForm()

    if form.validate_on_submit():
        agency_ein = form.agency.data
        first_name = form.first_name.data
        last_name = form.last_name.data
        email = form.email.data

        user = Users.query.filter(
            func.lower(Users.email) == email.lower(),
            Users.is_nyc_employee == True).first()

        if user is not None:
            flash('{} {} has already been added.'.format(
                first_name, last_name),
                  category='warning')
        else:
            new_user = Users(
                guid=generate_guid(),
                first_name=first_name,
                last_name=last_name,
                email=email,
                email_validated=False,
                is_nyc_employee=True,
                is_anonymous_requester=False,
            )
            create_object(new_user)

            agency_user = AgencyUsers(user_guid=new_user.guid,
                                      agency_ein=agency_ein,
                                      is_agency_active=False,
                                      is_agency_admin=False,
                                      is_primary_agency=True)
            create_object(agency_user)

            agency = Agencies.query.filter_by(ein=agency_ein).one()
            admin_emails = get_agency_admin_emails(agency)
            send_email(
                subject='User {} Added'.format(new_user.fullname),
                to=admin_emails,
                template='email_templates/email_agency_user_added',
                agency_name=agency.name,
                name=new_user.fullname,
            )

            content_id = 'login_screenshot'
            image = {
                'path': current_app.config['LOGIN_IMAGE_PATH'],
                'content_id': content_id
            }
            send_email(subject='OpenRecords Portal',
                       to=[new_user.email],
                       email_content=render_template(
                           'email_templates/email_user_added.html',
                           agency_name=agency.name,
                           content_id=content_id,
                           domain=new_user.email.split('@')[1],
                           name=new_user.fullname),
                       image=image)

            send_email(
                subject='User {} Added'.format(new_user.fullname),
                to=[OPENRECORDS_DL_EMAIL],
                email_content=
                '{} has been added to OpenRecords. Add {} to the service desk.'
                .format(new_user.fullname, new_user.email))

            flash('{} has been added.'.format(new_user.fullname),
                  category='success')
        return redirect(url_for('admin.add_user'))

    return render_template('admin/add_user.html', form=form)