put permissions into a mixin

This commit is contained in:
Lisa P 2016-03-15 13:20:17 +01:00 committed by Piotr Sarnacki
parent 3618488151
commit 2220334265
3 changed files with 47 additions and 29 deletions

View File

@ -1,7 +1,8 @@
import Ember from 'ember';
import config from 'travis/config/environment';
import Permissions from 'travis/mixins/permissions';
export default Ember.Component.extend({
export default Ember.Component.extend(Permissions, {
popup: Ember.inject.service(),
classNames: ['option-button'],
classNameBindings: ['isOpen:display'],
@ -22,37 +23,16 @@ export default Ember.Component.extend({
return this.toggleProperty('isOpen');
}
},
hasPermission: function() {
var permissions;
if (permissions = this.get('currentUser.permissions')) {
return permissions.contains(parseInt(this.get('repo.id')));
}
}.property('currentUser.permissions.length', 'repo.id'),
hasPushPermission: function() {
var permissions;
if (permissions = this.get('currentUser.pushPermissions')) {
return permissions.contains(parseInt(this.get('repo.id')));
}
}.property('currentUser.pushPermissions.length', 'repo.id'),
hasAdminPermission: function() {
var permissions;
if (permissions = this.get('currentUser.adminPermissions')) {
return permissions.contains(parseInt(this.get('repo.id')));
}
}.property('currentUser.adminPermissions.length', 'repo.id'),
displaySettingsLink: function() {
return this.get('hasPushPermission');
}.property('hasPushPermission'),
return this.hasPushPermission(this.get('currentUser'), this.get('repo.id'));
}.property('currentUser.pushPermissions', 'repo.id'),
displayCachesLink: function() {
return this.get('hasPushPermission') && config.endpoints.caches;
}.property('hasPushPermission'),
return this.hasPushPermission(this.get('currentUser'), this.get('repo.id')) && config.endpoints.caches;
}.property('currentUser.pushPermissions', 'repo.id'),
displayStatusImages: function() {
return this.get('hasPermission');
}.property('hasPermission')
return this.hasPermission(this.get('currentUser'), this.get('repo.id'));
}.property('currentUser.permissions', 'repo.id')
});

26
app/mixins/permissions.js Normal file
View File

@ -0,0 +1,26 @@
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);
}
}
});

View File

@ -0,0 +1,12 @@
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);
});