简体   繁体   English

flask-admin:如何根据个人用户而不是角色自定义视图?

[英]flask-admin: how to customize the view according to the individual user instead of the role?

As we all know, flask-admin allows the users with same role have the same privilege. 众所周知,flask-admin允许具有相同角色的用户拥有相同的特权。 However, in my app I'd like to restrict only the user who created a query to view and edit. 但是,在我的应用程序中,我只想限制创建查询的用户进行查看和编辑。

For example, there are many users with the same role called "developers" , he/she creates a code review and wait for approval. 例如,有许多用户具有相同的角色,称为“开发人员” ,他/她创建代码审查并等待批准。

The pic below shows developer Gina can see developer Bill's project in the list(which is not desired). 下图显示了开发人员Gina可以在列表中看到开发人员Bill的项目(这是不希望的)。 What I'd like to achieve is only Gina and the reviewers( Wesker and Steve here) can view this project in the list. 我只想实现Gina,而审阅者(此处为WeskerSteve )可以在列表中查看此项目。

I've put the code here , if it's not appropriate I will remove the link. 我将代码放在这里 ,如果不合适,我将删除链接。 Thanks in advance. 提前致谢。

在此处输入图片说明

Overriding ModelView, you can filter results to show logged user own results: 覆盖ModelView,您可以过滤结果以显示记录的用户自己的结果:

def get_query(self):
  return self.session.query(self.model).filter(self.model.user==current_user)

def get_count_query(self):
  return self.session.query(func.count('*')).filter(self.model.user==current_user)

You can find more info here: Flask-Admin default filters 您可以在此处找到更多信息: Flask-Admin默认过滤器

You should have to create another user role to achieve your goal. 您必须创建另一个用户角色才能实现目标。 It will help you to save your time and you can easily customize the view specifically for the particular individual or group. 这将帮助您节省时间,并且可以轻松地为特定的个人或团体自定义视图。

Please go through this link 请通过此链接

Having the info you provided, a solution might look like the following 有了您提供的信息后,解决方案可能如下所示

  1. Rewrite/extend the logic which gathers the Project records so that there are only records belonging to the current user . 重写/扩展收集Project记录的逻辑,以便仅存在属于当前用户的记录。 An example to demonstrate might looks like this: 一个示例来演示可能如下所示:

    pseudo-sql 伪SQL

    SELECT team, name, project_name, version, ... FROM projects where name = current_user.name

  2. Rewrite/extend the logic responsible for saving the record so that it will verify that the current_user is allowed to make changes to the given project record 重写/扩展负责保存记录的逻辑,以便它将验证是否允许 current_user 更改给定的项目记录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM