From cdc825cc6a33f69a4b7cedcb51230b3de47acfb3 Mon Sep 17 00:00:00 2001 From: Matthew Dillon Date: Sat, 4 Jul 2015 11:34:31 -0700 Subject: [PATCH] Moving authorization to utils --- app/pods/components/add-button/component.js | 7 ++++--- app/pods/components/species-details/component.js | 8 +++----- app/pods/components/strain-details/component.js | 8 +++----- app/utils/user-can-add.js | 3 +++ app/utils/user-can-edit.js | 5 +++++ tests/unit/utils/user-can-add-test.js | 10 ++++++++++ tests/unit/utils/user-can-edit-test.js | 10 ++++++++++ 7 files changed, 38 insertions(+), 13 deletions(-) create mode 100644 app/utils/user-can-add.js create mode 100644 app/utils/user-can-edit.js create mode 100644 tests/unit/utils/user-can-add-test.js create mode 100644 tests/unit/utils/user-can-edit-test.js diff --git a/app/pods/components/add-button/component.js b/app/pods/components/add-button/component.js index 857bde3..8075297 100644 --- a/app/pods/components/add-button/component.js +++ b/app/pods/components/add-button/component.js @@ -1,8 +1,9 @@ import Ember from 'ember'; +import userCanAdd from '../../../utils/user-can-add'; export default Ember.Component.extend({ canAdd: function() { - let role = this.get('session.currentUser.role'); - return (role === 'W') || (role === 'A'); - }.property('session.currentUser.role') + let user_role = this.get('session.currentUser.role'); + return userCanAdd(user_role); + }.property('session.currentUser.role').readOnly(), }); diff --git a/app/pods/components/species-details/component.js b/app/pods/components/species-details/component.js index 2950361..6070141 100644 --- a/app/pods/components/species-details/component.js +++ b/app/pods/components/species-details/component.js @@ -1,15 +1,13 @@ import Ember from 'ember'; +import userCanEdit from '../../../utils/user-can-edit'; export default Ember.Component.extend({ classNames: ['grid-1'], isEditing: false, canEdit: function() { - let role = this.get('session.currentUser.role'); - let id = this.get('session.currentUser.id'); - let author = this.get('species.createdBy'); - return (role === 'W' && (+id === author)) || (role === 'A'); - }.property('session.currentUser.role', 'session.currentUser.id', 'species.createdBy'), + return userCanEdit(this.get('session.currentUser'), this.get('species.createdBy')); + }.property('session.currentUser', 'species.createdBy').readOnly(), actions: { save: function() { diff --git a/app/pods/components/strain-details/component.js b/app/pods/components/strain-details/component.js index 87949ee..b483565 100644 --- a/app/pods/components/strain-details/component.js +++ b/app/pods/components/strain-details/component.js @@ -1,15 +1,13 @@ import Ember from 'ember'; +import userCanEdit from '../../../utils/user-can-edit'; export default Ember.Component.extend({ classNames: ['grid-1'], isEditing: false, canEdit: function() { - let role = this.get('session.currentUser.role'); - let id = this.get('session.currentUser.id'); - let author = this.get('strain.createdBy'); - return (role === 'W' && (+id === author)) || (role === 'A'); - }.property('session.currentUser.role', 'session.currentUser.id', 'strain.createdBy'), + return userCanEdit(this.get('session.currentUser'), this.get('strain.createdBy')); + }.property('session.currentUser', 'strain.createdBy').readOnly(), actions: { save: function() { diff --git a/app/utils/user-can-add.js b/app/utils/user-can-add.js new file mode 100644 index 0000000..68a135b --- /dev/null +++ b/app/utils/user-can-add.js @@ -0,0 +1,3 @@ +export default function userCanAdd(role) { + return (role === 'W') || (role === 'A'); +} diff --git a/app/utils/user-can-edit.js b/app/utils/user-can-edit.js new file mode 100644 index 0000000..20146d5 --- /dev/null +++ b/app/utils/user-can-edit.js @@ -0,0 +1,5 @@ +export default function userCanEdit(currentUser, author) { + let id = currentUser.id; + let role = currentUser.role; + return (role === 'W' && (+id === author)) || (role === 'A'); +} diff --git a/tests/unit/utils/user-can-add-test.js b/tests/unit/utils/user-can-add-test.js new file mode 100644 index 0000000..d94174e --- /dev/null +++ b/tests/unit/utils/user-can-add-test.js @@ -0,0 +1,10 @@ +import userCanAdd from '../../../utils/user-can-add'; +import { module, test } from 'qunit'; + +module('Unit | Utility | user can add'); + +// Replace this with your real tests. +test('it works', function(assert) { + var result = userCanAdd(); + assert.ok(result); +}); diff --git a/tests/unit/utils/user-can-edit-test.js b/tests/unit/utils/user-can-edit-test.js new file mode 100644 index 0000000..a14b4da --- /dev/null +++ b/tests/unit/utils/user-can-edit-test.js @@ -0,0 +1,10 @@ +import userCanEdit from '../../../utils/user-can-edit'; +import { module, test } from 'qunit'; + +module('Unit | Utility | user can edit'); + +// Replace this with your real tests. +test('it works', function(assert) { + var result = userCanEdit(); + assert.ok(result); +});