Revision 9a06d96f snf-astakos-app/astakos/im/management/commands/user-add.py
b/snf-astakos-app/astakos/im/management/commands/user-add.py | ||
---|---|---|
41 | 41 |
from django.core.validators import validate_email |
42 | 42 |
from django.core.exceptions import ValidationError |
43 | 43 |
|
44 |
from astakos.im.models import AstakosUser, AstakosGroup, Membership
|
|
45 |
from astakos.im.util import reserved_email
|
|
44 |
from astakos.im.models import AstakosUser |
|
45 |
from astakos.im.api.callpoint import AstakosDjangoDBCallpoint
|
|
46 | 46 |
|
47 | 47 |
from ._common import add_user_permission |
48 | 48 |
|
49 | 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 |
|
|
50 | 56 |
class Command(BaseCommand): |
51 |
args = "<email> <first name> <last name> <affiliation>"
|
|
57 |
args = "<email>" |
|
52 | 58 |
help = "Create a user" |
53 | 59 |
|
54 | 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"), |
|
55 | 77 |
make_option('--active', |
56 | 78 |
action='store_true', |
57 |
dest='active', |
|
79 |
dest='is_active',
|
|
58 | 80 |
default=False, |
59 | 81 |
help="Activate user"), |
60 | 82 |
make_option('--admin', |
61 | 83 |
action='store_true', |
62 |
dest='admin',
|
|
84 |
dest='is_superuser',
|
|
63 | 85 |
default=False, |
64 | 86 |
help="Give user admin rights"), |
65 |
make_option('--password', |
|
66 |
dest='password', |
|
67 |
metavar='PASSWORD', |
|
68 |
help="Set user's password"), |
|
69 |
make_option('--add-group', |
|
70 |
dest='add-group', |
|
71 |
help="Add user group"), |
|
72 |
make_option('--add-permission', |
|
73 |
dest='add-permission', |
|
74 |
help="Add user permission") |
|
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)") |
|
75 | 95 |
) |
76 | 96 |
|
77 | 97 |
def handle(self, *args, **options): |
78 |
if len(args) != 4:
|
|
98 |
if len(args) != 1:
|
|
79 | 99 |
raise CommandError("Invalid number of arguments") |
80 | 100 |
|
81 |
args = [a.decode('utf8') for a in args] |
|
82 |
email, first, last, affiliation = args |
|
83 |
|
|
84 |
#try: |
|
85 |
# validate_email(email) |
|
86 |
#except ValidationError: |
|
87 |
# raise CommandError("Invalid email") |
|
88 |
|
|
89 |
username = uuid4().hex[:30] |
|
90 |
password = options.get('password') |
|
91 |
if password is None: |
|
92 |
password = AstakosUser.objects.make_random_password() |
|
93 |
|
|
94 |
if reserved_email(email): |
|
95 |
raise CommandError("A user with this email already exists") |
|
101 |
email = args[0].decode('utf8') |
|
96 | 102 |
|
97 |
user = AstakosUser(username=username, first_name=first, last_name=last, |
|
98 |
email=email, affiliation=affiliation, |
|
99 |
provider='local') |
|
100 |
user.set_password(password) |
|
101 |
user.renew_token() |
|
103 |
try: |
|
104 |
validate_email(email) |
|
105 |
except ValidationError: |
|
106 |
raise CommandError("Invalid email") |
|
102 | 107 |
|
103 |
if options['active']:
|
|
104 |
user.is_active = True
|
|
105 |
if options['admin']:
|
|
106 |
user.is_superuser = True
|
|
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()
|
|
107 | 112 |
|
108 | 113 |
try: |
109 |
user.save() |
|
114 |
c = AstakosDjangoDBCallpoint() |
|
115 |
c.create_users((u,)) |
|
110 | 116 |
except socket.error, e: |
111 | 117 |
raise CommandError(e) |
112 | 118 |
except ValidationError, e: |
113 |
raise CommandError(e) |
|
114 |
else: |
|
115 |
msg = "Created user id %d" % (user.id,) |
|
116 |
if options['password'] is None: |
|
117 |
msg += " with password '%s'" % (password,) |
|
118 |
self.stdout.write(msg + '\n') |
|
119 |
|
|
120 |
groupname = options.get('add-group') |
|
121 |
if groupname is not None: |
|
122 |
try: |
|
123 |
group = AstakosGroup.objects.get(name=groupname) |
|
124 |
Membership(group=group, |
|
125 |
person=user, date_joined=datetime.now()).save() |
|
126 |
self.stdout.write( |
|
127 |
'Group: %s added successfully\n' % groupname) |
|
128 |
except AstakosGroup.DoesNotExist, e: |
|
129 |
self.stdout.write( |
|
130 |
'Group named %s does not exist\n' % groupname) |
|
131 |
|
|
132 |
pname = options.get('add-permission') |
|
133 |
if pname is not None: |
|
134 |
try: |
|
135 |
r, created = add_user_permission(user, pname) |
|
136 |
if created: |
|
137 |
self.stdout.write( |
|
138 |
'Permission: %s created successfully\n' % pname) |
|
139 |
if r > 0: |
|
140 |
self.stdout.write( |
|
141 |
'Permission: %s added successfully\n' % pname) |
|
142 |
elif r == 0: |
|
143 |
self.stdout.write( |
|
144 |
'User has already permission: %s\n' % pname) |
|
145 |
except Exception, e: |
|
146 |
raise CommandError(e) |
|
119 |
raise CommandError(e) |
Also available in: Unified diff