苗火 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}}



 

评论:
Davidloose
2021-09-26 10:27 回复
Hello guys. And Bye.

neversurrenderboys ;)
Ronni1eAmubs
2021-09-24 19:04 回复
Thank you
Fluoxifar
2021-09-16 10:14 回复
Hey there are using Wordpress for your site platform? I'm new to the blog world but I'm trying to get started and set up my own. Do you need any coding knowledge to make your own blog? Any help would be really appreciated!
hed
2021-07-27 06:59 回复
Hi guys!
LarrySlods
2021-07-26 22:56 回复
This text is worth everyone's attention. When can I find out more?
Frankinjew
2021-07-10 06:57 回复
I blog quite often and I genuinely thank you for your content. The article has truly peaked my interest. I will book mark your website and keep checking for new information about once per week. I opted in for your RSS feed as well.
Kevinhaw
2021-07-07 02:29 回复
Excuse, that I interfere, but you could not give little bit more information.
CarlosClulk
2021-07-06 22:58 回复
In my opinion you are not right. I am assured. Let's discuss it. Write to me in PM, we will talk.
Darnellinves
2021-07-06 22:38 回复
You commit an error. I can defend the position.
1 2
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容