make it a util
This commit is contained in:
parent
65cf81169b
commit
26ca2875ae
|
@ -1,9 +1,9 @@
|
||||||
import Ember from 'ember';
|
import Ember from 'ember';
|
||||||
import { githubCommit as githubCommitUrl } from 'travis/utils/urls';
|
import { githubCommit as githubCommitUrl } from 'travis/utils/urls';
|
||||||
import config from 'travis/config/environment';
|
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',
|
tagName: 'li',
|
||||||
classNameBindings: ['repo.default_branch.last_build.state'],
|
classNameBindings: ['repo.default_branch.last_build.state'],
|
||||||
classNames: ['rows', 'rows--dashboard'],
|
classNames: ['rows', 'rows--dashboard'],
|
||||||
|
@ -17,11 +17,11 @@ export default Ember.Component.extend(Permissions, {
|
||||||
}.property('repo'),
|
}.property('repo'),
|
||||||
|
|
||||||
displayMenuTofu: function() {
|
displayMenuTofu: function() {
|
||||||
return this.hasPushPermission(this.get('currentUser'), this.get('repo.id'));
|
return hasPushPermission(this.get('currentUser'), this.get('repo.id'));
|
||||||
},
|
},
|
||||||
|
|
||||||
displayActivateLink: function() {
|
displayActivateLink: function() {
|
||||||
return this.hasAdminPermission(this.get('currentUser'), this.get('repo.id'));
|
return hasAdminPermission(this.get('currentUser'), this.get('repo.id'));
|
||||||
},
|
},
|
||||||
|
|
||||||
actions: {
|
actions: {
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
import Ember from 'ember';
|
import Ember from 'ember';
|
||||||
import config from 'travis/config/environment';
|
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(),
|
popup: Ember.inject.service(),
|
||||||
classNames: ['option-button'],
|
classNames: ['option-button'],
|
||||||
classNameBindings: ['isOpen:display'],
|
classNameBindings: ['isOpen:display'],
|
||||||
|
@ -24,15 +24,15 @@ export default Ember.Component.extend(Permissions, {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
displaySettingsLink: function() {
|
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'),
|
}.property('currentUser.pushPermissions', 'repo.id'),
|
||||||
|
|
||||||
displayCachesLink: function() {
|
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'),
|
}.property('currentUser.pushPermissions', 'repo.id'),
|
||||||
|
|
||||||
displayStatusImages: function() {
|
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')
|
}.property('currentUser.permissions', 'repo.id')
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -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
26
app/utils/permission.js
Normal 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};
|
|
@ -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);
|
|
||||||
});
|
|
15
tests/unit/utils/permission-test.js
Normal file
15
tests/unit/utils/permission-test.js
Normal 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));
|
||||||
|
|
||||||
|
});
|
Loading…
Reference in New Issue
Block a user