RafaelVidaurre / angular-permission

Simple route authorization via roles/permissions
MIT License
1.13k stars 212 forks source link

Ui-Router seems to be ignoring permissions..? #182

Closed roberttolton closed 8 years ago

roberttolton commented 8 years ago

Hi, so, I can't get this to work. Here's my app file:

/**
 * @title TES Sales Presenter Admin
 * @description Angular.JS App
 * @version 0.0.1
 * @author Robert Tolton
 * @email rob@fuelintegrated.com
 */

var App = angular.module( "App", [ "permission", "ui.router", "satellizer", "ngSanitize", "ui.bootstrap", "dialogs.main", "ncy-angular-breadcrumb", "ngResource", "angular-loading-bar", "ngAnimate", "angular-growl", "ngPrettyJson", "angularMoment", "ui.select" ] );

angular.element( document ).ready( function () {

    angular
        .module( "App" )
        .config( config )
        .run( run );

    config.$inject = [ "$stateProvider", "$urlRouterProvider", "$authProvider", "$httpProvider", "$provide", "ConfigProvider", "growlProvider" ];

    function config ( $stateProvider, $urlRouterProvider, $authProvider, $httpProvider, $provide, ConfigProvider, growlProvider ) {

        console.log( "App: Config" );

        function redirectWhenLoggedOut( $q, $injector ) {

            return {

                responseError: function( rejection ) {

                    var $state = $injector.get( "$state" );

                    var rejectionReasons = [ "token_not_provided", "token_expired", "token_absent", "token_invalid"];

                    angular.forEach( rejectionReasons, function ( value, key ) {

                        if ( rejection.data.error === value ) {

                            localStorage.removeItem( "user" );

                            $state.go( "auth" );
                        }

                    } );

                    return $q.reject( rejection );
                }
            }
        }

        $provide.factory( "redirectWhenLoggedOut", redirectWhenLoggedOut );

        $httpProvider.interceptors.push( "redirectWhenLoggedOut" );

        $authProvider.loginUrl = "/api/authenticate";

        $urlRouterProvider.otherwise( "/auth" );

        $urlRouterProvider.when( "", "/" );

        growlProvider.globalTimeToLive( 3000 );
        growlProvider.onlyUniqueMessages( false );
        growlProvider.globalPosition( "bottom-right" );
        growlProvider.globalDisableIcons( true );

        $stateProvider
            .state( "auth", {
                url: "/auth",
                templateUrl: "/assets/js/admin-app/partials/auth.html",
                controller: "AuthController as auth"
            })
            .state( "presentation", {
                url: "/presentation",
                abstract: true,
                data: {
                    permissions: {
                        only: [ "user", "admin" ],
                        redirectTo: "auth"
                    }
                },
                views: {
                    "": {
                        templateUrl: "/assets/js/admin-app/partials/presentation/index.html",
                        controller: "PresentationIndexController"
                    }
                },
                ncyBreadcrumb: {
                    label: "Presentations"
                }
            } )
            .state( "presentation.list", {
                url: "/list/:pageNum",
                data: {
                    permissions: {
                        only: [ "user", "admin" ],
                        redirectTo: "auth"
                    }
                },
                views: {
                    "main@presentation": {
                        templateUrl: "/assets/js/admin-app/partials/presentation/list.html",
                        controller: "PresentationListController"
                    }
                },
                ncyBreadcrumb: {
                    parent: "presentation",
                    label: "List"
                },
                params: {
                    pageNum: {
                        value: null,
                        squash: true
                    }
                }
            } )
            .state( "presentation.new", {
                url: "/new",
                data: {
                    permissions: {
                        only: [ "user", "admin" ],
                        redirectTo: "auth"
                    }
                },
                views: {
                    "main@presentation": {
                        templateUrl: "/assets/js/admin-app/partials/presentation/new.html",
                        controller: "PresentationNewController"
                    }
                },
                ncyBreadcrumb: {
                    parent: "presentation",
                    label: "New"
                }
            } )
            .state( "presentation.edit", {
                url: "/edit/:id",
                data: {
                    permissions: {
                        only: [ "user", "admin" ],
                        redirectTo: "auth"
                    }
                },
                views: {
                    "main@presentation": {
                        templateUrl: "/assets/js/admin-app/partials/presentation/edit.html",
                        controller: "PresentationEditController"
                    }
                },
                ncyBreadcrumb: {
                    parent: "presentation",
                    label: "Edit"
                }
            } );

    }

    function run ( $rootScope, $state, RoleStore ) {

        console.log( "App: Run" );

        $rootScope.authenticated = false;

        $rootScope.currentUser = null;

        $rootScope.$on( "$stateChangeStart", function ( event, toState ) {

            console.log( "Event On: $stateChangeStart" );

            var user = JSON.parse( localStorage.getItem( "user" ) );

            if ( user ) {

                $rootScope.authenticated = true;

                $rootScope.currentUser = user;

                if( toState.name === "auth" ) {

                    event.preventDefault();

                    $state.go( "presentation.list" );
                }
            }

        } );

        RoleStore
            .defineRole( "anonymous", [], function ( stateParams ) {
                if ( !$rootScope.currentUser ) {
                    return true;
                }
                return false;
            } );

    }

    angular.bootstrap( "#App", [ "App" ] );

} );

I get no errors, but visiting any state other than auth doesn't redirect me back to auth, the view and controller are successfully run even though I don't have any of the required roles..?

masterspambot commented 8 years ago

Are you using newest version (v2.1.2)? Because I solved problems with redirections there.

roberttolton commented 8 years ago

Yep, I'm using v2.1.2

masterspambot commented 8 years ago

I'll try to take a look at the evening where problem might be...

roberttolton commented 8 years ago

Ok it's now redirecting me back to auth if I'm not logged in, but after I click sign in in my app, I don't get redirected to the first view, even though it's the value of "toState":

Event On: $stateChangeStart
app.js:170 presentation.list

So the $state.go( "presentation.list" ); at the bottom of my $stateChangeStart isn't working, or it's being blocked?

masterspambot commented 8 years ago

So can you prep some plunkr to reproduce the problem @fuelintegrated ? That would ease me lot time.

roberttolton commented 8 years ago

Hmm, I'm not sure I can replicate this in a Plunkr since it authorises against a Laravel 5 API, would you want access to the repo?

masterspambot commented 8 years ago

If that is possible i can try to give some help. I'm almost sure you simply improperly manage your permissions.

roberttolton commented 8 years ago

Do you have an email so I can send you a .zip file? I've had to continue work, and currently I've got this other package working (it says it was inspired by angular-permission): https://github.com/ryandrewjohnson/ui-router.grant

Though it's not as fully featured.

masterspambot commented 8 years ago

@fuelintegrated I send it to you through contact form on your company site. So I'm closing this issue.