def seed_user(email, role, dept): ''' Creates a new user in the database. ''' from beacon.models.users import User, Department seed_email = email if email else app.config.get('SEED_EMAIL') user_exists = User.query.filter(User.email == seed_email).first() department = Department.query.filter( Department.name == db.func.lower(dept) ).first() if user_exists: print 'User {email} already exists'.format(email=seed_email) else: try: new_user = User.create( email=seed_email, created_at=datetime.datetime.utcnow(), role_id=role, department=department if department else None ) db.session.add(new_user) db.session.commit() print 'User {email} successfully created!'.format(email=seed_email) except Exception, e: print 'Something went wrong: {exception}'.format(exception=e.message)
def auth(): '''Endpoint from AJAX request for authentication from persona ''' data = urllib.urlencode({ 'assertion': request.form.get('assertion'), 'audience': current_app.config.get('BROWSERID_URL') }) req = urllib2.Request('https://verifier.login.persona.org/verify', data) response = json.loads(urllib2.urlopen(req).read()) if response.get('status') != 'okay': current_app.logger.debug( 'REJECTEDUSER: User login rejected from persona. Messages: {}'. format(response)) abort(403) next_url = request.args.get('next', None) email = response.get('email') user = User.query.filter(User.email == email).first() domain = email.split('@')[1] if len(email.split('@')) > 1 else None if user: login_user(user) flash('Logged in successfully!', 'alert-success') current_app.logger.debug( 'LOGIN: User {} logged in successfully'.format(user.email)) return next_url if next_url else '/' elif AcceptedEmailDomains.valid_domain(domain): user = User.create( email=email, role=Role.query.filter(Role.name == 'staff').first(), department=Department.query.filter( Department.name == 'New User').first()) login_user(user) current_app.logger.debug( 'NEWUSER: New User {} successfully created'.format(user.email)) return '/users/profile' else: current_app.logger.debug( 'NOTINDB: User {} not in DB -- aborting!'.format(email)) abort(403)
def auth(): '''Endpoint from AJAX request for authentication from persona ''' data = urllib.urlencode({ 'assertion': request.form.get('assertion'), 'audience': current_app.config.get('BROWSERID_URL') }) req = urllib2.Request('https://verifier.login.persona.org/verify', data) response = json.loads(urllib2.urlopen(req).read()) if response.get('status') != 'okay': current_app.logger.debug('REJECTEDUSER: User login rejected from persona. Messages: {}'.format(response)) abort(403) next_url = request.args.get('next', None) email = response.get('email') user = User.query.filter(User.email == email).first() domain = email.split('@')[1] if len(email.split('@')) > 1 else None if user: login_user(user) flash('Logged in successfully!', 'alert-success') current_app.logger.debug('LOGIN: User {} logged in successfully'.format(user.email)) return next_url if next_url else '/' elif AcceptedEmailDomains.valid_domain(domain): user = User.create( email=email, role=Role.query.filter(Role.name == 'staff').first(), department=Department.query.filter(Department.name == 'New User').first() ) login_user(user) current_app.logger.debug('NEWUSER: New User {} successfully created'.format(user.email)) return '/users/profile' else: current_app.logger.debug('NOTINDB: User {} not in DB -- aborting!'.format(email)) abort(403)
def parse_contact(contact_email, department): '''Finds or creates a user as the contact Arguments: contact_email: The email address of the :py:class:`~purchasing.models.users.User`. If the user cannot be found in the database, the domain of their email must match the configured ``CITY_DOMAIN`` department: The :py:class:`~purchasing.models.users.Department` of the user Returns: The ID of the new/existing contact ''' # get our department contact, build it if we don't have it yet contact = User.query.filter(User.email == contact_email).first() if contact is None: contact = User.create( email=contact_email, role=Role.query.filter(Role.name == 'staff').first(), department=department) return contact.id
def seed_user(email, role, dept): ''' Creates a new user in the database. ''' from beacon.models.users import User, Department seed_email = email if email else app.config.get('SEED_EMAIL') user_exists = User.query.filter(User.email == seed_email).first() department = Department.query.filter( Department.name == db.func.lower(dept)).first() if user_exists: print 'User {email} already exists'.format(email=seed_email) else: try: new_user = User.create( email=seed_email, created_at=datetime.datetime.utcnow(), role_id=role, department=department if department else None) db.session.add(new_user) db.session.commit() print 'User {email} successfully created!'.format(email=seed_email) except Exception, e: print 'Something went wrong: {exception}'.format( exception=e.message)
def parse_contact(contact_email, department): '''Finds or creates a user as the contact Arguments: contact_email: The email address of the :py:class:`~purchasing.models.users.User`. If the user cannot be found in the database, the domain of their email must match the configured ``CITY_DOMAIN`` department: The :py:class:`~purchasing.models.users.Department` of the user Returns: The ID of the new/existing contact ''' # get our department contact, build it if we don't have it yet contact = User.query.filter(User.email == contact_email).first() if contact is None: contact = User.create( email=contact_email, roles=[Role.query.filter(Role.name == 'staff').first()], department=department ) return contact.id
def load_user(userid): return User.get_by_id(int(userid))