def user_import(_users, email_subject, email_template, creator, instance, reinvite=False): names = [] created = [] mailed = [] errors = False users = [] for user_info in _users: try: name = user_info['user_name'] email = user_info['email'] try: display_name = user_info['display_name'] names.append(name) if reinvite: user = model.User.find(name) else: user = model.User.create(name, email, display_name=display_name, autojoin=False) user.activation_code = user.IMPORT_MARKER + random_token() password = random_token() user_info['password'] = password user.password = password for badge in user_info['user_badges']: badge.assign(user, creator=creator) model.meta.Session.add(user) model.meta.Session.commit() users.append(user) created.append(user.user_name) url = base_url("/user/%s/activate?c=%s" % (user.user_name, user.activation_code), instance=instance, absolute=True) user_info['url'] = url body = email_template.format(*user_info.get('rest', []), **user_info) to_user(user, email_subject, body, decorate_body=False) mailed.append(user.user_name) except Exception, E: log.error('user import for user %s, email %s, exception %s' % (user_info['user_name'], user_info['email'], E)) errors = True continue except Exception, E: log.error('user import invalid user exception %s' % E) errors = True continue
def user_import(_users, email_subject, email_template, creator, instance, reinvite=False): names = [] created = [] mailed = [] errors = False users = [] for user_info in _users: try: name = user_info['user_name'] email = user_info['email'] try: display_name = user_info['display_name'] names.append(name) if reinvite: user = model.User.find(name) else: user = model.User.create(name, email, display_name=display_name, autojoin=False) user.activation_code = user.IMPORT_MARKER + random_token() password = random_token() user_info['password'] = password user.password = password for badge in user_info['user_badges']: badge.assign(user, creator=creator) model.meta.Session.add(user) model.meta.Session.commit() users.append(user) created.append(user.user_name) url = base_url( "/user/%s/activate?c=%s" % (user.user_name, user.activation_code), instance=instance, absolute=True) user_info['url'] = url body = email_template.format(*user_info.get('rest', []), **user_info) to_user(user, email_subject, body, decorate_body=False) mailed.append(user.user_name) except Exception, E: log.error('user import for user %s, email %s, exception %s' % (user_info['user_name'], user_info['email'], E)) errors = True continue except Exception, E: log.error('user import invalid user exception %s' % E) errors = True continue
def create(cls, user_name, email, password=None, locale=None, openid_identity=None, global_admin=False, display_name=None, autojoin=True, shibboleth_persistent_id=None): """ Create a user. If user_name is None, a random user name is generated. """ from group import Group from membership import Membership import adhocracy.lib.util as util if password is None: password = util.random_token() import adhocracy.i18n as i18n if locale is None: locale = i18n.get_default_locale() while user_name is None: # Note: This can theoretically lead to IntegrityErrors if the same # username is generated at the same time. This is very unlikely # though. from adhocracy.lib.util import random_username try_user_name = random_username() if cls.find(try_user_name) is None: user_name = try_user_name from adhocracy.lib import helpers as h h.flash(_('The random username %s has been assigned to you.') % user_name, 'success') user = User(user_name, email, password, locale, display_name=display_name) meta.Session.add(user) # Add the global default group default_group = Group.by_code(Group.CODE_DEFAULT) default_membership = Membership(user, None, default_group) meta.Session.add(default_membership) # Autojoin the user in instances config_autojoin = config.get('adhocracy.instances.autojoin') if autojoin and config_autojoin: user.fix_autojoin(commit=False) if global_admin: admin_group = Group.by_code(Group.CODE_ADMIN) admin_membership = Membership(user, None, admin_group) meta.Session.add(admin_membership) if openid_identity is not None: from adhocracy.model.openid import OpenID openid = OpenID(unicode(openid_identity), user) meta.Session.add(openid) if shibboleth_persistent_id is not None: from adhocracy.model.shibboleth import Shibboleth shib = Shibboleth(shibboleth_persistent_id, user) meta.Session.add(shib) meta.Session.flush() return user
def create(cls, user_name, email, password=None, locale=None, openid_identity=None, global_admin=False, display_name=None): from group import Group from membership import Membership from openid import OpenID import adhocracy.lib.util as util if password is None: password = util.random_token() import adhocracy.i18n as i18n if locale is None: locale = i18n.get_default_locale() user = User(user_name, email, password, locale, display_name=display_name) meta.Session.add(user) # Add the global default group default_group = Group.by_code(Group.CODE_DEFAULT) default_membership = Membership(user, None, default_group) meta.Session.add(default_membership) # Autojoin the user in instances config_autojoin = config.get('adhocracy.instances.autojoin') if config_autojoin: instances = Instance.all() if config_autojoin != 'ALL': instance_keys = [ key.strip() for key in config_autojoin.split(",") ] instances = [ instance for instance in instances if instance.key in instance_keys ] for instance in instances: autojoin_membership = Membership(user, instance, instance.default_group) meta.Session.add(autojoin_membership) if global_admin: admin_group = Group.by_code(Group.CODE_ADMIN) admin_membership = Membership(user, None, admin_group) meta.Session.add(admin_membership) if openid_identity is not None: openid = OpenID(unicode(openid_identity), user) meta.Session.add(openid) meta.Session.flush() return user
def create(cls, user_name, email, password=None, locale=None, openid_identity=None, global_admin=False): from group import Group from membership import Membership from openid import OpenID import adhocracy.lib.util as util if password is None: password = util.random_token() import adhocracy.i18n as i18n if locale is None: locale = i18n.get_default_locale() user = User(user_name, email, password, locale) meta.Session.add(user) default_group = Group.by_code(Group.CODE_DEFAULT) default_membership = Membership(user, None, default_group) meta.Session.add(default_membership) if global_admin: admin_group = Group.by_code(Group.CODE_ADMIN) admin_membership = Membership(user, None, admin_group) meta.Session.add(admin_membership) if openid_identity is not None: openid = OpenID(unicode(openid_identity), user) meta.Session.add(openid) meta.Session.flush() return user
def reset(self, id): c.page_user = get_entity_or_abort(model.User, id, instance_filter=False) try: if c.page_user.reset_code != self.form_result.get('c'): raise ValueError() new_password = random_token() c.page_user.password = new_password model.meta.Session.add(c.page_user) model.meta.Session.commit() body = ( _("your password has been reset. It is now:") + "\r\n\r\n " + new_password + "\r\n\r\n" + _("Please login and change the password in your user " "settings.") + "\n\n" + _("Your user name to login is: %s") % c.page_user.user_name) libmail.to_user(c.page_user, _("Your new password"), body) h.flash( _("Success. You have been sent an email with your new " "password."), 'success') except Exception: h.flash( _("The reset code is invalid. Please repeat the password" " recovery procedure."), 'error') redirect('/login')
def _create_users(self, form_result): names = [] created = [] mailed = [] errors = False users = [] for user_info in form_result['users_csv']: try: name = user_info['user_name'] email = user_info['email'] display_name = user_info['display_name'] names.append(name) user = model.User.create(name, email, display_name=display_name) user.activation_code = user.IMPORT_MARKER + random_token() password = random_token() user_info['password'] = password user.password = password model.meta.Session.add(user) model.meta.Session.commit() users.append(user) created.append(user.user_name) url = base_url(c.instance, path="/user/%s/activate?c=%s" % (user.user_name, user.activation_code)) user_info['url'] = url body = form_result['email_template'].format(**user_info) to_user(user, form_result['email_subject'], body, decorate_body=False) mailed.append(user.user_name) if c.instance: membership = model.Membership(user, c.instance, c.instance.default_group) model.meta.Session.expunge(membership) model.meta.Session.add(membership) model.meta.Session.commit() except Exception, E: log.error('user import for user %s, email %s, exception %s' % (name, email, E)) errors = True continue
def _create_users(self, form_result, format='html'): names = [] created = [] mailed = [] errors = False users = [] for user_info in form_result['users_csv']: try: name = user_info['user_name'] email = user_info['email'] display_name = user_info['display_name'] names.append(name) user = model.User.create(name, email, display_name=display_name, autojoin=False) user.activation_code = user.IMPORT_MARKER + random_token() password = random_token() user_info['password'] = password user.password = password for badge in user_info['user_badges']: badge.assign(user, creator=c.user) model.meta.Session.add(user) model.meta.Session.commit() users.append(user) created.append(user.user_name) url = base_url("/user/%s/activate?c=%s" % (user.user_name, user.activation_code), absolute=True) user_info['url'] = url body = form_result['email_template'].format( *user_info.get('rest', []), **user_info) to_user(user, form_result['email_subject'], body, decorate_body=False) mailed.append(user.user_name) except Exception, E: log.error('user import for user %s, email %s, exception %s' % (name, email, E)) errors = True continue
def _create_users(self, form_result): names = [] created = [] mailed = [] errors = False users = [] for user_info in form_result['users_csv']: try: name = user_info['user_name'] email = user_info['email'] display_name = user_info['display_name'] names.append(name) user = model.User.create(name, email, display_name=display_name) user.activation_code = user.IMPORT_MARKER + random_token() password = random_token() user_info['password'] = password user.password = password model.meta.Session.add(user) model.meta.Session.commit() users.append(user) created.append(user.user_name) url = base_url(c.instance, path="/user/%s/activate?c=%s" % ( user.user_name, user.activation_code)) user_info['url'] = url body = form_result['email_template'].format(**user_info) to_user(user, form_result['email_subject'], body, decorate_body=False) mailed.append(user.user_name) if c.instance: membership = model.Membership(user, c.instance, c.instance.default_group) model.meta.Session.expunge(membership) model.meta.Session.add(membership) model.meta.Session.commit() except Exception, E: log.error('user import for user %s, email %s, exception %s' % (name, email, E)) errors = True continue
def create(cls, user_name, email, password=None, locale=None, openid_identity=None, global_admin=False, display_name=None): from group import Group from membership import Membership from openid import OpenID import adhocracy.lib.util as util if password is None: password = util.random_token() import adhocracy.i18n as i18n if locale is None: locale = i18n.get_default_locale() user = User(user_name, email, password, locale, display_name=display_name) meta.Session.add(user) # Add the global default group default_group = Group.by_code(Group.CODE_DEFAULT) default_membership = Membership(user, None, default_group) meta.Session.add(default_membership) # Autojoin the user in instances config_autojoin = config.get('adhocracy.instances.autojoin') if config_autojoin: instances = Instance.all() if config_autojoin != 'ALL': instance_keys = [key.strip() for key in config_autojoin.split(",")] instances = [instance for instance in instances if instance.key in instance_keys] for instance in instances: autojoin_membership = Membership(user, instance, instance.default_group) meta.Session.add(autojoin_membership) if global_admin: admin_group = Group.by_code(Group.CODE_ADMIN) admin_membership = Membership(user, None, admin_group) meta.Session.add(admin_membership) if openid_identity is not None: openid = OpenID(unicode(openid_identity), user) meta.Session.add(openid) meta.Session.flush() return user
def reset_request(self): c.page_user = model.User.find_by_email(self.form_result.get('email')) if c.page_user is None: msg = _("There is no user registered with that email address.") return htmlfill.render(self.reset_form(), errors=dict(email=msg)) c.page_user.reset_code = random_token() model.meta.Session.add(c.page_user) model.meta.Session.commit() url = h.base_url(c.instance, path="/user/%s/reset?c=%s" % (c.page_user.user_name, c.page_user.reset_code)) body = _("you have requested that your password be reset. In order " "to confirm the validity of your claim, please open the " "link below in your browser:") + "\r\n\r\n " + url libmail.to_user(c.page_user, _("Reset your password"), body) return render("/user/reset_pending.html")
def reset(self, id): c.page_user = get_entity_or_abort(model.User, id, instance_filter=False) try: if c.page_user.reset_code != self.form_result.get('c'): raise ValueError() new_password = random_token() c.page_user.password = new_password model.meta.Session.add(c.page_user) model.meta.Session.commit() body = (_("your password has been reset. It is now:") + "\r\n\r\n " + new_password + "\r\n\r\n" + _("Please login and change the password in your user " "settings.")) libmail.to_user(c.page_user, _("Your new password"), body) h.flash(_("Success. You have been sent an email with your new " "password."), 'success') except Exception: h.flash(_("The reset code is invalid. Please repeat the password" " recovery procedure."), 'error') redirect('/login')
def _set_email(self, email): import adhocracy.lib.util as util if not self._email == email: self.activation_code = util.random_token() self._email = email
def create(cls, user_name, email, password=None, locale=None, openid_identity=None, global_admin=False, display_name=None, autojoin=True, shibboleth_persistent_id=None): """ Create a user. If user_name is None, a random user name is generated. """ from group import Group from membership import Membership import adhocracy.lib.util as util if password is None: password = util.random_token() import adhocracy.i18n as i18n if locale is None: locale = i18n.get_default_locale() while user_name is None: # Note: This can theoretically lead to IntegrityErrors if the same # username is generated at the same time. This is very unlikely # though. from adhocracy.lib.util import random_username try_user_name = random_username() if cls.find(try_user_name) is None: user_name = try_user_name from adhocracy.lib import helpers as h h.flash( _('The random username %s has been assigned to you.') % user_name, 'success') user = User(user_name, email, password, locale, display_name=display_name) meta.Session.add(user) # Add the global default group default_group = Group.by_code(Group.CODE_DEFAULT) default_membership = Membership(user, None, default_group) meta.Session.add(default_membership) # Autojoin the user in instances config_autojoin = config.get('adhocracy.instances.autojoin') if autojoin and config_autojoin: user.fix_autojoin(commit=False) if global_admin: admin_group = Group.by_code(Group.CODE_ADMIN) admin_membership = Membership(user, None, admin_group) meta.Session.add(admin_membership) if openid_identity is not None: from adhocracy.model.openid import OpenID openid = OpenID(unicode(openid_identity), user) meta.Session.add(openid) if shibboleth_persistent_id is not None: from adhocracy.model.shibboleth import Shibboleth shib = Shibboleth(shibboleth_persistent_id, user) meta.Session.add(shib) meta.Session.flush() return user
def initialize_welcome(self): """ Sign up the user for the welcome feature (on user import or so) """ import adhocracy.lib.util as util self.welcome_code = util.random_token() self._password = None