angular-eha.couchdb-auth
Advanced tools
Comparing version 0.1.1 to 0.1.2
{ | ||
"name": "angular-eha.couchdb-auth", | ||
"description": "CouchDB auth wrapper", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"authors": [ | ||
@@ -6,0 +6,0 @@ "Matt Richards <matt@lucidmoon.co.uk>" |
@@ -7,2 +7,8 @@ # Change Log | ||
## v0.1.2 | ||
### Added | ||
- `eha-show-for-role` directive | ||
- Allow definition of `adminRoles` during config | ||
## v0.1.0 - 01/04/2015 | ||
@@ -9,0 +15,0 @@ |
@@ -44,3 +44,2 @@ ;(function() { | ||
.then(function(user) { | ||
console.log('GOT USER'); | ||
return getSession() | ||
@@ -129,7 +128,15 @@ .then(function() { | ||
user.hasRole = function(role) { | ||
return this.roles.indexOf(role) > -1; | ||
var self = this; | ||
if (angular.isArray(role)) { | ||
var matches = role.filter(function(r) { | ||
return self.roles.indexOf(r) > -1; | ||
}); | ||
return !!matches.length; | ||
} else if (angular.isString(role)) { | ||
return this.roles.indexOf(role) > -1; | ||
} | ||
}; | ||
user.isAdmin = function() { | ||
return this.hasRole('_admin'); | ||
return this.hasRole(options.adminRoles); | ||
}; | ||
@@ -160,3 +167,2 @@ return user; | ||
.catch(function(err) { | ||
console.log(err); | ||
return $q.reject(err); | ||
@@ -207,3 +213,4 @@ }); | ||
localStorageNamespace: 'eha', | ||
localStorageStoreName: 'auth' | ||
localStorageStoreName: 'auth', | ||
adminRoles: ['_admin'] | ||
}; | ||
@@ -213,2 +220,3 @@ | ||
options = angular.extend(options, config); | ||
$localForageProvider.config({ | ||
@@ -229,2 +237,3 @@ name: options.localStorageNamespace, | ||
this.requireAdminUser = function(ehaCouchDbAuthService, $q) { | ||
return ehaCouchDbAuthService.getCurrentUser() | ||
@@ -359,2 +368,56 @@ .then(function(user) { | ||
angular.module('eha.couchdb-auth.show-for-role.directive', []) | ||
.directive('ehaShowForRole', ['ehaCouchDbAuthService', '$animate', '$parse', '$q', '$log', function(ehaCouchDbAuthService, | ||
$animate, | ||
$parse, | ||
$q, | ||
$log) { | ||
var NG_HIDE_CLASS = 'ng-hide'; | ||
var NG_HIDE_IN_PROGRESS_CLASS = 'ng-hide-animate'; | ||
return { | ||
restrict: 'A', | ||
link: function(scope, element, attributes) { | ||
function checkRoles(requiredRoles) { | ||
ehaCouchDbAuthService.getCurrentUser() | ||
.then(function(user) { | ||
if (user && user.hasRole(requiredRoles)) { | ||
$animate.removeClass(element, NG_HIDE_CLASS, { | ||
tempClasses: NG_HIDE_IN_PROGRESS_CLASS | ||
}); | ||
return true; | ||
} | ||
return $q.reject('Role not found'); | ||
}) | ||
.catch(function(err) { | ||
$log.error(err); | ||
$animate.addClass(element, NG_HIDE_CLASS, { | ||
tempClasses: NG_HIDE_IN_PROGRESS_CLASS | ||
}); | ||
}); | ||
} | ||
// Hide by default | ||
element.addClass('ng-hide'); | ||
var attr = $parse(attributes.ehaShowForRole)(scope); | ||
var requiredRoles; | ||
if (angular.isArray(attr)) { | ||
requiredRoles = attr; | ||
} else if (angular.isString(attr)) { | ||
requiredRoles = [attr]; | ||
} else { | ||
throw Error('You must pass a string or an array of strings'); | ||
} | ||
checkRoles(requiredRoles); | ||
ehaCouchDbAuthService.on('authenticationStateChange', function() { | ||
checkRoles(requiredRoles); | ||
}); | ||
} | ||
}; | ||
}]); | ||
angular.module('eha.couchdb-auth.show-authenticated.directive', []) | ||
@@ -396,2 +459,3 @@ .directive('ehaShowAuthenticated', ['ehaCouchDbAuthService', '$animate', function(ehaCouchDbAuthService, $animate) { | ||
'eha.couchdb-auth.auth.service', | ||
'eha.couchdb-auth.show-for-role.directive', | ||
'eha.couchdb-auth.show-authenticated.directive' | ||
@@ -398,0 +462,0 @@ ]); |
@@ -1,1 +0,1 @@ | ||
!function(){"use strict";function a(a,b,c,d,e,f){function g(){return d.when(b.all("_session").customGET())}function h(a){return d.when(b.all("_session").customPOST({name:a.username,password:a.password})).then(s).then(function(a){return console.log("GOT USER"),g().then(function(){return a})}).then(function(a){return a&&a.ok?(u.$broadcast("authenticationStateChange"),c.log("couchdb:login:success",a),a):(c.log("couchdb:login:failure:unknown"),d.reject(new Error))})["catch"](function(a){return 401===a.status?(c.log("couchdb:login:failure:invalid-credentials",a),d.reject(new Error("Invalid Credentials"))):(c.log("couchdb:login:failure:unknown",a),d.reject(new Error(a)))})}function i(){return t=null,e.removeItem("user")}function j(a){return e.setItem("user",a)}function k(){return e.getItem("user")}function l(){return d.when(b.all("_session").remove()).then(i)["finally"](function(){u.$broadcast("authenticationStateChange")})}function m(a){return a.token&&a.password?d.when(b.all("reset-password").customPOST({token:a.token,password:a.password})):a.email?d.when(b.all("reset-password").customPOST({email:a.email,callbackUrl:"http://localhost:5000/#/reset-password"})):void 0}function n(){return d.reject("NOT_IMPLEMENTED")}function o(){return d.reject("NOT_IMPLEMENTED")}function p(){return d.reject("NOT_IMPLEMENTED")}function q(a){return a.hasRole=function(a){return this.roles.indexOf(a)>-1},a.isAdmin=function(){return this.hasRole("_admin")},a}function r(){return t?d.when(q(t)):k().then(function(a){return a?(t=a,q(a)):d.reject("User not found")}).then(function(a){return g().then(function(){return a})})["catch"](function(a){return console.log(a),d.reject(a)})}function s(a){return a?(a=a.plain(),t={name:a.name,roles:a.roles,bearerToken:a.bearerToken},j(a)):void d.reject("No user found")}var t,u=f.$new(!0);return u.$on("unauthorized",function(){i()}),{signIn:h,signOut:l,resetPassword:m,accounts:{add:n,update:o,remove:p},getSession:g,getCurrentUser:r,on:u.$on.bind(u),trigger:u.$broadcast.bind(u)}}var b=angular.module("eha.couchdb-auth.auth.service",["restangular","LocalForageModule","ngCookies"]);b.provider("ehaCouchDbAuthService",["$localForageProvider","ehaCouchDbAuthHttpInterceptorProvider","$httpProvider",function(b,c,d){var e={localStorageNamespace:"eha",localStorageStoreName:"auth"};this.config=function(a){e=angular.extend(e,a),b.config({name:e.localStorageNamespace,storeName:e.localStorageStoreName}),a.interceptor&&(c.config({url:a.url,hosts:a.interceptor.hosts}),d.interceptors.push("ehaCouchDbAuthHttpInterceptor"))},this.requireAdminUser=function(a,b){return a.getCurrentUser().then(function(c){return c&&!c.isAdmin()?(a.trigger("unauthorized"),b.reject("unauthorized")):c})["catch"](function(){return a.trigger("unauthenticated"),b.reject("unauthenticated")})},this.requireAuthenticatedUser=function(a,b){return a.getCurrentUser().then(function(a){return a})["catch"](function(){return a.trigger("unauthenticated"),b.reject("unauthenticated")})},this.$get=["Restangular","$log","$q","$localForage","$rootScope",function(b,c,d,f,g){var h=b.withConfig(function(a){a.setBaseUrl(e.url),e.defaultHttpFields&&a.setDefaultHttpFields(e.defaultHttpFields)});return new a(e,h,c,d,f,g)}]}]),"undefined"!=typeof module&&module.exports&&(module.exports=b)}(),function(){"use strict";function a(a,b){function c(b){var c=a.hosts.filter(function(a){return b.indexOf(a)>-1});return!!c.length}var d=b.get("$q"),e=b.get("$log");return{request:function(a){if(c(a.url)){var f=b.get("ehaCouchDbAuthService");return f.getCurrentUser().then(function(b){return b&&b.bearerToken&&(a.headers.Authorization="Bearer "+b.bearerToken),a})["catch"](function(b){return e.error(b),a})}return d.when(a)},responseError:function(a){if(401===a.status&&c(a.config.url)){var e=b.get("ehaCouchDbAuthService");e.trigger("unauthorized")}return d.reject(a)}}}var b=angular.module("eha.couchdb-auth.http-interceptor",[]);b.provider("ehaCouchDbAuthHttpInterceptor",function(){var b={};this.config=function(a){b=a},this.$get=["$injector",function(c){return new a(b,c)}]}),"undefined"!=typeof module&&module.exports&&(module.exports=b)}(),angular.module("eha.couchdb-auth.show-authenticated.directive",[]).directive("ehaShowAuthenticated",["ehaCouchDbAuthService","$animate",function(a,b){var c="ng-hide",d="ng-hide-animate";return{restrict:"A",link:function(e,f){function g(){a.getCurrentUser().then(function(){b.removeClass(f,c,{tempClasses:d})})["catch"](function(){b.addClass(f,c,{tempClasses:d})})}f.addClass("ng-hide"),g(),a.on("authenticationStateChange",g)}}}]),function(){"use strict";var a=angular.module("eha.couchdb-auth",["eha.couchdb-auth.http-interceptor","eha.couchdb-auth.auth.service","eha.couchdb-auth.show-authenticated.directive"]);"undefined"!=typeof module&&module.exports&&(module.exports=a)}(); | ||
!function(){"use strict";function a(a,b,c,d,e,f){function g(){return d.when(b.all("_session").customGET())}function h(a){return d.when(b.all("_session").customPOST({name:a.username,password:a.password})).then(s).then(function(a){return g().then(function(){return a})}).then(function(a){return a&&a.ok?(u.$broadcast("authenticationStateChange"),c.log("couchdb:login:success",a),a):(c.log("couchdb:login:failure:unknown"),d.reject(new Error))})["catch"](function(a){return 401===a.status?(c.log("couchdb:login:failure:invalid-credentials",a),d.reject(new Error("Invalid Credentials"))):(c.log("couchdb:login:failure:unknown",a),d.reject(new Error(a)))})}function i(){return t=null,e.removeItem("user")}function j(a){return e.setItem("user",a)}function k(){return e.getItem("user")}function l(){return d.when(b.all("_session").remove()).then(i)["finally"](function(){u.$broadcast("authenticationStateChange")})}function m(a){return a.token&&a.password?d.when(b.all("reset-password").customPOST({token:a.token,password:a.password})):a.email?d.when(b.all("reset-password").customPOST({email:a.email,callbackUrl:"http://localhost:5000/#/reset-password"})):void 0}function n(){return d.reject("NOT_IMPLEMENTED")}function o(){return d.reject("NOT_IMPLEMENTED")}function p(){return d.reject("NOT_IMPLEMENTED")}function q(b){return b.hasRole=function(a){var b=this;if(angular.isArray(a)){var c=a.filter(function(a){return b.roles.indexOf(a)>-1});return!!c.length}return angular.isString(a)?this.roles.indexOf(a)>-1:void 0},b.isAdmin=function(){return this.hasRole(a.adminRoles)},b}function r(){return t?d.when(q(t)):k().then(function(a){return a?(t=a,q(a)):d.reject("User not found")}).then(function(a){return g().then(function(){return a})})["catch"](function(a){return d.reject(a)})}function s(a){return a?(a=a.plain(),t={name:a.name,roles:a.roles,bearerToken:a.bearerToken},j(a)):void d.reject("No user found")}var t,u=f.$new(!0);return u.$on("unauthorized",function(){i()}),{signIn:h,signOut:l,resetPassword:m,accounts:{add:n,update:o,remove:p},getSession:g,getCurrentUser:r,on:u.$on.bind(u),trigger:u.$broadcast.bind(u)}}var b=angular.module("eha.couchdb-auth.auth.service",["restangular","LocalForageModule","ngCookies"]);b.provider("ehaCouchDbAuthService",["$localForageProvider","ehaCouchDbAuthHttpInterceptorProvider","$httpProvider",function(b,c,d){var e={localStorageNamespace:"eha",localStorageStoreName:"auth",adminRoles:["_admin"]};this.config=function(a){e=angular.extend(e,a),b.config({name:e.localStorageNamespace,storeName:e.localStorageStoreName}),a.interceptor&&(c.config({url:a.url,hosts:a.interceptor.hosts}),d.interceptors.push("ehaCouchDbAuthHttpInterceptor"))},this.requireAdminUser=function(a,b){return a.getCurrentUser().then(function(c){return c&&!c.isAdmin()?(a.trigger("unauthorized"),b.reject("unauthorized")):c})["catch"](function(){return a.trigger("unauthenticated"),b.reject("unauthenticated")})},this.requireAuthenticatedUser=function(a,b){return a.getCurrentUser().then(function(a){return a})["catch"](function(){return a.trigger("unauthenticated"),b.reject("unauthenticated")})},this.$get=["Restangular","$log","$q","$localForage","$rootScope",function(b,c,d,f,g){var h=b.withConfig(function(a){a.setBaseUrl(e.url),e.defaultHttpFields&&a.setDefaultHttpFields(e.defaultHttpFields)});return new a(e,h,c,d,f,g)}]}]),"undefined"!=typeof module&&module.exports&&(module.exports=b)}(),function(){"use strict";function a(a,b){function c(b){var c=a.hosts.filter(function(a){return b.indexOf(a)>-1});return!!c.length}var d=b.get("$q"),e=b.get("$log");return{request:function(a){if(c(a.url)){var f=b.get("ehaCouchDbAuthService");return f.getCurrentUser().then(function(b){return b&&b.bearerToken&&(a.headers.Authorization="Bearer "+b.bearerToken),a})["catch"](function(b){return e.error(b),a})}return d.when(a)},responseError:function(a){if(401===a.status&&c(a.config.url)){var e=b.get("ehaCouchDbAuthService");e.trigger("unauthorized")}return d.reject(a)}}}var b=angular.module("eha.couchdb-auth.http-interceptor",[]);b.provider("ehaCouchDbAuthHttpInterceptor",function(){var b={};this.config=function(a){b=a},this.$get=["$injector",function(c){return new a(b,c)}]}),"undefined"!=typeof module&&module.exports&&(module.exports=b)}(),angular.module("eha.couchdb-auth.show-for-role.directive",[]).directive("ehaShowForRole",["ehaCouchDbAuthService","$animate","$parse","$q","$log",function(a,b,c,d,e){var f="ng-hide",g="ng-hide-animate";return{restrict:"A",link:function(h,i,j){function k(c){a.getCurrentUser().then(function(a){return a&&a.hasRole(c)?(b.removeClass(i,f,{tempClasses:g}),!0):d.reject("Role not found")})["catch"](function(a){e.error(a),b.addClass(i,f,{tempClasses:g})})}i.addClass("ng-hide");var l,m=c(j.ehaShowForRole)(h);if(angular.isArray(m))l=m;else{if(!angular.isString(m))throw Error("You must pass a string or an array of strings");l=[m]}k(l),a.on("authenticationStateChange",function(){k(l)})}}}]),angular.module("eha.couchdb-auth.show-authenticated.directive",[]).directive("ehaShowAuthenticated",["ehaCouchDbAuthService","$animate",function(a,b){var c="ng-hide",d="ng-hide-animate";return{restrict:"A",link:function(e,f){function g(){a.getCurrentUser().then(function(){b.removeClass(f,c,{tempClasses:d})})["catch"](function(){b.addClass(f,c,{tempClasses:d})})}f.addClass("ng-hide"),g(),a.on("authenticationStateChange",g)}}}]),function(){"use strict";var a=angular.module("eha.couchdb-auth",["eha.couchdb-auth.http-interceptor","eha.couchdb-auth.auth.service","eha.couchdb-auth.show-for-role.directive","eha.couchdb-auth.show-authenticated.directive"]);"undefined"!=typeof module&&module.exports&&(module.exports=a)}(); |
@@ -30,2 +30,3 @@ module.exports = function(grunt) { | ||
'src/http-interceptor.js', | ||
'src/show-for-role.directive.js', | ||
'src/show-authenticated.directive.js', | ||
@@ -141,6 +142,6 @@ 'src/index.js' | ||
'clean', | ||
'concat:scripts', | ||
'ngAnnotate', | ||
@@ -147,0 +148,0 @@ 'copy:scripts', |
{ | ||
"name": "angular-eha.couchdb-auth", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "CouchDB auth wrapper", | ||
@@ -5,0 +5,0 @@ "main": [ |
@@ -5,3 +5,3 @@ # angular-eha.couchdb-auth | ||
A simple Angular.js CouchDB auth interface. Works great directly with a CouchDB instance, works even better with a [CouchDB proxy/wrapper API](https://github.com/eHealthAfrica/hapi-couchdb-auth-bearer-plugin). | ||
A simple Angular.js CouchDB auth interface. Works great directly with a CouchDB instance, works even better with a [CouchDB proxy/wrapper API](https://github.com/eHealthAfrica/hapi-couchdb-auth-bearer-plugin). | ||
@@ -17,3 +17,3 @@ ## Installation | ||
bower install --save angular-eha.couchdb-auth | ||
## Usage | ||
@@ -32,5 +32,6 @@ | ||
localStorageNamespace: 'mnutrition', // Namespace for localstorage (default: lf) | ||
adminRoles: ['admin'], // 'Admin' CouchDB role. (default: `['_admin']`) | ||
interceptor: { // Enable HTTP Interceptor (default: false) | ||
hosts: [ // Configure hostnames that should be intercepted | ||
'http://mycouchdb.com' | ||
'http://mycouchdb.com' | ||
] | ||
@@ -84,3 +85,3 @@ } | ||
_Promise/A+_ Checks the local environment for a user, failing that checks local storage and finally attempts to GET the `_session/` endpoint of the CouchDB url. | ||
_Promise/A+_ Checks the local environment for a user, failing that checks local storage and finally attempts to GET the `_session/` endpoint of the CouchDB url. | ||
@@ -112,2 +113,16 @@ _Returns a promise_ | ||
### `eha-show-for-role` directive | ||
A simple directive to hide/show dom elements for users depending on their access control (role) level. Accepts either a single string or an array of strings. | ||
e.g: | ||
```html | ||
<!-- single string. must be an expression. n.b. `'`s are required --> | ||
<div eha-show-for-role="'admin'"></div> | ||
<!-- an array of strings --> | ||
<div eha-show-for-role="['role1', 'role2']"></div> | ||
``` | ||
## License | ||
@@ -114,0 +129,0 @@ |
@@ -44,3 +44,2 @@ ;(function() { | ||
.then(function(user) { | ||
console.log('GOT USER'); | ||
return getSession() | ||
@@ -129,7 +128,15 @@ .then(function() { | ||
user.hasRole = function(role) { | ||
return this.roles.indexOf(role) > -1; | ||
var self = this; | ||
if (angular.isArray(role)) { | ||
var matches = role.filter(function(r) { | ||
return self.roles.indexOf(r) > -1; | ||
}); | ||
return !!matches.length; | ||
} else if (angular.isString(role)) { | ||
return this.roles.indexOf(role) > -1; | ||
} | ||
}; | ||
user.isAdmin = function() { | ||
return this.hasRole('_admin'); | ||
return this.hasRole(options.adminRoles); | ||
}; | ||
@@ -160,3 +167,2 @@ return user; | ||
.catch(function(err) { | ||
console.log(err); | ||
return $q.reject(err); | ||
@@ -207,3 +213,4 @@ }); | ||
localStorageNamespace: 'eha', | ||
localStorageStoreName: 'auth' | ||
localStorageStoreName: 'auth', | ||
adminRoles: ['_admin'] | ||
}; | ||
@@ -213,2 +220,3 @@ | ||
options = angular.extend(options, config); | ||
$localForageProvider.config({ | ||
@@ -229,2 +237,3 @@ name: options.localStorageNamespace, | ||
this.requireAdminUser = function(ehaCouchDbAuthService, $q) { | ||
return ehaCouchDbAuthService.getCurrentUser() | ||
@@ -231,0 +240,0 @@ .then(function(user) { |
@@ -6,2 +6,3 @@ ;(function() { | ||
'eha.couchdb-auth.auth.service', | ||
'eha.couchdb-auth.show-for-role.directive', | ||
'eha.couchdb-auth.show-authenticated.directive' | ||
@@ -8,0 +9,0 @@ ]); |
69818
21
1386
132