Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Helpers when copying existing roles between litus installations
function exportRole() {
var $actions = $('#actions'),
$name = $('#name'),
$parents = $('#parents');
// TODO change to new selectors for dev-form
var result = {};
// extract the name
if ($name.length > 0) {
result.name = $name.val();
} else {
var matches = document.location.pathname.match(/.*\/admin\/role\/edit\/name\/([^/]+)\/?($|\?.*)/);
if (matches) {
result.name = matches[1];
}
}
// extract the parents
result.parents = $parents.find('option:selected').toArray().map(
function (e) {
return $(e).text();
}
);
// extract the actions
result.actions = $actions.find('option:selected').toArray().map(
function (e) {
var $this = $(e);
return $this.parent().attr('label') + '.' + $this.text();
}
);
return result;
}
function importRole(data) {
var _contains = function contains(arr, el) {
return arr.indexOf(el) >= 0;
}
var $actions = $('#actions'),
$name = $('#name'),
$parents = $('#parents');
// TODO change to new selectors for dev-form
// check if name is input
if ($name.length > 0) {
$name.val(data.name);
} else if (data.name != null) {
var matches = document.location.pathname.match(/.*\/admin\/role\/edit\/name\/([^/]+)\/?($|\?.*)/);
if (!matches || matches[1] !== data.name) {
throw new Error('Trying to import "' + data.name + '", but current role is ' + (matches ? '"' + matches[1] + '"' : 'unnamed'));
}
}
$parents.find('option').each(function () {
var $this = $(this);
$this.prop('selected', _contains(data.parents, $this.text()));
});
$actions.find('option').each(function () {
var $this = $(this);
var optionName = $this.parent().attr('label') + '.' + $this.text();
$this.prop('selected', _contains(data.actions, optionName));
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment