1 angular.module('egAdminPermGrpTreeApp',
2 ['ngRoute','ui.bootstrap','egCoreMod','egUiMod','treeControl'])
4 .config(function($routeProvider, $locationProvider, $compileProvider) {
5 $locationProvider.html5Mode(true);
6 $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|blob):/);
8 var resolver = {delay :
9 ['egStartup', function(egStartup) {return egStartup.go()}]}
11 $routeProvider.when('/admin/local/permission/grp_tree_display_entry', {
12 templateUrl : './admin/local/permission/t_grp_tree_display_entry',
13 controller : 'PermGrpTreeCtrl',
17 // catch admin/local/permission/grp_penalty_threshold
19 '<eg-embed-frame allow-escape="true" min-height="min_height" url="local_admin_url" handlers="funcs"></eg-embed-frame>';
20 $routeProvider.when('/admin/local/:schema/:page', {
21 template: eframe_template,
22 controller: 'EmbedConifyCtl',
26 $routeProvider.otherwise({redirectTo : '/admin/local/permission/grp_tree'});
29 .factory('egPermGrpTreeSvc',
30 ['$q','egCore', function($q , egCore) {
39 // determine which user groups our user is not allowed to modify
40 service.set_edit_profiles = function() {
41 var all_app_perms = [];
42 var failed_perms = [];
44 // extract the application permissions
45 angular.forEach(service.profiles, function(grp) {
46 if (grp.application_perm())
47 all_app_perms.push(grp.application_perm());
50 // fill in service.edit_profiles by inspecting failed_perms
51 function traverse_grp_tree(grp, failed) {
53 failed_perms.indexOf(grp.application_perm()) > -1;
55 if (!failed) service.edit_profiles.push(grp);
58 service.profiles.filter( // children of grp
59 function(p) { return p.parent() == grp.id() }),
60 function(child) {traverse_grp_tree(child, failed)}
64 return egCore.perm.hasPermAt(all_app_perms, true).then(
66 angular.forEach(all_app_perms, function(p) {
67 if (perm_orgs[p].length == 0)
71 traverse_grp_tree(egCore.env.pgt.tree);
76 service.get_perm_groups = function() {
78 service.profiles = egCore.env.pgt.list;
79 return service.set_edit_profiles();
81 return egCore.pcrud.search('pgt', {parent : null},
82 {flesh : -1, flesh_fields : {pgt : ['children']}}
85 egCore.env.absorbTree(tree, 'pgt')
86 service.profiles = egCore.env.pgt.list;
87 return service.set_edit_profiles();
93 service.fetchDisplayEntries = function(ou_id) {
94 service.edit_profiles = [];
95 service.get_perm_groups();
96 return egCore.pcrud.search('pgtde',
97 {org : egCore.org.ancestors(ou_id, true)},
100 pgtde: ['grp', 'org']
102 order_by: {pgtde : 'id'}
105 ).then(function(entries) {
106 service.pgtde_array = [];
107 service.disabled_entries = [];
108 angular.forEach(entries, function(entry) {
109 service.pgtde_array.push(entry);
114 service.organizeDisplayEntries = function(selectedOrg) {
115 service.display_entries = [];
117 angular.forEach(service.pgtde_array, function(pgtde) {
118 if (pgtde.org().id() == selectedOrg) {
119 if (!pgtde.child_entries) pgtde.child_entries = [];
121 angular.forEach(service.display_entries, function(entry) {
122 if (pgtde.parent() && pgtde.parent() == entry.id()) {
123 entry.child_entries.push(pgtde);
127 if (service.iterateChildEntries(pgtde, entry)) {
133 if (!pgtde.parent() || !isChild) {
134 service.display_entries.push(pgtde);
140 service.iterateChildEntries = function(pgtde, entry) {
141 if (entry.child_entries.length) {
142 return angular.forEach(entry.child_entries, function(child) {
143 if (pgtde.parent() == child.id()) {
144 child.child_entries.push(pgtde);
147 return service.iterateChildEntries(pgtde, child);
153 service.updateDisplayEntries = function(tree, ou_id) {
154 return egCore.pcrud.search('pgtde',
158 pgtde: ['grp', 'org']
160 order_by: {pgtde : 'id'}
163 ).then(function(entries) {
164 return egCore.pcrud.update(tree).then(function(res) {
170 service.removeDisplayEntries = function(entries) {
171 return egCore.pcrud.remove(entries).then(function(res) {
176 service.addDisplayEntries = function(entries) {
177 return egCore.pcrud.create(entries).then(function(res) {
182 service.findEntry = function(id, entries) {
184 angular.forEach(entries, function(entry) {
186 if (!entry.child_entries) entry.child_entries = [];
187 if (id == entry.id()) {
189 } else if (entry.child_entries.length) {
190 match = service.findEntry(id, entry.child_entries);
201 .controller('PermGrpTreeCtrl',
202 ['$scope','$q','$timeout','$location','$uibModal','egCore','egPermGrpTreeSvc', 'ngToast', 'egProgressDialog',
203 function($scope , $q , $timeout , $location , $uibModal , egCore , egPermGrpTreeSvc, ngToast, egProgressDialog) {
204 $scope.perm_tree = [{
207 name: function() {return egCore.strings.ROOT_NODE_NAME;}
213 $scope.display_entries = [];
214 $scope.new_entries = [];
215 $scope.tree_options = {nodeChildren: 'child_entries'};
216 $scope.selected_entry;
217 $scope.expanded_nodes = [];
218 $scope.orderby = ['position()','grp().name()'];
220 if (!$scope.selectedOrg)
221 $scope.selectedOrg = egCore.org.get(egCore.auth.user().ws_ou());
223 $scope.updateSelection = function(node, selected) {
224 $scope.selected_entry = node;
225 if (!selected) $scope.selected_entry = null;
228 $scope.setPosition = function(node, direction) {
229 var newPos = node.position();
232 siblings = egPermGrpTreeSvc.findEntry(node.parent(), $scope.perm_tree[0].child_entries).child_entries;
234 siblings = $scope.perm_tree[0].child_entries;
236 if (direction == 'up' && newPos < siblings.length) newPos++;
237 if (direction == 'down' && newPos > 1) newPos--;
239 angular.forEach(siblings, function(entry) {
240 if (entry.position() == newPos) entry.position(node.position());
241 angular.forEach($scope.display_entries, function(display_entry) {
242 if (display_entry.id() == entry.id()) {
243 if (display_entry.position() == newPos) {
244 display_entry.position(node.position);
250 angular.forEach($scope.display_entries, function(display_entry) {
251 if (display_entry.id() == node.id()) {
252 display_entry.position(newPos);
256 node.position(newPos);
259 $scope.addChildEntry = function(node) {
261 $scope.openAddDialog(node, $scope.disabled_entries, $scope.edit_profiles, $scope.display_entries, $scope.selectedOrg)
262 .then(function(res) {
266 var new_entry = new egCore.idl.pgtde();
267 new_entry.org($scope.selectedOrg.id());
268 new_entry.grp(res.selected_grp);
269 new_entry.position(1);
270 new_entry.child_entries = [];
272 if (res.selected_parent) {
273 new_entry.parent(res.selected_parent);
274 angular.forEach($scope.expanded_nodes, function(expanded_node) {
275 if (expanded_node == res.selected_parent) is_expanded = true;
277 if (!is_expanded) $scope.expanded_nodes.push(res.selected_parent);
279 angular.forEach($scope.expanded_nodes, function(expanded_node) {
280 if (expanded_node == $scope.perm_tree[0]) is_expanded = true;
282 if (!is_expanded) $scope.expanded_nodes.push($scope.perm_tree[0]);
285 $scope.display_entries.push(new_entry);
286 egPermGrpTreeSvc.addDisplayEntries([new_entry]).then(function(addRes) {
288 if (res.is_root || !res.selected_parent) {
289 angular.forEach($scope.perm_tree[0].child_entries, function(entry) {
290 var newPos = entry.position();
292 entry.position(newPos);
293 siblings.push(entry);
296 var parent = egPermGrpTreeSvc.findEntry(res.selected_parent.id(), $scope.perm_tree[0].child_entries);
297 angular.forEach(parent.child_entries, function(entry) {
298 var newPos = entry.position();
300 entry.position(newPos);
301 siblings.push(entry);
305 egPermGrpTreeSvc.updateDisplayEntries(siblings).then(function(updateRes) {
306 ngToast.create(egCore.strings.ADD_SUCCESS);
307 $scope.refreshTree($scope.selectedOrg, $scope.selected_entry);
310 ngToast.create(egCore.strings.ADD_FAILURE);
317 $scope.openAddDialog = function(node, disabled_entries, edit_profiles, display_entries, selected_org) {
319 return $uibModal.open({
320 templateUrl : './admin/local/permission/t_pgtde_add_dialog',
323 '$scope','$uibModalInstance',
324 function($scope , $uibModalInstance) {
325 var getIsRoot = function() {
326 if (!node || node.permanent) return true;
330 var getSelectedParent = function() {
331 if (!node || node.permanent) return $scope.perm_tree;
335 var available_profiles = [];
336 angular.forEach(edit_profiles, function(grp) {
337 grp._filter_grp = false;
338 angular.forEach(display_entries, function(entry) {
339 if (entry.org().id() == selected_org.id()) {
340 if (entry.grp().id() == grp.id()) grp._filter_grp = true;
343 if (!grp._filter_grp) available_profiles.push(grp);
347 is_root : getIsRoot(),
348 selected_parent : getSelectedParent(),
349 edit_profiles : available_profiles,
350 display_entries : display_entries,
351 selected_org : selected_org
354 $scope.context.selected_grp = $scope.context.edit_profiles[0];
356 $scope.ok = function() {
357 $uibModalInstance.close($scope.context);
360 $scope.cancel = function() {
361 $uibModalInstance.dismiss();
368 var iteratePermTree = function(arr1, arr2) {
369 angular.forEach(arr1, function(entry) {
371 if (entry.child_entries) iteratePermTree(entry.child_entries, arr2);
375 $scope.removeEntry = function(node) {
376 $scope.disabled_entries.push(node);
377 iteratePermTree(node.child_entries, $scope.disabled_entries);
381 siblings = egPermGrpTreeSvc.findEntry(node.parent(), $scope.perm_tree[0].child_entries).child_entries;
383 siblings = $scope.perm_tree[0].child_entries;
385 angular.forEach(siblings, function(sibling) {
386 var newPos = sibling.position();
387 if (node.position() < sibling.position()) {
390 sibling.position(newPos);
393 $scope.selected_entry = null;
395 egPermGrpTreeSvc.removeDisplayEntries($scope.disabled_entries).then(function(res) {
397 ngToast.create(egCore.strings.REMOVE_SUCCESS);
398 $scope.refreshTree($scope.selectedOrg);
400 ngToast.create(egCore.strings.REMOVE_FAILURE);
405 var getDisplayEntries = function() {
406 $scope.edit_profiles = egPermGrpTreeSvc.edit_profiles;
407 egPermGrpTreeSvc.organizeDisplayEntries($scope.selectedOrg.id());
408 $scope.perm_tree[0].child_entries = egPermGrpTreeSvc.display_entries;
409 $scope.display_entries = egPermGrpTreeSvc.pgtde_array;
410 $scope.new_entries = [];
411 $scope.disabled_entries = [];
412 $scope.selected_entry = $scope.perm_tree[0];
413 if (!$scope.expanded_nodes.length) iteratePermTree($scope.perm_tree, $scope.expanded_nodes);
416 $scope.saveEntries = function() {
417 egProgressDialog.open();
419 // Save Remaining Display Entries
420 egPermGrpTreeSvc.updateDisplayEntries($scope.display_entries, $scope.selectedOrg.id())
421 .then(function(res) {
423 ngToast.create(egCore.strings.UPDATE_SUCCESS);
424 $scope.refreshTree($scope.selectedOrg, $scope.selected_entry);
426 ngToast.create(egCore.strings.UPDATE_FAILURE);
428 }).finally(egProgressDialog.close);
431 $scope.org_changed = function(org) {
432 $scope.refreshTree(org.id());
435 $scope.refreshTree = function(ou_id, node) {
436 egPermGrpTreeSvc.fetchDisplayEntries(ou_id).then(function() {
438 if (node) $scope.selected_entry = node;
442 egCore.startup.go(function() {
443 $scope.refreshTree(egCore.auth.user().ws_ou());
447 .controller('EmbedConifyCtl',
448 ['$scope','$routeParams','$location','egCore',
449 function($scope , $routeParams , $location , egCore) {
452 ses : egCore.auth.token(),
455 var conify_path = '/eg/conify/global/' +
456 $routeParams.schema + '/' + $routeParams.page;
458 $scope.min_height = 800;
460 // embed URL must include protocol/domain or it will be loaded via
461 // push-state, resulting in an infinitely nested pages.
462 $scope.local_admin_url =
463 $location.absUrl().replace(/\/eg\/staff.*/, conify_path);
465 console.log('Loading local admin URL: ' + $scope.local_admin_url);