I am creating a rather large cms-type application with Backbone and Knockout and Knockback (ko + bb bridge library) and I am trying to find a good way to abstract permissions. Also sorry in advance for the novel.
First of all, this is a rather non-standard architecture, and the second question that you can ask is why do not you use something more comprehensive, like Ember or Angular? Case accepted. Right at that moment. :)
So here is my difficulty. I want an elegant api at the controller level and a viewmodel for permissions.
I have an object available to me that looks like this:
{ 'api/pages': { create: true, read: true, update: true, destroy: true }, 'api/links': { create: false, read: true, update: false, destroy: false } ... }
So, in my router / controllers, I update my collections / models / view modes and then invoke an individual rendering method on an existing view. The look takes care of things such as the release of viewmodels.
initialize: function() { this.pages = new PagesCollection(); this.links = new LinksCollection(); }, list: function() { var vm = new PageListViewmodel(this.pages, this.links);
So the problem with this is that these collections are completely unstructured, i.e. the router knows nothing about them.
But I need to redirect to an unauthorized page if you are not allowed to view a specific resource.
So, with the example above, I was thinking about coding in filters before / after? But where would you indicate what each router method is trying to access?
list: function() { this.authorize([this.pages, this.links], ['read'], function(pages, links) {
The previous code is really awkward.
For viewmodels, which are more understandable, I had the idea to do something like this - ala Ruby CanCan:
this.currentUser.can('read', collection) // true or false // can() would just look at the endpoint and compare to my perms object.