realms-wiki/realms/modules/auth/ldap/models.py

55 lines
1.2 KiB
Python
Raw Normal View History

2015-10-15 23:55:38 +03:00
from flask import render_template
2015-10-15 01:36:22 +03:00
from flask.ext.login import login_user
from realms import ldap
from flask_ldap_login import LDAPLoginForm
from ..models import BaseUser
2015-10-15 23:55:38 +03:00
2015-10-15 01:36:22 +03:00
users = {}
2015-11-22 19:19:11 +02:00
2015-10-15 01:36:22 +03:00
@ldap.save_user
def save_user(username, userdata):
2015-10-15 23:55:38 +03:00
user = User(userdata.get('username'), userdata.get('email'))
users[user.id] = user
return user
2015-10-15 01:36:22 +03:00
2015-11-22 19:19:11 +02:00
2015-10-15 01:36:22 +03:00
class User(BaseUser):
type = 'ldap'
2015-10-15 23:55:38 +03:00
def __init__(self, username, email='null@localhost.local', password=None):
2015-10-15 01:36:22 +03:00
self.id = username
self.username = username
2015-10-15 23:55:38 +03:00
self.email = email
self.password = password
@property
def auth_token_id(self):
return self.password
@staticmethod
def load_user(*args, **kwargs):
return User.get_by_id(args[0])
@staticmethod
def get_by_id(user_id):
return users.get(user_id)
2015-10-15 01:36:22 +03:00
@staticmethod
def login_form():
form = LDAPLoginForm()
return render_template('auth/ldap/login.html', form=form)
@staticmethod
2015-10-15 23:55:38 +03:00
def auth(user, password):
password = User.hash_password(password)
user.password = password
users[user.id] = user
if user:
login_user(user, remember=True)
return True
else:
return False