0

I am using Durandal in an asp.net application which is all working well. What I would like to achieve is to put something into the routing of it so if required I can stop the current route and redirect.

The reason for this is I want to permission base some routes where the permissions are stored in a database. So during the routing I want to check the route, use web api accordingly to check if they have access to that route and redirect if so OR use a method on the viewmodel to check this and redirect accordingly. I do use the activate function on the viewmodel, I wondered if the route can be redirected here perhaps?

Has anyone done this before?

EDIT: Following the great answer below the following is the code I eventually used on a test route to get this working. The web api function HasAccessToRoute part returns a bool:

define(['durandal/http', 'plugins/router', 'knockout', 'durandal/app'], function (http, router, ko, app) {

function viewModel() {
    var self = this;

    self.canActivate = function () {
        return http.ajaxRequest("get", "/api/route/hasaccesstoroute?route=test")
            .done(function (result) {
                if (!result) {
                    app.showMessage("Test area cannot be accessed");
                }
            });            
    };
}

var model = new viewModel();
return model;
});
user1166905
  • 2,612
  • 7
  • 43
  • 75

1 Answers1

1

Yes, it is possible. Take a look at canActivate here. You can return a promise in your canActivate handler and fetch your authorization profiles asynchronously. Once you have the authorization profile, you can then resolve your canActivate with either true or false, accordingly. This is what we do.

Also, the routes in Durandal are client-side, not server-side. Or are you doing server-side rendering with, say, Razor? If not, then the only time you would be going out to the server, essentially, is to obtain data, usually through a RESTful Web API (although you can do this with action-based routes as well).

This is an important point since canActivate is a client-side handler.

  • Thanks, I'll take a look. I am using jQuery, Knockout etc on client side and using as you say Web Api for any data requirements so I can hopefully make an ajax call during this function and change the promise accordingly. – user1166905 Jan 05 '15 at 18:41
  • Out of interest what does Durandal do in the case of a false return? i.e. canActivate is no – user1166905 Jan 05 '15 at 18:42
  • It simply does not route. By the way, I think your comment got cut off: I have "i.e. canActivate is no", and that's it. –  Jan 05 '15 at 20:03
  • So can the function be used to route somewhere else instead? I need a notification or something when it happens to make it clear to users. – user1166905 Jan 05 '15 at 21:50
  • Yes, it can be used for that purpose, although that's server-side thinking. You can use a Durandal dialog (or a custom Durandal dialog) to show your message, which the user then acknowledges with OK, and then he remains where he was trying to route from, never goes anywhere. There's no need to route your user all over the app for such a scenario. –  Jan 06 '15 at 00:30
  • Thanks, not used a Durandal dialog as yet so I'll look into that. With the route I know there is a redirectTo option in Durandal so was thinking of this. – user1166905 Jan 06 '15 at 09:04
  • So, staying on topic, was my initial answer helpful? I want to make sure I've got you pointed in the right direction. –  Jan 06 '15 at 09:21
  • Yes I have marked as answer and editing my question with some code to show others what I did, thanks. – user1166905 Jan 06 '15 at 09:30
  • 1
    Another option is to use guardRoute. You can intercept the route, do your validation via ajax call to the server, and then depending on the result, continue on or abort the navigation with a message. http://durandaljs.com/documentation/api.html#class/Router/method/guardRoute – mwill Jan 06 '15 at 21:04