Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / management / commands / user_update.py @ 304acb60

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