D
D
Dmitry Kuznetsov2016-04-25 16:34:41
Angular
Dmitry Kuznetsov, 2016-04-25 16:34:41

How to make access only for administrators?

I want to make it so that authorized users with admin access can follow admin links. Links for authorized users work, but not for admins. At the same time, if the user is not authorized and follows the admin link, he still sees the page.
PS: I do not understand how to do it, I have been fighting for 5-6 hours. All methods are working ... Thank you in advance!
app.run...

App.run(['$rootScope', '$location', 'User', function($rootScope, $location, User){
    $rootScope.$on('$routeChangeStart', function(event, next, current){
        if(next.$$route.auth){
            if(!User.AuthCheck()){
                $location.path('/');
            }

            if(next.$$route.originalPath == '/'){
                if(User.AuthCheck()){
                    $location.path(current.$$route.originalPath);
                }
            }
        }

        if(next.$$route.admin){
            if(!User.AuthCheck()){
                $location.path('/');
            }

            if(User.checkAdmin()){
                $location.path(current.$$route.originalPath);
            }
        }
    });

    $rootScope.AuthCheck = function(){
        return User.AuthCheck();
    }

    $rootScope.checkAdmin = function(){
        return User.checkAdmin();
    }
}]);

Routes:
// Освновные роуты
    $routeProvide
        .when('/', {
            templateUrl: 'view/home.html',
            controller: 'HomeController',
        });

    // Для авторизованных
    $routeProvide
        .when('/auth/logout', {
            templateUrl: 'view/auth/logout.html',
            controller: 'UserController',
            auth: true
        });

    $routeProvide
        .when('/admin', {
            templateUrl: 'view/admin/home.html',
            controller: 'AdminController',
            auth: true,
            admin: true
        });

    $routeProvide.otherwise('/');

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexander, 2016-04-25
@ghost1k

in the page controller for administrators, you can check whether it is authorized or not, if not, send it somewhere.

D
DieZz, 2016-04-26
@DieZz

I use ui-router, I usually do this:

angular.module('app.config').run(run);

function run($rootScope, $state, $auth, $stateParams) {
    $rootScope.$on('$stateChangeStart', function (event, toState, toStateParams) {
        if (!$auth.isAuthenticated() && toState.name !== 'signin') {
            $rootScope.toState = toState;
            $rootScope.toStateParams = toStateParams;
            event.preventDefault();
             $state.go('signin', {});
         }
    });
}

In the same way, you can check whether the user can access the admin panel, and if he can, let him, if we redirect back.

D
denakol, 2016-04-28
@denakol

I am using ui-router along with the angular-permission module .
You can create different roles and redirect the user based on them.

D
Dmitry Kuznetsov, 2016-04-30
@dima9595

I solved the problem myself, in my own way) Thanks to everyone who helped!
Here is the code of my solution, suddenly it will come in handy)

$rootScope.$on('$routeChangeStart', function(event, next, current){
        if(next.$$route.auth){
            if(!User.AuthCheck()){
                $location.path('/');
            }

            if(next.$$route.originalPath == '/'){
                if(User.AuthCheck()){
                    $location.path(current.$$route.originalPath);
                }
            }
        }

        if(next.$$route.admin){
            if(!User.checkAdmin()){
                $location.path('/');
            }
        }
    });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question