Revision 52cdfec7

/dev/null
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
from random import choice
36
from string import digits, lowercase, uppercase
37
from uuid import uuid4
38
from time import time
39
from os.path import abspath
40

  
41
from django.core.management.base import BaseCommand, CommandError
42
from django.contrib.auth.models import Group
43

  
44
from ._common import add_group_permission
45

  
46
class Command(BaseCommand):
47
    args = "<groupname> [<permission> ...]"
48
    help = "Insert group"
49
    
50
    def handle(self, *args, **options):
51
        if len(args) < 1:
52
            raise CommandError("Invalid number of arguments")
53
        
54
        name = args[0].decode('utf8')
55
        
56
        try:
57
            Group.objects.get(name=name)
58
            raise CommandError("A group with this name already exists")
59
        except Group.DoesNotExist, e:
60
            group = Group(name=name)
61
            group.save()
62
            msg = "Created group id %d" % (group.id,)
63
            self.stdout.write(msg + '\n')
64
            try:
65
                for pname in args[1:]:
66
                    r, created = add_group_permission(group, pname)
67
                    if created:
68
                        self.stdout.write('Permission: %s created successfully\n' % pname)
69
                    if r == 0:
70
                        self.stdout.write('Group has already permission: %s\n' % pname)
71
                    else:
72
                        self.stdout.write('Permission: %s added successfully\n' % pname)
73
            except Exception, e:
74
                raise CommandError(e)
/dev/null
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, Permission
38
from django.contrib.contenttypes.models import ContentType
39
from django.core.exceptions import ValidationError
40

  
41
from astakos.im.models import AstakosUser
42
from ._common import add_group_permission
43

  
44
class Command(BaseCommand):
45
    args = "<groupname> <permission> [<permissions> ...]"
46
    help = "Add group permissions"
47
    
48
    def handle(self, *args, **options):
49
        if len(args) < 2:
50
            raise CommandError("Please provide a group name and at least one permission")
51
        
52
        group = None
53
        try:
54
            if args[0].isdigit():
55
                group = Group.objects.get(id=args[0])
56
            else:
57
                group = Group.objects.get(name=args[0])
58
        except Group.DoesNotExist, e:
59
            raise CommandError("Invalid group")
60
        
61
        try:
62
            content_type = ContentType.objects.get(app_label='im',
63
                                                       model='astakosuser')
64
            for pname in args[1:]:
65
                r, created = add_group_permission(group, pname)
66
                if created:
67
                    self.stdout.write('Permission: %s created successfully\n' % pname)
68
                if r == 0:
69
                    self.stdout.write('Group has already permission: %s\n' % pname)
70
                else:
71
                    self.stdout.write('Permission: %s added successfully\n' % pname)
72
        except Exception, e:
73
            raise CommandError(e)
/dev/null
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
from random import choice
36
from string import digits, lowercase, uppercase
37
from uuid import uuid4
38
from time import time
39
from os.path import abspath
40

  
41
from django.core.management.base import BaseCommand, CommandError
42

  
43
from astakos.im.models import ApprovalTerms
44

  
45
class Command(BaseCommand):
46
    args = "<location>"
47
    help = "Insert approval terms"
48
    
49
    def handle(self, *args, **options):
50
        if len(args) != 1:
51
            raise CommandError("Invalid number of arguments")
52
        
53
        location = abspath(args[0].decode('utf8'))
54
        try:
55
            f = open(location, 'r')
56
        except IOError:
57
            raise CommandError("Invalid location")
58
        
59
        terms = ApprovalTerms(location=location)
60
        terms.save()
61
        
62
        msg = "Created term id %d" % (terms.id,)
63
        self.stdout.write(msg + '\n')
/dev/null
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
import socket
35

  
36
from optparse import make_option
37
from random import choice
38
from string import digits, lowercase, uppercase
39
from uuid import uuid4
40

  
41
from django.core.management.base import BaseCommand, CommandError
42
from django.core.validators import validate_email
43
from django.core.exceptions import ValidationError
44
from django.contrib.auth.models import Group
45

  
46
from astakos.im.models import AstakosUser
47
from astakos.im.util import reserved_email
48

  
49
from ._common import add_user_permission
50

  
51
class Command(BaseCommand):
52
    args = "<email> <first name> <last name> <affiliation>"
53
    help = "Create a user"
54
    
55
    option_list = BaseCommand.option_list + (
56
        make_option('--active',
57
            action='store_true',
58
            dest='active',
59
            default=False,
60
            help="Activate user"),
61
        make_option('--admin',
62
            action='store_true',
63
            dest='admin',
64
            default=False,
65
            help="Give user admin rights"),
66
        make_option('--password',
67
            dest='password',
68
            metavar='PASSWORD',
69
            help="Set user's password"),
70
        make_option('--add-group',
71
            dest='add-group',
72
            help="Add user group"),
73
        make_option('--add-permission',
74
            dest='add-permission',
75
            help="Add user permission")
76
        )
77
    
78
    def handle(self, *args, **options):
79
        if len(args) != 4:
80
            raise CommandError("Invalid number of arguments")
81
        
82
        args = [a.decode('utf8') for a in args]
83
        email, first, last, affiliation = args
84
        
85
        try:
86
            validate_email( email )
87
        except ValidationError:
88
            raise CommandError("Invalid email")
89
        
90
        username =  uuid4().hex[:30]
91
        password = options.get('password')
92
        if password is None:
93
            password = AstakosUser.objects.make_random_password()
94
        
95
        if reserved_email(email):
96
            raise CommandError("A user with this email already exists")
97
        
98
        user = AstakosUser(username=username, first_name=first, last_name=last,
99
                           email=email, affiliation=affiliation,
100
                           provider='local')
101
        user.set_password(password)
102
        user.renew_token()
103
        
104
        if options['active']:
105
            user.is_active = True
106
        if options['admin']:
107
            user.is_admin = True
108
        
109
        try:
110
            user.save()
111
        except socket.error, e:
112
            raise CommandError(e)
113
        except ValidationError, e:
114
            raise CommandError(e)
115
        else:
116
            msg = "Created user id %d" % (user.id,)
117
            if options['password'] is None:
118
                msg += " with password '%s'" % (password,)
119
            self.stdout.write(msg + '\n')
120
            
121
            groupname = options.get('add-group')
122
            if groupname is not None:
123
                try:
124
                    group = Group.objects.get(name=groupname)
125
                    user.groups.add(group)
126
                    self.stdout.write('Group: %s added successfully\n' % groupname)
127
                except Group.DoesNotExist, e:
128
                    self.stdout.write('Group named %s does not exist\n' % groupname)
129
            
130
            pname = options.get('add-permission')
131
            if pname is not None:
132
                try:
133
                    r, created = add_user_permission(user, pname)
134
                    if created:
135
                        self.stdout.write('Permission: %s created successfully\n' % pname)
136
                    if r > 0:
137
                        self.stdout.write('Permission: %s added successfully\n' % pname)
138
                    elif r==0:
139
                        self.stdout.write('User has already permission: %s\n' % pname)
140
                except Exception, e:
141
                    raise CommandError(e)
b/snf-astakos-app/astakos/im/management/commands/group_add.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
from random import choice
36
from string import digits, lowercase, uppercase
37
from uuid import uuid4
38
from time import time
39
from os.path import abspath
40

  
41
from django.core.management.base import BaseCommand, CommandError
42
from django.contrib.auth.models import Group
43

  
44
from ._common import add_group_permission
45

  
46
class Command(BaseCommand):
47
    args = "<groupname> [<permission> ...]"
48
    help = "Insert group"
49
    
50
    def handle(self, *args, **options):
51
        if len(args) < 1:
52
            raise CommandError("Invalid number of arguments")
53
        
54
        name = args[0].decode('utf8')
55
        
56
        try:
57
            Group.objects.get(name=name)
58
            raise CommandError("A group with this name already exists")
59
        except Group.DoesNotExist, e:
60
            group = Group(name=name)
61
            group.save()
62
            msg = "Created group id %d" % (group.id,)
63
            self.stdout.write(msg + '\n')
64
            try:
65
                for pname in args[1:]:
66
                    r, created = add_group_permission(group, pname)
67
                    if created:
68
                        self.stdout.write('Permission: %s created successfully\n' % pname)
69
                    if r == 0:
70
                        self.stdout.write('Group has already permission: %s\n' % pname)
71
                    else:
72
                        self.stdout.write('Permission: %s added successfully\n' % pname)
73
            except Exception, e:
74
                raise CommandError(e)
b/snf-astakos-app/astakos/im/management/commands/group_add_permissions.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, Permission
38
from django.contrib.contenttypes.models import ContentType
39
from django.core.exceptions import ValidationError
40

  
41
from astakos.im.models import AstakosUser
42
from ._common import add_group_permission
43

  
44
class Command(BaseCommand):
45
    args = "<groupname> <permission> [<permissions> ...]"
46
    help = "Add group permissions"
47
    
48
    def handle(self, *args, **options):
49
        if len(args) < 2:
50
            raise CommandError("Please provide a group name and at least one permission")
51
        
52
        group = None
53
        try:
54
            if args[0].isdigit():
55
                group = Group.objects.get(id=args[0])
56
            else:
57
                group = Group.objects.get(name=args[0])
58
        except Group.DoesNotExist, e:
59
            raise CommandError("Invalid group")
60
        
61
        try:
62
            content_type = ContentType.objects.get(app_label='im',
63
                                                       model='astakosuser')
64
            for pname in args[1:]:
65
                r, created = add_group_permission(group, pname)
66
                if created:
67
                    self.stdout.write('Permission: %s created successfully\n' % pname)
68
                if r == 0:
69
                    self.stdout.write('Group has already permission: %s\n' % pname)
70
                else:
71
                    self.stdout.write('Permission: %s added successfully\n' % pname)
72
        except Exception, e:
73
            raise CommandError(e)
b/snf-astakos-app/astakos/im/management/commands/group_list.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

  
39
from astakos.im.models import AstakosUser
40

  
41
from ._common import format_bool
42

  
43

  
44
class Command(BaseCommand):
45
    help = "List g"
46
    
47
    option_list = BaseCommand.option_list + (
48
        make_option('-c',
49
            action='store_true',
50
            dest='csv',
51
            default=False,
52
            help="Use pipes to separate values"),
53
    )
54
    
55
    def handle(self, *args, **options):
56
        if args:
57
            raise CommandError("Command doesn't accept any arguments")
58
        
59
        groups = Group.objects.all()
60
        
61
        labels = ('id', 'name', 'permissions')
62
        columns = (3, 12, 50)
63
        
64
        if not options['csv']:
65
            line = ' '.join(l.rjust(w) for l, w in zip(labels, columns))
66
            self.stdout.write(line + '\n')
67
            sep = '-' * len(line)
68
            self.stdout.write(sep + '\n')
69
        
70
        for group in groups:
71
            fields = (str(group.id), group.name,
72
                      ','.join(p.codename for p in group.permissions.all()))
73
            
74
            if options['csv']:
75
                line = '|'.join(fields)
76
            else:
77
                line = ' '.join(f.rjust(w) for f, w in zip(fields, columns))
78
            
79
            self.stdout.write(line.encode('utf8') + '\n')
b/snf-astakos-app/astakos/im/management/commands/group_remove_permissions.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
from ._common import remove_group_permission
42

  
43
class Command(BaseCommand):
44
    args = "<groupname> <permission> [<permissions> ...]"
45
    help = "Remove group permissions"
46
    
47
    def handle(self, *args, **options):
48
        if len(args) < 2:
49
            raise CommandError("Please provide a group name and at least one permission")
50
        
51
        group = None
52
        try:
53
            if args[0].isdigit():
54
                group = Group.objects.get(id=args[0])
55
            else:
56
                group = Group.objects.get(name=args[0])
57
        except Group.DoesNotExist, e:
58
            raise CommandError("Invalid group")
59
        
60
        try:
61
            for pname in args[1:]:
62
                r = remove_group_permission(group, pname)
63
                if r < 0:
64
                    self.stdout.write('Invalid permission codename: %s\n' % pname)
65
                elif r == 0:
66
                    self.stdout.write('Group has not permission: %s\n' % pname)
67
                elif r > 0:
68
                    self.stdout.write('Permission: %s removed successfully\n' % pname)
69
        except Exception, e:
70
            raise CommandError(e)
b/snf-astakos-app/astakos/im/management/commands/invitation_list.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

  
38
from astakos.im.models import Invitation
39

  
40
from ._common import format_bool
41

  
42

  
43
class Command(BaseCommand):
44
    help = "List invitations"
45
    
46
    option_list = BaseCommand.option_list + (
47
        make_option('-c',
48
            action='store_true',
49
            dest='csv',
50
            default=False,
51
            help="Use pipes to separate values"),
52
        )
53
    
54
    def handle(self, *args, **options):
55
        if args:
56
            raise CommandError("Command doesn't accept any arguments")
57
        
58
        invitations = Invitation.objects.all()
59
        
60
        labels = ('id', 'inviter', 'email', 'real name', 'code', 'consumed')
61
        columns = (3, 24, 24, 24, 20, 4, 8)
62
        
63
        if not options['csv']:
64
            line = ' '.join(l.rjust(w) for l, w in zip(labels, columns))
65
            self.stdout.write(line + '\n')
66
            sep = '-' * len(line)
67
            self.stdout.write(sep + '\n')
68
        
69
        for invitation in invitations:
70
            id = str(invitation.id)
71
            code = str(invitation.code)
72
            consumed = format_bool(invitation.is_consumed)
73
            fields = (id, invitation.inviter.email, invitation.username, invitation.realname,
74
                      code, consumed)
75
            
76
            if options['csv']:
77
                line = '|'.join(fields)
78
            else:
79
                line = ' '.join(f.rjust(w) for f, w in zip(fields, columns))
80
            
81
            self.stdout.write(line.encode('utf8') + '\n')
b/snf-astakos-app/astakos/im/management/commands/invitation_show.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 django.core.management.base import BaseCommand, CommandError
35

  
36
from astakos.im.models import Invitation
37

  
38
from ._common import format_bool, format_date
39

  
40

  
41
class Command(BaseCommand):
42
    args = "<invitation ID>"
43
    help = "Show invitation info"
44
    
45
    def handle(self, *args, **options):
46
        if len(args) != 1:
47
            raise CommandError("Please provide an invitation id")
48
        
49
        try:
50
            invitation = Invitation.objects.get(id=int(args[0]))
51
        except Invitation.DoesNotExist:
52
            raise CommandError("Unknown invitation id '%s'" % (args[0],))
53
        
54
        kv = {
55
            'id': invitation.id,
56
            'real name': invitation.realname,
57
            'email': invitation.username,
58
            'code': invitation.code,
59
            'consumed': format_bool(invitation.is_consumed),
60
            'date created': format_date(invitation.created),
61
            'date consumed': format_date(invitation.consumed),
62
            'inviter real name': invitation.inviter.realname,
63
            'invitater email': invitation.inviter.email,
64
        }
65
        
66
        for key, val in sorted(kv.items()):
67
            line = '%s: %s\n' % (key.rjust(18), val)
68
            self.stdout.write(line.encode('utf8'))
/dev/null
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
import socket
35

  
36
from django.core.management.base import BaseCommand, CommandError
37
from django.db.utils import IntegrityError
38
from django.db import transaction
39

  
40
from astakos.im.functions import invite, SendMailError
41
from astakos.im.models import Invitation
42

  
43
from ._common import get_user
44

  
45
@transaction.commit_manually
46
class Command(BaseCommand):
47
    args = "<inviter id or email> <email> <real name>"
48
    help = "Invite a user"
49
    
50
    def handle(self, *args, **options):
51
        if len(args) != 3:
52
            raise CommandError("Invalid number of arguments")
53
        
54
        inviter = get_user(args[0], is_active=True)
55
        if not inviter:
56
            raise CommandError("Unknown inviter")
57
        if  not inviter.is_active:
58
            raise CommandError("Inactive inviter")
59
        
60
        if inviter.invitations > 0:
61
            email = args[1]
62
            realname = args[2]
63
            
64
            try:
65
                invitation = Invitation(username = email, realname=realname, inviter=inviter)
66
                invite(invitation, inviter)
67
                self.stdout.write("Invitation sent to '%s'\n" % (email,))
68
            except SendMailError, e:
69
                transaction.rollback()
70
                raise CommandError(e.message)
71
            except IntegrityError, e:
72
                transaction.rollback()
73
                raise CommandError("There is already an invitation for %s" % (email,))
74
            else:
75
                transaction.commit()
76
        else:
77
            raise CommandError("No invitations left")
/dev/null
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 astakos.im.models import AstakosUser
40

  
41
from ._common import format_bool
42

  
43

  
44
class Command(BaseCommand):
45
    help = "List g"
46
    
47
    option_list = BaseCommand.option_list + (
48
        make_option('-c',
49
            action='store_true',
50
            dest='csv',
51
            default=False,
52
            help="Use pipes to separate values"),
53
    )
54
    
55
    def handle(self, *args, **options):
56
        if args:
57
            raise CommandError("Command doesn't accept any arguments")
58
        
59
        groups = Group.objects.all()
60
        
61
        labels = ('id', 'name', 'permissions')
62
        columns = (3, 12, 50)
63
        
64
        if not options['csv']:
65
            line = ' '.join(l.rjust(w) for l, w in zip(labels, columns))
66
            self.stdout.write(line + '\n')
67
            sep = '-' * len(line)
68
            self.stdout.write(sep + '\n')
69
        
70
        for group in groups:
71
            fields = (str(group.id), group.name,
72
                      ','.join(p.codename for p in group.permissions.all()))
73
            
74
            if options['csv']:
75
                line = '|'.join(fields)
76
            else:
77
                line = ' '.join(f.rjust(w) for f, w in zip(fields, columns))
78
            
79
            self.stdout.write(line.encode('utf8') + '\n')
/dev/null
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

  
38
from astakos.im.models import Invitation
39

  
40
from ._common import format_bool
41

  
42

  
43
class Command(BaseCommand):
44
    help = "List invitations"
45
    
46
    option_list = BaseCommand.option_list + (
47
        make_option('-c',
48
            action='store_true',
49
            dest='csv',
50
            default=False,
51
            help="Use pipes to separate values"),
52
        )
53
    
54
    def handle(self, *args, **options):
55
        if args:
56
            raise CommandError("Command doesn't accept any arguments")
57
        
58
        invitations = Invitation.objects.all()
59
        
60
        labels = ('id', 'inviter', 'email', 'real name', 'code', 'consumed')
61
        columns = (3, 24, 24, 24, 20, 4, 8)
62
        
63
        if not options['csv']:
64
            line = ' '.join(l.rjust(w) for l, w in zip(labels, columns))
65
            self.stdout.write(line + '\n')
66
            sep = '-' * len(line)
67
            self.stdout.write(sep + '\n')
68
        
69
        for invitation in invitations:
70
            id = str(invitation.id)
71
            code = str(invitation.code)
72
            consumed = format_bool(invitation.is_consumed)
73
            fields = (id, invitation.inviter.email, invitation.username, invitation.realname,
74
                      code, consumed)
75
            
76
            if options['csv']:
77
                line = '|'.join(fields)
78
            else:
79
                line = ' '.join(f.rjust(w) for f, w in zip(fields, columns))
80
            
81
            self.stdout.write(line.encode('utf8') + '\n')
/dev/null
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

  
38
from astakos.im.models import Service
39

  
40
class Command(BaseCommand):
41
    help = "List g"
42

  
43
    option_list = BaseCommand.option_list + (
44
        make_option('-c',
45
            action='store_true',
46
            dest='csv',
47
            default=False,
48
            help="Use pipes to separate values"),
49
    )
50

  
51
    def handle(self, *args, **options):
52
        if args:
53
            raise CommandError("Command doesn't accept any arguments")
54

  
55
        services = Service.objects.all()
56

  
57
        labels = ('id', 'name', 'url', 'auth_token', 'icon')
58
        columns = (3, 12, 40, 20, 20)
59

  
60
        if not options['csv']:
61
            line = ' '.join(l.rjust(w) for l, w in zip(labels, columns))
62
            self.stdout.write(line + '\n')
63
            sep = '-' * len(line)
64
            self.stdout.write(sep + '\n')
65

  
66
        for service in services:
67
            fields = (str(service.id), service.name, service.url,
68
                    service.auth_token,
69
                    service.icon)
70

  
71
            if options['csv']:
72
                line = '|'.join(fields)
73
            else:
74
                line = ' '.join(f.rjust(w) for f, w in zip(fields, columns))
75

  
76
            self.stdout.write(line.encode('utf8') + '\n')
/dev/null
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

  
38
from astakos.im.models import AstakosUser
39

  
40
from ._common import format_bool
41

  
42

  
43
class Command(BaseCommand):
44
    help = "List users"
45
    
46
    option_list = BaseCommand.option_list + (
47
        make_option('-c',
48
            action='store_true',
49
            dest='csv',
50
            default=False,
51
            help="Use pipes to separate values"),
52
        make_option('-p',
53
            action='store_true',
54
            dest='pending',
55
            default=False,
56
            help="List only users pending activation"),
57
        make_option('-n',
58
            action='store_true',
59
            dest='pending_send_mail',
60
            default=False,
61
            help="List only users who have not received activation"),
62
        )
63
    
64
    def handle(self, *args, **options):
65
        if args:
66
            raise CommandError("Command doesn't accept any arguments")
67
        
68
        users = AstakosUser.objects.all()
69
        if options['pending']:
70
            users = users.filter(is_active=False)
71
        elif options['pending_send_mail']:
72
            users = users.filter(is_active=False, activation_sent=None)
73
        
74
        labels = ('id', 'email', 'real name', 'active', 'admin', 'provider', 'groups')
75
        columns = (3, 24, 24, 6, 5, 12, 24)
76
        
77
        if not options['csv']:
78
            line = ' '.join(l.rjust(w) for l, w in zip(labels, columns))
79
            self.stdout.write(line + '\n')
80
            sep = '-' * len(line)
81
            self.stdout.write(sep + '\n')
82
        
83
        for user in users:
84
            id = str(user.id)
85
            active = format_bool(user.is_active)
86
            admin = format_bool(user.is_superuser)
87
            fields = (id, user.email, user.realname, active, admin, user.provider,
88
                      ','.join([g.name for g in user.groups.all()]))
89
            
90
            if options['csv']:
91
                line = '|'.join(fields)
92
            else:
93
                line = ' '.join(f.rjust(w) for f, w in zip(fields, columns))
94
            
95
            self.stdout.write(line.encode('utf8') + '\n')
/dev/null
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, Permission
38
from django.contrib.contenttypes.models import ContentType
39
from django.core.exceptions import ValidationError
40

  
41
from astakos.im.models import AstakosUser
42
from ._common import remove_user_permission, add_user_permission
43

  
44
class Command(BaseCommand):
45
    args = "<user ID>"
46
    help = "Modify a user's attributes"
47
    
48
    option_list = BaseCommand.option_list + (
49
        make_option('--invitations',
50
            dest='invitations',
51
            metavar='NUM',
52
            help="Update user's invitations"),
53
        make_option('--level',
54
            dest='level',
55
            metavar='NUM',
56
            help="Update user's level"),
57
        make_option('--password',
58
            dest='password',
59
            metavar='PASSWORD',
60
            help="Set user's password"),
61
        make_option('--provider',
62
            dest='provider',
63
            metavar='PROVIDER',
64
            help="Set user's provider"),
65
        make_option('--renew-token',
66
            action='store_true',
67
            dest='renew_token',
68
            default=False,
69
            help="Renew the user's token"),
70
        make_option('--renew-password',
71
            action='store_true',
72
            dest='renew_password',
73
            default=False,
74
            help="Renew the user's password"),
75
        make_option('--set-admin',
76
            action='store_true',
77
            dest='admin',
78
            default=False,
79
            help="Give user admin rights"),
80
        make_option('--set-noadmin',
81
            action='store_true',
82
            dest='noadmin',
83
            default=False,
84
            help="Revoke user's admin rights"),
85
        make_option('--set-active',
86
            action='store_true',
87
            dest='active',
88
            default=False,
89
            help="Change user's state to inactive"),
90
        make_option('--set-inactive',
91
            action='store_true',
92
            dest='inactive',
93
            default=False,
94
            help="Change user's state to inactive"),
95
        make_option('--add-group',
96
            dest='add-group',
97
            help="Add user group"),
98
        make_option('--delete-group',
99
            dest='delete-group',
100
            help="Delete user group"),
101
        make_option('--add-permission',
102
            dest='add-permission',
103
            help="Add user permission"),
104
        make_option('--delete-permission',
105
            dest='delete-permission',
106
            help="Delete user permission"),
107
        )
108
    
109
    def handle(self, *args, **options):
110
        if len(args) != 1:
111
            raise CommandError("Please provide a user ID")
112
        
113
        if args[0].isdigit():
114
            user = AstakosUser.objects.get(id=int( args[0]))
115
        else:
116
            raise CommandError("Invalid ID")
117
        
118
        if not user:
119
            raise CommandError("Unknown user")
120
        
121
        if options.get('admin'):
122
            user.is_superuser = True
123
        elif options.get('noadmin'):
124
            user.is_superuser = False
125
        
126
        if options.get('active'):
127
            user.is_active = True
128
        elif options.get('inactive'):
129
            user.is_active = False
130
        
131
        invitations = options.get('invitations')
132
        if invitations is not None:
133
            user.invitations = int(invitations)
134
        
135
        groupname = options.get('add-group')
136
        if groupname is not None:
137
            try:
138
                group = Group.objects.get(name=groupname)
139
                user.groups.add(group)
140
            except Group.DoesNotExist, e:
141
                self.stdout.write("Group named %s does not exist\n" % groupname)
142
        
143
        groupname = options.get('delete-group')
144
        if groupname is not None:
145
            try:
146
                group = Group.objects.get(name=groupname)
147
                user.groups.remove(group)
148
            except Group.DoesNotExist, e:
149
                self.stdout.write("Group named %s does not exist\n" % groupname)
150
        
151
        pname = options.get('add-permission')
152
        if pname is not None:
153
            try:
154
                r, created = add_user_permission(user, pname)
155
                if created:
156
                    self.stdout.write('Permission: %s created successfully\n' % pname)
157
                if r > 0:
158
                    self.stdout.write('Permission: %s added successfully\n' % pname)
159
                elif r==0:
160
                    self.stdout.write('User has already permission: %s\n' % pname)
161
            except Exception, e:
162
                raise CommandError(e)
163
        
164
        pname  = options.get('delete-permission')
165
        if pname is not None and not user.has_perm(pname):
166
            try:
167
                r = remove_user_permission(user, pname)
168
                if r < 0:
169
                    self.stdout.write('Invalid permission codename: %s\n' % pname)
170
                elif r == 0:
171
                    self.stdout.write('User has not permission: %s\n' % pname)
172
                elif r > 0:
173
                    self.stdout.write('Permission: %s removed successfully\n' % pname)
174
            except Exception, e:
175
                raise CommandError(e)
176
        
177
        level = options.get('level')
178
        if level is not None:
179
            user.level = int(level)
180
        
181
        password = options.get('password')
182
        if password is not None:
183
            user.set_password(password)
184
        
185
        provider = options.get('provider')
186
        if provider is not None:
187
            user.provider = provider
188
        
189
        
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff