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