Create a new user account.
(data,
db,
users,
emails,
config,
messages)
| 84 | config = 'ddserver.config:Config', |
| 85 | messages = 'ddserver.interface.message:MessageManager') |
| 86 | def post_signup(data, |
| 87 | db, |
| 88 | users, |
| 89 | emails, |
| 90 | config, |
| 91 | messages): |
| 92 | ''' Create a new user account. ''' |
| 93 | |
| 94 | if not config.signup.enabled: |
| 95 | messages.error('User registration is currently disabled. Sorry.') |
| 96 | bottle.redirect('/') |
| 97 | |
| 98 | # Get domain part of email |
| 99 | email_domain = data.email.split('@', 1)[1] |
| 100 | |
| 101 | with db.cursor() as cur: |
| 102 | cur.execute(''' |
| 103 | INSERT |
| 104 | INTO users |
| 105 | SET `username` = %(username)s, |
| 106 | `email` = %(email)s, |
| 107 | `admin` = 0, |
| 108 | `active` = 0, |
| 109 | `created` = CURRENT_TIMESTAMP |
| 110 | ''', {'username': data.username, |
| 111 | 'email': data.email}) |
| 112 | |
| 113 | # Check if the user can be activated directly |
| 114 | if (config.signup.allowed_maildomains == 'any' or |
| 115 | email_domain in config.signup.allowed_maildomains): |
| 116 | # Generate auth code |
| 117 | users.generate_authcode(data.username) |
| 118 | |
| 119 | # Get user record |
| 120 | user = users[data.username] |
| 121 | |
| 122 | # Send out activation mail |
| 123 | try: |
| 124 | emails.to_user('signup_activate.mail', |
| 125 | user = user) |
| 126 | |
| 127 | except: |
| 128 | # Failed to send activation email. |
| 129 | # We reset the authcode in this case, so an admin can send |
| 130 | # a new one after fixing email issues |
| 131 | with db.cursor() as cur: |
| 132 | cur.execute(''' |
| 133 | UPDATE users |
| 134 | SET `authcode` = %(authcode)s |
| 135 | WHERE `username` = %(username)s |
| 136 | ''', {'authcode': None, |
| 137 | 'username': data.username}) |
| 138 | |
| 139 | messages.error('Failed to send activation email. Please contact an administrator at %s' % |
| 140 | config.contact.email) |
| 141 | |
| 142 | else: |
| 143 | messages.success('Your account has been created. You should receive an activation email in some minutes.') |