Skip to content

Commit

Permalink
Add flask interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Leaman committed Jan 19, 2014
1 parent 74cc8ee commit 57da0d0
Show file tree
Hide file tree
Showing 27 changed files with 7,861 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
access-control
==============

Prototype code for the door access control system
Code for the door access control system
16 changes: 16 additions & 0 deletions access/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from flask import Flask
from flask.ext.login import LoginManager
from flask.ext.sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config.from_object('config')

db = SQLAlchemy(app)

login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
login_manager.login_message_category = 'warning'
login_manager.needs_refresh_message_category = 'warning'

from access import views
2 changes: 2 additions & 0 deletions access/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ROLE_USER = 0
ROLE_ADMIN = 1
27 changes: 27 additions & 0 deletions access/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from flask.ext.wtf import Form
from wtforms import TextField, BooleanField, PasswordField
from wtforms.validators import Required, EqualTo


class LoginForm(Form):
email = TextField('email', validators=[Required()])
password = PasswordField('password', validators=[Required()])
remember_me = BooleanField('remember_me', default=False)


class NewAdminForm(Form):
password = PasswordField('password', validators=[
Required(),
EqualTo('confirm', message='Passwords must match')
])
confirm = PasswordField('confirm_password', validators=[Required()])


class NewKeyForm(Form):
key_id = TextField('key_id', validators=[Required()])


class NewUserForm(Form):
name = TextField('name', validators=[Required()])
email = TextField('email', validators=[Required()])
key_id = TextField('key_id', validators=[Required()])
55 changes: 55 additions & 0 deletions access/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import hashlib
import uuid

from access import db
from access.constants import ROLE_USER, ROLE_ADMIN


class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(128), unique=True, nullable=False)
email = db.Column(db.String(128), unique=True, nullable=False)
role = db.Column(db.SmallInteger, default=ROLE_USER)
key_id = db.Column(db.Integer, unique=True)
pw_hash = db.Column(db.String(128))
pw_salt = db.Column(db.String(32))

def __init__(self, name, email, key_id):
self.name = name
self.email = email
self.key_id = key_id

def make_admin(self, password):
self.role = ROLE_ADMIN
self.pw_salt = uuid.uuid4().hex
self.pw_hash = hashlib.sha512(password + self.pw_salt).hexdigest()

def make_user(self):
self.role = ROLE_USER
self.pw_salt = None
self.pw_hash = None

def check_password(self, password):
if self.role != ROLE_ADMIN:
return False
check_hash = hashlib.sha512(password + self.pw_salt).hexdigest()
return check_hash == self.pw_hash

def is_authenticated(self):
return True

def is_active(self):
"""
We're only allowing admins to login, that is only
admins will be considered active.
"""
return self.role == ROLE_ADMIN

def is_anonymous(self):
return False

def get_id(self):
return unicode(self.id)

def __repr__(self):
return '<User %r>' % (self.email)
Loading

0 comments on commit 57da0d0

Please sign in to comment.