Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / management / commands / authpolicy-set.py @ 9577d199

History | View | Annotate | Download (3.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.db import transaction
37
from django.core.management.base import BaseCommand, CommandError
38

    
39
from astakos.im.models import AuthProviderPolicyProfile as Profile
40
from astakos.im.models import AstakosUser, Group
41

    
42
option_list = BaseCommand.option_list + (
43
    make_option('--group',
44
                action='append',
45
                dest='groups',
46
                default=[],
47
                help="Assign profile to the provided group id. Option may "
48
                     "be used more than once."),
49
    make_option('--user',
50
                action='append',
51
                dest='users',
52
                default=[],
53
                help="Assign profile to the provided user id. Option may "
54
                     "be used more than once.")
55
)
56

    
57

    
58
@transaction.commit_on_success
59
def update_profile(profile, users, groups):
60
    profile.groups.all().delete()
61
    profile.users.all().delete()
62
    profile.groups.add(*groups)
63
    profile.users.add(*users)
64

    
65

    
66
class Command(BaseCommand):
67
    args = "<name> <provider_name>"
68
    help = "Assign an existing authentication provider policy profile to " + \
69
           "a user or group. All previously set "
70
    option_list = option_list
71

    
72
    def handle(self, *args, **options):
73
        if len(args) < 1:
74
            raise CommandError("Invalid number of arguments")
75

    
76
        name = args[0].strip()
77
        try:
78
            profile = Profile.objects.get(name=name)
79
        except Profile.DoesNotExist:
80
            raise CommandError("Invalid profile name")
81

    
82
        users = []
83
        try:
84
            users = [AstakosUser.objects.get(pk=int(pk)) for pk in
85
                     options.get('users')]
86
        except AstakosUser.DoesNotExist:
87
            raise CommandError("Invalid user id")
88

    
89
        groups = []
90
        try:
91
            groups = [Group.objects.get(pk=int(pk)) for pk in
92
                      options.get('groups')]
93
        except Group.DoesNotExist:
94
            raise CommandError("Invalid group id")
95

    
96
        update_profile(profile, users, groups)