Exemplo n.º 1
0
    def create_users(self, user_repo, **form_data):
        """Create users from a remote source using an importer object and
        avoiding the creation of repeated users"""

        from pybossa.view.account import create_account

        n = 0
        failed_users = 0
        invalid_values = set()
        importer = self._create_importer_for(**form_data)
        for user_data in importer.users():
            found = user_repo.search_by_email(email_addr=user_data['email_addr'].lower())
            if not found:
                full_name = user_data['fullname']
                project_slugs = user_data.get('project_slugs')
                form = self._create_user_form(user_data)
                if not form.validate():
                    failed_users += 1
                    current_app.logger.error(u'Failed to import user {}, {}'
                        .format(full_name, form.errors))
                    invalid_values.update(form.errors.keys())
                    continue
                user_data['metadata']['admin'] = current_user.name
                user_data['password'] = form.password.data
                create_account(user_data, project_slugs=project_slugs)
                n += 1
        if n > 0:
            msg = str(n) + " " + gettext('new users were imported successfully. ')
        else:
            msg = gettext('It looks like there were no new users created. ')

        if failed_users:
            msg += str(failed_users) + gettext(' user import failed for incorrect values of ') + ', '.join(invalid_values) + '.'
        return msg
Exemplo n.º 2
0
def handle_bloomberg_response():
    sso_settings = current_app.config.get('BSSO_SETTINGS')
    auth = OneLogin_Saml2_Auth(prepare_onelogin_request(), sso_settings)
    auth.process_response()
    errors = auth.get_errors()
    if errors:
        # BSSO was unable to authenticate the user
        error_reason = auth.get_last_error_reason()
        current_app.logger.error('BSSO auth error(s): %s %s', errors,
                                 error_reason)
        flash(gettext('There was a problem during the sign in process.'),
              'error')
        return redirect(url_for('home.home'))
    elif auth.is_authenticated:
        # User is authenticated on BSSO, load user from GIGwork API.
        attributes = auth.get_attributes()
        user = user_repo.get_by(
            email_addr=unicode(attributes['emailAddress'][0]).lower())
        if user is not None:
            # User is authenticated on BSSO and already has a GIGwork account.
            return _sign_in_user(user, next_url=request.form.get('RelayState'))
        else:
            # User is authenticated on BSSO, but does not yet have a GIGwork account, auto create one.
            user_data = {}
            try:
                user_data['fullname'] = attributes['firstName'][
                    0] + " " + attributes['lastName'][0]
                user_data['email_addr'] = attributes['emailAddress'][0]
                user_data['name'] = attributes['username'][0]
                user_data['data_access'] = ["L4"]
                user_data['password'] = generate_password()
                create_account(user_data, auto_create=True)
                flash('A new account has been created for you using BSSO.')
                user = user_repo.get_by(
                    email_addr=unicode(user_data['email_addr'].lower()))
                return _sign_in_user(user,
                                     next_url=request.form.get('RelayState'))
            except Exception as error:
                brand = current_app.config['BRAND']
                current_app.logger.exception(
                    'Auto-account creation error: %s, for user attributes: %s',
                    error, attributes)
                flash(
                    gettext(
                        'There was a problem signing you in. Please contact your {} administrator.'
                        .format(brand)), 'error')
                return redirect(url_for('home.home'))
    else:
        # Failed to authenticate user on BSSO.
        current_app.logger.exception('BSSO login error')
        flash(
            gettext(
                'We were unable authenticate and log you into an account. Please contact a Gigwork administrator.'
            ), 'error')
        return redirect(url_for('home.home'))
Exemplo n.º 3
0
    def create_users(self, user_repo, **form_data):
        """Create users from a remote source using an importer object and
        avoiding the creation of repeated users"""

        from pybossa.view.account import create_account
        n = 0
        failcount = 0
        importer = self._create_importer_for(**form_data)
        failed_user_imports = []
        for user_data in importer.users():
            try:
                found = user_repo.search_by_email(
                    email_addr=user_data['email_addr'].lower())
                if not found:
                    password = user_data['password']
                    is_password_valid, message = check_password_strength(
                        password)
                    if not is_password_valid:
                        failed_user_imports.append(user_data['fullname'])
                        failcount += 1
                    else:
                        project_slugs = user_data['project_slugs'].split()
                        create_account(user_data, project_slugs=project_slugs)
                        n += 1
            except Exception:
                current_app.logger.exception('Error in create_user')
        if n > 0:
            msg = str(n) + " " + gettext(
                'new users were imported successfully.')
        else:
            msg = gettext('It looks like there were no new users created.')
        if failcount > 0:
            msg += str(failcount) + gettext(
                ' user(s) could not be imported due to weak password.')
            current_app.logger.error(
                'Failed to import users due to password mismatch: {0}'.format(
                    ",".join(failed_user_imports)))
        return msg