-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
110 changed files
with
5,403 additions
and
454 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+150 Bytes
(100%)
...ettings.test.js-snapshots/Settings-checks-the-page-visuals-1-chromium-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+162 Bytes
(100%)
...ettings.test.js-snapshots/Settings-checks-the-page-visuals-3-chromium-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import React, { createContext, useContext, type ReactNode } from 'react'; | ||
import { useSelector } from 'react-redux'; | ||
|
||
import { type State } from 'loot-core/client/state-types'; | ||
|
||
import { useServerURL } from '../components/ServerContext'; | ||
|
||
import { type Permissions } from './types'; | ||
|
||
type AuthContextType = { | ||
hasPermission: (permission?: Permissions) => boolean; | ||
}; | ||
|
||
const AuthContext = createContext<AuthContextType | undefined>(undefined); | ||
|
||
type AuthProviderProps = { | ||
children?: ReactNode; | ||
}; | ||
|
||
export const AuthProvider = ({ children }: AuthProviderProps) => { | ||
const userData = useSelector((state: State) => state.user.data); | ||
const serverUrl = useServerURL(); | ||
|
||
const hasPermission = (permission?: Permissions) => { | ||
if (!permission) { | ||
return true; | ||
} | ||
|
||
return ( | ||
!serverUrl || | ||
userData?.permission?.toUpperCase() === permission?.toUpperCase() | ||
); | ||
}; | ||
|
||
return ( | ||
<AuthContext.Provider value={{ hasPermission }}> | ||
{children} | ||
</AuthContext.Provider> | ||
); | ||
}; | ||
|
||
export const useAuth = () => { | ||
const context = useContext(AuthContext); | ||
if (context === undefined) { | ||
throw new Error('useAuth must be used within an AuthProvider'); | ||
} | ||
return context; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { useEffect, useState, type ReactElement } from 'react'; | ||
import { useSelector } from 'react-redux'; | ||
|
||
import { type RemoteFile, type SyncedLocalFile } from 'loot-core/types/file'; | ||
|
||
import { View } from '../components/common/View'; | ||
import { useMetadataPref } from '../hooks/useMetadataPref'; | ||
|
||
import { useAuth } from './AuthProvider'; | ||
import { type Permissions } from './types'; | ||
|
||
type ProtectedRouteProps = { | ||
permission: Permissions; | ||
element: ReactElement; | ||
validateOwner?: boolean; | ||
}; | ||
|
||
export const ProtectedRoute = ({ | ||
element, | ||
permission, | ||
validateOwner, | ||
}: ProtectedRouteProps) => { | ||
const { hasPermission } = useAuth(); | ||
const [permissionGranted, setPermissionGranted] = useState(false); | ||
const [cloudFileId] = useMetadataPref('cloudFileId'); | ||
const allFiles = useSelector(state => state.budgets.allFiles || []); | ||
const remoteFiles = allFiles.filter( | ||
(f): f is SyncedLocalFile | RemoteFile => | ||
f.state === 'remote' || f.state === 'synced' || f.state === 'detached', | ||
); | ||
const currentFile = remoteFiles.find(f => f.cloudFileId === cloudFileId); | ||
const userData = useSelector(state => state.user.data); | ||
|
||
useEffect(() => { | ||
const hasRequiredPermission = hasPermission(permission); | ||
setPermissionGranted(hasRequiredPermission); | ||
|
||
if (!hasRequiredPermission && validateOwner) { | ||
if (currentFile) { | ||
setPermissionGranted( | ||
currentFile.usersWithAccess.some(u => u.userId === userData?.userId), | ||
); | ||
} | ||
} | ||
}, [ | ||
cloudFileId, | ||
permission, | ||
validateOwner, | ||
hasPermission, | ||
currentFile, | ||
userData, | ||
]); | ||
|
||
return permissionGranted ? ( | ||
element | ||
) : ( | ||
<View | ||
style={{ | ||
margin: '50px', | ||
}} | ||
> | ||
<h3>You don't have permission to view this page</h3> | ||
</View> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export enum Permissions { | ||
ADMINISTRATOR = 'ADMIN', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.