├── .gitignore ├── gulpfile.js ├── dist ├── angular-authorization.min.js └── angular-authorization.js ├── bower.json ├── package.json ├── LICENSE ├── src └── angular-authorization.js └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | bower_components 3 | -------------------------------------------------------------------------------- /gulpfile.js: -------------------------------------------------------------------------------- 1 | var gulp = require('gulp'); 2 | var rename = require('gulp-rename'); 3 | var uglify = require('gulp-uglify'); 4 | 5 | gulp.task('compress', function() { 6 | return gulp.src('src/*.js') 7 | .pipe(uglify()) 8 | .pipe(rename({ 9 | extname: '.min.js' 10 | })) 11 | .pipe(gulp.dest('dist')); 12 | }); 13 | 14 | gulp.task('copy', function() { 15 | return gulp.src('src/*.js') 16 | .pipe(gulp.dest('dist')); 17 | }); 18 | 19 | gulp.task('default', ['copy', 'compress']); 20 | -------------------------------------------------------------------------------- /dist/angular-authorization.min.js: -------------------------------------------------------------------------------- 1 | angular.module("authorization",["ui.router"]).run(["$rootScope","$state","Authorization",function(t,e,a){t.$on("$stateChangeSuccess",function(t,i,o,r,d){a.authorized||(!a.memorizedState||r.data&&r.data.redirectTo&&i.name===r.data.redirectTo||a.clear(),i.data&&i.data.authorization&&i.data.redirectTo&&(i.data.memory&&(a.memorizedState=i.name),e.go(i.data.redirectTo)))})}]).service("Authorization",["$state",function(t){this.authorized=!1,this.memorizedState=null;var e=function(){this.authorized=!1,this.memorizedState=null},a=function(e){this.authorized=!0;var a=this.memorizedState?this.memorizedState:e;t.go(a)};return{authorized:this.authorized,memorizedState:this.memorizedState,clear:e,go:a}}]); -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "angular-authorization", 3 | "version": "0.0.2", 4 | "authors": [ 5 | "Woody Rousseau " 6 | ], 7 | "description": "Basic state authorization with Angular UIRouter", 8 | "main": "dist/angular-authorization.js", 9 | "keywords": [ 10 | "angular", 11 | "authorization", 12 | "route", 13 | "state", 14 | "ui-router" 15 | ], 16 | "license": "MIT", 17 | "ignore": [ 18 | "**/.*", 19 | "node_modules", 20 | "bower_components", 21 | "test", 22 | "tests", 23 | "src" 24 | ], 25 | "dependencies": { 26 | "angular": "~1.4.3", 27 | "angular-ui-router": "~0.2.15" 28 | }, 29 | "repository": { 30 | "type": "git", 31 | "url": "git://github.com/wrousseau/angular-authorization.git" 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "angular-authorization", 3 | "version": "0.0.2", 4 | "description": "Basic state authorization with Angular UIRouter", 5 | "main": "dist/angular-permission.js", 6 | "repository": { 7 | "type": "git", 8 | "url": "git+https://github.com/wrousseau/angular-authorization.git" 9 | }, 10 | "scripts": { 11 | "test": "echo \"Error: no test specified\" && exit 1" 12 | }, 13 | "keywords": [ 14 | "angular", 15 | "authorization", 16 | "route", 17 | "state", 18 | "ui-router" 19 | ], 20 | "author": "Woody Rousseau ", 21 | "license": "MIT", 22 | "bugs": { 23 | "url": "https://github.com/wrousseau/angular-authorization/issues" 24 | }, 25 | "homepage": "https://github.com/wrousseau/angular-authorization", 26 | "devDependencies": { 27 | "gulp": "^3.9.0", 28 | "gulp-rename": "^1.2.2", 29 | "gulp-uglify": "^1.2.0" 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Woody Rousseau 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /dist/angular-authorization.js: -------------------------------------------------------------------------------- 1 | angular.module('authorization', [ 2 | 'ui.router', 3 | ]) 4 | 5 | .run(['$rootScope', '$state', 'Authorization', function($rootScope, $state, Authorization) { 6 | 7 | $rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) { 8 | if (!Authorization.authorized) { 9 | if (Authorization.memorizedState && (!fromState.data || !fromState.data.redirectTo || toState.name !== fromState.data.redirectTo)) { 10 | Authorization.clear(); 11 | } 12 | if (toState.data && toState.data.authorization && toState.data.redirectTo) { 13 | if (toState.data.memory) { 14 | Authorization.memorizedState = toState.name; 15 | } 16 | $state.go(toState.data.redirectTo); 17 | } 18 | } 19 | 20 | }); 21 | }]) 22 | 23 | .service('Authorization', ['$state', function($state) { 24 | 25 | this.authorized = false, 26 | this.memorizedState = null; 27 | 28 | var 29 | clear = function() { 30 | this.authorized = false; 31 | this.memorizedState = null; 32 | }, 33 | 34 | go = function(fallback) { 35 | this.authorized = true; 36 | var targetState = this.memorizedState ? this.memorizedState : fallback; 37 | $state.go(targetState); 38 | }; 39 | 40 | return { 41 | authorized: this.authorized, 42 | memorizedState: this.memorizedState, 43 | clear: clear, 44 | go: go 45 | }; 46 | }]); 47 | -------------------------------------------------------------------------------- /src/angular-authorization.js: -------------------------------------------------------------------------------- 1 | angular.module('authorization', [ 2 | 'ui.router', 3 | ]) 4 | 5 | .run(['$rootScope', '$state', 'Authorization', function($rootScope, $state, Authorization) { 6 | 7 | $rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) { 8 | if (!Authorization.authorized) { 9 | if (Authorization.memorizedState && (!fromState.data || !fromState.data.redirectTo || toState.name !== fromState.data.redirectTo)) { 10 | Authorization.clear(); 11 | } 12 | if (toState.data && toState.data.authorization && toState.data.redirectTo) { 13 | if (toState.data.memory) { 14 | Authorization.memorizedState = toState.name; 15 | } 16 | $state.go(toState.data.redirectTo); 17 | } 18 | } 19 | 20 | }); 21 | }]) 22 | 23 | .service('Authorization', ['$state', function($state) { 24 | 25 | this.authorized = false, 26 | this.memorizedState = null; 27 | 28 | var 29 | clear = function() { 30 | this.authorized = false; 31 | this.memorizedState = null; 32 | }, 33 | 34 | go = function(fallback) { 35 | this.authorized = true; 36 | var targetState = this.memorizedState ? this.memorizedState : fallback; 37 | $state.go(targetState); 38 | }; 39 | 40 | return { 41 | authorized: this.authorized, 42 | memorizedState: this.memorizedState, 43 | clear: clear, 44 | go: go 45 | }; 46 | }]); 47 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # angular-authorization 2 | 3 | > Basic state authorization with Angular UIRouter 4 | 5 | Angular Authorization provides a simple way to include states that require authorization. 6 | Current features include: 7 | 8 | - Preventing access to specified states 9 | - Redirecting to another state when lacking authorization 10 | - Remembering the state the user tried to access to immediately redirect to it once authentified 11 | 12 | ## Requirements 13 | 14 | [AngularUI Router](https://github.com/angular-ui/ui-router) is required. 15 | 16 | ## Installation 17 | 18 | You can install the package with Bower and add it to your dependencies: 19 | 20 | `bower install --save angular-authorization` 21 | 22 | Make sure you include the following script tags (you can pick the minified version if wanted) 23 | 24 | ```html 25 | 26 | 27 | ``` 28 | 29 | Finaly make sure it is included in your module dependencies: 30 | 31 | ```javascript 32 | angular.module('myApp', ['ui.router', ..., 'authorization']); 33 | ``` 34 | 35 | ## Usage 36 | 37 | Angular Authorization includes the `Authorization` service which includes two properties : 38 | 39 | - `authorized`: a boolean determining wether or not the user is currently authorized to access restricted routes. 40 | - `memorizedState`: a string being the name of the state the user was last trying to reach. 41 | 42 | It also provides two functions: 43 | 44 | - `clear()` which clears both properties 45 | - `go(fallback)` which is to be called when the user logs in with success. It authorizes the user, and also performs a `$state.go`, except that it tries to use the memorized state if available, relying on the given state fallback argument if not. 46 | 47 | Angular Authorization also enriches the AngularUI Router in the way depicted in this configuration example: 48 | 49 | ```javascript 50 | .config(function ($stateProvider, $urlRouterProvider) { 51 | 52 | $urlRouterProvider.otherwise('/'); 53 | 54 | $stateProvider 55 | .state('home', { 56 | url: '/', 57 | template: '

Home

' 58 | }) 59 | .state("login", { 60 | url: "/login", 61 | template: '', 62 | controller: function($scope, $state, Authorization) { 63 | $scope.onLogin = function() { 64 | Authorization.go('private'); 65 | }; 66 | } 67 | }) 68 | .state('private', { 69 | url: '/private', 70 | template: '

Private

', 71 | data: { 72 | authorization: true, 73 | redirectTo: 'login' 74 | } 75 | }) 76 | .state('secret', { 77 | url: '/secret', 78 | template: '

Secret

', 79 | data: { 80 | authorization: true, 81 | redirectTo: 'login', 82 | memory: true 83 | } 84 | }); 85 | }) 86 | ``` 87 | 88 | The following flags can be added to any state's data object : 89 | 90 | - `authorization` marks the state as requiring authorization 91 | - `redirectTo` can be any non-restricted state on which the user is to be redirected if trying to access a restricted state. Its presence is required if `authorization` is truthy. 92 | - `memory`, if truthy, means that if the user tries to reach this restricted state, is redirected to the `redirectTo` state and succesfully gets authorized (`Authorization.go` called), he/she will be redirected to this state (which was the initial goal) 93 | 94 | ## Demo 95 | 96 | A simple demo is given in this [Plunker](http://codepen.io/anon/pen/OVrvVX). 97 | 98 | ## Contributing 99 | 100 | Angular Authorization is still under development. Bug reports, pull requests and feature requests are more than welcome. 101 | 102 | If you require a more complex role system, for instance where several roles can exist, [Angular Permission](https://github.com/Narzerus/angular-permission) is a good alternative. 103 | 104 | ## Roadmap 105 | 106 | - Unit testing / 100% Coverage 107 | - Ngdoc 108 | - Code Climate 109 | --------------------------------------------------------------------------------