make it a util

This commit is contained in:
Lisa P 2016-03-16 14:18:57 +01:00 committed by Piotr Sarnacki
parent 65cf81169b
commit 26ca2875ae
6 changed files with 50 additions and 47 deletions

View File

@ -1,9 +1,9 @@
import Ember from 'ember';
import { githubCommit as githubCommitUrl } from 'travis/utils/urls';
import config from 'travis/config/environment';
import Permissions from 'travis/mixins/permissions';
import { hasAdminPermission, hasPushPermission } from 'travis/utils/permission';
export default Ember.Component.extend(Permissions, {
export default Ember.Component.extend({
tagName: 'li',
classNameBindings: ['repo.default_branch.last_build.state'],
classNames: ['rows', 'rows--dashboard'],
@ -17,11 +17,11 @@ export default Ember.Component.extend(Permissions, {
}.property('repo'),
displayMenuTofu: function() {
return this.hasPushPermission(this.get('currentUser'), this.get('repo.id'));
return hasPushPermission(this.get('currentUser'), this.get('repo.id'));
},
displayActivateLink: function() {
return this.hasAdminPermission(this.get('currentUser'), this.get('repo.id'));
return hasAdminPermission(this.get('currentUser'), this.get('repo.id'));
},
actions: {

View File

@ -1,8 +1,8 @@
import Ember from 'ember';
import config from 'travis/config/environment';
import Permissions from 'travis/mixins/permissions';
import { hasPermission, hasPushPermission } from 'travis/utils/permission';
export default Ember.Component.extend(Permissions, {
export default Ember.Component.extend({
popup: Ember.inject.service(),
classNames: ['option-button'],
classNameBindings: ['isOpen:display'],
@ -24,15 +24,15 @@ export default Ember.Component.extend(Permissions, {
}
},
displaySettingsLink: function() {
return this.hasPushPermission(this.get('currentUser'), this.get('repo.id'));
return hasPushPermission(this.get('currentUser'), this.get('repo.id'));
}.property('currentUser.pushPermissions', 'repo.id'),
displayCachesLink: function() {
return this.hasPushPermission(this.get('currentUser'), this.get('repo.id')) && config.endpoints.caches;
return hasPushPermission(this.get('currentUser'), this.get('repo.id')) && config.endpoints.caches;
}.property('currentUser.pushPermissions', 'repo.id'),
displayStatusImages: function() {
return this.hasPermission(this.get('currentUser'), this.get('repo.id'));
return hasPermission(this.get('currentUser'), this.get('repo.id'));
}.property('currentUser.permissions', 'repo.id')
});

View File

@ -1,26 +0,0 @@
import Ember from 'ember';
export default Ember.Mixin.create({
hasPermission: function(currentUser, repoId) {
var id = parseInt(repoId);
var permissions;
if (permissions = this.get('currentUser.permissions')) {
return permissions.contains(id);
}
},
hasPushPermission: function(currentUser, repoId) {
var id = parseInt(repoId);
var permissions;
if (permissions = this.get('currentUser.pushPermissions')) {
return permissions.contains(id);
}
},
hasAdminPermission: function(currentUser, repoId) {
var id = parseInt(repoId);
var permissions;
if (permissions = this.get('currentUser.adminPermissions')) {
return permissions.contains(id);
}
}
});

26
app/utils/permission.js Normal file
View File

@ -0,0 +1,26 @@
var hasPermission = function(currentUser, repoId) {
var id = parseInt(repoId);
var permissions;
if (permissions = currentUser.get('permissions')) {
return permissions.contains(id);
}
};
var hasPushPermission = function(currentUser, repoId) {
var id = parseInt(repoId);
var permissions;
if (permissions = currentUser.get('pushPermissions')) {
return permissions.contains(id);
}
};
var hasAdminPermission = function(currentUser, repoId) {
var id = parseInt(repoId);
var permissions;
if (permissions = currentUser.get('adminPermissions')) {
return permissions.contains(id);
}
};
export {hasPermission, hasPushPermission, hasAdminPermission};

View File

@ -1,12 +0,0 @@
import Ember from 'ember';
import PermissionsMixin from 'travis/mixins/permissions';
import { module, test } from 'qunit';
module('Unit | Mixin | permissions');
// Replace this with your real tests.
test('it works', function(assert) {
let PermissionsObject = Ember.Object.extend(PermissionsMixin);
let subject = PermissionsObject.create();
assert.ok(subject);
});

View File

@ -0,0 +1,15 @@
import Ember from 'ember';
import { hasPermission } from 'travis/utils/permission';
import { module, test } from 'qunit';
module('Unit | Utility | permission');
test('it checks permissions', function(assert) {
let user = Ember.Object.create({permissions: [1]});
assert.ok(hasPermission(user, 1));
assert.ok(!hasPermission(user, 2));
});