My AngularJS application has an admin module that I want to make available only to those in the admin role. On the server, I placed the files for this module in one directory, and I have this web config in the same directory. This works, and if the user is not in the administrator role, they cannot load javascript files:
<?xml version="1.0" encoding="utf-8"?> <configuration> <system.webServer> <security> <authorization> <remove users="*" roles="" verbs="" /> <add accessType="Allow" roles="Admin" /> </authorization> </security> </system.webServer> </configuration>
So, the server side solution of my side is similar to the solution. However, I am completely stuck with what to do with the client, how to load scripts and add a module to my application after loading it . Here is what I have:
The files in the admin directory that I protected with web-config look like this:
admin.js
angular.module('admin', [])
homeController.js
angular.module('admin') .controller('AdminHomeController', ['$http', '$q', '$resource', '$scope', '_o', adminHomeController]); function adminHomeController($http, $q, $resource, $scope, _o) { .... ... }
My application level files are as follows:
app.js
var app = angular .module('app', ['ui.router', 'admin', 'home',]) .run(['$rootScope', '$state', '$stateParams', '$http', '$angularCacheFactory', appRun]) function appRun($rootScope, $state, $stateParams, $http, $angularCacheFactory) { $rootScope.$state = $state; $rootScope.$stateParams = $stateParams; }
app.config.js
app.config(['$controllerProvider', '$httpProvider', '$locationProvider', '$sceProvider', '$stateProvider', appConfig]); function appConfig($httpProvider, $locationProvider, $sceProvider, $stateProvider) { // I added this to help with loading the module after // the application has already loaded app.controllerProvider = $controllerProvider; // $sceProvider.enabled(false); $locationProvider.html5Mode(true); var admin = { name: 'admin', url: '/admin', views: { 'root': { templateUrl: '/Content/app/admin/partials/home.html', }, 'content': { templateUrl: '/Content/app/admin/partials/overview.html', }, } }; var adminContent = { name: 'admin.content', parent: 'admin', url: '/:content', views: { 'root': { templateUrl: '/Content/app/admin/partials/home.html', }, 'content': { templateUrl: function (stateParams) { return '/Content/app/admin/partials/' + stateParams.content + '.html'; }, } } }; var home = { name: 'home', url: '/home', views: { 'root': { templateUrl: '/Content/app/home/partials/home.html', }, 'content': { templateUrl: '/Content/app/home/partials/overview.html', }, } }; var homeContent = { name: 'home.content', parent: 'home', url: '/:content', views: { 'root': { templateUrl: '/Content/app/home/partials/home.html', }, 'content': { templateUrl: function (stateParams) { return '/Content/app/home/partials/' + stateParams.content + '.html'; }, } } }; $stateProvider .state(admin) .state(adminContent) .state(home) .state(homeContent); }
When a user logs in, I know if this is a user of the admin role, since I have a security token that shows:
{ "access_token":"abcdefg", "token_type":"bearer", "expires_in":1209599, "userName":"xx", "roles":"Admin", ".issued":"Fri, 30 May 2014 12:23:53 GMT", ".expires":"Fri, 13 Jun 2014 12:23:53 GMT" }
If the user of the admin role, then I want
Download the scripts of the admin module: /Content/app/admin/admin.js and /Content/app/admin/homeController.js from the server. I already set it up like this for calls to $ http: $http.defaults.headers.common.Authorization = 'Bearer ' + user.data.bearerToken; , so the Bearer token must be sent when receiving the scripts:
Add admin module to application
Can someone give me some tips on how I can do these two things. After reading about require.js, I feel like I would not want to use it as a solution. I would like as simple as possible.
From what I understand until AngularJS resolves this, I need to make it so that I can enter my controller. So I already added this in appConfig:
app.controllerProvider = $controllerProvider;
But how can I upload two javascript files and how to add them to AngularJS so that the user can start using the controller functions inside the admin module? I saw something about $ script.js that is used by the Angular team. Is this a good solution and how can I implement it to satisfy my rather simple need.