-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tom Leaman
committed
Jan 19, 2014
1 parent
74cc8ee
commit 57da0d0
Showing
27 changed files
with
7,861 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
ROLE_USER = 0 | ||
ROLE_ADMIN = 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Oops, something went wrong.