Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
dashboard
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
AURA
dashboard
Commits
88502bce
Commit
88502bce
authored
11 months ago
by
Konrad Mohrfeldt
Browse files
Options
Downloads
Patches
Plain Diff
feat: add auth store shorthands for testing permissions and ownership
parent
b8a8f51a
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/stores/auth.ts
+29
-10
29 additions, 10 deletions
src/stores/auth.ts
with
29 additions
and
10 deletions
src/stores/auth.ts
+
29
−
10
View file @
88502bce
...
...
@@ -101,19 +101,26 @@ export function useOnAuthBehaviour(behaviour: (user?: SteeringUser) => unknown |
export
type
Authorizer
=
(
userPermissions
:
string
[])
=>
boolean
function
_hasUserPermission
(
user
:
SteeringUser
|
undefined
|
null
,
requiredPermissions
:
string
[]
|
Authorizer
,
)
{
if
(
user
?.
isPrivileged
)
return
true
const
userPermissions
=
(
user
?.
permissions
??
[])
as
string
[]
const
permissionChecker
=
requiredPermissions
if
(
typeof
permissionChecker
===
'
function
'
)
return
permissionChecker
(
userPermissions
)
else
{
return
(
permissionChecker
.
length
===
0
||
permissionChecker
.
every
((
perm
)
=>
userPermissions
.
includes
(
perm
))
)
}
}
export
function
useHasUserPermission
(
requiredPermissions
:
MaybeRefOrGetter
<
string
[]
|
Authorizer
>
)
{
const
authStore
=
useAuthStore
()
return
computed
(()
=>
{
if
(
authStore
.
steeringUser
?.
isPrivileged
)
return
true
const
userPermissions
=
(
authStore
.
steeringUser
?.
permissions
??
[])
as
string
[]
const
permissionChecker
=
toValue
(
requiredPermissions
)
if
(
typeof
permissionChecker
===
'
function
'
)
return
permissionChecker
(
userPermissions
)
else
{
return
(
permissionChecker
.
length
===
0
||
permissionChecker
.
every
((
perm
)
=>
userPermissions
.
includes
(
perm
))
)
}
return
_hasUserPermission
(
authStore
.
steeringUser
,
toValue
(
requiredPermissions
))
})
}
...
...
@@ -203,11 +210,23 @@ export const useAuthStore = defineStore('auth', () => {
}
}
function
hasUserPermission
(
requiredPermissions
:
string
[]
|
Authorizer
)
{
return
_hasUserPermission
(
_steeringUser
.
value
,
requiredPermissions
)
}
function
isOwner
(
ownedBy
:
SteeringUser
[
'
id
'
][],
privilegedOwnsEverything
=
true
)
{
const
user
=
steeringUser
.
value
if
(
!
user
)
return
false
return
ownedBy
.
includes
(
user
.
id
)
||
(
privilegedOwnsEverything
&&
user
.
isPrivileged
)
}
return
{
currentUser
:
shallowReadonly
(
currentUser
),
steeringUser
,
sessionInitializationState
:
readonly
(
sessionInitializationState
),
isInitializingSession
:
readonly
(
isInitializingSession
),
hasUserPermission
,
isOwner
,
init
,
}
})
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment