def test_get_job_recipient_internal_uid(base_app, db, es_clear): expected_email = "*****@*****.**" user = User() user.email = expected_email user.active = True user.id = 23 db.session.add(user) test_user = UserIdentity(id="user", method="test", id_user=user.id) db.session.add(test_user) job = { "acquisition_source": { "datetime": "2019-07-04T11:21:22.611086", "email": "*****@*****.**", "internal_uid": user.id, "method": "submitter", "orcid": "0000-0002-8672-7088", "source": "submitter", "submission_number": "None", } } email = get_job_recipient(job) # The email is not the one in acquisition_source but in the user account assert email == expected_email
def create(cls, data, **kwargs): """User record creation. :param cls - class object :param data - dictionary representing a user record """ with db.session.begin_nested(): email = data.pop('email', None) roles = data.pop('roles', None) cls._validate(data=data) password = data.pop('password', data.get('birth_date', '123456')) user = BaseUser(password=hash_password(password), profile=data, active=True) db.session.add(user) profile = user.profile for field in cls.profile_fields: value = data.get(field) if value is not None: if field == 'birth_date': value = datetime.strptime(value, '%Y-%m-%d') setattr(profile, field, value) # send the reset password notification for new users if email: user.email = email db.session.merge(user) db.session.commit() if user.email: send_reset_password_instructions(user) confirm_user(user) return cls(user)
def login_callback(user_info): """Login user base on SSO context (create one if necessary). Function should not raise an exception if `user_info` is not valid or `User` was not found in database. """ from invenio_accounts.models import User from invenio.ext.login import (authenticate, login_redirect, current_user) from invenio.ext.sqlalchemy import db user_info['group'] = fetch_groups(user_info['group']).values() user_info['external'] = fetch_external(user_info.get('external')) try: auth = authenticate(user_info['email'], login_method='SSO') if auth is None: user = User() user.nickname = user_info['nickname'] user.email = user_info['email'] user.password = '' user.settings = {'login_method': 'SSO'} db.session.add(user) db.session.commit() auth = authenticate(user_info['email'], login_method='SSO') if auth is None: return redirect('/') current_user.info['group'] = current_user.get('group', []) + \ user_info['group'] current_user.save() except: flash('Problem with login (%s)' % (str(user_info)), 'error') return redirect('/') return login_redirect()
def create_user(db, email): u = User() u.email = email u.active = True db.session.add(u) db.session.commit() return u
def login_callback(user_info): """Login user base on SSO context (create one if necessary). Function should not raise an exception if `user_info` is not valid or `User` was not found in database. """ from invenio_accounts.models import User from invenio_ext.login import (authenticate, login_redirect, current_user) from invenio_ext.sqlalchemy import db user_info['group'] = fetch_groups(user_info['group']).values() user_info['external'] = fetch_external(user_info.get('external')) try: auth = authenticate(user_info['email'], login_method='SSO') if auth is None: user = User() user.nickname = user_info['nickname'] user.email = user_info['email'] user.password = '' user.settings = {'login_method': 'SSO'} db.session.add(user) db.session.commit() auth = authenticate(user_info['email'], login_method='SSO') if auth is None: return redirect('/') current_user.info['group'] = current_user.get('group', []) + \ user_info['group'] current_user.save() except: flash('Problem with login (%s)' % (str(user_info)), 'error') return redirect('/') return login_redirect()
def create_user_from_data(data): """Create a user and set the profile fields from a data. :param data: A dict containing a mix of patron and user data. :returns: The modified dict. """ from .modules.users.api import User data = deepcopy(data) profile_fields = [ 'first_name', 'last_name', 'street', 'postal_code', 'gender', 'city', 'birth_date', 'username', 'home_phone', 'business_phone', 'mobile_phone', 'other_phone', 'keep_history', 'country', 'email' ] user = User.get_by_username(data.get('username')) if not user: with db.session.begin_nested(): # create the user user = BaseUser(password=hash_password( data.get('birth_date', '123456')), profile=dict(), active=True) db.session.add(user) # set the user fields if data.get('email') is not None: user.email = data['email'] profile = user.profile # set the profile for field in profile_fields: value = data.get(field) if value is not None: if field == 'birth_date': value = datetime.strptime(value, '%Y-%m-%d') setattr(profile, field, value) db.session.merge(user) db.session.commit() confirm_user(user) user_id = user.id else: user_id = user.user.id # remove the user fields from the data for field in profile_fields: try: del data[field] except KeyError: pass data['user_id'] = user_id return data
def sync_user_and_profile(cls, data): """Create or update the rero user with the patron data. :param data - dict representing the patron data """ created = False # start a session to be able to rollback if the data are not valid user = cls._get_user_by_data(data) with db.session.begin_nested(): # need to create the user if not user: birth_date = data.get('birth_date') # sanity check if not birth_date: raise RecordValidationError('birth_date field is required') # the default password is the birth date user = User(email=data.get('email'), password=hash_password(birth_date), profile=dict(), active=True) db.session.add(user) created = True else: if user.email != data.get('email'): user.email = data.get('email') # update all common fields if user.profile is None: user.profile = UserProfile(user_id=user.id) profile = user.profile for field in cls.profile_fields: # date field need conversion if field == 'birth_date': setattr(profile, field, datetime.strptime(data.get(field), '%Y-%m-%d')) elif field == 'keep_history': setattr(profile, field, data.get('patron', {}).get(field, True)) else: setattr(profile, field, data.get(field, '')) db.session.merge(user) data['user_id'] = user.id if created: # the fresh created user return user