Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (4.3 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
import string
35

    
36
from optparse import make_option
37

    
38
from django.core.management.base import BaseCommand, CommandError
39

    
40
from astakos.im.models import AuthProviderPolicyProfile as Profile
41

    
42
option_list = list(BaseCommand.option_list) + [
43
    make_option('--update',
44
                action='store_true',
45
                dest='update',
46
                default=False,
47
                help="Update an existing profile."),
48
    make_option('--exclusive',
49
                action='store_true',
50
                dest='exclusive',
51
                default=False,
52
                help="Apply policies to all authentication providers "
53
                     "except the one provided."),
54
]
55

    
56
POLICIES = ['add', 'remove', 'create', 'login', 'limit', 'required',
57
            'automoderate']
58

    
59
for p in POLICIES:
60
    option_list.append(make_option('--unset-%s-policy' % p,
61
                                   action='store_true',
62
                                   dest='unset_policy_%s' % p,
63
                                   help="Unset %s policy (only when --update)"
64
                                   % p.title()))
65
    option_list.append(make_option('--%s-policy' % p,
66
                                   action='store',
67
                                   dest='policy_%s' % p,
68
                                   help="%s policy" % p.title()))
69

    
70

    
71
class Command(BaseCommand):
72
    args = "<name> <provider_name>"
73
    help = "Create a new authentication provider policy profile"
74
    option_list = option_list
75

    
76
    def handle(self, *args, **options):
77
        if len(args) < 2:
78
            raise CommandError("Invalid number of arguments")
79

    
80
        profile = None
81
        update = options.get('update')
82
        name = args[0].strip()
83
        provider = args[1].strip()
84

    
85
        if Profile.objects.filter(name=name).count():
86
            if update:
87
                profile = Profile.objects.get(name=name)
88
            else:
89
                raise CommandError("Profile with the same name already exists")
90

    
91
        if not profile:
92
            profile = Profile()
93

    
94
        profile.name = name
95
        profile.provider = provider
96
        profile.is_exclusive = options.get('exclusive')
97

    
98
        for policy, value in options.iteritems():
99
            if policy.startswith('policy_') and value is not None:
100
                if isinstance(value, basestring) and value[0] in string.digits:
101
                    value = int(value)
102
                if value == 'False' or value == '0':
103
                    value = False
104
                if value == 'True' or value == '1':
105
                    value = True
106
                setattr(profile, policy, value)
107

    
108
            if update and policy.startswith('unset_'):
109
                policy = policy.replace('unset_', '')
110
                setattr(profile, policy, None)
111

    
112

    
113
        profile.save()
114
        if update:
115
            print "Profile updated"
116
        else:
117
            print "Profile stored"