forked from cloudbees-days/user-registration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
58 lines (45 loc) · 1.89 KB
/
app.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
import os
from flask import Flask, render_template, redirect
from python_freeipa import ClientMeta
import logging
import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration
from forms import SignupForm
sentry_sdk.init(
dsn="https://fbb82a738d724494870da1ffa5e67f7d@sentry.io/5176646",
integrations=[FlaskIntegration()],
)
app = Flask(__name__)
app.config["SECRET_KEY"] = os.environ.get("SECRET_KEY") or "ookeemo7ohGhahx9da9l"
INVITE_CODE_ENV = os.environ.get("INVITE_CODE", "")
ELEVATED_INVITE_CODE_ENV = os.environ.get("ELEVATED_INVITE_CODE", "")
USER_GROUP_ENV = os.environ.get("USER_GROUP", "")
ELEVATED_USER_GROUP_ENV = os.environ.get("ELEVATED_USER_GROUP", "")
TARGET_URL = os.environ.get("TARGET_URL", "")
logger = logging.getLogger("registration")
@app.route("/", methods=["GET", "POST"])
def registration():
form = SignupForm()
if form.validate_on_submit():
client = ClientMeta(os.environ.get("LDAP_SERVER"), verify_ssl=False)
client.login(os.environ.get("LDAP_ADMIN"), os.environ.get("LDAP_PASSWORD"))
full_name = f"{form.first_name.data} {form.last_name.data}"
client.user_add(
a_uid=form.username.data,
o_givenname=form.first_name.data,
o_sn=form.last_name.data,
o_cn=full_name,
o_userpassword=form.password.data,
o_preferredlanguage="EN",
)
if form.invite_code.data == INVITE_CODE_ENV:
client.group_add_member(USER_GROUP_ENV, o_user=form.username.data)
elif form.invite_code.data == ELEVATED_INVITE_CODE_ENV:
client.group_add_member(ELEVATED_USER_GROUP_ENV, o_user=form.username.data)
return redirect("/success")
return render_template("register.html", form=form)
@app.route("/success")
def success():
return render_template("success.html", target_url=TARGET_URL)
if __name__ == "__main__":
app.run()