diff --git a/dist/basyt.angular.js b/dist/basyt.angular.js index 28cfbde..2d4ceb9 100644 --- a/dist/basyt.angular.js +++ b/dist/basyt.angular.js @@ -15,7 +15,7 @@ angular.module('basyt-angular', ['ui.router']) socket: window.SOCKET_URL, socketOptions: window.SOCKET_OPTS }) - .run(['$rootScope', '$state', '$injector', 'BasytAuth', 'BasytAnonState', 'BasytAuthMessages', function($rootScope, $state, BasytAuth, $injector, BasytAnonState, BasytAuthMessages){ + .run(['$rootScope', '$state', '$injector', 'BasytAuth', 'BasytAnonState', 'BasytAuthMessages', function($rootScope, $state, $injector, BasytAuth, BasytAnonState, BasytAuthMessages){ var $alert = $injector.get('$alert'); $rootScope.$on("$stateChangeStart", function(event, next) { if (next.role) { diff --git a/dist/basyt.angular.min.js b/dist/basyt.angular.min.js index 0d2992f..7f85585 100644 --- a/dist/basyt.angular.min.js +++ b/dist/basyt.angular.min.js @@ -1 +1 @@ -angular.module("basyt-angular",["ui.router"]).config(["$httpProvider",function(a){a.interceptors.push("BasytAuthInterceptor")}]).value("BasytAnonState","login").value("BasytAuthMessages",{loginRequired:"Login Required",loginSuccess:"Login Successful",loginFailed:"Login Failed",logoutSuccess:"Logout Successful",authFailed:"Authorization Failed"}).value("BasytServer",{host:window.API_URL,socket:window.SOCKET_URL,socketOptions:window.SOCKET_OPTS}).run(["$rootScope","$state","$injector","BasytAuth","BasytAnonState","BasytAuthMessages",function(a,b,c,d,e,f){var g=d.get("$alert");a.$on("$stateChangeStart",function(a,d){d.role&&(c.isAuthenticated(d.role)||(g&&g({title:f.authFailed,type:"danger",duration:6}),b.go(e),a.preventDefault()))}),a.$on("user:anonymous",function(){angular.isDefined(b.current.role)&&"ANON"!==b.current.role&&g&&g({title:f.loginRequired,type:"danger",duration:6}),b.go(e)})}]),angular.module("basyt-angular").factory("BasytAuth",["BasytLocalStore","BasytRequest","$q","$rootScope","$injector","BasytAuthMessages",function(a,b,c,d,e,f){var g={user_state:"ANON"},h=g,i=e.get("$alert"),j=function(b){return a.set("auth_token",b.result.token),delete b.result.token,a.set("auth_user",JSON.stringify(b.result)),h=b.result,d.activeUser=h,d.activeUser.user_state=m.isLord()?"LORD":m.isAdmin()?"ADMIN":"USER",i&&i({title:f.loginSuccess,type:"info",duration:3}),d.$broadcast("user:login",b.result),h},k=function(b){a.unset("auth_token"),a.unset("auth_user"),h=g,d.activeUser=h,!b&&i&&i({title:f.logoutSuccess,type:"info",duration:3}),d.$broadcast("user:logout")},l=function(a){var b=c.defer();return k(!0),b.reject(a),i&&i({title:f.loginFailed,type:"danger",duration:6}),b.promise},m={isAuthenticated:function(b,c){if(a.get("auth_token")){if(c&&m.authenticate(),angular.isUndefined(h.id)){var e=a.get("auth_user");if(!e)return!1;if(h=angular.fromJson(e),angular.isUndefined(h.id))return!1;d.activeUser=h,d.activeUser.user_state=m.isLord()?"LORD":m.isAdmin()?"ADMIN":"USER"}return"USER"!=b?angular.isUndefined(h.id)?!1:angular.isDefined(h.roles)?h.roles.indexOf(b)>-1:!1:!0}return!1},getUser:function(){return h},isLord:function(){return angular.isDefined(h.roles)?h.roles.indexOf("LORD")>-1:!1},isAdmin:function(){return angular.isDefined(h.roles)?h.roles.indexOf("ADMIN")>-1||h.roles.indexOf("LORD")>-1:!1},login:function(a){return k(!0),b("user:login",{data:a}).then(j,l)},logout:k,register:function(a){return k(!0),b("user:register",{user:a}).then(j,l)},authenticate:function(){return b("user:authenticate").then(function(){},k)}};return d.activeUser=h,m}]),angular.module("basyt-angular").factory("BasytAuthInterceptor",["$q","BasytLocalStore","$rootScope",function(a,b,c){return{request:function(a){var c;return b.get("auth_token")&&(c=b.get("auth_token")),c&&(a.headers.Authorization="Bearer "+c),a},responseError:function(d){return(401===d.status||403===d.status)&&(b.unset("auth_token"),b.unset("auth_user"),c.$broadcast("user:anonymous")),a.reject(d)}}}]),angular.module("basyt-angular").factory("BasytEntityBridge",["BasytRequest","BasytSocket","$rootScope","filterFilter","$q",function(a,b,c,d,e){var f=function(a,c){var d=this;this.endpoint=a+":list",this.socketChannel="entity:"+a,this.map=c,this.isLoaded=!1,this.listeners=[],this.value=[],b.on("entity:update:"+a,function(){d.reload(!1)}),this.reload(!0)};return f.prototype.bind=function(){var a=e.defer(),b=this,c=a.promise;return this.listeners.push(a),c.list=this.value,c.item=function(a){var c=d(b.value,{id:a});return angular.isArray(c)?c[0]:null},c.unbind=function(){a.resolve(!0),b.listeners.splice(b.listeners.indexOf(a),1)},c.listen=function(b){return a.promise.then(function(){},function(){},b)},c},f.prototype.reload=function(c){var d=this;a(this.endpoint,{params:{deep:!0}}).then(function(a){d.map&&angular.forEach(a.result,d.map),d.value=a.result,d.isLoaded=!0,angular.forEach(d.listeners,function(a){a.notify(d.value)}),c&&b.subscribe(d.socketChannel)},function(){d.value=[],d.isLoaded=!0})},f}]),angular.module("basyt-angular").factory("BasytLocalStore",["$window",function(a){return{get:function(b){return a.localStorage.getItem(b)},set:function(b,c){return a.localStorage.setItem(b,c)},unset:function(b){return a.localStorage.removeItem(b)}}}]),angular.module("basyt-angular").factory("BasytRequest",["$http","$q","$rootScope","$urlMatcherFactory","BasytServer",function(a,b,c,d,e){var f,g,h=!1,i=b.defer();return a.get(e.host).then(function(a){return f=a.data.routes,h=!0,c.$broadcast("basyt:request:ready"),i.resolve(f),f},function(){i.reject(!1)}),g=i.promise,function(c,h){return g.then(function(){if(f.hasOwnProperty(c)){var g=angular.copy(h)||{};g.hasOwnProperty("urlParams")?(g.url=e.host+d.compile(f[c].path).format(g.urlParams),delete g.urlParams):g.url=e.host+f[c].path;var i=angular.extend({},f[c],g);return a(i).error(function(a){var c=b.defer();return c.reject(a.data?a.data.err:!1),c.promise}).then(function(a){var c=b.defer();return a.data&&a.data.success?c.resolve(a.data):c.reject(a.data?a.data.err:!1),c.promise})}return null},function(){return null})}}]),angular.module("basyt-angular").factory("BasytSocket",["$rootScope","$q","BasytServer","BasytLocalStore",function(a,b,c,d){var e,f=b.defer();return connect=function(){e=io.connect(c.socket,c.socketOptions),e.on("authenticated",function(){f.resolve(!0),a.$broadcast("basyt:socket:ready")}).emit("authenticate",{token:d.get("auth_token")})},angular.isDefined(io)&&connect(),{on:function(a,b){e&&e.on(a,b),f.promise.then(function(){e.on(a,b)})},off:function(a,b){e&&e.removeAllListeners(a,b)},subscribe:function(a,b){e.emit("subscribe",{resource:a,data:b})},unsubscribe:function(a,b){e.emit("unsubscribe",{resource:a,data:b})},emit:function(a,b){e.emit(a,b)},connect:connect}}]),angular.module("basyt-angular").factory("BasytUserSettings",["$rootScope","BasytRequest","BasytAuth",function(a,b,c){var d,e=!1,f={isReady:function(){return e},getSettings:function(){return e?d:{}},reload:function(){b("user_settings:get").then(function(b){d=b.result||{},e=!0,a.$broadcast("basyt:user_settings:ready")})}};return c.isAuthenticated&&f.reload(),a.$on("user:login",function(){f.reload()}),a.$on("user:logout",function(){e=!1}),a.$on("user:anonymous",function(){e=!1}),f}]); \ No newline at end of file +angular.module("basyt-angular",["ui.router"]).config(["$httpProvider",function(a){a.interceptors.push("BasytAuthInterceptor")}]).value("BasytAnonState","login").value("BasytAuthMessages",{loginRequired:"Login Required",loginSuccess:"Login Successful",loginFailed:"Login Failed",logoutSuccess:"Logout Successful",authFailed:"Authorization Failed"}).value("BasytServer",{host:window.API_URL,socket:window.SOCKET_URL,socketOptions:window.SOCKET_OPTS}).run(["$rootScope","$state","$injector","BasytAuth","BasytAnonState","BasytAuthMessages",function(a,b,c,d,e,f){var g=c.get("$alert");a.$on("$stateChangeStart",function(a,c){c.role&&(d.isAuthenticated(c.role)||(g&&g({title:f.authFailed,type:"danger",duration:6}),b.go(e),a.preventDefault()))}),a.$on("user:anonymous",function(){angular.isDefined(b.current.role)&&"ANON"!==b.current.role&&g&&g({title:f.loginRequired,type:"danger",duration:6}),b.go(e)})}]),angular.module("basyt-angular").factory("BasytAuth",["BasytLocalStore","BasytRequest","$q","$rootScope","$injector","BasytAuthMessages",function(a,b,c,d,e,f){var g={user_state:"ANON"},h=g,i=e.get("$alert"),j=function(b){return a.set("auth_token",b.result.token),delete b.result.token,a.set("auth_user",JSON.stringify(b.result)),h=b.result,d.activeUser=h,d.activeUser.user_state=m.isLord()?"LORD":m.isAdmin()?"ADMIN":"USER",i&&i({title:f.loginSuccess,type:"info",duration:3}),d.$broadcast("user:login",b.result),h},k=function(b){a.unset("auth_token"),a.unset("auth_user"),h=g,d.activeUser=h,!b&&i&&i({title:f.logoutSuccess,type:"info",duration:3}),d.$broadcast("user:logout")},l=function(a){var b=c.defer();return k(!0),b.reject(a),i&&i({title:f.loginFailed,type:"danger",duration:6}),b.promise},m={isAuthenticated:function(b,c){if(a.get("auth_token")){if(c&&m.authenticate(),angular.isUndefined(h.id)){var e=a.get("auth_user");if(!e)return!1;if(h=angular.fromJson(e),angular.isUndefined(h.id))return!1;d.activeUser=h,d.activeUser.user_state=m.isLord()?"LORD":m.isAdmin()?"ADMIN":"USER"}return"USER"!=b?angular.isUndefined(h.id)?!1:angular.isDefined(h.roles)?h.roles.indexOf(b)>-1:!1:!0}return!1},getUser:function(){return h},isLord:function(){return angular.isDefined(h.roles)?h.roles.indexOf("LORD")>-1:!1},isAdmin:function(){return angular.isDefined(h.roles)?h.roles.indexOf("ADMIN")>-1||h.roles.indexOf("LORD")>-1:!1},login:function(a){return k(!0),b("user:login",{data:a}).then(j,l)},logout:k,register:function(a){return k(!0),b("user:register",{user:a}).then(j,l)},authenticate:function(){return b("user:authenticate").then(function(){},k)}};return d.activeUser=h,m}]),angular.module("basyt-angular").factory("BasytAuthInterceptor",["$q","BasytLocalStore","$rootScope",function(a,b,c){return{request:function(a){var c;return b.get("auth_token")&&(c=b.get("auth_token")),c&&(a.headers.Authorization="Bearer "+c),a},responseError:function(d){return(401===d.status||403===d.status)&&(b.unset("auth_token"),b.unset("auth_user"),c.$broadcast("user:anonymous")),a.reject(d)}}}]),angular.module("basyt-angular").factory("BasytEntityBridge",["BasytRequest","BasytSocket","$rootScope","filterFilter","$q",function(a,b,c,d,e){var f=function(a,c){var d=this;this.endpoint=a+":list",this.socketChannel="entity:"+a,this.map=c,this.isLoaded=!1,this.listeners=[],this.value=[],b.on("entity:update:"+a,function(){d.reload(!1)}),this.reload(!0)};return f.prototype.bind=function(){var a=e.defer(),b=this,c=a.promise;return this.listeners.push(a),c.list=this.value,c.item=function(a){var c=d(b.value,{id:a});return angular.isArray(c)?c[0]:null},c.unbind=function(){a.resolve(!0),b.listeners.splice(b.listeners.indexOf(a),1)},c.listen=function(b){return a.promise.then(function(){},function(){},b)},c},f.prototype.reload=function(c){var d=this;a(this.endpoint,{params:{deep:!0}}).then(function(a){d.map&&angular.forEach(a.result,d.map),d.value=a.result,d.isLoaded=!0,angular.forEach(d.listeners,function(a){a.notify(d.value)}),c&&b.subscribe(d.socketChannel)},function(){d.value=[],d.isLoaded=!0})},f}]),angular.module("basyt-angular").factory("BasytLocalStore",["$window",function(a){return{get:function(b){return a.localStorage.getItem(b)},set:function(b,c){return a.localStorage.setItem(b,c)},unset:function(b){return a.localStorage.removeItem(b)}}}]),angular.module("basyt-angular").factory("BasytRequest",["$http","$q","$rootScope","$urlMatcherFactory","BasytServer",function(a,b,c,d,e){var f,g,h=!1,i=b.defer();return a.get(e.host).then(function(a){return f=a.data.routes,h=!0,c.$broadcast("basyt:request:ready"),i.resolve(f),f},function(){i.reject(!1)}),g=i.promise,function(c,h){return g.then(function(){if(f.hasOwnProperty(c)){var g=angular.copy(h)||{};g.hasOwnProperty("urlParams")?(g.url=e.host+d.compile(f[c].path).format(g.urlParams),delete g.urlParams):g.url=e.host+f[c].path;var i=angular.extend({},f[c],g);return a(i).error(function(a){var c=b.defer();return c.reject(a.data?a.data.err:!1),c.promise}).then(function(a){var c=b.defer();return a.data&&a.data.success?c.resolve(a.data):c.reject(a.data?a.data.err:!1),c.promise})}return null},function(){return null})}}]),angular.module("basyt-angular").factory("BasytSocket",["$rootScope","$q","BasytServer","BasytLocalStore",function(a,b,c,d){var e,f=b.defer();return connect=function(){e=io.connect(c.socket,c.socketOptions),e.on("authenticated",function(){f.resolve(!0),a.$broadcast("basyt:socket:ready")}).emit("authenticate",{token:d.get("auth_token")})},angular.isDefined(io)&&connect(),{on:function(a,b){e&&e.on(a,b),f.promise.then(function(){e.on(a,b)})},off:function(a,b){e&&e.removeAllListeners(a,b)},subscribe:function(a,b){e.emit("subscribe",{resource:a,data:b})},unsubscribe:function(a,b){e.emit("unsubscribe",{resource:a,data:b})},emit:function(a,b){e.emit(a,b)},connect:connect}}]),angular.module("basyt-angular").factory("BasytUserSettings",["$rootScope","BasytRequest","BasytAuth",function(a,b,c){var d,e=!1,f={isReady:function(){return e},getSettings:function(){return e?d:{}},reload:function(){b("user_settings:get").then(function(b){d=b.result||{},e=!0,a.$broadcast("basyt:user_settings:ready")})}};return c.isAuthenticated&&f.reload(),a.$on("user:login",function(){f.reload()}),a.$on("user:logout",function(){e=!1}),a.$on("user:anonymous",function(){e=!1}),f}]); \ No newline at end of file diff --git a/module.js b/module.js index 30ab7d3..de97573 100644 --- a/module.js +++ b/module.js @@ -15,7 +15,7 @@ angular.module('basyt-angular', ['ui.router']) socket: window.SOCKET_URL, socketOptions: window.SOCKET_OPTS }) - .run(['$rootScope', '$state', '$injector', 'BasytAuth', 'BasytAnonState', 'BasytAuthMessages', function($rootScope, $state, BasytAuth, $injector, BasytAnonState, BasytAuthMessages){ + .run(['$rootScope', '$state', '$injector', 'BasytAuth', 'BasytAnonState', 'BasytAuthMessages', function($rootScope, $state, $injector, BasytAuth, BasytAnonState, BasytAuthMessages){ var $alert = $injector.get('$alert'); $rootScope.$on("$stateChangeStart", function(event, next) { if (next.role) {