Commit 71a0a298 by Marcus Efraimsson

permissions: remove client validation and handle server validation

parent f76b98d2
...@@ -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(
{}, {},
......
...@@ -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>
); );
} }
......
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', () => {
......
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(); resetNewTypeInternal();
}, },
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) => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment