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 user_create(grant_admin): """Creates new user""" user = User() user_type = 'user' if not grant_admin else 'admin' print() name = prompt("First name") surname = prompt("Last name") affiliation = prompt("Affiliation") print() username = prompt("Enter username") email = prompt_email() if email is None: return password = prompt_pass() if password is None: return user.first_name = to_unicode(name) user.last_name = to_unicode(surname) user.affiliation = to_unicode(affiliation) user.email = email user.is_admin = grant_admin print_user_info(user) if prompt_bool(cformat("%{yellow}Create the new {}?").format(user_type), default=True): # TODO: adapt to new authentication system, create local identity with username/password # user.identities.append(UserIdentity(..., identifier=username, password=password)) db.session.add(user) transaction.commit() success("New {} created successfully with ID: {}".format( user_type, user.id))
def user_create(grant_admin): """Creates new user""" user = User() user_type = 'user' if not grant_admin else 'admin' print() name = prompt("First name") surname = prompt("Last name") affiliation = prompt("Affiliation") print() username = prompt("Enter username") email = prompt_email() if email is None: return password = prompt_pass() if password is None: return user.first_name = to_unicode(name) user.last_name = to_unicode(surname) user.affiliation = to_unicode(affiliation) user.email = email user.is_admin = grant_admin print_user_info(user) if prompt_bool(cformat("%{yellow}Create the new {}?").format(user_type), default=True): # TODO: adapt to new authentication system, create local identity with username/password # user.identities.append(UserIdentity(..., identifier=username, password=password)) db.session.add(user) transaction.commit() success("New {} created successfully with ID: {}".format(user_type, user.id))
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(email, data, identity=None, settings=None, other_emails=None, from_moderation=True): """Create a new user. This may also convert a pending user to a proper user in case the email address matches such a user. :param email: The primary email address of the user. :param data: The data used to populate the user. :param identity: An `Identity` to associate with the user. :param settings: A dict containing user settings. :param other_emails: A set of email addresses that are also used to check for a pending user. They will also be added as secondary emails to the user. :param from_moderation: Whether the user was created through the moderation process or manually by an admin. """ if other_emails is None: other_emails = set() if settings is None: settings = {} settings.setdefault('timezone', config.DEFAULT_TIMEZONE) settings.setdefault('lang', config.DEFAULT_LOCALE) settings.setdefault('suggest_categories', False) # Get a pending user if there is one user = User.query.filter(~User.is_deleted, User.is_pending, User.all_emails.in_({email} | set(other_emails))).first() if not user: user = User() if email 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(email) else: user.email = email user.populate_from_dict(data, skip={'synced_fields'}) user.is_pending = False user.secondary_emails |= other_emails user.favorite_users.add(user) if identity is not None: user.identities.add(identity) db.session.add(user) db.session.flush() user.populate_from_dict(data, keys={ 'synced_fields' }) # this is a setting, so the user must have an ID user.settings.set_multi(settings) db.session.flush() signals.users.registered.send(user, from_moderation=from_moderation, identity=identity) db.session.flush() return user
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 test_emails(db): user = User(first_name='Guinea', last_name='Pig') db.session.add(user) db.session.flush() assert user.email is None assert not user.secondary_emails user.email = '*****@*****.**' db.session.flush() assert user.all_emails == {'*****@*****.**'} user.secondary_emails.add('*****@*****.**') db.session.flush() assert user.all_emails == {'*****@*****.**', '*****@*****.**'}
def create_user(email, data, identity=None, settings=None, other_emails=None, from_moderation=True): """Create a new user. This may also convert a pending user to a proper user in case the email address matches such a user. :param email: The primary email address of the user. :param data: The data used to populate the user. :param identity: An `Identity` to associate with the user. :param settings: A dict containing user settings. :param other_emails: A set of email addresses that are also used to check for a pending user. They will also be added as secondary emails to the user. :param from_moderation: Whether the user was created through the moderation process or manually by an admin. """ if other_emails is None: other_emails = set() # Get a pending user if there is one user = User.find_first( ~User.is_deleted, User.is_pending, User.all_emails.contains(db.func.any(list({email} | set(other_emails))))) if not user: user = User() if email 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(email) else: user.email = email user.populate_from_dict(data) user.is_pending = False user.secondary_emails |= other_emails user.favorite_users.add(user) if identity is not None: user.identities.add(identity) db.session.add(user) if settings: user.settings.set_multi(settings) db.session.flush() signals.users.registered.send(user, from_moderation=from_moderation, identity=identity) db.session.flush() return user
def test_emails(db): user = User(first_name='Guinea', last_name='Pig') db.session.add(user) db.session.flush() assert user.email is None assert not user.secondary_emails user.email = '*****@*****.**' db.session.flush() assert user.all_emails == {'*****@*****.**'} user.secondary_emails.add('*****@*****.**') db.session.flush() db.session.expire(user) # all_emails is only updated after expire (or commit) assert user.all_emails == {'*****@*****.**', '*****@*****.**'}
def create_user(email, data, identity=None, settings=None, other_emails=None, from_moderation=True): """Create a new user. This may also convert a pending user to a proper user in case the email address matches such a user. :param email: The primary email address of the user. :param data: The data used to populate the user. :param identity: An `Identity` to associate with the user. :param settings: A dict containing user settings. :param other_emails: A set of email addresses that are also used to check for a pending user. They will also be added as secondary emails to the user. :param from_moderation: Whether the user was created through the moderation process or manually by an admin. """ if other_emails is None: other_emails = set() if settings is None: settings = {} settings.setdefault('timezone', config.DEFAULT_TIMEZONE) settings.setdefault('lang', config.DEFAULT_LOCALE) settings.setdefault('suggest_categories', False) # Get a pending user if there is one user = User.query.filter(~User.is_deleted, User.is_pending, User.all_emails.in_({email} | set(other_emails))).first() if not user: user = User() if email 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(email) else: user.email = email user.populate_from_dict(data, skip={'synced_fields'}) user.is_pending = False user.secondary_emails |= other_emails user.favorite_users.add(user) if identity is not None: user.identities.add(identity) db.session.add(user) db.session.flush() user.populate_from_dict(data, keys={'synced_fields'}) # this is a setting, so the user must have an ID user.settings.set_multi(settings) db.session.flush() signals.users.registered.send(user, from_moderation=from_moderation, identity=identity) db.session.flush() return user
def _create_user(id_, first_name=u'Guinea', last_name=u'Pig', rb_admin=False, admin=False, email=None, groups=None, legacy=False): user = User.get(id_) if user: return user.as_avatar if legacy else user user = User() user.id = id_ user.first_name = first_name user.last_name = last_name user.email = email or u'{}@example.com'.format(id_) user.is_admin = admin user.local_groups = {g.group for g in (groups or ())} db.session.add(user) db.session.flush() if rb_admin: rb_settings.acls.add_principal('admin_principals', user) db.session.flush() return user.as_avatar if legacy else user
def _create_user(id_, name=u'Pig', surname=u'Guinea', rb_admin=False, email=None, groups=None): user = User.get(id_) if user: return user.as_avatar user = User() user.id = id_ user.first_name = name user.last_name = surname user.email = email or u'{}@example.com'.format(id_) user.local_groups = {g.group for g in (groups or ())} db.session.add(user) db.session.flush() if rb_admin: rb_settings.set('admin_principals', rb_settings.get('admin_principals') + [user.as_principal]) db.session.flush() _users.add(user) avatar = user.as_avatar avatar.email = user.email return avatar
def create_user(email, data, identity=None, settings=None, other_emails=None, from_moderation=True): """Create a new user. This may also convert a pending user to a proper user in case the email address matches such a user. :param email: The primary email address of the user. :param data: The data used to populate the user. :param identity: An `Identity` to associate with the user. :param settings: A dict containing user settings. :param other_emails: A set of email addresses that are also used to check for a pending user. They will also be added as secondary emails to the user. :param from_moderation: Whether the user was created through the moderation process or manually by an admin. """ if other_emails is None: other_emails = set() # Get a pending user if there is one user = User.find_first(~User.is_deleted, User.is_pending, User.all_emails.contains(db.func.any(list({email} | set(other_emails))))) if not user: user = User() if email 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(email) else: user.email = email user.populate_from_dict(data) user.is_pending = False user.secondary_emails |= other_emails user.favorite_users.add(user) if identity is not None: user.identities.add(identity) db.session.add(user) if settings: user.settings.set_multi(settings) db.session.flush() signals.users.registered.send(user, from_moderation=from_moderation, identity=identity) db.session.flush() return 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()