def _process_POST(self): if User.has_rows(): return redirect(url_for('misc.index')) setup_form = BootstrapForm(request.form) if not setup_form.validate(): flash(_("Some fields are invalid. Please, correct them and submit the form again."), 'error') return redirect(url_for('bootstrap.index')) # Creating new user user = User() user.first_name = to_unicode(setup_form.first_name.data) user.last_name = to_unicode(setup_form.last_name.data) user.affiliation = to_unicode(setup_form.affiliation.data) user.email = to_unicode(setup_form.email.data) user.is_admin = True identity = Identity(provider='indico', identifier=setup_form.username.data, password=setup_form.password.data) user.identities.add(identity) db.session.add(user) db.session.flush() user.settings.set('timezone', Config.getInstance().getDefaultTimezone()) user.settings.set('lang', to_unicode(setup_form.language.data)) login_user(user, identity) full_name = user.full_name # needed after the session closes transaction.commit() # Configuring server's settings minfo = HelperMaKaCInfo.getMaKaCInfoInstance() minfo.setOrganisation(setup_form.affiliation.data) minfo.setLang(setup_form.language.data) message = get_template_module('bootstrap/flash_messages.html').bootstrap_success(name=full_name) flash(Markup(message), 'success') # Activate instance tracking if setup_form.enable_tracking.data: contact_name = setup_form.contact_name.data contact_email = setup_form.contact_email.data try: register_instance(contact_name, contact_email) except (HTTPError, ValueError) as err: message = get_template_module('bootstrap/flash_messages.html').community_error(err=err) category = 'error' except Timeout: message = get_template_module('bootstrap/flash_messages.html').community_timeout() category = 'error' except RequestException as exc: message = get_template_module('bootstrap/flash_messages.html').community_exception(exc=exc) category = 'error' else: message = get_template_module('bootstrap/flash_messages.html').community_success() category = 'success' flash(Markup(message), category) return redirect(url_for('misc.index'))
def _create_user(self, form, handler): data = form.data existing_user_id = session.get('register_pending_user') if existing_user_id: # Get pending user and set it as non-pending user = User.get(existing_user_id) user.is_pending = False else: user = User(first_name=data['first_name'], last_name=data['last_name'], email=data['email'], address=data.get('address', ''), phone=data.get('phone', ''), affiliation=data['affiliation']) identity = handler.create_identity(data) user.identities.add(identity) user.secondary_emails = handler.extra_emails - {user.email} user.favorite_users.add(user) db.session.add(user) minfo = HelperMaKaCInfo.getMaKaCInfoInstance() timezone = session.timezone if timezone == 'LOCAL': timezone = minfo.getTimezone() user.settings.set('timezone', timezone) user.settings.set('lang', session.lang or minfo.getLang()) handler.update_user(user, form) db.session.flush() login_user(user, identity) msg = _('You have sucessfully registered your Indico profile. ' 'Check <a href="{url}">your profile</a> for further details and settings.') flash(Markup(msg).format(url=url_for('users.user_profile')), 'success') return handler.redirect_success()
def _process_POST(self): if User.query.has_rows(): return redirect(url_for_index()) setup_form = BootstrapForm(request.form) if not setup_form.validate(): flash(_("Some fields are invalid. Please, correct them and submit the form again."), 'error') return redirect(url_for('bootstrap.index')) # Creating new user user = User() user.first_name = to_unicode(setup_form.first_name.data) user.last_name = to_unicode(setup_form.last_name.data) user.affiliation = to_unicode(setup_form.affiliation.data) user.email = to_unicode(setup_form.email.data) user.is_admin = True identity = Identity(provider='indico', identifier=setup_form.username.data, password=setup_form.password.data) user.identities.add(identity) db.session.add(user) db.session.flush() user.settings.set('timezone', Config.getInstance().getDefaultTimezone()) user.settings.set('lang', session.lang or Config.getInstance().getDefaultLocale()) login_user(user, identity) full_name = user.full_name # needed after the session closes transaction.commit() # Configuring server's settings minfo = HelperMaKaCInfo.getMaKaCInfoInstance() minfo.setOrganisation(setup_form.affiliation.data) message = get_template_module('bootstrap/flash_messages.html').bootstrap_success(name=full_name) flash(Markup(message), 'success') # Activate instance tracking if setup_form.enable_tracking.data: contact_name = setup_form.contact_name.data contact_email = setup_form.contact_email.data try: register_instance(contact_name, contact_email) except (HTTPError, ValueError) as err: message = get_template_module('bootstrap/flash_messages.html').community_error(err=err) category = 'error' except Timeout: message = get_template_module('bootstrap/flash_messages.html').community_timeout() category = 'error' except RequestException as exc: message = get_template_module('bootstrap/flash_messages.html').community_exception(exc=exc) category = 'error' else: message = get_template_module('bootstrap/flash_messages.html').community_success() category = 'success' flash(Markup(message), category) return redirect(url_for_index())
def _handle_submit(self, setup_form): # Creating new user user = User() user.first_name = setup_form.first_name.data user.last_name = setup_form.last_name.data user.affiliation = setup_form.affiliation.data user.email = setup_form.email.data user.is_admin = True identity = Identity(provider='indico', identifier=setup_form.username.data, password=setup_form.password.data) user.identities.add(identity) db.session.add(user) db.session.flush() user.settings.set('timezone', config.DEFAULT_TIMEZONE) user.settings.set('lang', session.lang or config.DEFAULT_LOCALE) login_user(user, identity) full_name = user.full_name # needed after the session closes db.session.commit() # Configuring server's settings core_settings.set('site_organization', setup_form.affiliation.data) message = get_template_module( 'bootstrap/flash_messages.html').bootstrap_success(name=full_name) flash(Markup(message), 'success') # Activate instance tracking if setup_form.enable_tracking.data: contact_name = setup_form.contact_name.data contact_email = setup_form.contact_email.data try: register_instance(contact_name, contact_email) except (HTTPError, ValueError) as err: message = get_template_module('bootstrap/flash_messages.html' ).community_error(err=str(err)) category = 'error' except Timeout: message = get_template_module( 'bootstrap/flash_messages.html').community_timeout() category = 'error' except RequestException as exc: message = get_template_module( 'bootstrap/flash_messages.html').community_exception( err=str(exc)) category = 'error' else: message = get_template_module( 'bootstrap/flash_messages.html').community_success() category = 'success' flash(Markup(message), category) return redirect(url_for_index())
def _create_user(self, form, handler): user, identity = register_user(**self._prepare_registration_data(form, handler)) login_user(user, identity) msg = _('You have sucessfully registered your Indico profile. ' 'Check <a href="{url}">your profile</a> for further details and settings.') flash(Markup(msg).format(url=url_for('users.user_profile')), 'success') db.session.flush() return handler.redirect_success()
def _create_identity(self): identity = Identity(user=self.user, provider=self.identity_info['provider'], identifier=self.identity_info['identifier'], data=self.identity_info['data'], multipass_data=self.identity_info['multipass_data']) logger.info('Created new identity for %s: %s', self.user, identity) del session['login_identity_info'] db.session.flush() login_user(self.user, identity) return multipass.redirect_success()
def _reset_password(self, identity): form = ResetPasswordForm() if form.validate_on_submit(): identity.password = form.password.data flash(_("Your password has been changed successfully."), 'success') login_user(identity.user, identity) # We usually come here from a multipass login page so we should have a target url return multipass.redirect_success() form.username.data = identity.identifier return WPAuth.render_template('reset_password.html', form=form, identity=identity, widget_attrs={'username': {'disabled': True}})
def impersonate_user(user): """Impersonate another user as an admin""" from indico.modules.auth import login_user, logger current_user = session.user # We don't overwrite a previous entry - the original (admin) user should be kept there # XXX: Don't change this to setdefault - building `session_data` pops stuff from the session if 'login_as_orig_user' not in session: session['login_as_orig_user'] = { 'session_data': {k: session.pop(k) for k in session.keys() if k[0] != '_' or k in ('_timezone', '_lang')}, 'user_id': session.user.id, 'user_name': session.user.get_full_name(last_name_first=False, last_name_upper=False) } login_user(user, admin_impersonation=True) logger.info('Admin %r is impersonating user %r', current_user, user)
def _create_user(self, form, handler, pending_user): data = form.data if pending_user: user = pending_user user.is_pending = False else: user = User() form.populate_obj(user, skip={'email'}) if form.email.data in user.secondary_emails: # This can happen if there's a pending user who has a secondary email # for some weird reason which should now become the primary email... user.make_email_primary(form.email.data) else: user.email = form.email.data identity = handler.create_identity(data) user.identities.add(identity) user.secondary_emails |= handler.get_all_emails(form) - {user.email} user.favorite_users.add(user) db.session.add(user) minfo = HelperMaKaCInfo.getMaKaCInfoInstance() timezone = session.timezone if timezone == 'LOCAL': timezone = Config.getInstance().getDefaultTimezone() user.settings.set('timezone', timezone) user.settings.set('lang', session.lang or minfo.getLang()) handler.update_user(user, form) db.session.flush() # notify everyone of user creation signals.users.registered.send(user) login_user(user, identity) msg = _('You have sucessfully registered your Indico profile. ' 'Check <a href="{url}">your profile</a> for further details and settings.') flash(Markup(msg).format(url=url_for('users.user_profile')), 'success') db.session.flush() return handler.redirect_success()