如果有人好奇,我自己解决了这个问题:
首先,我使用此处的步骤将 flask-ldap3-login 与 Flask-SQLAlchemy 集成 - https://github.com/nickw444/flask-ldap3-login/issues/26
我的保存用户方法现在如下所示:
@ldap_manager.save_user
def save_user(dn, username, data, memberships):
id=int(data.get("uidNumber"))
if 'group-goes-here' in data.get("memberOf"):
user=User.query.filter_by(id=id).first()
if not user:
user=User(
id=int(id),
dn=dn,
username=username,
email=data['mail'],
firstname=data['givenName'],
lastname=data['sn']
)
db.session.add(user)
db.session.commit()
return user
因此,基本上只要用户输入有效的 LDAP 凭据,它就会转到 AD 以检索他们的组成员身份,并且它是一个简单的 if 'group-goes-here' in data.get("memberOf"): 确定是否将用户保存在我的用户模型中并将其返回给处理程序。
@auth.route('/login', methods=['GET', 'POST'])
def login():
# Redirect users who are not logged in.
form = LDAPLoginForm()
if form.validate_on_submit():
if form.user:
login_user(form.user)
else:
flash('Login Failed', 'warning')
return redirect(url_for('auth.login'))
return redirect(url_for('main.home'))
希望这可以帮助!