让peewee管理员使用烧瓶安全性

时间:2013-12-08 21:01:05

标签: python flask flask-security flask-peewee

我喜欢peewee和Flask安全性,它们都能很好地协同工作,但是我无法使peewee的管理功能工作。我真的不确定如何做到这一点,但我真的想一起使用它们。我尝试过很多东西,但似乎没什么用。

我目前的代码如下:

import datetime
from flask import Flask, abort, redirect, render_template, url_for, flash, request
from flask_peewee.auth import Auth, BaseUser
from flask_peewee.db import Database
from flask_peewee.admin import Admin, ModelAdmin
from flask_peewee.rest import RestAPI, UserAuthentication
from peewee import *
from flask.ext.security import *
# Create app
app = Flask(__name__)
app.config['DEBUG'] = True
app.config['SECRET_KEY'] = 'super-secret'
app.config['DATABASE'] = {
    'name': 'ee.db',
    'engine': 'peewee.SqliteDatabase',
}

# Create database connection object
db = Database(app)

class Role(db.Model, RoleMixin):
    name = TextField(unique=True)
    description = TextField(null=True)

class RoleAdmin(ModelAdmin):
    columns = ('name', 'description')

class User(db.Model, UserMixin):
    username = TextField()
    password = TextField()
    email = TextField()
    role = ForeignKeyField(Role)
    active = BooleanField(default=True)
    confirmed_at = DateTimeField(null=True)

class UserAdmin(ModelAdmin):
    columns = ('username', 'email', 'role', 'active', 'confirmed_at')

class UserRoles(db.Model):
    user = ForeignKeyField(User, related_name='roles')
    role = ForeignKeyField(Role, related_name='users')
    name = property(lambda self: self.role.name)
    description = property(lambda self: self.role.description)

class UserRolesAdmin(ModelAdmin):
    columns = ('user', 'role', 'name', 'description')

class Note(db.Model):
    user = ForeignKeyField(User, related_name='notes')
    title = CharField()
    message = TextField()
    created = DateTimeField(default=datetime.datetime.now)
    def __unicode__(self):
        return '%s: %s %s' % (self.user.username, self.message, self.created)

class NoteAdmin(ModelAdmin):
    columns = ("user", "message", "created")
    foreign_key_lookups = {'user': 'username'}


class CustomAuth(Auth):
    def get_user_model(self):
        return User

    def get_model_admin(self):
        return UserAdmin

class CustomAdmin(Admin):
    def check_user_permission(self, user):
        if has_role(current_user).name == "admin":
            return True

# Setup Flask-Security
user_datastore = PeeweeUserDatastore(db, User, Role, UserRoles)
security = Security(app, user_datastore)

#Setup Flask-Peewee admin
auth = CustomAuth(app, db)
admin = CustomAdmin(app, auth)
admin.register(Note, NoteAdmin)
admin.register(User, UserAdmin)
admin.setup()

#create a RestAPI container
api = RestAPI(app)
#register the Note model
api.register(Note)
api.setup()

#instantiate the user auth
user_auth = UserAuthentication(auth)

# Views
@app.route("/")
@login_required
def index():
    messages = Note.select()
    return render_template("base.html",
    notes = messages,
    username = current_user.username,
    role = current_user.role.name
    )

@app.route("/add", methods = ['post'])
@login_required
def add():
    user = current_user
    now = datetime.datetime.now()
    Note.create(user = current_user.get_id(), title = request.form["title"], message = request.form["message"], created = now)
    flash('New entry was successfully posted')
    return redirect(url_for('index'))

@app.route("/delete/<id>")
@login_required
def delete(id):
    note = Note.get(Note.id == id)
    note.delete_instance()
    flash('Entry was successfully removed')
    return redirect(url_for('index'))

if __name__ == '__main__':
    for Model in (Role, User, UserRoles):
        Model.drop_table(fail_silently=True)
        Model.create_table(fail_silently=True)
    user_datastore.create_role(name="admin", description="admin")
    user_datastore.create_user(username="Ben", email='hello', password='password', active=True, confirmed_at=datetime.datetime(1935,9,9,9,9), role=1)
    user_datastore.add_role_to_user('hello', "admin")
    Note.create_table(fail_silently=True)
    app.run(port = 8080)

当我尝试登录Peewee Admin时,出现以下错误:`AttributeError:'User'对象没有属性'check_password'

编辑:

完全调试:

File "/anaconda/lib/python2.7/site-packages/flask_peewee/auth.py", line 170, in login

form.password.data,

File "/anaconda/lib/python2.7/site-packages/flask_peewee/auth.py", line 128, in authenticate

 if not user.check_password(password):

AttributeError: 'User' object has no attribute 'check_password

我真的不明白为什么会引发一个AttributeError。密码已注册,并导入BaseUser类(check_password需要)。我做错了什么?

1 个答案:

答案 0 :(得分:0)

您的User类需要实现check_password和set_password方法,因为Flask-Peewee会调用这些方法来验证请求并更新模型。

Flask-Peewee提供了一个类BaseUser,可以将简单的实现混合到User中,但是当你还使用Flask-Security时,这可能没用。您需要以他们调用Flask-Security来比较和散列密码的方式自己实现这些方法,因为两个项目的密码散列方法不同,并且您只在数据库中保存一个散列。 / p>