Skip to content

Instantly share code, notes, and snippets.

@whisher
Created April 23, 2014 08:06
Show Gist options
  • Save whisher/11206445 to your computer and use it in GitHub Desktop.
Save whisher/11206445 to your computer and use it in GitHub Desktop.
'use strict';
//Global service for global variables
angular.module('mean.system').factory('Global', [
function() {
var _this = this;
_this._data = {
user: window.user,
authenticated: !! window.user,
isAdmin: !!window.user && (_.indexOf(window.user.roles, 'admin') !== -1)
};
return _this._data;
}
]);
'use strict';
angular.module('mean.system').controller('HeaderController', ['$scope', '$rootScope', 'Global', 'Menus',
function($scope, $rootScope, Global, Menus) {
$scope.global = Global;
$scope.menus = {};
console.log($scope.global);
// Default hard coded menu items for main menu
var mainMenu = [
{
'roles': ['annonymous'],
'title': 'Create New annonymous',
'link': 'create admin'
},
{
'roles': ['authenticated'],
'title': 'Articles',
'link': 'all articles'
},
{
'roles': ['authenticated'],
'title': 'Create New Article',
'link': 'create article'
}
];
var adminMenu = [
{
'roles': ['admin'],
'title': 'Create New admin 1',
'link': 'create admin'
},
{
'roles': ['admin'],
'title': 'Create New admin 2',
'link': 'create admin'
}
];
var currentMenu = ($scope.global.isAdmin) ? adminMenu : mainMenu;
// Query menus added by modules. Only returns menus that user is allowed to see.
function queryMenu(name, defaultMenu) {
Menus.query({
name: name,
defaultMenu: defaultMenu
}, function(menu) {
$scope.menus[name] = menu;
});
}
// Query server for menus and check permissions
queryMenu('main', currentMenu);
$scope.isCollapsed = false;
$rootScope.$on('loggedin', function() {
$scope.global = {
authenticated: !! $rootScope.user,
user: $rootScope.user,
isAdmin: !!$rootScope.user && (_.indexOf($rootScope.user.roles, 'admin') !== -1)
};
var currentMenu = ($scope.global.isAdmin) ? adminMenu : mainMenu;
queryMenu('main', currentMenu);
});
}
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment