Configurable enable email change mechanism
[astakos] / snf-astakos-app / astakos / im / management / commands / modifyuser.py
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
42 class Command(BaseCommand):
43     args = "<user ID>"
44     help = "Modify a user's attributes"
45     
46     option_list = BaseCommand.option_list + (
47         make_option('--invitations',
48             dest='invitations',
49             metavar='NUM',
50             help="Update user's invitations"),
51         make_option('--level',
52             dest='level',
53             metavar='NUM',
54             help="Update user's level"),
55         make_option('--password',
56             dest='password',
57             metavar='PASSWORD',
58             help="Set user's password"),
59         make_option('--renew-token',
60             action='store_true',
61             dest='renew_token',
62             default=False,
63             help="Renew the user's token"),
64         make_option('--set-admin',
65             action='store_true',
66             dest='admin',
67             default=False,
68             help="Give user admin rights"),
69         make_option('--set-noadmin',
70             action='store_true',
71             dest='noadmin',
72             default=False,
73             help="Revoke user's admin rights"),
74         make_option('--set-active',
75             action='store_true',
76             dest='active',
77             default=False,
78             help="Change user's state to inactive"),
79         make_option('--set-inactive',
80             action='store_true',
81             dest='inactive',
82             default=False,
83             help="Change user's state to inactive"),
84         make_option('--add-group',
85             dest='add-group',
86             help="Add user group"),
87         make_option('--delete-group',
88             dest='delete-group',
89             help="Delete user group"),
90         )
91     
92     def handle(self, *args, **options):
93         if len(args) != 1:
94             raise CommandError("Please provide a user ID")
95         
96         if args[0].isdigit():
97             user = AstakosUser.objects.get(id=int( args[0]))
98         else:
99             raise CommandError("Invalid ID")
100         
101         if not user:
102             raise CommandError("Unknown user")
103         
104         if options.get('admin'):
105             user.is_superuser = True
106         elif options.get('noadmin'):
107             user.is_superuser = False
108         
109         if options.get('active'):
110             user.is_active = True
111         elif options.get('inactive'):
112             user.is_active = False
113         
114         invitations = options.get('invitations')
115         if invitations is not None:
116             user.invitations = int(invitations)
117         
118         groupname = options.get('add-group')
119         if groupname is not None:
120             try:
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)
125         
126         groupname = options.get('delete-group')
127         if groupname is not None:
128             try:
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)
133         
134         level = options.get('level')
135         if level is not None:
136             user.level = int(level)
137         
138         password = options.get('password')
139         if password is not None:
140             user.set_password(password)
141         
142         if options['renew_token']:
143             user.renew_token()
144         
145         try:
146             user.save()
147         except ValidationError, e:
148             raise CommandError(e)