0
# Web Views
1
2
Flask-AppBuilder view classes for web interface integration, providing customized security views that integrate with Airflow's permission model and web interface. These views handle the web UI for user, role, and permission management.
3
4
## Capabilities
5
6
### User Management Views
7
8
Customized user model views for different authentication types with Airflow-specific permission mappings.
9
10
```python { .api }
11
class CustomUserDBModelView(MultiResourceUserMixin, UserDBModelView):
12
"""
13
Customize permission names for FAB's builtin UserDBModelView.
14
Handles database-authenticated users with password management capabilities.
15
16
Attributes:
17
- class_permission_name: Permission resource name mapping
18
- method_permission_name: HTTP method to permission mapping
19
- base_permissions: List of base permissions required
20
"""
21
22
class CustomUserLDAPModelView(MultiResourceUserMixin, UserLDAPModelView):
23
"""
24
Customize permission names for FAB's builtin UserLDAPModelView.
25
Handles LDAP-authenticated users without password management.
26
"""
27
28
class CustomUserOAuthModelView(MultiResourceUserMixin, UserOAuthModelView):
29
"""
30
Customize permission names for FAB's builtin UserOAuthModelView.
31
Handles OAuth-authenticated users.
32
"""
33
34
class CustomUserOIDModelView(MultiResourceUserMixin, UserOIDModelView):
35
"""
36
Customize permission names for FAB's builtin UserOIDModelView.
37
Handles OpenID-authenticated users.
38
"""
39
40
class CustomUserRemoteUserModelView(MultiResourceUserMixin, UserRemoteUserModelView):
41
"""
42
Customize permission names for FAB's builtin UserRemoteUserModelView.
43
Handles remote user authentication (e.g., from web server).
44
"""
45
```
46
47
### Multi-Resource User Mixin
48
49
Base mixin providing dynamic permission mapping for user views based on request context.
50
51
```python { .api }
52
class MultiResourceUserMixin:
53
"""
54
Remaps UserModelView permissions to new resources and actions.
55
Provides dynamic permission name resolution based on the current request.
56
57
Attributes:
58
- _class_permission_name: Base permission resource name
59
- class_permission_name_mapping: Mapping of methods to permission resources
60
- method_permission_name: HTTP method to permission action mapping
61
- base_permissions: List of base permissions required
62
"""
63
64
@property
65
def class_permission_name(self) -> str:
66
"""
67
Returns appropriate permission name depending on request method name.
68
Dynamically resolves permission resource based on current request context.
69
"""
70
71
@class_permission_name.setter
72
def class_permission_name(self, name: str) -> None:
73
"""Set the class permission name."""
74
75
@expose("/show/<pk>", methods=["GET"])
76
@has_access
77
def show(self, pk):
78
"""
79
Custom show method that removes userinfoedit action from template.
80
Displays user details with appropriate action buttons.
81
"""
82
```
83
84
### Role Management Views
85
86
Customized role management view with Airflow-specific permissions.
87
88
```python { .api }
89
class CustomRoleModelView(RoleModelView):
90
"""
91
Customize permission names for FAB's builtin RoleModelView.
92
Manages roles with custom permission mappings for Airflow integration.
93
94
Attributes:
95
- class_permission_name: RESOURCE_ROLE
96
- method_permission_name: Method to permission mappings
97
- base_permissions: CRUD permissions for role management
98
"""
99
```
100
101
### Permission and Action Views
102
103
Views for managing permissions, actions, and resources in the security system.
104
105
```python { .api }
106
class ActionModelView(PermissionModelView):
107
"""
108
Customize permission names for FAB's builtin PermissionModelView.
109
Manages security actions (can_read, can_edit, etc.).
110
111
Attributes:
112
- class_permission_name: RESOURCE_ACTION
113
- route_base: "/actions"
114
- method_permission_name: Method mappings
115
- base_permissions: Read permission for action management
116
- list_title: "List Actions"
117
- label_columns: Column label mappings
118
"""
119
120
class PermissionPairModelView(PermissionViewModelView):
121
"""
122
Customize permission names for FAB's builtin PermissionViewModelView.
123
Manages permission pairs (action-resource combinations).
124
125
Attributes:
126
- class_permission_name: RESOURCE_PERMISSION
127
- route_base: "/permissions"
128
- list_columns: ["action", "resource"]
129
- label_columns: Action and resource label mappings
130
"""
131
132
class ResourceModelView(ViewMenuModelView):
133
"""
134
Customize permission names for FAB's builtin ViewMenuModelView.
135
Manages security resources (Users, DAGs, etc.).
136
137
Attributes:
138
- class_permission_name: RESOURCE_RESOURCE
139
- route_base: "/resources"
140
- list_title: "List Resources"
141
- label_columns: Resource name label mappings
142
"""
143
```
144
145
### Password Management Views
146
147
Views for password reset and user profile management functionality.
148
149
```python { .api }
150
class CustomResetMyPasswordView(ResetMyPasswordView):
151
"""
152
Customize permission names for FAB's builtin ResetMyPasswordView.
153
Allows users to reset their own passwords.
154
155
Attributes:
156
- class_permission_name: RESOURCE_MY_PASSWORD
157
- method_permission_name: GET/POST method mappings
158
- base_permissions: Read and edit permissions
159
"""
160
161
class CustomResetPasswordView(ResetPasswordView):
162
"""
163
Customize permission names for FAB's builtin ResetPasswordView.
164
Allows administrators to reset user passwords.
165
166
Attributes:
167
- class_permission_name: RESOURCE_PASSWORD
168
- method_permission_name: GET/POST method mappings
169
- base_permissions: Read and edit permissions
170
"""
171
172
class CustomUserInfoEditView(UserInfoEditView):
173
"""
174
Customize permission names for FAB's builtin UserInfoEditView.
175
Allows users to edit their own profile information.
176
177
Attributes:
178
- class_permission_name: RESOURCE_MY_PROFILE
179
- route_base: "/userinfoeditview"
180
- method_permission_name: GET/POST method mappings
181
- base_permissions: Read and edit permissions
182
"""
183
```
184
185
### User Statistics View
186
187
View for displaying user statistics and charts.
188
189
```python { .api }
190
class CustomUserStatsChartView(UserStatsChartView):
191
"""
192
Customize permission names for FAB's builtin UserStatsChartView.
193
Displays user statistics and login charts.
194
195
Attributes:
196
- class_permission_name: RESOURCE_USER_STATS_CHART
197
- route_base: "/userstatschartview"
198
- method_permission_name: Chart and list method mappings
199
- base_permissions: Read permission for statistics access
200
"""
201
```
202
203
## Permission Resource Constants
204
205
The views use permission resources defined in `airflow.security.permissions`:
206
207
```python { .api }
208
# Resource Constants
209
RESOURCE_ACTION = "Permissions"
210
RESOURCE_MY_PASSWORD = "My Password"
211
RESOURCE_MY_PROFILE = "My Profile"
212
RESOURCE_PASSWORD = "Passwords"
213
RESOURCE_PERMISSION = "Permission Views"
214
RESOURCE_RESOURCE = "View Menus"
215
RESOURCE_ROLE = "Roles"
216
RESOURCE_USER = "Users"
217
RESOURCE_USER_STATS_CHART = "User Stats Chart"
218
219
# Action Constants
220
ACTION_CAN_CREATE = "can_create"
221
ACTION_CAN_READ = "can_read"
222
ACTION_CAN_EDIT = "can_edit"
223
ACTION_CAN_DELETE = "can_delete"
224
```
225
226
## Usage Examples
227
228
### Integrating Views with Security Manager
229
230
```python
231
from airflow.www.fab_security.views import (
232
CustomUserDBModelView,
233
CustomRoleModelView,
234
ActionModelView
235
)
236
from flask_appbuilder import AppBuilder
237
238
# Views are automatically registered by the security manager
239
security_manager = SecurityManager(app_builder)
240
241
# Access configured views
242
user_view = security_manager.user_view
243
role_view = app_builder.find_view_by_name("List Roles")
244
```
245
246
### Custom View Configuration
247
248
```python
249
# Views have customizable titles and labels
250
class MyCustomUserView(CustomUserDBModelView):
251
list_title = "Company Users"
252
show_title = "User Details"
253
edit_title = "Modify User"
254
255
# Custom column labels
256
label_columns = {
257
'username': 'Login Name',
258
'email': 'Email Address',
259
'first_name': 'Given Name'
260
}
261
```
262
263
### Permission Method Mapping
264
265
```python
266
# Views map HTTP methods to permission actions
267
method_permission_name = {
268
'list': 'read', # GET /users -> requires 'can_read' on 'Users'
269
'show': 'read', # GET /users/1 -> requires 'can_read' on 'Users'
270
'add': 'create', # POST /users -> requires 'can_create' on 'Users'
271
'edit': 'edit', # PUT /users/1 -> requires 'can_edit' on 'Users'
272
'delete': 'delete', # DELETE /users/1 -> requires 'can_delete' on 'Users'
273
}
274
```
275
276
### Dynamic Permission Resolution
277
278
```python
279
# MultiResourceUserMixin provides context-aware permissions
280
class ExampleView(MultiResourceUserMixin):
281
class_permission_name_mapping = {
282
'userinfo': RESOURCE_MY_PROFILE, # User viewing own profile
283
'userinfoedit': RESOURCE_MY_PROFILE, # User editing own profile
284
'resetpassword': RESOURCE_PASSWORD, # Admin resetting passwords
285
}
286
287
# Permission resource changes based on the action being performed
288
```
289
290
### View Registration
291
292
```python
293
# Views are registered with specific menu locations and icons
294
app_builder.add_view(
295
CustomUserDBModelView,
296
"List Users",
297
icon="fa-user",
298
label="List Users",
299
category="Security",
300
category_icon="fa-cogs",
301
category_label="Security"
302
)
303
```
304
305
## Route Configuration
306
307
Views define custom route bases and URL patterns:
308
- User views: Default Flask-AppBuilder routes
309
- Actions: `/actions/`
310
- Permissions: `/permissions/`
311
- Resources: `/resources/`
312
- User info edit: `/userinfoeditview/`
313
- User stats: `/userstatschartview/`
314
315
## Template Integration
316
317
Views integrate with Airflow's web interface templates and provide customized:
318
- List views with filtering and sorting
319
- Detail views with related object links
320
- Edit forms with validation
321
- Custom action buttons and menus
322
- Integration with Airflow's navbar and menu system