1 # Copyright 2012 GRNET S.A. All rights reserved.
3 # Redistribution and use in source and binary forms, with or
4 # without modification, are permitted provided that the following
7 # 1. Redistributions of source code must retain the above
8 # copyright notice, this list of conditions and the following
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.
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.
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.
34 from optparse import make_option
36 from django.core.management.base import BaseCommand, CommandError
37 from django.contrib.auth.models import Group, Permission
38 from django.contrib.contenttypes.models import ContentType
39 from django.core.exceptions import ValidationError
41 from astakos.im.models import AstakosUser
42 from ._common import remove_user_permission, add_user_permission
44 class Command(BaseCommand):
46 help = "Modify a user's attributes"
48 option_list = BaseCommand.option_list + (
49 make_option('--invitations',
52 help="Update user's invitations"),
53 make_option('--level',
56 help="Update user's level"),
57 make_option('--password',
60 help="Set user's password"),
61 make_option('--provider',
64 help="Set user's provider"),
65 make_option('--renew-token',
69 help="Renew the user's token"),
70 make_option('--renew-password',
72 dest='renew_password',
74 help="Renew the user's password"),
75 make_option('--set-admin',
79 help="Give user admin rights"),
80 make_option('--set-noadmin',
84 help="Revoke user's admin rights"),
85 make_option('--set-active',
89 help="Change user's state to inactive"),
90 make_option('--set-inactive',
94 help="Change user's state to inactive"),
95 make_option('--add-group',
97 help="Add user group"),
98 make_option('--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"),
109 def handle(self, *args, **options):
111 raise CommandError("Please provide a user ID")
113 if args[0].isdigit():
114 user = AstakosUser.objects.get(id=int( args[0]))
116 raise CommandError("Invalid ID")
119 raise CommandError("Unknown user")
121 if options.get('admin'):
122 user.is_superuser = True
123 elif options.get('noadmin'):
124 user.is_superuser = False
126 if options.get('active'):
127 user.is_active = True
128 elif options.get('inactive'):
129 user.is_active = False
131 invitations = options.get('invitations')
132 if invitations is not None:
133 user.invitations = int(invitations)
135 groupname = options.get('add-group')
136 if groupname is not None:
138 group = Group.objects.get(name=groupname)
139 user.groups.add(group)
140 except Group.DoesNotExist, e:
141 self.stdout.write("Group named %s does not exist\n" % groupname)
143 groupname = options.get('delete-group')
144 if groupname is not None:
146 group = Group.objects.get(name=groupname)
147 user.groups.remove(group)
148 except Group.DoesNotExist, e:
149 self.stdout.write("Group named %s does not exist\n" % groupname)
151 pname = options.get('add-permission')
152 if pname is not None:
154 r, created = add_user_permission(user, pname)
156 self.stdout.write('Permission: %s created successfully\n' % pname)
158 self.stdout.write('Permission: %s added successfully\n' % pname)
160 self.stdout.write('User has already permission: %s\n' % pname)
162 raise CommandError(e)
164 pname = options.get('delete-permission')
165 if pname is not None and not user.has_perm(pname):
167 r = remove_user_permission(user, pname)
169 self.stdout.write('Invalid permission codename: %s\n' % pname)
171 self.stdout.write('User has not permission: %s\n' % pname)
173 self.stdout.write('Permission: %s removed successfully\n' % pname)
175 raise CommandError(e)
177 level = options.get('level')
178 if level is not None:
179 user.level = int(level)
181 password = options.get('password')
182 if password is not None:
183 user.set_password(password)
185 provider = options.get('provider')
186 if provider is not None:
187 user.provider = provider
191 if options['renew_password']:
192 password = AstakosUser.objects.make_random_password()
193 user.set_password(password)
195 if options['renew_token']:
200 except ValidationError, e:
201 raise CommandError(e)
204 self.stdout.write('User\'s new password: %s\n' % password)