0

I'm working in a Single Page web App, witch will require user/password protection. Modules will be loading depending on the user profile, to restrict the access on a roll-based manner.

I'm using ng-view to load the templates corresponding the user id.

How can I selectively load the corresponding JavaScript file of the template's controller?

<script script src="restrictetd/path/MyAngularControl.js"></script>

There is no point in downloading a JavaScript file if the user has no access to that particular template.

I really appreciate any advice.

cirtrus
  • 497
  • 5
  • 16
Hojendiz
  • 40
  • 6

1 Answers1

0

You are possibly over complicating your app. Unless your application is utterly massive it will be easier to maintain if you just deliver all of the needed JavaScript to the client. It is clients job to not show actions a user should not be able to do but it is the servers job to actually enforce that.

However to do what you want to do is possible. First thing to do in the route would be to download the JavaScript file and the template but have the route initially return nothing or some loading indicator. Once the JS file is loaded add a script tag to the HEAD or process the JS file to get it loaded. Then load the template and compile it.

One caveat of this process is that NO new modules can be used. I would recommend adding any be controllers / directive to the module you are using for ng-app. New things can be defined this way in an already initalize Angular app however you cannot redefine anything. So if controller xyz already exists, and you load another controller xyz the second load will not do anything.

Enzey
  • 5,254
  • 1
  • 18
  • 20
  • I think you are right, maybe i'm over complicating my app, but i'm worried about users reading parts of the code that they shouldn't have access. – Hojendiz Apr 23 '15 at 15:26
  • Having access to the code should not allow anything bad to happen and the server should block any unauthorized actions. Not giving them the source initially does not mean they could not get it another way. All of the script files would still need to be listed in the code somewhere and they could just download the files directly. – Enzey Apr 23 '15 at 15:32
  • I'll follow your advice. Thank you very much – Hojendiz Apr 23 '15 at 15:45