diff options
Diffstat (limited to 'contrib')
-rwxr-xr-x | contrib/create_account.py | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/contrib/create_account.py b/contrib/create_account.py new file mode 100755 index 0000000..cb4392a --- /dev/null +++ b/contrib/create_account.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +import sys +from os.path import dirname, abspath +sys.path.append(dirname(dirname(abspath(__file__)))) + +from account import AccountService, NoSuchUserError +from app import app +from flask import g, url_for +from utils import make_confirmation + +""" +Create an account. + +The default operation is to send an activation mail to the given address. So +the only difference to the register form on the website is that the username +blacklist is not checked. +The user can click the link and enter a password to finish account creation. + +Usage: + $0 username email +""" + +def main(username, mail): + service = AccountService(app.config['LDAP_HOST'], app.config['LDAP_BASE_DN'], + app.config['LDAP_ADMIN_USER'], app.config['LDAP_ADMIN_PASS'], + app.all_services) + + try: + service.get_by_uid(username) + except NoSuchUserError: + pass + else: + raise CreationError(u'There is already a user named %s' % username) + + try: + u = service.get_by_mail(mail) + except NoSuchUserError: + pass + else: + raise CreationError(u'There is already a user with email %s (uid: %s)' % (mail, u.uid)) + + confirm_token = make_confirmation('register', (username, mail)) + confirm_link = url_for('register_complete', token=confirm_token, _external=True) + + print confirm_link + + +class CreationError(ValueError): + pass + + +if __name__ == '__main__': + if len(sys.argv) == 3: + #XXX: I have the strong feeling that could be done better + try: + with app.test_request_context(base_url='http://%s/' % (app.config['SERVER_NAME'] or 'localhost')): + main(*sys.argv[1:]) + except CreationError, e: + print 'Error:', e + else: + print "Usage: %s username email" % sys.argv[0] |