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