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