forked from apache/airflow
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdefault_login.py
90 lines (73 loc) · 2.58 KB
/
default_login.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# -*- coding: utf-8 -*-
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
"""
Override this file to handle your authenticating / login.
Copy and alter this file and put in your PYTHONPATH as airflow_login.py,
the new module will override this one.
"""
import flask_login
from flask_login import login_required, current_user, logout_user # noqa: F401
from flask import url_for, redirect
from airflow import settings # noqa: F401
from airflow import models
from airflow.utils.db import provide_session
DEFAULT_USERNAME = 'airflow'
login_manager = flask_login.LoginManager()
login_manager.login_view = 'airflow.login' # Calls login() below
login_manager.login_message = None
class DefaultUser(object):
def __init__(self, user):
self.user = user
@property
def is_active(self):
"""Required by flask_login"""
return True
@property
def is_authenticated(self):
"""Required by flask_login"""
return True
@property
def is_anonymous(self):
"""Required by flask_login"""
return False
def data_profiling(self):
"""Provides access to data profiling tools"""
return True
def is_superuser(self):
"""Access all the things"""
return True
@login_manager.user_loader
@provide_session
def load_user(userid, session=None):
user = session.query(models.User).filter(models.User.id == userid).first()
return DefaultUser(user)
@provide_session
def login(self, request, session=None):
user = session.query(models.User).filter(
models.User.username == DEFAULT_USERNAME).first()
if not user:
user = models.User(
username=DEFAULT_USERNAME,
is_superuser=True)
session.merge(user)
session.commit()
flask_login.login_user(DefaultUser(user))
session.commit()
return redirect(request.args.get("next") or url_for("index"))