root / snf-astakos-app / astakos / im / management / commands / modifyuser.py @ 18ffbee1
History | View | Annotate | Download (4.5 kB)
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 |
|
39 |
from ._common import get_user |
40 |
|
41 |
|
42 |
class Command(BaseCommand): |
43 |
args = "<user ID or email>"
|
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('--group',
|
85 |
dest='group',
|
86 |
help="Extend user groups"),
|
87 |
) |
88 |
|
89 |
def handle(self, *args, **options): |
90 |
if len(args) != 1: |
91 |
raise CommandError("Please provide a user ID or email") |
92 |
|
93 |
user = get_user(args[0])
|
94 |
if not user: |
95 |
raise CommandError("Unknown user") |
96 |
|
97 |
if options.get('admin'): |
98 |
user.is_superuser = True
|
99 |
elif options.get('noadmin'): |
100 |
user.is_superuser = False
|
101 |
|
102 |
if options.get('active'): |
103 |
user.is_active = True
|
104 |
elif options.get('inactive'): |
105 |
user.is_active = False
|
106 |
|
107 |
invitations = options.get('invitations')
|
108 |
if invitations is not None: |
109 |
user.invitations = int(invitations)
|
110 |
|
111 |
groupname = options.get('group')
|
112 |
if groupname is not None: |
113 |
try:
|
114 |
group = Group.objects.get(name=groupname) |
115 |
user.groups.add(group) |
116 |
except Group.DoesNotExist, e:
|
117 |
raise CommandError("Group named %s does not exist." % groupname) |
118 |
|
119 |
level = options.get('level')
|
120 |
if level is not None: |
121 |
user.level = int(level)
|
122 |
|
123 |
password = options.get('password')
|
124 |
if password is not None: |
125 |
user.set_password(password) |
126 |
|
127 |
if options['renew_token']: |
128 |
user.renew_token() |
129 |
|
130 |
user.save() |