-
Notifications
You must be signed in to change notification settings - Fork 24
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
Add switch orga to legacy routes #8257
Changes from 1 commit
5a4b16d
6b6d8e9
e6db2c3
7bd0e44
fb87422
6f4d19c
57f5135
b826739
65e39d5
a865502
4f03707
1981fbf
f31575f
4a56a33
61c0a88
08c0de7
ae3f9be
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -234,39 +234,57 @@ class AuthenticationController @Inject()( | |
*/ | ||
|
||
def accessibleBySwitching(datasetId: Option[String], | ||
datasetDirectoryName: Option[String], | ||
organizationId: Option[String], | ||
annotationId: Option[String], | ||
workflowHash: Option[String]): Action[AnyContent] = sil.SecuredAction.async { | ||
implicit request => | ||
for { | ||
datasetIdValidated <- Fox.runOptional(datasetId)(ObjectId.fromString(_)) | ||
isSuperUser <- multiUserDAO.findOne(request.identity._multiUser).map(_.isSuperUser) | ||
selectedOrganization <- if (isSuperUser) | ||
accessibleBySwitchingForSuperUser(datasetIdValidated, annotationId, workflowHash) | ||
accessibleBySwitchingForSuperUser(datasetIdValidated, | ||
datasetDirectoryName, | ||
organizationId, | ||
annotationId, | ||
workflowHash) | ||
else | ||
accessibleBySwitchingForMultiUser(request.identity._multiUser, datasetIdValidated, annotationId, workflowHash) | ||
accessibleBySwitchingForMultiUser(request.identity._multiUser, | ||
datasetIdValidated, | ||
datasetDirectoryName, | ||
organizationId, | ||
annotationId, | ||
workflowHash) | ||
_ <- bool2Fox(selectedOrganization._id != request.identity._organization) // User is already in correct orga, but still could not see dataset. Assume this had a reason. | ||
selectedOrganizationJs <- organizationService.publicWrites(selectedOrganization) | ||
} yield Ok(selectedOrganizationJs) | ||
} | ||
|
||
private def accessibleBySwitchingForSuperUser(datasetIdOpt: Option[ObjectId], | ||
datasetDirectoryNameOpt: Option[String], | ||
organizationIdOpt: Option[String], | ||
annotationIdOpt: Option[String], | ||
workflowHashOpt: Option[String]): Fox[Organization] = { | ||
implicit val ctx: DBAccessContext = GlobalAccessContext | ||
(datasetIdOpt, annotationIdOpt, workflowHashOpt) match { | ||
case (Some(datasetId), None, None) => | ||
(datasetIdOpt, datasetDirectoryNameOpt, organizationIdOpt, annotationIdOpt, workflowHashOpt) match { | ||
case (Some(datasetId), None, None, None, None) => | ||
for { | ||
dataset <- datasetDAO.findOne(datasetId) | ||
organization <- organizationDAO.findOne(dataset._organization) | ||
} yield organization | ||
case (None, Some(annotationId), None) => | ||
case (None, Some(datasetDirectoryName), Some(organizationId), None, None) => | ||
for { | ||
dataset <- datasetDAO.findOneByDirectoryNameAndOrganization(datasetDirectoryName, organizationId) | ||
organization <- organizationDAO.findOne(dataset._organization) | ||
} yield organization | ||
case (None, Some(annotationId), None, None, None) => | ||
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. Critical issue: Incorrect parameter mapping in pattern matching In the pattern matching expressions of case (None, Some(annotationId), None, None, None) => // ... Here, Apply this diff to correct the parameter mapping: - case (None, Some(annotationId), None, None, None) =>
+ case (None, None, None, Some(annotationId), None) => Ensure that Also applies to: 323-327 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. oh good catch. That's what I get for using ai auto completion 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. @MichaelBuessemeyer, I'm glad I could help catch that issue! Let me know if there's anything else I can assist with. If you found this review helpful, would you consider giving us a shout-out on X? |
||
for { | ||
annotationObjectId <- ObjectId.fromString(annotationId) | ||
annotation <- annotationDAO.findOne(annotationObjectId) // Note: this does not work for compound annotations. | ||
user <- userDAO.findOne(annotation._user) | ||
organization <- organizationDAO.findOne(user._organization) | ||
} yield organization | ||
case (None, None, Some(workflowHash)) => | ||
case (None, None, None, None, Some(workflowHash)) => | ||
for { | ||
workflow <- voxelyticsDAO.findWorkflowByHash(workflowHash) | ||
organization <- organizationDAO.findOne(workflow._organization) | ||
|
@@ -277,26 +295,38 @@ class AuthenticationController @Inject()( | |
|
||
private def accessibleBySwitchingForMultiUser(multiUserId: ObjectId, | ||
datasetIdOpt: Option[ObjectId], | ||
datasetDirectoryNameOpt: Option[String], | ||
organizationIdOpt: Option[String], | ||
annotationIdOpt: Option[String], | ||
workflowHashOpt: Option[String]): Fox[Organization] = | ||
for { | ||
identities <- userDAO.findAllByMultiUser(multiUserId) | ||
selectedIdentity <- Fox.find(identities)(identity => | ||
canAccessDatasetOrAnnotationOrWorkflow(identity, datasetIdOpt, annotationIdOpt, workflowHashOpt)) | ||
selectedIdentity <- Fox.find(identities)( | ||
identity => | ||
canAccessDatasetOrAnnotationOrWorkflow(identity, | ||
datasetIdOpt, | ||
datasetDirectoryNameOpt, | ||
organizationIdOpt, | ||
annotationIdOpt, | ||
workflowHashOpt)) | ||
selectedOrganization <- organizationDAO.findOne(selectedIdentity._organization)(GlobalAccessContext) | ||
} yield selectedOrganization | ||
|
||
private def canAccessDatasetOrAnnotationOrWorkflow(user: User, | ||
datasetIdOpt: Option[ObjectId], | ||
datasetDirectoryNameOpt: Option[String], | ||
organizationIdOpt: Option[String], | ||
annotationIdOpt: Option[String], | ||
workflowHashOpt: Option[String]): Fox[Boolean] = { | ||
val ctx = AuthorizedAccessContext(user) | ||
(datasetIdOpt, annotationIdOpt, workflowHashOpt) match { | ||
case (Some(datasetId), None, None) => | ||
(datasetIdOpt, datasetDirectoryNameOpt, organizationIdOpt, annotationIdOpt, workflowHashOpt) match { | ||
case (Some(datasetId), None, None, None, None) => | ||
canAccessDataset(ctx, datasetId) | ||
case (None, Some(annotationId), None) => | ||
case (None, Some(datasetDirectoryName), Some(organizationId), None, None) => | ||
canAccessDatasetByDirectoryNameAndOrganization(ctx, datasetDirectoryName, organizationId) | ||
case (None, Some(annotationId), None, None, None) => | ||
canAccessAnnotation(user, ctx, annotationId) | ||
case (None, None, Some(workflowHash)) => | ||
case (None, None, None, None, Some(workflowHash)) => | ||
canAccessWorkflow(user, workflowHash) | ||
case _ => Fox.failure("Can either test access for dataset or annotation or workflow, not a combination") | ||
} | ||
|
@@ -307,6 +337,13 @@ class AuthenticationController @Inject()( | |
foundFox.futureBox.map(_.isDefined) | ||
} | ||
|
||
private def canAccessDatasetByDirectoryNameAndOrganization(ctx: DBAccessContext, | ||
datasetDirectoryName: String, | ||
organizationId: String): Fox[Boolean] = { | ||
val foundFox = datasetDAO.findOneByDirectoryNameAndOrganization(datasetDirectoryName, organizationId)(ctx) | ||
foundFox.futureBox.map(_.isDefined) | ||
} | ||
|
||
private def canAccessAnnotation(user: User, ctx: DBAccessContext, annotationId: String): Fox[Boolean] = { | ||
val foundFox = for { | ||
annotationIdParsed <- ObjectId.fromString(annotationId) | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,46 +1,53 @@ | ||||||
import type { RouteComponentProps } from "react-router-dom"; | ||||||
import { withRouter } from "react-router-dom"; | ||||||
import React from "react"; | ||||||
import type React from "react"; | ||||||
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. Correct the import statement for React The import statement Please update the import statement to include the React namespace: -import type React from "react";
+import React from "react"; 📝 Committable suggestion
Suggested change
|
||||||
import { useState } from "react"; | ||||||
import { useEffectOnlyOnce } from "libs/react_hooks"; | ||||||
|
||||||
type Props = { | ||||||
redirectTo: () => Promise<string>; | ||||||
history: RouteComponentProps["history"]; | ||||||
pushToHistory?: boolean; | ||||||
errorComponent?: React.ReactNode; | ||||||
}; | ||||||
|
||||||
class AsyncRedirect extends React.PureComponent<Props> { | ||||||
static defaultProps = { | ||||||
pushToHistory: true, | ||||||
}; | ||||||
|
||||||
componentDidMount() { | ||||||
this.redirect(); | ||||||
} | ||||||
|
||||||
async redirect() { | ||||||
const newPath = await this.props.redirectTo(); | ||||||
|
||||||
if (newPath.startsWith(location.origin)) { | ||||||
// The link is absolute which react-router does not support | ||||||
// apparently. See https://stackoverflow.com/questions/42914666/react-router-external-link | ||||||
if (this.props.pushToHistory) { | ||||||
location.assign(newPath); | ||||||
} else { | ||||||
location.replace(newPath); | ||||||
const AsyncRedirect: React.FC<Props> = ({ | ||||||
redirectTo, | ||||||
history, | ||||||
pushToHistory = true, | ||||||
errorComponent, | ||||||
}: Props) => { | ||||||
const [hasError, setHasError] = useState(false); | ||||||
useEffectOnlyOnce(() => { | ||||||
const performRedirect = async () => { | ||||||
try { | ||||||
const newPath = await redirectTo(); | ||||||
|
||||||
if (newPath.startsWith(location.origin)) { | ||||||
// The link is absolute which react-router does not support | ||||||
// apparently. See https://stackoverflow.com/questions/42914666/react-router-external-link | ||||||
if (pushToHistory) { | ||||||
location.assign(newPath); | ||||||
} else { | ||||||
location.replace(newPath); | ||||||
} | ||||||
return; | ||||||
} | ||||||
|
||||||
if (pushToHistory) { | ||||||
history.push(newPath); | ||||||
} else { | ||||||
history.replace(newPath); | ||||||
} | ||||||
} catch (e) { | ||||||
setHasError(true); | ||||||
throw e; | ||||||
} | ||||||
return; | ||||||
} | ||||||
}; | ||||||
performRedirect(); | ||||||
}); | ||||||
|
||||||
if (this.props.pushToHistory) { | ||||||
this.props.history.push(newPath); | ||||||
} else { | ||||||
this.props.history.replace(newPath); | ||||||
} | ||||||
} | ||||||
|
||||||
render() { | ||||||
return null; | ||||||
} | ||||||
} | ||||||
return hasError && errorComponent ? errorComponent : null; | ||||||
}; | ||||||
|
||||||
export default withRouter<RouteComponentProps & Props, any>(AsyncRedirect); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
import { createExplorational, getAnnotationInformation, getShortLink } from "admin/admin_rest_api"; | ||
import { | ||
createExplorational, | ||
getAnnotationInformation, | ||
getShortLink, | ||
isDatasetAccessibleBySwitching, | ||
} from "admin/admin_rest_api"; | ||
import AcceptInviteView from "admin/auth/accept_invite_view"; | ||
import AuthTokenView from "admin/auth/auth_token_view"; | ||
import ChangePasswordView from "admin/auth/change_password_view"; | ||
|
@@ -44,7 +49,7 @@ import type { OxalisState } from "oxalis/store"; | |
import HelpButton from "oxalis/view/help_modal"; | ||
import TracingLayoutView from "oxalis/view/layouting/tracing_layout_view"; | ||
import React from "react"; | ||
import { connect } from "react-redux"; | ||
import { connect, useSelector } from "react-redux"; | ||
// @ts-expect-error ts-migrate(2305) FIXME: Module '"react-router-dom"' has no exported member... Remove this comment to see the full error message | ||
import { type ContextRouter, Link, type RouteProps } from "react-router-dom"; | ||
import { Redirect, Route, Router, Switch } from "react-router-dom"; | ||
|
@@ -69,6 +74,9 @@ import { | |
getOrganizationForDataset, | ||
} from "admin/api/disambiguate_legacy_routes"; | ||
import { getDatasetIdOrNameFromReadableURLPart } from "oxalis/model/accessors/dataset_accessor"; | ||
import { useFetch } from "libs/react_helpers"; | ||
import { BrainSpinnerWithError, CoverWithLogin } from "components/brain_spinner"; | ||
import Toast from "libs/toast"; | ||
|
||
const { Content } = Layout; | ||
|
||
|
@@ -128,6 +136,37 @@ const SecuredRouteWithErrorBoundary: React.FC<GetComponentProps<typeof SecuredRo | |
); | ||
}; | ||
|
||
const LegacyLinkDisambiguateErrorView: React.FC<{ | ||
directoryName: string; | ||
organizationId: string; | ||
}> = ({ directoryName, organizationId }) => { | ||
const user = useSelector((state: OxalisState) => state.activeUser); | ||
const organizationToSwitchTo = useFetch( | ||
async () => { | ||
return user | ||
? isDatasetAccessibleBySwitching({ directoryName, organizationId, type: "VIEW" }) | ||
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. hmm, now
but maybe this isn't possible? wdyt? 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.
Sadly that's not possible in the current model of the backend. @fm3 what do you think about adjusting the permissions for the disambiguation route to include positive answers in case the user can access the dataset when they would switch into the correct organization?
Working with the old format is technically possible but here we have the reverse problem: In case the user opens a URL with the new schema 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. fine by me, don’t have a strong opinion here 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. Ok I'll implement 1. |
||
: null; | ||
}, | ||
null, | ||
[directoryName, organizationId, user], | ||
); | ||
return user ? ( | ||
<BrainSpinnerWithError | ||
gotUnhandledError={false} | ||
organizationToSwitchTo={organizationToSwitchTo} | ||
/> | ||
) : ( | ||
<CoverWithLogin | ||
onLoggedIn={() => { | ||
// Close existing error toasts for "Not Found" errors before trying again. | ||
// If they get relevant again, they will be recreated anyway. | ||
Toast.close("404"); | ||
location.reload(); | ||
}} | ||
/> | ||
); | ||
}; | ||
|
||
class ReactRouter extends React.Component<Props> { | ||
tracingView = ({ match }: ContextRouter) => { | ||
const initialMaybeCompoundType = | ||
|
@@ -198,16 +237,24 @@ class ReactRouter extends React.Component<Props> { | |
); | ||
}; | ||
|
||
tracingViewModeLegacy = ({ match, location }: ContextRouter) => ( | ||
<AsyncRedirect | ||
redirectTo={async () => { | ||
const datasetName = match.params.datasetName || ""; | ||
const organizationId = match.params.organizationId || ""; | ||
const datasetId = await getDatasetIdFromNameAndOrganization(datasetName, organizationId); | ||
return `/datasets/${datasetName}-${datasetId}/view${location.search}${location.hash}`; | ||
}} | ||
/> | ||
); | ||
tracingViewModeLegacy = ({ match, location }: ContextRouter) => { | ||
const datasetName = match.params.datasetName || ""; | ||
const organizationId = match.params.organizationId || ""; | ||
return ( | ||
<AsyncRedirect | ||
redirectTo={async () => { | ||
const datasetId = await getDatasetIdFromNameAndOrganization(datasetName, organizationId); | ||
return `/datasets/${datasetName}-${datasetId}/view${location.search}${location.hash}`; | ||
}} | ||
errorComponent={ | ||
<LegacyLinkDisambiguateErrorView | ||
directoryName={datasetName} | ||
organizationId={organizationId} | ||
/> | ||
} | ||
/> | ||
); | ||
}; | ||
|
||
tracingViewMode = ({ match }: ContextRouter) => { | ||
const { datasetId, datasetName } = getDatasetIdOrNameFromReadableURLPart( | ||
|
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.
🛠️ Refactor suggestion
Re-evaluate the organization comparison logic
The check:
Prevents switching organizations if the selected organization is the same as the current one. However, there may be valid scenarios where a user lacks access to a dataset within their current organization due to permission restrictions. Consider revising this logic to allow for permission checks even when the organization is the same, or provide a more specific error message to guide the user.