summaryrefslogtreecommitdiffstats
path: root/contrib/create_account.py
blob: cb4392a3b207d6e2b3a7f0f32a2f5ec37b25b7c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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]