Skip to content

Commit

Permalink
Merge branch 'pwreset'
Browse files Browse the repository at this point in the history
  • Loading branch information
emillon committed Dec 15, 2014
2 parents 87cea83 + 0f66746 commit 54151fb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
7 changes: 5 additions & 2 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ class User(db.Model):

def __init__(self, login, password, workfactor=12):
self.name = login
salt = bcrypt.gensalt(workfactor)
self.password = bcrypt.hashpw(password.encode('utf-8'), salt)
self.set_password(password, workfactor=workfactor)

def is_active(self):
"""
Expand Down Expand Up @@ -67,6 +66,10 @@ def generate(fake):
user = User(username, password, workfactor=4)
return user

def set_password(self, clear, workfactor=12):
salt = bcrypt.gensalt(workfactor)
self.password = bcrypt.hashpw(clear.encode('utf-8'), salt)


class Document(db.Model):
"""
Expand Down
25 changes: 22 additions & 3 deletions manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import os.path
import random
import string
import sys
from random import choice

import faker
from flask.ext.migrate import MigrateCommand
Expand Down Expand Up @@ -39,6 +41,13 @@
"""


def generate_password():
letters = string.ascii_letters + string.digits
length = 20
password = ''.join(random.choice(letters) for _ in range(length))
return password


def main():
manager = Manager(create_app)
manager.add_command('db', MigrateCommand)
Expand All @@ -53,9 +62,7 @@ def create():
@manager.command
def makeadmin():
"Create an admin user with a random password"
letters = string.ascii_letters + string.digits
length = 20
password = ''.join(random.choice(letters) for _ in range(length))
password = generate_password()
user = User('admin', password)
user.role = ROLE_ADMIN
db.session.add(user)
Expand Down Expand Up @@ -90,6 +97,18 @@ def fake():
db.session.add(obj)
db.session.commit()

@manager.command
def resetpassword(username):
"Reset password for a user"
user = User.query.filter_by(name=username).first()
if user is None:
print "No such user."
sys.exit(1)
password = generate_password()
user.set_password(password)
db.session.commit()
print password

manager.run()

if __name__ == '__main__':
Expand Down

0 comments on commit 54151fb

Please sign in to comment.