-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't show role_only_columns if not role; allow viewing view if role …
…in roles_accepted
- Loading branch information
Brian Peterson
committed
Aug 21, 2019
1 parent
4c51ebd
commit 8d824f1
Showing
5 changed files
with
83 additions
and
5 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,5 @@ | ||
|
||
from .base import SecureAdminBlueprint | ||
from .security import (SUPER_ROLE, SecureRedirectIndex, SecureDefaultIndex, | ||
SecureModelView) | ||
SecureModelView, scaffold_form_respecting_roles, | ||
scaffold_list_columns_respecting_roles) |
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
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
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
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,41 @@ | ||
|
||
from flask_admin.contrib.sqla.view import ModelView as SQLAModelView | ||
from .data import SUPER_ROLE | ||
from flask_security import current_user | ||
from flask_admin.contrib.sqla.form import get_form as get_sqla_form | ||
|
||
|
||
def scaffold_list_columns_respecting_roles(self): | ||
""" Respect a new view option, `role_only_columns`, | ||
in the list view. Must be a dictionary mapping | ||
between role names and columns which only users | ||
with this role are allowed to see. """ | ||
columns = SQLAModelView.scaffold_list_columns(self) | ||
role_only_columns = self.role_only_columns or dict() | ||
super_only_columns = role_only_columns.get(SUPER_ROLE) or [] | ||
if current_user and not current_user.has_role(SUPER_ROLE): | ||
columns = [c for c in columns if c not in super_only_columns] | ||
return columns | ||
|
||
|
||
def scaffold_form_respecting_roles(self): | ||
""" Just like regular SQLAModelView `scaffold_form()`, | ||
except that we exclude `role_only_columns` | ||
if user does not have the expected role. """ | ||
exclude = list(self.form_excluded_columns or []) | ||
role_only_columns = self.role_only_columns or dict() | ||
super_only_columns = role_only_columns.get(SUPER_ROLE) or [] | ||
if current_user and not current_user.has_role(SUPER_ROLE): | ||
exclude.extend(role_only_columns) | ||
converter = self.model_form_converter(self.session, self) | ||
form_class = get_sqla_form(self.model, converter, | ||
base_class=self.form_base_class, | ||
only=self.form_columns, | ||
exclude=exclude, | ||
field_args=self.form_args, | ||
ignore_hidden=self.ignore_hidden, | ||
extra_fields=self.form_extra_fields) | ||
|
||
if self.inline_models: | ||
form_class = self.scaffold_inline_form_models(form_class) | ||
return form_class |