コード例 #1
0
ファイル: views.py プロジェクト: ktamas8/flask
def login():
    if current_user.is_authenticated():
        flash('You are already logged in.')
        return redirect(url_for('auth.home'))

    form = LoginForm(request.form)

    if request.method == 'POST' and form.validate():
        username = request.form.get('username')
        password = request.form.get('password')

        try:
            User.try_login(username, password)
        except ldap.INVALID_CREDENTIALS:
            flash('Invalid username or password. Please try again.', 'danger')
            return render_template('login.html', form=form)

        user = User.query.filter_by(username=username).first()

        if not user:
            user = User(username, password)
            db.session.add(user)
            db.session.commit()
        login_user(user)
        flash('You have successfully logged in.', 'success')
        return redirect(url_for('auth.home'))

    if form.errors:
        flash(form.errors, 'danger')

    return render_template('login.html', form=form)
コード例 #2
0
ファイル: views.py プロジェクト: josue-acosta/daycare
def register():
	if current_user.is_authenticated:
		return redirect(url_for("child.dashboard"))

	form = RegistrationForm(meta={'csrf': False})

	if form.validate_on_submit():
		try:
			existing_user = User.objects.get(username=form.username.data)

			if existing_user:
				print("That username is already taken. Please try another one.")
			
			return render_template("register.html", form=form)

		except User.DoesNotExist:
			pass

		user = User (
			username = form.username.data,
			password = form.password.data
		)

		user.save()

		return redirect(url_for("auth.login"))

	return render_template("register.html", form=form)
コード例 #3
0
def ldap_login():
    if current_user.is_authenticated:
        flash('Your are already logged in.', 'info')
        return redirect(url_for('auth.home'))

    form = LoginForm()

    if form.validate_on_submit():
        username = request.form.get('username')
        password = request.form.get('password')
        try:
            conn = get_ldap_connection()
            conn.simple_bind_s('cn=%s,dc=example,dc=org' % username, password)
        except ldap.INVALID_CREDENTIALS:
            flash('Invalid username or password. Please try again.', 'danger')
            return render_template('login.html', form=form)

        user = User.query.filter_by(username=username).first()
        if not user:
            user = User(username, password)
            db.session.add(user)
            db.session.commit()

        login_user(user)
        flash('You have successfully logged in.', 'success')
        return redirect(url_for('auth.home'))

    if form.errors:
        flash(form.errors, 'danger')

    return render_template('login.html', form=form)
コード例 #4
0
def register():
    if current_user.is_authenticated:
        flash('Your are already logged in.', 'info')
        return redirect(url_for('auth.home'))

    form = RegistrationForm()

    if form.validate_on_submit():
        username = request.form.get('username')
        password = request.form.get('password')
        existing_username = User.query.filter_by(username=username).first()
        if existing_username:
            flash('This username has been already taken. Try another one.',
                  'warning')
            return render_template('register.html', form=form)
        user = User(username, password)
        db.session.add(user)
        db.session.commit()
        flash('You are now registered. Please login.', 'success')
        return redirect(url_for('auth.login'))

    if form.errors:
        flash(form.errors, 'danger')

    return render_template('register.html', form=form)
コード例 #5
0
ファイル: views.py プロジェクト: weihaixiaoseu/flaskCookBook
def after_login(resp):
    username = resp.nickname or resp.email
    if not username:
        flash('Invalid login. Please try again.', 'danger')
        return redirect(url_for('auth.login'))
    user = User.query.filter_by(username=username).first()
    if user is None:
        user = User(username, '')
        db.session.add(user)
        db.session.commit()
    login_user(user)
    return redirect(url_for('auth.home'))
コード例 #6
0
ファイル: views.py プロジェクト: josh-ivey/workoutapp
def facebook_login():
    if not facebook.authorized:
        return redirect(url_for("facebook.login"))
    resp = facebook.get("/me?fields=name,email")
    user = User.query.filter_by(username=resp.json()["email"]).first()
    if not user:
        user = User(resp.json()["email"], '')
        db.session.add(user)
        db.session.commit()
    login_user(user)
    flash("Login in as name=%s using Facebook login" % (resp.json()['name']), 'success')
    return redirect(request.args.get('next', url_for('auth.routine')))
コード例 #7
0
def login():
    if current_user.is_authenticated:
        return 'You are already logged in.'

    if request.json:
        username = (request.json.get('username')).upper()
        password = request.json.get('password')
 
        try:
            User.try_login(username, password)
        except ldap.INVALID_CREDENTIALS:
            #return "failure"
            return jsonify({"result":"failure"})

        user = User.query.filter_by(username=username).first()
 
        if not user:
            user = User(username, password)
            db.session.add(user)
            db.session.commit()
        login_user(user)
        #return "Success"
        return jsonify({"result":"success"})
コード例 #8
0
def twitter_login():
    if not twitter.authorized:
        return redirect(url_for("twitter.login"))

    resp = twitter.get("account/verify_credentials.json")

    user = User.query.filter_by(username=resp.json()["screen_name"]).first()
    if not user:
        user = User(resp.json()["screen_name"], '')
        db.session.add(user)
        db.session.commit()

    login_user(user)
    flash('Logged in as name=%s using Twitter login' % (resp.json()['name']),
          'success')
    return redirect(request.args.get('next', url_for('auth.home')))
コード例 #9
0
def google_login():
    if not google.authorized:
        return redirect(url_for("google.login"))

    resp = google.get("/oauth2/v1/userinfo")

    user = User.query.filter_by(username=resp.json()["email"]).first()
    if not user:
        user = User(resp.json()["email"], '')
        db.session.add(user)
        db.session.commit()

    login_user(user)
    flash('Logged in as name=%s using Google login' % (resp.json()['name']),
          'success')
    return redirect(request.args.get('next', url_for('auth.home')))
コード例 #10
0
ファイル: views.py プロジェクト: weihaixiaoseu/flaskCookBook
def twitter_authorized(resp):
    if resp is None:
        return 'Access denied: reason=%s error=%s' % (
            request.args['error_reason'],
            request.args['error_description']
        )
    session['twitter_oauth_token'] = resp['oauth_token'] + \
            resp['oauth_token_secret']

    user = User.query.filter_by(username=resp['screen_name']).first()
    if not user:
        user = User(resp['screen_name'], '')
        db.session.add(user)
        db.session.commit()

    login_user(user)
    flash('Logged in as twitter handle=%s' % resp['screen_name'])
    return redirect(request.args.get('next'))
コード例 #11
0
ファイル: views.py プロジェクト: weihaixiaoseu/flaskCookBook
def facebook_authorized(resp):
    if resp is None:
        return 'Access denied: reason=%s error=%s' % (
            request.args['error_reason'],
            request.args['error_description']
        )
    session['facebook_oauth_token'] = (resp['access_token'], '')
    me = facebook.get('/me')

    user = User.query.filter_by(username=me.data['email']).first()
    if not user:
        user = User(me.data['email'], '')
        db.session.add(user)
        db.session.commit()

    login_user(user)
    flash(
        'Logged in as id=%s name=%s' % (me.data['id'], me.data['name']),
        'success'
    )
    return redirect(request.args.get('next'))
コード例 #12
0
def user_create_admin():
    form = AdminUserCreateForm(request.form)

    if form.validate():
        username = form.username.data
        password = form.password.data
        admin = form.admin.data
        existing_username = User.query.filter_by(username=username).first()
        if existing_username:
            flash('This username has been already taken. Try another one.',
                  'warning')
            return render_template('register.html', form=form)
        user = User(username, password, admin)
        db.session.add(user)
        db.session.commit()
        flash('New User Created.', 'info')
        return redirect(url_for('auth.users_list_admin'))

    if form.errors:
        flash(form.errors, 'danger')

    return render_template('user-create-admin.html', form=form)
コード例 #13
0
ファイル: views.py プロジェクト: weihaixiaoseu/flaskCookBook
def google_authorized(resp):
    if resp is None:
        return 'Access denied: reason=%s error=%s' % (
            request.args['error_reason'],
            request.args['error_description']
        )
    session['google_oauth_token'] = (resp['access_token'], '')
    userinfo = requests.get(GOOGLE_OAUTH2_USERINFO_URL, params=dict(
        access_token=resp['access_token'],
    )).json()

    user = User.query.filter_by(username=userinfo['email']).first()
    if not user:
        user = User(userinfo['email'], '')
        db.session.add(user)
        db.session.commit()

    login_user(user)
    flash(
        'Logged in as id=%s name=%s' % (userinfo['id'], userinfo['name']),
        'success'
    )
    return redirect(url_for('auth.home'))
コード例 #14
0
def signup():

    form = SignupForm(request.form)

    if request.method == 'POST' and form.validate():
        cn = request.form.get('cn')
        givenName = request.form.get('givenName')
        sn = request.form.get('sn')
        departmentNumber = request.form.get('departmentNumber')
        telephoneNumber = request.form.get('telephoneNumber')
        userPassword = request.form.get('userPassword')

        result = User.try_signup(cn, givenName, sn, departmentNumber, telephoneNumber, userPassword)
        if result == 'success':
            flash('You have successfully sign un.', 'success')
            return redirect(url_for('auth.login'))
        else:
            flash(result, 'danger')
            return render_template('signup.html', form=form)
    if form.errors:
        flash(form.errors, 'danger')

    return render_template('signup.html', form=form)