Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / management / commands / user-add.py @ 398a9604

History | View | Annotate | Download (4.3 kB)

1 d8934b9a Sofia Papagiannaki
# Copyright 2012 GRNET S.A. All rights reserved.
2 d8934b9a Sofia Papagiannaki
#
3 d8934b9a Sofia Papagiannaki
# Redistribution and use in source and binary forms, with or
4 d8934b9a Sofia Papagiannaki
# without modification, are permitted provided that the following
5 d8934b9a Sofia Papagiannaki
# conditions are met:
6 d8934b9a Sofia Papagiannaki
#
7 d8934b9a Sofia Papagiannaki
#   1. Redistributions of source code must retain the above
8 d8934b9a Sofia Papagiannaki
#      copyright notice, this list of conditions and the following
9 d8934b9a Sofia Papagiannaki
#      disclaimer.
10 d8934b9a Sofia Papagiannaki
#
11 d8934b9a Sofia Papagiannaki
#   2. Redistributions in binary form must reproduce the above
12 d8934b9a Sofia Papagiannaki
#      copyright notice, this list of conditions and the following
13 d8934b9a Sofia Papagiannaki
#      disclaimer in the documentation and/or other materials
14 d8934b9a Sofia Papagiannaki
#      provided with the distribution.
15 d8934b9a Sofia Papagiannaki
#
16 d8934b9a Sofia Papagiannaki
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 d8934b9a Sofia Papagiannaki
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 d8934b9a Sofia Papagiannaki
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 d8934b9a Sofia Papagiannaki
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 d8934b9a Sofia Papagiannaki
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 d8934b9a Sofia Papagiannaki
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 d8934b9a Sofia Papagiannaki
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 d8934b9a Sofia Papagiannaki
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 d8934b9a Sofia Papagiannaki
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 d8934b9a Sofia Papagiannaki
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 d8934b9a Sofia Papagiannaki
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 d8934b9a Sofia Papagiannaki
# POSSIBILITY OF SUCH DAMAGE.
28 d8934b9a Sofia Papagiannaki
#
29 d8934b9a Sofia Papagiannaki
# The views and conclusions contained in the software and
30 d8934b9a Sofia Papagiannaki
# documentation are those of the authors and should not be
31 d8934b9a Sofia Papagiannaki
# interpreted as representing official policies, either expressed
32 d8934b9a Sofia Papagiannaki
# or implied, of GRNET S.A.
33 d8934b9a Sofia Papagiannaki
34 d8934b9a Sofia Papagiannaki
import socket
35 d8934b9a Sofia Papagiannaki
36 d8934b9a Sofia Papagiannaki
from optparse import make_option
37 d8934b9a Sofia Papagiannaki
38 d8934b9a Sofia Papagiannaki
from django.core.management.base import BaseCommand, CommandError
39 d8934b9a Sofia Papagiannaki
from django.core.validators import validate_email
40 d8934b9a Sofia Papagiannaki
from django.core.exceptions import ValidationError
41 d8934b9a Sofia Papagiannaki
42 9a06d96f Olga Brani
from astakos.im.models import AstakosUser
43 04e05445 Sofia Papagiannaki
from astakos.im.functions import activate
44 d8934b9a Sofia Papagiannaki
45 9a06d96f Olga Brani
46 d8934b9a Sofia Papagiannaki
class Command(BaseCommand):
47 07a1f977 Sofia Papagiannaki
    args = "<email> <first name> <last name>"
48 d8934b9a Sofia Papagiannaki
    help = "Create a user"
49 5ce3ce4f Sofia Papagiannaki
50 d8934b9a Sofia Papagiannaki
    option_list = BaseCommand.option_list + (
51 9a06d96f Olga Brani
        make_option('--password',
52 9a06d96f Olga Brani
                    dest='password',
53 9a06d96f Olga Brani
                    metavar='PASSWORD',
54 9a06d96f Olga Brani
                    help="Set user's password"),
55 d8934b9a Sofia Papagiannaki
        make_option('--active',
56 5ce3ce4f Sofia Papagiannaki
                    action='store_true',
57 04e05445 Sofia Papagiannaki
                    dest='active',
58 5ce3ce4f Sofia Papagiannaki
                    default=False,
59 04e05445 Sofia Papagiannaki
                    help="Set active"),
60 d8934b9a Sofia Papagiannaki
        make_option('--admin',
61 5ce3ce4f Sofia Papagiannaki
                    action='store_true',
62 9a06d96f Olga Brani
                    dest='is_superuser',
63 5ce3ce4f Sofia Papagiannaki
                    default=False,
64 5ce3ce4f Sofia Papagiannaki
                    help="Give user admin rights"),
65 9a06d96f Olga Brani
        make_option('-g',
66 9a06d96f Olga Brani
                    action='append',
67 9a06d96f Olga Brani
                    dest='groups',
68 9a06d96f Olga Brani
                    help="Add user group (may be used multiple times)"),
69 9a06d96f Olga Brani
        make_option('-p',
70 9a06d96f Olga Brani
                    action='append',
71 9a06d96f Olga Brani
                    dest='permissions',
72 9a06d96f Olga Brani
                    help="Add user permission (may be used multiple times)")
73 5ce3ce4f Sofia Papagiannaki
    )
74 5ce3ce4f Sofia Papagiannaki
75 d8934b9a Sofia Papagiannaki
    def handle(self, *args, **options):
76 07a1f977 Sofia Papagiannaki
        if len(args) != 3:
77 d8934b9a Sofia Papagiannaki
            raise CommandError("Invalid number of arguments")
78 5ce3ce4f Sofia Papagiannaki
79 b095201e Sofia Papagiannaki
        email, first_name, last_name = map(lambda arg: arg.decode('utf8'),
80 b095201e Sofia Papagiannaki
                                           args[:3])
81 04e05445 Sofia Papagiannaki
82 9a06d96f Olga Brani
        try:
83 9a06d96f Olga Brani
            validate_email(email)
84 9a06d96f Olga Brani
        except ValidationError:
85 9a06d96f Olga Brani
            raise CommandError("Invalid email")
86 5ce3ce4f Sofia Papagiannaki
87 b095201e Sofia Papagiannaki
        password = options['password'] or \
88 b095201e Sofia Papagiannaki
            AstakosUser.objects.make_random_password()
89 5ce3ce4f Sofia Papagiannaki
90 d8934b9a Sofia Papagiannaki
        try:
91 b095201e Sofia Papagiannaki
            u = AstakosUser(email=email,
92 b095201e Sofia Papagiannaki
                            first_name=first_name,
93 b095201e Sofia Papagiannaki
                            last_name=last_name,
94 b095201e Sofia Papagiannaki
                            password=password,
95 b095201e Sofia Papagiannaki
                            is_superuser=options['is_superuser'])
96 b095201e Sofia Papagiannaki
            u.save()
97 b095201e Sofia Papagiannaki
98 04e05445 Sofia Papagiannaki
        except BaseException, e:
99 3b99d2d6 Sofia Papagiannaki
            raise CommandError(e)
100 3b99d2d6 Sofia Papagiannaki
        else:
101 b095201e Sofia Papagiannaki
            self.stdout.write('User created successfully ')
102 b095201e Sofia Papagiannaki
            if not options.get('password'):
103 b095201e Sofia Papagiannaki
                self.stdout.write('with password: %s\n' % password)
104 07a1f977 Sofia Papagiannaki
            else:
105 b095201e Sofia Papagiannaki
                self.stdout.write('\n')
106 b095201e Sofia Papagiannaki
107 b095201e Sofia Papagiannaki
            try:
108 b095201e Sofia Papagiannaki
                u.add_auth_provider('local')
109 b095201e Sofia Papagiannaki
                map(u.add_permission, options['permissions'])
110 b095201e Sofia Papagiannaki
                map(u.add_group, options['groups'])
111 b095201e Sofia Papagiannaki
112 b095201e Sofia Papagiannaki
                if options['active']:
113 b095201e Sofia Papagiannaki
                    activate(u)
114 b095201e Sofia Papagiannaki
            except BaseException, e:
115 b095201e Sofia Papagiannaki
                import traceback
116 b095201e Sofia Papagiannaki
                traceback.print_exc()
117 b095201e Sofia Papagiannaki
                raise CommandError(e)