Skip to content

Commit

Permalink
update permissions for notebook creation and test
Browse files Browse the repository at this point in the history
Signed-off-by: Steve Cassidy <[email protected]>
  • Loading branch information
stevecassidy committed Dec 13, 2023
1 parent 2e08772 commit 0147108
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 86 deletions.
17 changes: 11 additions & 6 deletions src/api/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
saveUser,
userHasPermission,
userIsClusterAdmin,
userCanCreateNotebooks,
} from '../couchdb/users';
import {
CLUSTER_ADMIN_GROUP_NAME,
Expand Down Expand Up @@ -87,20 +88,24 @@ api.get('/notebooks/', requireAuthenticationAPI, async (req, res) => {
* POST to /notebooks/ to create a new notebook
*/
api.post('/notebooks/', requireAuthenticationAPI, async (req, res) => {
// post a new notebook
// user must be cluster admin

if (userIsClusterAdmin(req.user)) {
if (req.user && userCanCreateNotebooks(req.user)) {
const uiSpec = req.body['ui-specification'];
const projectName = req.body.name;
const metadata = req.body.metadata;

try {
const projectID = await createNotebook(projectName, uiSpec, metadata);
res.json({notebook: projectID});
if (projectID) {
// allow this user to modify the new notebook
addProjectRoleToUser(req.user, projectID, 'admin');
await saveUser(req.user);
res.json({notebook: projectID});
} else {
res.json({error: 'error creating the notebook'});
res.status(500).end();

Check warning on line 105 in src/api/routes.ts

View check run for this annotation

Codecov / codecov/patch

src/api/routes.ts#L104-L105

Added lines #L104 - L105 were not covered by tests
}
} catch (err) {
res.json({error: 'there was an error creating the notebook'});
console.log('Error creating notebook', err);
res.status(500).end();
}
} else {
Expand Down
3 changes: 3 additions & 0 deletions src/couchdb/notebooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,9 @@ export async function countRecordsInNotebook(
const dataDB = await getDataDB(project_id);
try {
const res = await dataDB.query('index/recordCount');
if (res.rows.length === 0) {
return 0;
}
return res.rows[0].value;

Check warning on line 863 in src/couchdb/notebooks.ts

View check run for this annotation

Codecov / codecov/patch

src/couchdb/notebooks.ts#L863

Added line #L863 was not covered by tests
} catch (error) {
console.log(error);

Check warning on line 865 in src/couchdb/notebooks.ts

View check run for this annotation

Codecov / codecov/patch

src/couchdb/notebooks.ts#L865

Added line #L865 was not covered by tests
Expand Down
5 changes: 4 additions & 1 deletion src/couchdb/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@

import {ProjectRole} from 'faims3-datamodel/build/src/types';
import {getUsersDB} from '.';
import {CLUSTER_ADMIN_GROUP_NAME, NOTEBOOK_CREATOR_GROUP_NAME} from '../buildconfig';
import {
CLUSTER_ADMIN_GROUP_NAME,
NOTEBOOK_CREATOR_GROUP_NAME,
} from '../buildconfig';
import {NonUniqueProjectID, ProjectID} from 'faims3-datamodel';
import {
AllProjectRoles,
Expand Down
Loading

0 comments on commit 0147108

Please sign in to comment.