Skip to content
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
wants to merge 8 commits into
base: master
Choose a base branch
from
90 changes: 53 additions & 37 deletions wiki/wiki/doctype/wiki_page/wiki_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Copy link
Contributor

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(...)

Copy link

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!!

Copy link
Contributor

@blaggacao blaggacao Sep 13, 2024

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.

Copy link

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


# 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:
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change may represent a potential performance regression for no obvious reason. has_permission is not the cheapest of methods to run on anonymous requests.

if not permitted:
action = permtype
if action == "write":
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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)

Expand Down