def delete(user_id): """Delete the user with id ``user_id`` If the ``revoke`` property is set to true, then the user will be removed from the whitelist, and not be allowed to make an account again. **Route:** ``/admin/users/delete/<user_id>`` **Methods:** ``POST`` """ object_id = ObjectId(user_id) if User.objects(id=object_id).count() != 1: abort(401) user = User.objects().get(id=object_id) # Update whitelist try: wl = Whitelist.objects().get(email=user.email) wl.redeemed = False wl.save() except DoesNotExist: pass user.delete() # Log out if a user is attempting to delete themselves if 'gplus_id' in session and user.gplus_id == session['gplus_id']: flash('You deleted yourself successfully. Logging out.', MESSAGE_FLASH) return redirect(url_for('.logout'), 303) flash('User deleted successfully.', MESSAGE_FLASH) return redirect(url_for('.index'), code=303)
def __call__(self, form, field): """Called internally by :mod:`wtforms` on validation of the field. :param form: The parent form :type form: :class:`Form` :param field: The field to validate :type field: :class:`Field` :raises: :class:`wtforms.validators.ValidationError` """ if form.user_type.data != 'fake_user' and \ Whitelist.objects(email=field.data).count(): raise ValidationError(self.message)
def create_profile(): """Create a profile (filling in the form with openid data), and register it in the database. **Route:** ``/admin/create-profile`` **Methods:** ``GET, POST`` """ if g.user is not None and 'gplus_id' in session: # use code=303 to avoid POSTing to the next page. return redirect(url_for('admin.index'), code=303) form = CreateProfileForm(request.form, name=request.args['name'], email=request.args['email'], next=request.args['next']) if form.validate_on_submit(): if User.objects(email=form.email.data).count() != 0: # A user with this email already exists. Override it. user = User.objects.get(email=form.email.data) user.openid = session['openid'] user.name = form.name.data flash('Account with this email already exists. Overridden.', MESSAGE_FLASH) user.register_login() user.save() else: # Retreive their user type from the whitelist then remove them. wl = Whitelist.objects().get(email=form.email.data) user_type = wl.user_type wl.redeemed = True wl.save() # Create a brand new user user = User(email=form.email.data, name=form.name.data, gplus_id=session['gplus_id'], user_type=user_type, image_url=request.args.get('image_url')) flash('Account created successfully.', MESSAGE_FLASH) user.register_login() user.save() # redirect to the next url or the root of the application ('/') if form.next.data: # use code=303 to avoid POSTing to the next page. return redirect(form.next.data, code=303) # use code=303 to avoid POSTing to the next page. return redirect('/', code=303) return render_template('eventum_auth/create_profile.html', image_url=request.args.get('image_url'), form=form)
def add(): """Add and email to the whitelist. **Route:** ``/admin/whitelist/add`` **Methods:** ``POST`` """ form = AddToWhitelistForm(request.form) if form.user_type.data == 'fake_user': if form.validate_on_submit(): fake_id = str(uuid.uuid4()) fake_email = fake_id[:10] + "@fake-users.com" filename = form.fake_user_image.data try: fake_image = Image.objects().get(filename=filename) fake_user = User(email=fake_email, gplus_id=fake_id, name=form.name.data, user_type=form.user_type.data, image=fake_image) except Image.DoesNotExist: fake_user = User(email=fake_email, gplus_id=fake_id, name=form.name.data, user_type=form.user_type.data) fake_user.save() else: current_app.logger.warning(form.errors) else: user_exists = User.objects(email=form.email.data).count() != 0 if form.validate_on_submit() and not user_exists: wl = Whitelist(email=form.email.data, user_type=form.user_type.data) wl.save() else: current_app.logger.warning(form.errors) return redirect(url_for('users.index'))
def delete(email): """Delete ``email`` from the whitelist. **Route:** ``/admin/whitelist/delete/<email>`` **Methods:** ``POST`` :param str email: The email address to delete from the whitelist. """ if Whitelist.objects(email=email).count() > 0: Whitelist.objects.get(email=email).delete() flash("Whitelist entry revoked successfully.", MESSAGE_FLASH) return redirect(url_for('users.index')) flash('No such user in the database.', ERROR_FLASH) return redirect(url_for('users.index'))
def index(): """View and manage users. Whitelisted users are the only ones allowed to make user accounts. **Route:** ``/admin/users`` **Methods:** ``GET`` """ upload_form = UploadImageForm() whitelist_form = AddToWhitelistForm() return render_template('eventum_users/users.html', whitelist_form=whitelist_form, upload_form=upload_form, whitelist=Whitelist.objects(redeemed=False), users=User.objects(), images=Image.objects(), current_user=g.user)
def store_token(): """Do the oauth flow for Google plus sign in, storing the access token in the session, and redircting to create an account if appropriate. Because this method will be called from a ``$.ajax()`` request in JavaScript, we can't return ``redirect()``, so instead this method returns the URL that the user should be redirected to, and the redirect happens in html: .. code:: javascript success: function(response) { window.location.href = response.data.redirect_url; } **Route:** ``/admin/store-token`` **Methods:** ``POST`` """ if request.args.get('state', '') != session.get('state'): return json_error_message('Invalid state parameter.', 401) del session['state'] code = request.data try: # Upgrade the authorization code into a credentials object oauth_flow = flow_from_clientsecrets( current_app.config['EVENTUM_CLIENT_SECRETS_PATH'], scope='') oauth_flow.redirect_uri = 'postmessage' credentials = oauth_flow.step2_exchange(code) except FlowExchangeError: return json_error_message('Failed to upgrade the authorization code.', 401) gplus_id = credentials.id_token['sub'] # Store the access token in the session for later use. session['credentials'] = credentials.access_token session['gplus_id'] = gplus_id if User.objects(gplus_id=gplus_id).count() == 0: # A new user model must be made # Get the user's name and email to populate the form http = httplib2.Http() http = credentials.authorize(http) people_document = gplus_service.people().get(userId='me').execute( http=http) # The user must be whitelisted in order to create an account. email = people_document['emails'][0]['value'] if Whitelist.objects(email=email).count() != 1: return json_error_message('User has not been whitelisted.', 401, { 'whitelisted': False, 'email': email }) return json_success({ 'redirect_url': url_for('.create_profile', next=request.args.get('next'), name=people_document['displayName'], email=email, image_url=people_document['image']['url']) }) user = User.objects().get(gplus_id=gplus_id) user.register_login() user.save() # The user already exists. Redirect to the next url or # the root of the application ('/') if request.args.get('next'): return json_success({'redirect_url': request.args.get('next')}) return json_success({'redirect_url': request.url_root})
def store_token(): """Do the oauth flow for Google plus sign in, storing the access token in the session, and redircting to create an account if appropriate. Because this method will be called from a ``$.ajax()`` request in JavaScript, we can't return ``redirect()``, so instead this method returns the URL that the user should be redirected to, and the redirect happens in html: .. code:: javascript success: function(response) { window.location.href = response.data.redirect_url; } **Route:** ``/admin/store-token`` **Methods:** ``POST`` """ if request.args.get('state', '') != session.get('state'): return json_error_message('Invalid state parameter.', 401) del session['state'] code = request.data try: # Upgrade the authorization code into a credentials object oauth_flow = flow_from_clientsecrets( current_app.config['EVENTUM_CLIENT_SECRETS_PATH'], scope='') oauth_flow.redirect_uri = 'postmessage' credentials = oauth_flow.step2_exchange(code) except FlowExchangeError: return json_error_message('Failed to upgrade the authorization code.', 401) gplus_id = credentials.id_token['sub'] # Store the access token in the session for later use. session['credentials'] = credentials.access_token session['gplus_id'] = gplus_id if User.objects(gplus_id=gplus_id).count() == 0: # A new user model must be made # Get the user's name and email to populate the form http = httplib2.Http() http = credentials.authorize(http) people_document = gplus_service.people().get( userId='me').execute(http=http) # The user must be whitelisted in order to create an account. email = people_document['emails'][0]['value'] if Whitelist.objects(email=email).count() != 1: return json_error_message('User has not been whitelisted.', 401, {'whitelisted': False, 'email': email}) return json_success({ 'redirect_url': url_for('.create_profile', next=request.args.get('next'), name=people_document['displayName'], email=email, image_url=people_document['image']['url']) }) user = User.objects().get(gplus_id=gplus_id) user.register_login() user.save() # The user already exists. Redirect to the next url or # the root of the application ('/') if request.args.get('next'): return json_success({'redirect_url': request.args.get('next')}) return json_success({'redirect_url': request.url_root})