问题
i have an aplication homestay reservation built with flask.. every homestay have an user login, this login built with flask-security and every owner of the homestay have role User. and every user can input their homestay data with flask-admin. but unfortunately if a user input their data, the others user which have role User can seing the data have input too..
so.. my question how to separate the data if a user have the same role..? user A can just see his data, and user B so too..
this is my models.py code:
roles_users = database.Table(
'roles_users',
database.Column('user_id', database.Integer(), database.ForeignKey('user.id')),
database.Column('role_id', database.Integer(), database.ForeignKey('role.id'))
)
class Role(database.Model, RoleMixin):
id = database.Column(database.Integer(), primary_key=True)
name = database.Column(database.String(80), unique=True)
description = database.Column(database.String(255))
def __str__(self):
return self.name
class User(database.Model, UserMixin):
id = database.Column(database.Integer, primary_key=True)
first_name = database.Column(database.String(255))
last_name = database.Column(database.String(255))
email = database.Column(database.String(255), unique=True)
password = database.Column(database.String(255))
active = database.Column(database.Boolean())
confirmed_at = database.Column(database.DateTime())
roles = database.relationship('Role', secondary=roles_users,
backref=database.backref('users', lazy='dynamic'))
def __str__(self):
return self.email
class Room(database.Model):
__tablename__ = 'room'
room_id = Column(Integer, primary_key=True)
room_name = Column(String)
room_description = Column(String)
room_images = Column(database.Unicode(128))
room_price = Column(Integer)
user_id = Column(Integer, ForeignKey(User.id))
and this is my views.py :
class UserAccess(ModelView):
def is_accessible(self):
if not current_user.is_active or not current_user.is_authenticated:
return False
if current_user.has_role('user'):
return True
return False
def _handle_view(self, name, **kwargs):
"""
Override builtin _handle_view in order to redirect users when a view is not accessible.
"""
if not self.is_accessible():
if current_user.is_authenticated:
# permission denied
abort(403)
else:
# login
return redirect(url_for('security.login', next=request.url))
class Room(UserAccess):
form_overrides = dict(keterangan_kamar=CKEditorField)
create_template = 'admin/ckeditor.html'
edit_template = 'admin/ckeditor.html'
column_list = ('room_name', 'room_images', 'room_price')
def _list_thumbnail(view, context, model, name):
if not model.room_images:
return ''
return Markup('<img src="%s">' % url_for('static',
filename=form.thumbgen_filename(model.room_images)))
column_formatters = {
'room_images': _list_thumbnail
}
# Alternative way to contribute field is to override it completely.
# In this case, Flask-Admin won't attempt to merge various parameters for the field.
form_extra_fields = {
'room_images': form.ImageUploadField('Room Images',
base_path=file_path,
thumbnail_size=(100, 100, True))
}
回答1:
You need to add a permanent filter to your Room
view by overriding get_query and get_count_query
For example (note I've named your Room
class to RoomView
as you already have a class called Room
representing the database model):
class RoomView(UserAccess):
def get_query(self):
return self.session.query(self.model).filter(
Room.user_id == current_user.id
)
def get_count_query(self):
return self.session.query(func.count('*')).select_from(self.model).filter(
Room.user_id == current_user.id
)
# ... your code etc
来源:https://stackoverflow.com/questions/51521364/flask-security-separating-access-data-on-flask-admin