-
Notifications
You must be signed in to change notification settings - Fork 207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New permission for wiki space #217
Open
monolithonadmin
wants to merge
8
commits into
frappe:master
Choose a base branch
from
monolithon:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c279ea6
new Permission for wiki space
MuhammadZubair12 87957da
Updates query with builder
MuhammadZubair12 7b7dcfb
remove print statement and also update query structure
MuhammadZubair12 d8e75bc
Merge branch 'master' into master
Z4nzu ba8d5f7
[COMMIT] #217 Optimised code for New permission for wiki space.
hardik-limendo 20d0e93
Enhance permission handling for WikiPage in Frappe Wiki app
Z4nzu cab8d49
[COMMIT] Enhance Wiki Page Visibility and Permissions Handling
Z4nzu d63880e
Improved code.
Z4nzu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -50,17 +50,24 @@ def on_update(self): | |
update_index(self) | ||
|
||
def on_trash(self): | ||
frappe.db.sql("DELETE FROM `tabWiki Page Revision Item` WHERE wiki_page = %s", self.name) | ||
|
||
frappe.db.sql( | ||
"""DELETE FROM `tabWiki Page Revision` WHERE name in | ||
( | ||
SELECT name FROM `tabWiki Page Revision` | ||
EXCEPT | ||
SELECT DISTINCT parent from `tabWiki Page Revision Item` | ||
)""" | ||
if frappe.db.exists('Wiki Page Revision Item', {'wiki_page': self.name}): | ||
frappe.db.delete('Wiki Page Revision Item', {'wiki_page': self.name}) | ||
|
||
# Get names of revisions that are not referenced in `Wiki Page Revision Item` | ||
revisions_to_delete = frappe.db.get_all( | ||
"Wiki Page Revision", | ||
filters={ | ||
"name": ["not in", frappe.db.get_all( | ||
"Wiki Page Revision Item", | ||
fields=["distinct parent"] | ||
)] | ||
}, | ||
pluck="name" | ||
) | ||
|
||
if revisions_to_delete: | ||
frappe.db.delete("Wiki Page Revision", {"name": ["in", revisions_to_delete]}) | ||
|
||
for name in frappe.get_all("Wiki Page Patch", {"wiki_page": self.name, "new": 0}, pluck="name"): | ||
patch = frappe.get_doc("Wiki Page Patch", name) | ||
try: | ||
|
@@ -72,8 +79,7 @@ def on_trash(self): | |
for name in frappe.get_all("Wiki Page Patch", {"wiki_page": self.name, "new": 1}, pluck="name"): | ||
frappe.db.set_value("Wiki Page Patch", name, "wiki_page", "") | ||
|
||
wiki_sidebar_name = frappe.get_value("Wiki Group Item", {"wiki_page": self.name}) | ||
frappe.delete_doc("Wiki Group Item", wiki_sidebar_name) | ||
frappe.db.delete("Wiki Group Item", {"wiki_page": self.name}) | ||
|
||
clear_sidebar_cache() | ||
remove_index(self) | ||
|
@@ -151,9 +157,9 @@ def update_page(self, title, content, edit_message, raised_by=None): | |
self.save() | ||
|
||
def verify_permission(self, permtype): | ||
permitted = frappe.has_permission(self.doctype, permtype, self) | ||
if permtype == "read" and self.allow_guest: | ||
return True | ||
permitted = frappe.has_permission(self.doctype, permtype, self) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change may represent a potential performance regression for no obvious reason. |
||
if not permitted: | ||
action = permtype | ||
if action == "write": | ||
|
@@ -210,6 +216,25 @@ def get_context(self, context): | |
self.verify_permission("read") | ||
self.set_breadcrumbs(context) | ||
|
||
user_permissions = frappe.db.get_all("User Permission", filters={"user": frappe.session.user, "allow": "Wiki Space"}, pluck="for_value") | ||
if user_permissions: | ||
# Fetch all Wiki Groups associated with the user permissions | ||
wiki_groups = frappe.db.get_all( | ||
"Wiki Group Item", | ||
filters={"parent": ["in", user_permissions]}, | ||
pluck="wiki_page" | ||
) | ||
|
||
# Check if the current wiki page is in the allowed wiki groups | ||
if self.name not in wiki_groups: | ||
frappe.local.response["type"] = "redirect" | ||
frappe.local.response["location"] = "/" | ||
raise frappe.Redirect | ||
else: | ||
frappe.local.response["type"] = "redirect" | ||
frappe.local.response["location"] = "/" | ||
raise frappe.Redirect | ||
|
||
wiki_settings = frappe.get_single("Wiki Settings") | ||
wiki_space_name = frappe.get_value("Wiki Group Item", {"wiki_page": self.name}, "parent") | ||
wiki_space = frappe.get_doc("Wiki Space", wiki_space_name) | ||
|
@@ -284,6 +309,7 @@ def get_context(self, context): | |
|
||
def get_items(self, sidebar_items): | ||
topmost = frappe.get_value("Wiki Group Item", {"wiki_page": self.name}, ["parent"]) | ||
wikiSpace = frappe.get_all('Wiki Space', pluck='name') | ||
|
||
sidebar_html = frappe.cache().hget("wiki_sidebar", topmost) | ||
if not sidebar_html or frappe.conf.disable_website_cache or frappe.conf.developer_mode: | ||
|
@@ -295,6 +321,7 @@ def get_items(self, sidebar_items): | |
context.current_route = self.route | ||
context.collapse_sidebar_groups = wiki_settings.collapse_sidebar_groups | ||
context.sidebar_items = sidebar_items | ||
context.wikiSpace = wikiSpace | ||
context.wiki_search_scope = self.get_space_route() | ||
sidebar_html = frappe.render_template( | ||
"wiki/wiki/doctype/wiki_page/templates/web_sidebar.html", context | ||
|
@@ -305,6 +332,8 @@ def get_items(self, sidebar_items): | |
|
||
def get_sidebar_items(self): | ||
wiki_sidebar = frappe.get_doc("Wiki Space", {"route": self.get_space_route()}).wiki_sidebars | ||
user = frappe.session.user | ||
check = frappe.db.get_all("User Permission", {"user": user, "allow": "Wiki Space"}, ["user", "allow", "for_value", "name"]) | ||
sidebar = {} | ||
|
||
for sidebar_item in wiki_sidebar: | ||
|
@@ -313,31 +342,18 @@ def get_sidebar_items(self): | |
|
||
wiki_page = frappe.get_doc("Wiki Page", sidebar_item.wiki_page) | ||
|
||
if not wiki_page.allow_guest: | ||
permitted = frappe.has_permission(wiki_page.doctype, "read", wiki_page) | ||
if not permitted: | ||
continue | ||
|
||
if sidebar_item.parent_label not in sidebar: | ||
sidebar[sidebar_item.parent_label] = [ | ||
{ | ||
"name": wiki_page.name, | ||
"type": "Wiki Page", | ||
"title": wiki_page.title, | ||
"route": wiki_page.route, | ||
"group_name": sidebar_item.parent_label, | ||
} | ||
] | ||
else: | ||
sidebar[sidebar_item.parent_label] += [ | ||
{ | ||
"name": wiki_page.name, | ||
"type": "Wiki Page", | ||
"title": wiki_page.title, | ||
"route": wiki_page.route, | ||
"group_name": sidebar_item.parent_label, | ||
} | ||
] | ||
if wiki_page.allow_guest or frappe.has_permission(wiki_page.doctype, "read", wiki_page): | ||
page_info = { | ||
"name": wiki_page.name, | ||
"type": "Wiki Page", | ||
"title": wiki_page.title, | ||
"route": wiki_page.route, | ||
"group_name": sidebar_item.parent_label, | ||
} | ||
if sidebar_item.parent_label not in sidebar: | ||
sidebar[sidebar_item.parent_label] = [page_info] | ||
else: | ||
sidebar[sidebar_item.parent_label].append(page_info) | ||
|
||
return self.get_items(sidebar) | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
frappe.db.delete_if_exists(...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello @blaggacao, I am not getting you why are you trying to make it complex? I checked but i have not found delete_if_exists method in develop branch nor in version-15 branch.
Please correct me if I'm wrong!!
Thank you!!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/frappe/frappe/blob/develop/frappe%2F__init__.py#L1460
Sorry for the imprecise reference, here.
One db call is better than two.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is done!! Thank you @blaggacao
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
frappe/semgrep-rules#29