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
38 from django.core.exceptions import ValidationError
40 from astakos.im.models import AstakosUser
42 class Command(BaseCommand):
44 help = "Modify a user's attributes"
46 option_list = BaseCommand.option_list + (
47 make_option('--invitations',
50 help="Update user's invitations"),
51 make_option('--level',
54 help="Update user's level"),
55 make_option('--password',
58 help="Set user's password"),
59 make_option('--renew-token',
63 help="Renew the user's token"),
64 make_option('--set-admin',
68 help="Give user admin rights"),
69 make_option('--set-noadmin',
73 help="Revoke user's admin rights"),
74 make_option('--set-active',
78 help="Change user's state to inactive"),
79 make_option('--set-inactive',
83 help="Change user's state to inactive"),
84 make_option('--add-group',
86 help="Add user group"),
87 make_option('--delete-group',
89 help="Delete user group"),
92 def handle(self, *args, **options):
94 raise CommandError("Please provide a user ID")
97 user = AstakosUser.objects.get(id=int( args[0]))
99 raise CommandError("Invalid ID")
102 raise CommandError("Unknown user")
104 if options.get('admin'):
105 user.is_superuser = True
106 elif options.get('noadmin'):
107 user.is_superuser = False
109 if options.get('active'):
110 user.is_active = True
111 elif options.get('inactive'):
112 user.is_active = False
114 invitations = options.get('invitations')
115 if invitations is not None:
116 user.invitations = int(invitations)
118 groupname = options.get('add-group')
119 if groupname is not None:
121 group = Group.objects.get(name=groupname)
122 user.groups.add(group)
123 except Group.DoesNotExist, e:
124 raise CommandError("Group named %s does not exist." % groupname)
126 groupname = options.get('delete-group')
127 if groupname is not None:
129 group = Group.objects.get(name=groupname)
130 user.groups.remove(group)
131 except Group.DoesNotExist, e:
132 raise CommandError("Group named %s does not exist." % groupname)
134 level = options.get('level')
135 if level is not None:
136 user.level = int(level)
138 password = options.get('password')
139 if password is not None:
140 user.set_password(password)
142 if options['renew_token']:
147 except ValidationError, e:
148 raise CommandError(e)