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