Skip to content

Commit

Permalink
Apply formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaSBrown committed Jan 16, 2025
1 parent 8866891 commit 4e20244
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 42 deletions.
91 changes: 52 additions & 39 deletions core/database/foxx/api/repo.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,45 +132,58 @@ class Repo {
* @param {string} a_path - the POSIX path that is supposed to exist on the repo
* @returns {string} - posix path type
**/
pathType(a_path) {
// Ensure the repo exists
if (!this.#exists) { throw [g_lib.ERR_PERM_DENIED, "Repo does not exist " + this.#repo_id]; }

let repo = g_db._document(this.#repo_id);
if (!repo.path) { throw [g_lib.ERR_INTERNAL_FAULT, "Repo document is missing path: " + this.#repo_id]; }

// Get and sanitize the repo root path
let repo_root_path = repo.path.replace(/\/$/, '');
let sanitized_path = a_path.replace(/\/$/, '');

// Check if the sanitized path is exactly the repo root path
if (sanitized_path === repo_root_path) { return PathType.REPO_ROOT_PATH; }

// Check if the sanitized path is a valid base path
if (sanitized_path.length < repo_root_path.length && repo_root_path.startsWith(sanitized_path + "/")) {
return PathType.REPO_BASE_PATH;
}

// Ensure the sanitized path starts with the repo root path
if (!sanitized_path.startsWith(repo_root_path + "/")) { return PathType.UNKNOWN; }

// Get the relative path and its components
const relative_path = sanitized_path.substr(repo_root_path.length);
const relative_path_components = pathModule.splitPOSIXPath(relative_path);

// Map the first component to its corresponding PathType
const pathMapping = {
project: [PathType.REPO_PATH, PathType.PROJECT_PATH, PathType.PROJECT_RECORD_PATH],
user: [PathType.REPO_PATH, PathType.USER_PATH, PathType.USER_RECORD_PATH]
};

const firstComponent = relative_path_components[0];
if (pathMapping[firstComponent]) {
return pathMapping[firstComponent][relative_path_components.length - 1] || PathType.UNKNOWN;
}

return PathType.UNKNOWN;
}
pathType(a_path) {
// Ensure the repo exists
if (!this.#exists) {
throw [g_lib.ERR_PERM_DENIED, "Repo does not exist " + this.#repo_id];
}

let repo = g_db._document(this.#repo_id);
if (!repo.path) {
throw [g_lib.ERR_INTERNAL_FAULT, "Repo document is missing path: " + this.#repo_id];
}

// Get and sanitize the repo root path
let repo_root_path = repo.path.replace(/\/$/, "");
let sanitized_path = a_path.replace(/\/$/, "");

// Check if the sanitized path is exactly the repo root path
if (sanitized_path === repo_root_path) {
return PathType.REPO_ROOT_PATH;
}

// Check if the sanitized path is a valid base path
if (
sanitized_path.length < repo_root_path.length &&
repo_root_path.startsWith(sanitized_path + "/")
) {
return PathType.REPO_BASE_PATH;
}

// Ensure the sanitized path starts with the repo root path
if (!sanitized_path.startsWith(repo_root_path + "/")) {
return PathType.UNKNOWN;
}

// Get the relative path and its components
const relative_path = sanitized_path.substr(repo_root_path.length);
const relative_path_components = pathModule.splitPOSIXPath(relative_path);

// Map the first component to its corresponding PathType
const pathMapping = {
project: [PathType.REPO_PATH, PathType.PROJECT_PATH, PathType.PROJECT_RECORD_PATH],
user: [PathType.REPO_PATH, PathType.USER_PATH, PathType.USER_RECORD_PATH],
};

const firstComponent = relative_path_components[0];
if (pathMapping[firstComponent]) {
return (
pathMapping[firstComponent][relative_path_components.length - 1] || PathType.UNKNOWN
);
}

return PathType.UNKNOWN;
}
}

module.exports = { Repo, PathType };
6 changes: 3 additions & 3 deletions core/database/foxx/tests/authz.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ describe("Authz functions", () => {

let req_perm = g_lib.PERM_CREATE;

expect(() => authzModule.isRecordActionAuthorized(client, data_key, req_perm)).to.throw();
expect(() =>
authzModule.isRecordActionAuthorized(client, data_key, req_perm),
).to.throw();
});
});

Expand Down Expand Up @@ -203,7 +205,6 @@ describe("Authz functions", () => {
});

describe("unit_authz: 'Jack' is a creator of the an 'apples' document in the 'fruity' project, 'Mandy' is the admin of the 'condiments' project, 'Mandy' should not have access to the 'apples' document.", () => {

it("unit_authz: should return false.", () => {
let data_key = "apples";
let data_id = "d/" + data_key;
Expand Down Expand Up @@ -352,7 +353,6 @@ describe("Authz functions", () => {

g_db.u.save(tim, { waitForSync: true });


let req_perm = g_lib.PERM_READ;

expect(authzModule.isRecordActionAuthorized(tim, data_key, req_perm)).to.be.false;
Expand Down

0 comments on commit 4e20244

Please sign in to comment.