Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
71a0a298
Commit
71a0a298
authored
Feb 27, 2018
by
Marcus Efraimsson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
permissions: remove client validation and handle server validation
parent
f76b98d2
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
27 additions
and
105 deletions
+27
-105
public/app/core/components/Permissions/AddPermissions.jest.tsx
+1
-1
public/app/core/components/Permissions/AddPermissions.tsx
+0
-8
public/app/stores/PermissionsStore/PermissionsStore.jest.ts
+10
-65
public/app/stores/PermissionsStore/PermissionsStore.ts
+16
-31
No files found.
public/app/core/components/Permissions/AddPermissions.jest.tsx
View file @
71a0a298
...
@@ -17,7 +17,7 @@ describe('AddPermissions', () => {
...
@@ -17,7 +17,7 @@ describe('AddPermissions', () => {
])
])
);
);
backendSrv
.
post
=
jest
.
fn
();
backendSrv
.
post
=
jest
.
fn
(
()
=>
Promise
.
resolve
({})
);
store
=
RootStore
.
create
(
store
=
RootStore
.
create
(
{},
{},
...
...
public/app/core/components/Permissions/AddPermissions.tsx
View file @
71a0a298
...
@@ -135,14 +135,6 @@ class AddPermissions extends Component<IProps, any> {
...
@@ -135,14 +135,6 @@ class AddPermissions extends Component<IProps, any> {
</
div
>
</
div
>
</
div
>
</
div
>
</
form
>
</
form
>
{
permissions
.
error
?
(
<
div
className=
"gf-form width-17"
>
<
span
ng
-
if=
"ctrl.error"
className=
"text-error p-l-1"
>
<
i
className=
"fa fa-warning"
/>
{
permissions
.
error
}
</
span
>
</
div
>
)
:
null
}
</
div
>
</
div
>
);
);
}
}
...
...
public/app/stores/PermissionsStore/PermissionsStore.jest.ts
View file @
71a0a298
import
{
PermissionsStore
,
aclTypeValues
}
from
'./PermissionsStore'
;
import
{
PermissionsStore
}
from
'./PermissionsStore'
;
import
{
backendSrv
}
from
'test/mocks/common'
;
import
{
backendSrv
}
from
'test/mocks/common'
;
describe
(
'PermissionsStore'
,
()
=>
{
describe
(
'PermissionsStore'
,
()
=>
{
let
store
;
let
store
;
beforeEach
(()
=>
{
beforeEach
(
async
()
=>
{
backendSrv
.
get
.
mockReturnValue
(
backendSrv
.
get
.
mockReturnValue
(
Promise
.
resolve
([
Promise
.
resolve
([
{
id
:
2
,
dashboardId
:
1
,
role
:
'Viewer'
,
permission
:
1
,
permissionName
:
'View'
},
{
id
:
2
,
dashboardId
:
1
,
role
:
'Viewer'
,
permission
:
1
,
permissionName
:
'View'
},
...
@@ -20,7 +20,7 @@ describe('PermissionsStore', () => {
...
@@ -20,7 +20,7 @@ describe('PermissionsStore', () => {
])
])
);
);
backendSrv
.
post
=
jest
.
fn
();
backendSrv
.
post
=
jest
.
fn
(
()
=>
Promise
.
resolve
({})
);
store
=
PermissionsStore
.
create
(
store
=
PermissionsStore
.
create
(
{
{
...
@@ -32,14 +32,14 @@ describe('PermissionsStore', () => {
...
@@ -32,14 +32,14 @@ describe('PermissionsStore', () => {
}
}
);
);
return
store
.
load
(
1
,
false
,
false
);
await
store
.
load
(
1
,
false
,
false
);
});
});
it
(
'should save update on permission change'
,
()
=>
{
it
(
'should save update on permission change'
,
async
()
=>
{
expect
(
store
.
items
[
0
].
permission
).
toBe
(
1
);
expect
(
store
.
items
[
0
].
permission
).
toBe
(
1
);
expect
(
store
.
items
[
0
].
permissionName
).
toBe
(
'View'
);
expect
(
store
.
items
[
0
].
permissionName
).
toBe
(
'View'
);
store
.
updatePermissionOnIndex
(
0
,
2
,
'Edit'
);
await
store
.
updatePermissionOnIndex
(
0
,
2
,
'Edit'
);
expect
(
store
.
items
[
0
].
permission
).
toBe
(
2
);
expect
(
store
.
items
[
0
].
permission
).
toBe
(
2
);
expect
(
store
.
items
[
0
].
permissionName
).
toBe
(
'Edit'
);
expect
(
store
.
items
[
0
].
permissionName
).
toBe
(
'Edit'
);
...
@@ -47,69 +47,18 @@ describe('PermissionsStore', () => {
...
@@ -47,69 +47,18 @@ describe('PermissionsStore', () => {
expect
(
backendSrv
.
post
.
mock
.
calls
[
0
][
0
]).
toBe
(
'/api/dashboards/id/1/permissions'
);
expect
(
backendSrv
.
post
.
mock
.
calls
[
0
][
0
]).
toBe
(
'/api/dashboards/id/1/permissions'
);
});
});
it
(
'should save removed permissions automatically'
,
()
=>
{
it
(
'should save removed permissions automatically'
,
async
()
=>
{
expect
(
store
.
items
.
length
).
toBe
(
3
);
expect
(
store
.
items
.
length
).
toBe
(
3
);
store
.
removeStoreItem
(
2
);
await
store
.
removeStoreItem
(
2
);
expect
(
store
.
items
.
length
).
toBe
(
2
);
expect
(
store
.
items
.
length
).
toBe
(
2
);
expect
(
backendSrv
.
post
.
mock
.
calls
.
length
).
toBe
(
1
);
expect
(
backendSrv
.
post
.
mock
.
calls
.
length
).
toBe
(
1
);
expect
(
backendSrv
.
post
.
mock
.
calls
[
0
][
0
]).
toBe
(
'/api/dashboards/id/1/permissions'
);
expect
(
backendSrv
.
post
.
mock
.
calls
[
0
][
0
]).
toBe
(
'/api/dashboards/id/1/permissions'
);
});
});
describe
(
'when duplicate team permissions are added'
,
()
=>
{
beforeEach
(()
=>
{
const
newItem
=
{
teamId
:
10
,
team
:
'tester-team'
,
permission
:
1
,
dashboardId
:
1
,
};
store
.
resetNewType
();
store
.
newItem
.
setTeam
(
newItem
.
teamId
,
newItem
.
team
);
store
.
newItem
.
setPermission
(
newItem
.
permission
);
store
.
addStoreItem
();
store
.
newItem
.
setTeam
(
newItem
.
teamId
,
newItem
.
team
);
store
.
newItem
.
setPermission
(
newItem
.
permission
);
store
.
addStoreItem
();
});
it
(
'should return a validation error'
,
()
=>
{
expect
(
store
.
items
.
length
).
toBe
(
4
);
expect
(
store
.
error
).
toBe
(
'This permission exists already.'
);
expect
(
backendSrv
.
post
.
mock
.
calls
.
length
).
toBe
(
1
);
});
});
describe
(
'when duplicate user permissions are added'
,
()
=>
{
beforeEach
(()
=>
{
expect
(
store
.
items
.
length
).
toBe
(
3
);
const
newItem
=
{
userId
:
10
,
userLogin
:
'tester1'
,
permission
:
1
,
dashboardId
:
1
,
};
store
.
setNewType
(
aclTypeValues
.
USER
.
value
);
store
.
newItem
.
setUser
(
newItem
.
userId
,
newItem
.
userLogin
);
store
.
newItem
.
setPermission
(
newItem
.
permission
);
store
.
addStoreItem
();
store
.
setNewType
(
aclTypeValues
.
USER
.
value
);
store
.
newItem
.
setUser
(
newItem
.
userId
,
newItem
.
userLogin
);
store
.
newItem
.
setPermission
(
newItem
.
permission
);
store
.
addStoreItem
();
});
it
(
'should return a validation error'
,
()
=>
{
expect
(
store
.
items
.
length
).
toBe
(
4
);
expect
(
store
.
error
).
toBe
(
'This permission exists already.'
);
expect
(
backendSrv
.
post
.
mock
.
calls
.
length
).
toBe
(
1
);
});
});
describe
(
'when one inherited and one not inherited team permission are added'
,
()
=>
{
describe
(
'when one inherited and one not inherited team permission are added'
,
()
=>
{
beforeEach
(()
=>
{
beforeEach
(
async
()
=>
{
const
overridingItemForChildDashboard
=
{
const
overridingItemForChildDashboard
=
{
team
:
'MyTestTeam'
,
team
:
'MyTestTeam'
,
dashboardId
:
1
,
dashboardId
:
1
,
...
@@ -120,11 +69,7 @@ describe('PermissionsStore', () => {
...
@@ -120,11 +69,7 @@ describe('PermissionsStore', () => {
store
.
resetNewType
();
store
.
resetNewType
();
store
.
newItem
.
setTeam
(
overridingItemForChildDashboard
.
teamId
,
overridingItemForChildDashboard
.
team
);
store
.
newItem
.
setTeam
(
overridingItemForChildDashboard
.
teamId
,
overridingItemForChildDashboard
.
team
);
store
.
newItem
.
setPermission
(
overridingItemForChildDashboard
.
permission
);
store
.
newItem
.
setPermission
(
overridingItemForChildDashboard
.
permission
);
store
.
addStoreItem
();
await
store
.
addStoreItem
();
});
it
(
'should allowing overriding the inherited permission and not throw a validation error'
,
()
=>
{
expect
(
store
.
error
).
toBe
(
null
);
});
});
it
(
'should add new overriding permission'
,
()
=>
{
it
(
'should add new overriding permission'
,
()
=>
{
...
...
public/app/stores/PermissionsStore/PermissionsStore.ts
View file @
71a0a298
import
{
types
,
getEnv
,
flow
}
from
'mobx-state-tree'
;
import
{
types
,
getEnv
,
flow
}
from
'mobx-state-tree'
;
import
{
PermissionsStoreItem
}
from
'./PermissionsStoreItem'
;
import
{
PermissionsStoreItem
}
from
'./PermissionsStoreItem'
;
const
duplicateError
=
'This permission exists already.'
;
export
const
permissionOptions
=
[
export
const
permissionOptions
=
[
{
value
:
1
,
label
:
'View'
,
description
:
'Can view dashboards.'
},
{
value
:
1
,
label
:
'View'
,
description
:
'Can view dashboards.'
},
{
value
:
2
,
label
:
'Edit'
,
description
:
'Can add, edit and delete dashboards.'
},
{
value
:
2
,
label
:
'Edit'
,
description
:
'Can add, edit and delete dashboards.'
},
...
@@ -75,7 +73,6 @@ export const PermissionsStore = types
...
@@ -75,7 +73,6 @@ export const PermissionsStore = types
isFolder
:
types
.
maybe
(
types
.
boolean
),
isFolder
:
types
.
maybe
(
types
.
boolean
),
dashboardId
:
types
.
maybe
(
types
.
number
),
dashboardId
:
types
.
maybe
(
types
.
number
),
items
:
types
.
optional
(
types
.
array
(
PermissionsStoreItem
),
[]),
items
:
types
.
optional
(
types
.
array
(
PermissionsStoreItem
),
[]),
error
:
types
.
maybe
(
types
.
string
),
originalItems
:
types
.
optional
(
types
.
array
(
PermissionsStoreItem
),
[]),
originalItems
:
types
.
optional
(
types
.
array
(
PermissionsStoreItem
),
[]),
newType
:
types
.
optional
(
types
.
string
,
defaultNewType
),
newType
:
types
.
optional
(
types
.
string
,
defaultNewType
),
newItem
:
types
.
maybe
(
NewPermissionsItem
),
newItem
:
types
.
maybe
(
NewPermissionsItem
),
...
@@ -88,7 +85,6 @@ export const PermissionsStore = types
...
@@ -88,7 +85,6 @@ export const PermissionsStore = types
return
isDuplicate
(
it
,
item
);
return
isDuplicate
(
it
,
item
);
});
});
if
(
dupe
)
{
if
(
dupe
)
{
self
.
error
=
duplicateError
;
return
false
;
return
false
;
}
}
...
@@ -96,8 +92,7 @@ export const PermissionsStore = types
...
@@ -96,8 +92,7 @@ export const PermissionsStore = types
},
},
}))
}))
.
actions
(
self
=>
{
.
actions
(
self
=>
{
const
resetNewType
=
()
=>
{
const
resetNewTypeInternal
=
()
=>
{
self
.
error
=
null
;
self
.
newItem
=
NewPermissionsItem
.
create
();
self
.
newItem
=
NewPermissionsItem
.
create
();
};
};
...
@@ -115,11 +110,9 @@ export const PermissionsStore = types
...
@@ -115,11 +110,9 @@ export const PermissionsStore = types
self
.
items
=
items
;
self
.
items
=
items
;
self
.
originalItems
=
items
;
self
.
originalItems
=
items
;
self
.
fetching
=
false
;
self
.
fetching
=
false
;
self
.
error
=
null
;
}),
}),
addStoreItem
:
flow
(
function
*
addStoreItem
()
{
addStoreItem
:
flow
(
function
*
addStoreItem
()
{
self
.
error
=
null
;
let
item
=
{
let
item
=
{
type
:
self
.
newItem
.
type
,
type
:
self
.
newItem
.
type
,
permission
:
self
.
newItem
.
permission
,
permission
:
self
.
newItem
.
permission
,
...
@@ -147,19 +140,21 @@ export const PermissionsStore = types
...
@@ -147,19 +140,21 @@ export const PermissionsStore = types
throw
Error
(
'Unknown type: '
+
self
.
newItem
.
type
);
throw
Error
(
'Unknown type: '
+
self
.
newItem
.
type
);
}
}
if
(
!
self
.
isValid
(
item
))
{
const
updatedItems
=
self
.
items
.
peek
();
return
undefined
;
const
newItem
=
prepareItem
(
item
,
self
.
dashboardId
,
self
.
isFolder
,
self
.
isInRoot
)
;
}
updatedItems
.
push
(
newItem
);
self
.
items
.
push
(
prepareItem
(
item
,
self
.
dashboardId
,
self
.
isFolder
,
self
.
isInRoot
));
try
{
resetNewType
();
yield
updateItems
(
self
,
updatedItems
);
return
updateItems
(
self
);
self
.
items
.
push
(
newItem
);
resetNewTypeInternal
();
}
catch
{}
yield
Promise
.
resolve
();
}),
}),
removeStoreItem
:
flow
(
function
*
removeStoreItem
(
idx
:
number
)
{
removeStoreItem
:
flow
(
function
*
removeStoreItem
(
idx
:
number
)
{
self
.
error
=
null
;
self
.
items
.
splice
(
idx
,
1
);
self
.
items
.
splice
(
idx
,
1
);
return
updateItems
(
self
);
yield
updateItems
(
self
,
self
.
items
.
peek
()
);
}),
}),
updatePermissionOnIndex
:
flow
(
function
*
updatePermissionOnIndex
(
updatePermissionOnIndex
:
flow
(
function
*
updatePermissionOnIndex
(
...
@@ -167,9 +162,8 @@ export const PermissionsStore = types
...
@@ -167,9 +162,8 @@ export const PermissionsStore = types
permission
:
number
,
permission
:
number
,
permissionName
:
string
permissionName
:
string
)
{
)
{
self
.
error
=
null
;
self
.
items
[
idx
].
updatePermission
(
permission
,
permissionName
);
self
.
items
[
idx
].
updatePermission
(
permission
,
permissionName
);
return
updateItems
(
self
);
yield
updateItems
(
self
,
self
.
items
.
peek
()
);
}),
}),
setNewType
(
newType
:
string
)
{
setNewType
(
newType
:
string
)
{
...
@@ -177,7 +171,7 @@ export const PermissionsStore = types
...
@@ -177,7 +171,7 @@ export const PermissionsStore = types
},
},
resetNewType
()
{
resetNewType
()
{
resetNewType
();
resetNewType
Internal
();
},
},
toggleAddPermissions
()
{
toggleAddPermissions
()
{
...
@@ -190,12 +184,10 @@ export const PermissionsStore = types
...
@@ -190,12 +184,10 @@ export const PermissionsStore = types
};
};
});
});
const
updateItems
=
self
=>
{
const
updateItems
=
(
self
,
items
)
=>
{
self
.
error
=
null
;
const
backendSrv
=
getEnv
(
self
).
backendSrv
;
const
backendSrv
=
getEnv
(
self
).
backendSrv
;
const
updated
=
[];
const
updated
=
[];
for
(
let
item
of
self
.
items
)
{
for
(
let
item
of
items
)
{
if
(
item
.
inherited
)
{
if
(
item
.
inherited
)
{
continue
;
continue
;
}
}
...
@@ -208,16 +200,9 @@ const updateItems = self => {
...
@@ -208,16 +200,9 @@ const updateItems = self => {
});
});
}
}
let
res
;
return
backendSrv
.
post
(
`/api/dashboards/id/
${
self
.
dashboardId
}
/permissions`
,
{
try
{
res
=
backendSrv
.
post
(
`/api/dashboards/id/
${
self
.
dashboardId
}
/permissions`
,
{
items
:
updated
,
items
:
updated
,
});
});
}
catch
(
error
)
{
self
.
error
=
error
;
}
return
res
;
};
};
const
prepareServerResponse
=
(
response
,
dashboardId
:
number
,
isFolder
:
boolean
,
isInRoot
:
boolean
)
=>
{
const
prepareServerResponse
=
(
response
,
dashboardId
:
number
,
isFolder
:
boolean
,
isInRoot
:
boolean
)
=>
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment