Skip to content

Commit 6a38590

Browse files
committed
fix: check for permissions on reconnection
1 parent 2743754 commit 6a38590

File tree

2 files changed

+22
-34
lines changed

2 files changed

+22
-34
lines changed

packages/webui/src/client/ui/App.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ export const App: React.FC = function App() {
5252

5353
const [lastStart] = useState(Date.now())
5454

55-
const [roles, rolesReady] = useUserPermissions()
56-
console.log('rolesReady', rolesReady, roles)
55+
const [roles, _rolesReady] = useUserPermissions()
5756
const featureFlags = useFeatureFlags()
5857

5958
useEffect(() => {

packages/webui/src/client/ui/UserPermissions.tsx

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ import { parse as queryStringParse } from 'query-string'
1515
import { MeteorCall } from '../lib/meteorApi'
1616
import { UserPermissions } from '@sofie-automation/meteor-lib/dist/userPermissions'
1717
import { Settings } from '../lib/Settings'
18+
import { useTracker } from '../lib/ReactMeteorData/ReactMeteorData'
19+
import { Meteor } from 'meteor/meteor'
1820

1921
export type { UserPermissions }
2022

21-
export const UserPermissionsContext = React.createContext<Readonly<UserPermissions>>({
23+
const NO_PERMISSIONS: UserPermissions = Object.freeze({
2224
studio: false,
2325
configure: false,
2426
developer: false,
@@ -27,21 +29,16 @@ export const UserPermissionsContext = React.createContext<Readonly<UserPermissio
2729
gateway: false,
2830
})
2931

32+
export const UserPermissionsContext = React.createContext<Readonly<UserPermissions>>(NO_PERMISSIONS)
33+
3034
export function useUserPermissions(): [roles: UserPermissions, ready: boolean] {
3135
const location = window.location
3236

3337
const [ready, setReady] = useState(!Settings.enableHeaderAuth)
3438

3539
const [permissions, setPermissions] = useState<UserPermissions>(
3640
Settings.enableHeaderAuth
37-
? {
38-
studio: false,
39-
configure: false,
40-
developer: false,
41-
testing: false,
42-
service: false,
43-
gateway: false,
44-
}
41+
? NO_PERMISSIONS
4542
: {
4643
studio: getLocalAllowStudio(),
4744
configure: getLocalAllowConfigure(),
@@ -52,44 +49,36 @@ export function useUserPermissions(): [roles: UserPermissions, ready: boolean] {
5249
}
5350
)
5451

52+
const isConnected = useTracker(() => Meteor.status().connected, [], false)
53+
5554
useEffect(() => {
5655
if (!Settings.enableHeaderAuth) return
5756

58-
const interval = setInterval(() => {
59-
// TODO - this is a temorary hack!
60-
// TODO - this should also be triggered by ddp reconnecting
57+
// Do nothing when not connected. Persist the previous values.
58+
if (!isConnected) return
59+
60+
const checkPermissions = () => {
6161
MeteorCall.user
6262
.getUserPermissions()
6363
.then((v) => {
64-
setPermissions(
65-
v || {
66-
studio: false,
67-
configure: false,
68-
developer: false,
69-
testing: false,
70-
service: false,
71-
gateway: false,
72-
}
73-
)
64+
setPermissions(v || NO_PERMISSIONS)
7465
setReady(true)
7566
})
7667
.catch((e) => {
7768
console.error('Failed to set level', e)
78-
setPermissions({
79-
studio: false,
80-
configure: false,
81-
developer: false,
82-
testing: false,
83-
service: false,
84-
gateway: false,
85-
})
69+
setPermissions(NO_PERMISSIONS)
8670
})
87-
}, 30000) // Arbitrary poll interval
71+
}
72+
73+
const interval = setInterval(checkPermissions, 30000) // Arbitrary poll interval
74+
75+
// Initial check now
76+
checkPermissions()
8877

8978
return () => {
9079
clearInterval(interval)
9180
}
92-
}, [Settings.enableHeaderAuth])
81+
}, [Settings.enableHeaderAuth, isConnected])
9382

9483
useEffect(() => {
9584
if (Settings.enableHeaderAuth) return

0 commit comments

Comments
 (0)