root / snf-astakos-app / astakos / im / management / commands / user-update.py @ 4bdd7e3d
History | View | Annotate | Download (7.9 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 |
from optparse import make_option |
35 |
|
36 |
from django.core.management.base import BaseCommand, CommandError |
37 |
from django.contrib.auth.models import Group |
38 |
from django.core.exceptions import ValidationError |
39 |
|
40 |
from astakos.im.models import AstakosUser |
41 |
from ._common import remove_user_permission, add_user_permission |
42 |
|
43 |
|
44 |
class Command(BaseCommand): |
45 |
args = "<user ID>"
|
46 |
help = "Modify a user's attributes"
|
47 |
|
48 |
option_list = BaseCommand.option_list + ( |
49 |
make_option('--invitations',
|
50 |
dest='invitations',
|
51 |
metavar='NUM',
|
52 |
help="Update user's invitations"),
|
53 |
make_option('--level',
|
54 |
dest='level',
|
55 |
metavar='NUM',
|
56 |
help="Update user's level"),
|
57 |
make_option('--password',
|
58 |
dest='password',
|
59 |
metavar='PASSWORD',
|
60 |
help="Set user's password"),
|
61 |
make_option('--provider',
|
62 |
dest='provider',
|
63 |
metavar='PROVIDER',
|
64 |
help="Set user's provider"),
|
65 |
make_option('--renew-token',
|
66 |
action='store_true',
|
67 |
dest='renew_token',
|
68 |
default=False,
|
69 |
help="Renew the user's token"),
|
70 |
make_option('--renew-password',
|
71 |
action='store_true',
|
72 |
dest='renew_password',
|
73 |
default=False,
|
74 |
help="Renew the user's password"),
|
75 |
make_option('--set-admin',
|
76 |
action='store_true',
|
77 |
dest='admin',
|
78 |
default=False,
|
79 |
help="Give user admin rights"),
|
80 |
make_option('--set-noadmin',
|
81 |
action='store_true',
|
82 |
dest='noadmin',
|
83 |
default=False,
|
84 |
help="Revoke user's admin rights"),
|
85 |
make_option('--set-active',
|
86 |
action='store_true',
|
87 |
dest='active',
|
88 |
default=False,
|
89 |
help="Change user's state to inactive"),
|
90 |
make_option('--set-inactive',
|
91 |
action='store_true',
|
92 |
dest='inactive',
|
93 |
default=False,
|
94 |
help="Change user's state to inactive"),
|
95 |
make_option('--add-group',
|
96 |
dest='add-group',
|
97 |
help="Add user group"),
|
98 |
make_option('--delete-group',
|
99 |
dest='delete-group',
|
100 |
help="Delete user group"),
|
101 |
make_option('--add-permission',
|
102 |
dest='add-permission',
|
103 |
help="Add user permission"),
|
104 |
make_option('--delete-permission',
|
105 |
dest='delete-permission',
|
106 |
help="Delete user permission"),
|
107 |
) |
108 |
|
109 |
def handle(self, *args, **options): |
110 |
if len(args) != 1: |
111 |
raise CommandError("Please provide a user ID") |
112 |
|
113 |
if args[0].isdigit(): |
114 |
user = AstakosUser.objects.get(id=int(args[0])) |
115 |
else:
|
116 |
raise CommandError("Invalid ID") |
117 |
|
118 |
if not user: |
119 |
raise CommandError("Unknown user") |
120 |
|
121 |
if options.get('admin'): |
122 |
user.is_superuser = True
|
123 |
elif options.get('noadmin'): |
124 |
user.is_superuser = False
|
125 |
|
126 |
if options.get('active'): |
127 |
user.is_active = True
|
128 |
if not user.email_verified: |
129 |
user.email_verified = True
|
130 |
if not user.activation_sent: |
131 |
user.activation_sent = True
|
132 |
elif options.get('inactive'): |
133 |
user.is_active = False
|
134 |
|
135 |
invitations = options.get('invitations')
|
136 |
if invitations is not None: |
137 |
user.invitations = int(invitations)
|
138 |
|
139 |
groupname = options.get('add-group')
|
140 |
if groupname is not None: |
141 |
try:
|
142 |
group = Group.objects.get(name=groupname) |
143 |
user.groups.add(group) |
144 |
except Group.DoesNotExist, e:
|
145 |
self.stdout.write(
|
146 |
"Group named %s does not exist\n" % groupname)
|
147 |
|
148 |
groupname = options.get('delete-group')
|
149 |
if groupname is not None: |
150 |
try:
|
151 |
group = Group.objects.get(name=groupname) |
152 |
user.groups.remove(group) |
153 |
except Group.DoesNotExist, e:
|
154 |
self.stdout.write(
|
155 |
"Group named %s does not exist\n" % groupname)
|
156 |
|
157 |
pname = options.get('add-permission')
|
158 |
if pname is not None: |
159 |
try:
|
160 |
r, created = add_user_permission(user, pname) |
161 |
if created:
|
162 |
self.stdout.write(
|
163 |
'Permission: %s created successfully\n' % pname)
|
164 |
if r > 0: |
165 |
self.stdout.write(
|
166 |
'Permission: %s added successfully\n' % pname)
|
167 |
elif r == 0: |
168 |
self.stdout.write(
|
169 |
'User has already permission: %s\n' % pname)
|
170 |
except Exception, e: |
171 |
raise CommandError(e)
|
172 |
|
173 |
pname = options.get('delete-permission')
|
174 |
if pname is not None and not user.has_perm(pname): |
175 |
try:
|
176 |
r = remove_user_permission(user, pname) |
177 |
if r < 0: |
178 |
self.stdout.write(
|
179 |
'Invalid permission codename: %s\n' % pname)
|
180 |
elif r == 0: |
181 |
self.stdout.write('User has not permission: %s\n' % pname) |
182 |
elif r > 0: |
183 |
self.stdout.write(
|
184 |
'Permission: %s removed successfully\n' % pname)
|
185 |
except Exception, e: |
186 |
raise CommandError(e)
|
187 |
|
188 |
level = options.get('level')
|
189 |
if level is not None: |
190 |
user.level = int(level)
|
191 |
|
192 |
password = options.get('password')
|
193 |
if password is not None: |
194 |
user.set_password(password) |
195 |
|
196 |
provider = options.get('provider')
|
197 |
if provider is not None: |
198 |
user.provider = provider |
199 |
|
200 |
password = None
|
201 |
if options['renew_password']: |
202 |
password = AstakosUser.objects.make_random_password() |
203 |
user.set_password(password) |
204 |
|
205 |
if options['renew_token']: |
206 |
user.renew_token() |
207 |
|
208 |
try:
|
209 |
user.save() |
210 |
except ValidationError, e:
|
211 |
raise CommandError(e)
|
212 |
|
213 |
if password:
|
214 |
self.stdout.write('User\'s new password: %s\n' % password) |