苗火 Nicholas
[angularJS]How to use angular-acl?
2015-9-17 萧


Angular-acl is a service that allows you to protect/show content based on the current user's assigned role(s).



How to use angular-acl : 



 



/* This is app.js */



var app= angular.module('app', ['ngRoute','mm.acl','LocalStorageModule']);



app.run(['AclService', function (AclService) {



  // Set the ACL data. Normally, you'd fetch this from an API or something.

  // The data should have the roles as the property names,

  // with arrays listing their permissions as their value.

  var aclData = {

    guest: ['login'],

    admin: ['loginout', 'can_admin']

  }

  AclService.setAbilities(aclData);



  // Attach the member role to the current user

  AclService.attachRole('guest');

 

}]);





app.config(function($routeProvider){

 $routeProvider

  .when('/',{  

   controller:'indexController',   

   templateUrl:'other.html',

   publicAccess:true

  })

  .when('/home',{

        resolve : {

          'acl' : ['$q', 'AclService', function($q, AclService){

             if(AclService.can('can_admin')){

               // Has proper permissions

               return true;

             } else {

               // Does not have permission

               return $q.reject('Unauthorized');

             }

          }]

        },    

   

   controller:'homeController',   

   templateUrl:'home.html',

   publicAccess:true

  })

  .when('/admin',{

        resolve : {

          'acl' : ['$q', 'AclService', function($q, AclService){

             if(AclService.can('can_admin')){

               // Has proper permissions

               return true;

             } else {

               // Does not have permission

               return $q.reject('Unauthorized');

             }

          }]

        }, 

      

   controller:'adminController',

   templateUrl:'admin.html',

   publicAccess:true 

  })

  .otherwise({

   redirectTo:'/'

  });

});

 

app



.controller('rootController', function($scope, AclService){

 $scope.can = AclService.can;

 $scope.title="root";

 console.log("root controller");

 $scope.login = function(){

  console.log("login button is clicked");

  AclService.attachRole('admin');

  AclService.detachRole('guest'); 

 }

 $scope.loginout = function(){

  console.log("login out button is clicked");

  AclService.detachRole('admin'); 

  AclService.attachRole('guest');

 }  

})

.controller('indexController',function($scope, AclService){

 $scope.title="index";

 console.log("inddex controller");

})

.controller('homeController',function($scope){

 $scope.title="home";

 console.log("teset controller");

})

.controller('adminController',function($scope){

 $scope.title="admin hahahaha";

 console.log("admin controller"); 

}); 



 



/* This is html */



 



<html ng-app="app">



<body ng-controller="rootController">

index:{{title}}

<hr/>



<div ng-view></div>

    <script src="js/angular.js"></script>

    <script src="js/angular-route.js"></script> 

    <script src="js/angular-acl.js"></script> 

    <script src="js/angular-local-storage.js"></script>     



    <script src="js/app.js"></script>





    <a href="#home">home</a><br/>

    <a href="#other">other</a><br/>

    <a href="#admin">admin</a><br/>   

    

    <input type="button" ng-click="login()" value="Login" ng-show="can('login')" />

    <input type="button" ng-click="loginout()" value="Loginout" ng-show="can('loginout')" />   

    

</body>



</html>



 



/* Other pages like admin.html */



admin:

{{title}}



 

评论:
2022-05-26 09:04 回复
@Fluzak:The web host is Alibaba.
Fluzak
2022-05-18 18:13 回复
Nice blog here! Also your web site loads up fast! What web host are you using? Can I get your affiliate link to your host? I wish my site loaded up as fast as yours lol
mojoheadz
2022-01-18 06:00 回复
Everything is OK!...
Josephmaigh
2022-01-12 05:36 回复
I just want to say thank you for this great forum. I found a solution here on log.anycle.com for my issue.
ChesterHep
2021-12-21 20:33 回复
What good topic
AnthonyBub
2021-12-14 15:32 回复
Certainly, never it is impossible to be assured.
DavidNed
2021-12-13 18:44 回复
I think, that you are mistaken. Write to me in PM, we will talk.
Albertarive
2021-12-09 14:09 回复
In it something is also to me it seems it is very good idea. Completely with you I will agree.
Fluxal
2021-11-11 03:33 回复
Hi to all, since I am really keen of reading this weblog's post to be updated regularly. It carries fastidious material.
Williamtah
2021-10-22 11:06 回复
great post, very informative. I'm wondering why the opposite experts of this sector don't notice this. You must continue your writing. I'm confident, you've a huge readers' base already!
1 2
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容