Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / management / commands / user-update.py @ 66c90acd

History | View | Annotate | Download (7.4 kB)

1 ca96a346 Giorgos Verigakis
# Copyright 2012 GRNET S.A. All rights reserved.
2 df48dd1b Giorgos Verigakis
#
3 df48dd1b Giorgos Verigakis
# Redistribution and use in source and binary forms, with or
4 df48dd1b Giorgos Verigakis
# without modification, are permitted provided that the following
5 df48dd1b Giorgos Verigakis
# conditions are met:
6 df48dd1b Giorgos Verigakis
#
7 df48dd1b Giorgos Verigakis
#   1. Redistributions of source code must retain the above
8 df48dd1b Giorgos Verigakis
#      copyright notice, this list of conditions and the following
9 df48dd1b Giorgos Verigakis
#      disclaimer.
10 df48dd1b Giorgos Verigakis
#
11 df48dd1b Giorgos Verigakis
#   2. Redistributions in binary form must reproduce the above
12 df48dd1b Giorgos Verigakis
#      copyright notice, this list of conditions and the following
13 df48dd1b Giorgos Verigakis
#      disclaimer in the documentation and/or other materials
14 df48dd1b Giorgos Verigakis
#      provided with the distribution.
15 df48dd1b Giorgos Verigakis
#
16 df48dd1b Giorgos Verigakis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 df48dd1b Giorgos Verigakis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 df48dd1b Giorgos Verigakis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 df48dd1b Giorgos Verigakis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 df48dd1b Giorgos Verigakis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 df48dd1b Giorgos Verigakis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 df48dd1b Giorgos Verigakis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 df48dd1b Giorgos Verigakis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 df48dd1b Giorgos Verigakis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 df48dd1b Giorgos Verigakis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 df48dd1b Giorgos Verigakis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 df48dd1b Giorgos Verigakis
# POSSIBILITY OF SUCH DAMAGE.
28 df48dd1b Giorgos Verigakis
#
29 df48dd1b Giorgos Verigakis
# The views and conclusions contained in the software and
30 df48dd1b Giorgos Verigakis
# documentation are those of the authors and should not be
31 df48dd1b Giorgos Verigakis
# interpreted as representing official policies, either expressed
32 df48dd1b Giorgos Verigakis
# or implied, of GRNET S.A.
33 df48dd1b Giorgos Verigakis
34 df48dd1b Giorgos Verigakis
from optparse import make_option
35 df48dd1b Giorgos Verigakis
36 df48dd1b Giorgos Verigakis
from django.core.management.base import BaseCommand, CommandError
37 30dc8c1a Sofia Papagiannaki
from django.contrib.auth.models import Group, Permission
38 30dc8c1a Sofia Papagiannaki
from django.contrib.contenttypes.models import ContentType
39 0a569195 Sofia Papagiannaki
from django.core.exceptions import ValidationError
40 df48dd1b Giorgos Verigakis
41 0a569195 Sofia Papagiannaki
from astakos.im.models import AstakosUser
42 30dc8c1a Sofia Papagiannaki
from ._common import remove_user_permission, add_user_permission
43 df48dd1b Giorgos Verigakis
44 df48dd1b Giorgos Verigakis
class Command(BaseCommand):
45 0a569195 Sofia Papagiannaki
    args = "<user ID>"
46 df48dd1b Giorgos Verigakis
    help = "Modify a user's attributes"
47 df48dd1b Giorgos Verigakis
    
48 df48dd1b Giorgos Verigakis
    option_list = BaseCommand.option_list + (
49 df48dd1b Giorgos Verigakis
        make_option('--invitations',
50 df48dd1b Giorgos Verigakis
            dest='invitations',
51 df48dd1b Giorgos Verigakis
            metavar='NUM',
52 df48dd1b Giorgos Verigakis
            help="Update user's invitations"),
53 ebd369d0 Sofia Papagiannaki
        make_option('--level',
54 ebd369d0 Sofia Papagiannaki
            dest='level',
55 ebd369d0 Sofia Papagiannaki
            metavar='NUM',
56 ebd369d0 Sofia Papagiannaki
            help="Update user's level"),
57 df48dd1b Giorgos Verigakis
        make_option('--password',
58 df48dd1b Giorgos Verigakis
            dest='password',
59 df48dd1b Giorgos Verigakis
            metavar='PASSWORD',
60 df48dd1b Giorgos Verigakis
            help="Set user's password"),
61 cfb15117 Sofia Papagiannaki
        make_option('--provider',
62 cfb15117 Sofia Papagiannaki
            dest='provider',
63 cfb15117 Sofia Papagiannaki
            metavar='PROVIDER',
64 cfb15117 Sofia Papagiannaki
            help="Set user's provider"),
65 df48dd1b Giorgos Verigakis
        make_option('--renew-token',
66 df48dd1b Giorgos Verigakis
            action='store_true',
67 df48dd1b Giorgos Verigakis
            dest='renew_token',
68 df48dd1b Giorgos Verigakis
            default=False,
69 df48dd1b Giorgos Verigakis
            help="Renew the user's token"),
70 cfb15117 Sofia Papagiannaki
        make_option('--renew-password',
71 cfb15117 Sofia Papagiannaki
            action='store_true',
72 cfb15117 Sofia Papagiannaki
            dest='renew_password',
73 cfb15117 Sofia Papagiannaki
            default=False,
74 cfb15117 Sofia Papagiannaki
            help="Renew the user's password"),
75 df48dd1b Giorgos Verigakis
        make_option('--set-admin',
76 df48dd1b Giorgos Verigakis
            action='store_true',
77 df48dd1b Giorgos Verigakis
            dest='admin',
78 df48dd1b Giorgos Verigakis
            default=False,
79 df48dd1b Giorgos Verigakis
            help="Give user admin rights"),
80 df48dd1b Giorgos Verigakis
        make_option('--set-noadmin',
81 df48dd1b Giorgos Verigakis
            action='store_true',
82 df48dd1b Giorgos Verigakis
            dest='noadmin',
83 df48dd1b Giorgos Verigakis
            default=False,
84 df48dd1b Giorgos Verigakis
            help="Revoke user's admin rights"),
85 ebd369d0 Sofia Papagiannaki
        make_option('--set-active',
86 ebd369d0 Sofia Papagiannaki
            action='store_true',
87 ebd369d0 Sofia Papagiannaki
            dest='active',
88 ebd369d0 Sofia Papagiannaki
            default=False,
89 ebd369d0 Sofia Papagiannaki
            help="Change user's state to inactive"),
90 ebd369d0 Sofia Papagiannaki
        make_option('--set-inactive',
91 fc28d013 Sofia Papagiannaki
            action='store_true',
92 fc28d013 Sofia Papagiannaki
            dest='inactive',
93 fc28d013 Sofia Papagiannaki
            default=False,
94 fc28d013 Sofia Papagiannaki
            help="Change user's state to inactive"),
95 4e30244e Sofia Papagiannaki
        make_option('--add-group',
96 4e30244e Sofia Papagiannaki
            dest='add-group',
97 4e30244e Sofia Papagiannaki
            help="Add user group"),
98 4e30244e Sofia Papagiannaki
        make_option('--delete-group',
99 4e30244e Sofia Papagiannaki
            dest='delete-group',
100 4e30244e Sofia Papagiannaki
            help="Delete user group"),
101 30dc8c1a Sofia Papagiannaki
        make_option('--add-permission',
102 30dc8c1a Sofia Papagiannaki
            dest='add-permission',
103 30dc8c1a Sofia Papagiannaki
            help="Add user permission"),
104 30dc8c1a Sofia Papagiannaki
        make_option('--delete-permission',
105 30dc8c1a Sofia Papagiannaki
            dest='delete-permission',
106 30dc8c1a Sofia Papagiannaki
            help="Delete user permission"),
107 df48dd1b Giorgos Verigakis
        )
108 df48dd1b Giorgos Verigakis
    
109 df48dd1b Giorgos Verigakis
    def handle(self, *args, **options):
110 df48dd1b Giorgos Verigakis
        if len(args) != 1:
111 0a569195 Sofia Papagiannaki
            raise CommandError("Please provide a user ID")
112 0a569195 Sofia Papagiannaki
        
113 0a569195 Sofia Papagiannaki
        if args[0].isdigit():
114 0a569195 Sofia Papagiannaki
            user = AstakosUser.objects.get(id=int( args[0]))
115 0a569195 Sofia Papagiannaki
        else:
116 0a569195 Sofia Papagiannaki
            raise CommandError("Invalid ID")
117 df48dd1b Giorgos Verigakis
        
118 397d5cbe Giorgos Verigakis
        if not user:
119 397d5cbe Giorgos Verigakis
            raise CommandError("Unknown user")
120 df48dd1b Giorgos Verigakis
        
121 df48dd1b Giorgos Verigakis
        if options.get('admin'):
122 df48dd1b Giorgos Verigakis
            user.is_superuser = True
123 df48dd1b Giorgos Verigakis
        elif options.get('noadmin'):
124 df48dd1b Giorgos Verigakis
            user.is_superuser = False
125 df48dd1b Giorgos Verigakis
        
126 ebd369d0 Sofia Papagiannaki
        if options.get('active'):
127 ebd369d0 Sofia Papagiannaki
            user.is_active = True
128 ebd369d0 Sofia Papagiannaki
        elif options.get('inactive'):
129 ebd369d0 Sofia Papagiannaki
            user.is_active = False
130 ebd369d0 Sofia Papagiannaki
        
131 df48dd1b Giorgos Verigakis
        invitations = options.get('invitations')
132 df48dd1b Giorgos Verigakis
        if invitations is not None:
133 df48dd1b Giorgos Verigakis
            user.invitations = int(invitations)
134 df48dd1b Giorgos Verigakis
        
135 4e30244e Sofia Papagiannaki
        groupname = options.get('add-group')
136 18ffbee1 Sofia Papagiannaki
        if groupname is not None:
137 18ffbee1 Sofia Papagiannaki
            try:
138 18ffbee1 Sofia Papagiannaki
                group = Group.objects.get(name=groupname)
139 18ffbee1 Sofia Papagiannaki
                user.groups.add(group)
140 18ffbee1 Sofia Papagiannaki
            except Group.DoesNotExist, e:
141 30dc8c1a Sofia Papagiannaki
                self.stdout.write("Group named %s does not exist\n" % groupname)
142 18ffbee1 Sofia Papagiannaki
        
143 4e30244e Sofia Papagiannaki
        groupname = options.get('delete-group')
144 4e30244e Sofia Papagiannaki
        if groupname is not None:
145 4e30244e Sofia Papagiannaki
            try:
146 4e30244e Sofia Papagiannaki
                group = Group.objects.get(name=groupname)
147 4e30244e Sofia Papagiannaki
                user.groups.remove(group)
148 4e30244e Sofia Papagiannaki
            except Group.DoesNotExist, e:
149 30dc8c1a Sofia Papagiannaki
                self.stdout.write("Group named %s does not exist\n" % groupname)
150 30dc8c1a Sofia Papagiannaki
        
151 30dc8c1a Sofia Papagiannaki
        pname = options.get('add-permission')
152 30dc8c1a Sofia Papagiannaki
        if pname is not None:
153 30dc8c1a Sofia Papagiannaki
            try:
154 30dc8c1a Sofia Papagiannaki
                r, created = add_user_permission(user, pname)
155 30dc8c1a Sofia Papagiannaki
                if created:
156 30dc8c1a Sofia Papagiannaki
                    self.stdout.write('Permission: %s created successfully\n' % pname)
157 30dc8c1a Sofia Papagiannaki
                if r > 0:
158 30dc8c1a Sofia Papagiannaki
                    self.stdout.write('Permission: %s added successfully\n' % pname)
159 30dc8c1a Sofia Papagiannaki
                elif r==0:
160 30dc8c1a Sofia Papagiannaki
                    self.stdout.write('User has already permission: %s\n' % pname)
161 30dc8c1a Sofia Papagiannaki
            except Exception, e:
162 30dc8c1a Sofia Papagiannaki
                raise CommandError(e)
163 30dc8c1a Sofia Papagiannaki
        
164 30dc8c1a Sofia Papagiannaki
        pname  = options.get('delete-permission')
165 30dc8c1a Sofia Papagiannaki
        if pname is not None and not user.has_perm(pname):
166 30dc8c1a Sofia Papagiannaki
            try:
167 30dc8c1a Sofia Papagiannaki
                r = remove_user_permission(user, pname)
168 30dc8c1a Sofia Papagiannaki
                if r < 0:
169 30dc8c1a Sofia Papagiannaki
                    self.stdout.write('Invalid permission codename: %s\n' % pname)
170 30dc8c1a Sofia Papagiannaki
                elif r == 0:
171 30dc8c1a Sofia Papagiannaki
                    self.stdout.write('User has not permission: %s\n' % pname)
172 30dc8c1a Sofia Papagiannaki
                elif r > 0:
173 30dc8c1a Sofia Papagiannaki
                    self.stdout.write('Permission: %s removed successfully\n' % pname)
174 30dc8c1a Sofia Papagiannaki
            except Exception, e:
175 30dc8c1a Sofia Papagiannaki
                raise CommandError(e)
176 4e30244e Sofia Papagiannaki
        
177 ebd369d0 Sofia Papagiannaki
        level = options.get('level')
178 ebd369d0 Sofia Papagiannaki
        if level is not None:
179 ebd369d0 Sofia Papagiannaki
            user.level = int(level)
180 ebd369d0 Sofia Papagiannaki
        
181 df48dd1b Giorgos Verigakis
        password = options.get('password')
182 df48dd1b Giorgos Verigakis
        if password is not None:
183 df48dd1b Giorgos Verigakis
            user.set_password(password)
184 df48dd1b Giorgos Verigakis
        
185 cfb15117 Sofia Papagiannaki
        provider = options.get('provider')
186 cfb15117 Sofia Papagiannaki
        if provider is not None:
187 cfb15117 Sofia Papagiannaki
            user.provider = provider
188 cfb15117 Sofia Papagiannaki
        
189 cfb15117 Sofia Papagiannaki
        
190 cfb15117 Sofia Papagiannaki
        password = None
191 cfb15117 Sofia Papagiannaki
        if options['renew_password']:
192 cfb15117 Sofia Papagiannaki
            password = AstakosUser.objects.make_random_password()
193 cfb15117 Sofia Papagiannaki
            user.set_password(password)
194 cfb15117 Sofia Papagiannaki
        
195 df48dd1b Giorgos Verigakis
        if options['renew_token']:
196 df48dd1b Giorgos Verigakis
            user.renew_token()
197 df48dd1b Giorgos Verigakis
        
198 0a569195 Sofia Papagiannaki
        try:
199 0a569195 Sofia Papagiannaki
            user.save()
200 0a569195 Sofia Papagiannaki
        except ValidationError, e:
201 27e26a41 Sofia Papagiannaki
            raise CommandError(e)
202 cfb15117 Sofia Papagiannaki
        
203 cfb15117 Sofia Papagiannaki
        if password:
204 cfb15117 Sofia Papagiannaki
            self.stdout.write('User\'s new password: %s\n' % password)