1
1
import logging
2
+ from typing import Any
2
3
3
- from django .core .exceptions import ValidationError
4
+ from django .core .exceptions import ObjectDoesNotExist , ValidationError
4
5
from django .db import IntegrityError
5
6
6
7
from django_setup_configuration .configuration import BaseConfigurationStep
7
8
from django_setup_configuration .exceptions import ConfigurationRunFailed
8
9
10
+ from objects .core .models import ObjectType
9
11
from objects .setup_configuration .models .token_auth import (
10
12
TokenAuthGroupConfigurationModel ,
11
13
)
12
- from objects .token .models import TokenAuth
14
+ from objects .token .models import Permission , TokenAuth
13
15
14
16
logger = logging .getLogger (__name__ )
15
17
@@ -18,7 +20,7 @@ class TokenAuthConfigurationStep(
18
20
BaseConfigurationStep [TokenAuthGroupConfigurationModel ]
19
21
):
20
22
"""
21
- Configure tokens for other applications to access Objects API
23
+ Configure tokens with permissions for other applications to access Objects API
22
24
"""
23
25
24
26
namespace = "tokenauth"
@@ -27,14 +29,61 @@ class TokenAuthConfigurationStep(
27
29
verbose_name = "Configuration to set up authentication tokens for objects"
28
30
config_model = TokenAuthGroupConfigurationModel
29
31
32
+ def _full_clean (self , instance : Any ) -> None :
33
+ try :
34
+ instance .full_clean (exclude = ("id" ,), validate_unique = False )
35
+ except ValidationError as exception :
36
+ raise ConfigurationRunFailed (
37
+ ("Validation error(s) during instance cleaning: %s" % type (instance ))
38
+ ) from exception
39
+
40
+ def _configure_permissions (self , token : TokenAuth , permissions : list ) -> None :
41
+ if len (permissions ) == 0 :
42
+ logger .warning ("No permissions provided for %s" , token .identifier )
43
+
44
+ for permission in permissions :
45
+ try :
46
+ permission_kwargs = {
47
+ "token_auth" : token ,
48
+ "object_type" : ObjectType .objects .get (uuid = permission .object_type ),
49
+ "mode" : permission .mode ,
50
+ "use_fields" : permission .use_fields ,
51
+ "fields" : permission .fields ,
52
+ }
53
+ except ObjectDoesNotExist as exception :
54
+ raise ConfigurationRunFailed (
55
+ ("Object type with %s does not exist" % permission .object_type )
56
+ ) from exception
57
+
58
+ permission_instance = Permission (** permission_kwargs )
59
+ self ._full_clean (permission_instance )
60
+
61
+ try :
62
+ Permission .objects .update_or_create (
63
+ token_auth = permission_kwargs ["token_auth" ],
64
+ object_type = permission_kwargs ["object_type" ],
65
+ defaults = {
66
+ "mode" : permission_kwargs ["mode" ],
67
+ "use_fields" : permission_kwargs ["use_fields" ],
68
+ "fields" : permission_kwargs ["fields" ],
69
+ },
70
+ )
71
+ except IntegrityError as exception :
72
+ raise ConfigurationRunFailed (
73
+ (
74
+ "Failed configuring permission for token %s and object type %s"
75
+ % (token .identifier , permission .object_type )
76
+ )
77
+ ) from exception
78
+
30
79
def execute (self , model : TokenAuthGroupConfigurationModel ) -> None :
31
80
if len (model .items ) == 0 :
32
81
logger .warning ("No tokens provided for configuration" )
33
82
34
83
for item in model .items :
35
- logger .info (f "Configuring { item .identifier } " )
84
+ logger .info ("Configuring %s" , item .identifier )
36
85
37
- model_kwargs = {
86
+ token_kwargs = {
38
87
"identifier" : item .identifier ,
39
88
"token" : item .token ,
40
89
"contact_person" : item .contact_person ,
@@ -45,31 +94,24 @@ def execute(self, model: TokenAuthGroupConfigurationModel) -> None:
45
94
"is_superuser" : item .is_superuser ,
46
95
}
47
96
48
- token_instance = TokenAuth (** model_kwargs )
49
-
97
+ token_instance = TokenAuth (** token_kwargs )
98
+ self . _full_clean ( token_instance )
50
99
try :
51
- token_instance .full_clean (exclude = ("id" ,), validate_unique = False )
52
- except ValidationError as exception :
53
- exception_message = (
54
- f"Validation error(s) occured for { item .identifier } ."
55
- )
56
- raise ConfigurationRunFailed (exception_message ) from exception
57
-
58
- logger .debug (f"No validation errors found for { item .identifier } " )
59
-
60
- try :
61
- logger .debug (f"Saving { item .identifier } " )
62
-
63
- TokenAuth .objects .update_or_create (
100
+ logger .debug ("Saving %s" , item .identifier )
101
+ token , _ = TokenAuth .objects .update_or_create (
64
102
identifier = item .identifier ,
65
103
defaults = {
66
104
key : value
67
- for key , value in model_kwargs .items ()
105
+ for key , value in token_kwargs .items ()
68
106
if key != "identifier"
69
107
},
70
108
)
109
+
110
+ self ._configure_permissions (token , item .permissions )
111
+
71
112
except IntegrityError as exception :
72
- exception_message = f"Failed configuring token { item .identifier } ."
73
- raise ConfigurationRunFailed (exception_message ) from exception
113
+ raise ConfigurationRunFailed (
114
+ "Failed configuring token %s" % item .identifier
115
+ ) from exception
74
116
75
- logger .info (f "Configured { item .identifier } " )
117
+ logger .info ("Configured %s" , item .identifier )
0 commit comments