Skip to content

Commit

Permalink
fix: private workspace routing (backport #25904) (#25921)
Browse files Browse the repository at this point in the history
* refactor: local declaration

(cherry picked from commit facff87)

* fix: use workspace title instead of name

this is how workspaces work 🤷

(cherry picked from commit 8226792)

* refactor: Simplify workspace resolution

no need for if-elses eh

(cherry picked from commit 84cbe3d)

---------

Co-authored-by: Ankush Menat <ankush@frappe.io>
  • Loading branch information
mergify[bot] and ankush committed Apr 11, 2024
1 parent 34fbf6d commit c4eeb2e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 24 deletions.
41 changes: 20 additions & 21 deletions frappe/public/js/frappe/router.js
Expand Up @@ -177,13 +177,12 @@ frappe.router = {
// /app/user/user-001 = ["Form", "User", "user-001"]
// /app/event/view/calendar/default = ["List", "Event", "Calendar", "Default"]

let private_workspace = route[1] && `${route[1]}-${frappe.user.name.toLowerCase()}`;

if (frappe.workspaces[route[0]]) {
// public workspace
route = ["Workspaces", frappe.workspaces[route[0]].title];
} else if (route[0] == "private") {
// private workspace
let private_workspace = route[1] && `${route[1]}-${frappe.user.name.toLowerCase()}`;
if (!frappe.workspaces[private_workspace] && localStorage.new_workspace) {
let new_workspace = JSON.parse(localStorage.new_workspace);
if (frappe.router.slug(new_workspace.title) === route[1]) {
Expand Down Expand Up @@ -464,26 +463,26 @@ frappe.router = {
return "/app/" + path_string;
}

// Workspace
// Resolution order
// 1. User's default workspace in user doctype
// 2. Private home
// 3. Public home
// 4. First workspace in list
let private_home = `home-${frappe.user.name.toLowerCase()}`;
let default_page = null;
if (frappe.boot.user.default_workspace) {
default_page = frappe.router.slug(frappe.boot.user.default_workspace.name);
} else if (frappe.workspaces[private_home]) {
default_page = private_home;
} else if (frappe.workspaces["home"]) {
default_page = "home";
} else {
// Fallback to first workspace
default_page = Object.keys(frappe.workspaces)[0];
}

if (frappe.workspaces[default_page]?.public == false) {
default_page = "private/" + default_page;
}

if (default_page) {
return "/app/" + default_page;
let default_workspace = frappe.router.slug(frappe.boot.user.default_workspace?.name || "");

let workspace =
frappe.workspaces[default_workspace] ||
frappe.workspaces[private_home] ||
frappe.workspaces["home"] ||
Object.values(frappe.workspaces)[0];

if (workspace) {
return (
"/app/" +
(workspace.public ? "" : "private/") +
frappe.router.slug(workspace.title)
);
}

return "/app";
Expand Down
2 changes: 1 addition & 1 deletion frappe/public/js/frappe/views/workspace/workspace.js
Expand Up @@ -353,7 +353,7 @@ frappe.views.Workspace = class Workspace {

if (frappe.boot.user.default_workspace) {
default_page = {
name: frappe.boot.user.default_workspace.name,
name: frappe.boot.user.default_workspace.title,
public: frappe.boot.user.default_workspace.public,
};
} else if (
Expand Down
8 changes: 6 additions & 2 deletions frappe/utils/user.py
Expand Up @@ -235,8 +235,12 @@ def load_user(self):
self.build_permissions()

if d.get("default_workspace"):
public = frappe.get_cached_value("Workspace", d.default_workspace, "public")
d.default_workspace = {"name": d.default_workspace, "public": public}
workspace = frappe.get_cached_doc("Workspace", d.default_workspace)
d.default_workspace = {
"name": workspace.name,
"public": workspace.public,
"title": workspace.title,
}

d.name = self.name
d.onboarding_status = frappe.parse_json(d.onboarding_status)
Expand Down

0 comments on commit c4eeb2e

Please sign in to comment.