From 7dc54c9fee1643ab99cef6920f743e91ccd3301b Mon Sep 17 00:00:00 2001 From: Mehmet Kamil Sulubulut Date: Thu, 6 Aug 2015 16:40:56 +0300 Subject: [PATCH 1/2] dev branch --- dist/basyt.angular.js | 135 ++++++++++++++++++++------------------ dist/basyt.angular.min.js | 2 +- package.json | 2 +- 3 files changed, 72 insertions(+), 67 deletions(-) diff --git a/dist/basyt.angular.js b/dist/basyt.angular.js index 7ff4694..28cfbde 100644 --- a/dist/basyt.angular.js +++ b/dist/basyt.angular.js @@ -1,20 +1,25 @@ -angular.module('basyt.angular', ['ui.router']) +angular.module('basyt-angular', ['ui.router']) .config(['$httpProvider', function ($httpProvider) { - $httpProvider.interceptors.push('AuthInterceptor'); + $httpProvider.interceptors.push('BasytAuthInterceptor'); }]) .value('BasytAnonState', 'login') .value('BasytAuthMessages', { - 'loginRequired': 'Login Required', - 'loginSuccess': 'Login Successful', - 'loginFailed': 'Login Failed', - 'logoutSuccess': 'Logout Successful', - 'authFailed': 'Authorization Failed' + loginRequired: 'Login Required', + loginSuccess: 'Login Successful', + loginFailed: 'Login Failed', + logoutSuccess: 'Logout Successful', + authFailed: 'Authorization Failed' }) - .run(['$rootScope', 'Auth', '$state','BasytAnonState', '$injector', 'BasytAuthMessages', function($rootScope, Auth, $state, BasytAnonState, $injector, BasytAuthMessages){ + .value('BasytServer', { + host: window.API_URL, + socket: window.SOCKET_URL, + socketOptions: window.SOCKET_OPTS + }) + .run(['$rootScope', '$state', '$injector', 'BasytAuth', 'BasytAnonState', 'BasytAuthMessages', function($rootScope, $state, BasytAuth, $injector, BasytAnonState, BasytAuthMessages){ var $alert = $injector.get('$alert'); $rootScope.$on("$stateChangeStart", function(event, next) { if (next.role) { - if (!Auth.isAuthenticated(next.role)) { + if (!BasytAuth.isAuthenticated(next.role)) { if($alert) { $alert({ title: BasytAuthMessages.authFailed, @@ -39,8 +44,9 @@ angular.module('basyt.angular', ['ui.router']) $state.go(BasytAnonState); }); }]); -angular.module('basyt.angular') - .factory('Auth', ['BasytLocalStore', 'Request', '$q', '$rootScope', '$injector', 'BasytAuthMessages', function (BasytLocalStore, Request, $q, $rootScope, $injector, BasytAuthMessages) { + +angular.module('basyt-angular') + .factory('BasytAuth', ['BasytLocalStore', 'BasytRequest', '$q', '$rootScope', '$injector', 'BasytAuthMessages', function (BasytLocalStore, BasytRequest, $q, $rootScope, $injector, BasytAuthMessages) { var AnonUser = { user_state: 'ANON' }, @@ -95,13 +101,6 @@ angular.module('basyt.angular') return deferred.promise; }, Auth = { - authorize: function (access) { - if (access) { - return this.isAuthenticated(access); - } else { - return true; - } - }, isAuthenticated: function (access, remote) { if (BasytLocalStore.get('auth_token')) { if (remote) Auth.authenticate(); @@ -143,23 +142,24 @@ angular.module('basyt.angular') login: function (credentials, rememberMe) { logout(true); //if(rememberMe) - return Request('user:login', {data: credentials}).then(login, logoutReject); + return BasytRequest('user:login', {data: credentials}).then(login, logoutReject); }, logout: logout, register: function (formData) { logout(true); - return Request('user:register', {user: formData}).then(login, logoutReject); + return BasytRequest('user:register', {user: formData}).then(login, logoutReject); }, authenticate: function () { - return Request('user:authorize').then(function () { + return BasytRequest('user:authenticate').then(function () { }, logout); } }; $rootScope.activeUser = User; return Auth; }]); -angular.module('basyt.angular') - .factory('AuthInterceptor', ['$q', 'BasytLocalStore', '$rootScope', function ($q, BasytLocalStore, $rootScope) { + +angular.module('basyt-angular') + .factory('BasytAuthInterceptor', ['$q', 'BasytLocalStore', '$rootScope', function ($q, BasytLocalStore, $rootScope) { return { request: function (config) { var token; @@ -181,9 +181,10 @@ angular.module('basyt.angular') } }; }]); -angular.module('basyt.angular') - .service('DataSource', ['Request', '$rootScope', 'filterFilter', 'Socket', '$q', function (Request, $rootScope, filterFilter, Socket, $q) { - var DataSource = function (entityName, map) { + +angular.module('basyt-angular') + .factory('BasytEntityBridge', ['BasytRequest', 'BasytSocket', '$rootScope', 'filterFilter', '$q', function (BasytRequest, BasytSocket, $rootScope, filterFilter, $q) { + var EntityBridge = function (entityName, map) { var that = this; this.endpoint = entityName + ':list'; this.socketChannel = 'entity:' + entityName; @@ -191,13 +192,13 @@ angular.module('basyt.angular') this.isLoaded = false; this.listeners = []; this.value = []; - Socket.on('entity:update:' + entityName, function (message) { + BasytSocket.on('entity:update:' + entityName, function (message) { that.reload(false); }); this.reload(true); }; - DataSource.prototype.bind = function () { + EntityBridge.prototype.bind = function () { var deferred = $q.defer(), that = this, promise = deferred.promise; this.listeners.push(deferred); promise.list = this.value; @@ -216,9 +217,9 @@ angular.module('basyt.angular') }; return promise; }; - DataSource.prototype.reload = function (subscribe) { + EntityBridge.prototype.reload = function (subscribe) { var that = this; - Request(this.endpoint, {params: {deep: true}}) + BasytRequest(this.endpoint, {params: {deep: true}}) .then(function (data) { if (that.map) { angular.forEach(data.result, that.map); @@ -229,7 +230,7 @@ angular.module('basyt.angular') deferred.notify(that.value); }); if (subscribe) { - Socket.subscribe(that.socketChannel); + BasytSocket.subscribe(that.socketChannel); } }, function () { @@ -238,30 +239,26 @@ angular.module('basyt.angular') }); }; - return DataSource; + return EntityBridge; }]); -angular.module('basyt.angular') - .factory('BasytLocalStore', function() { +angular.module('basyt-angular') + .factory('BasytLocalStore', ['$window', function($window) { return { get: function(key) { - return localStorage.getItem(key); + return $window.localStorage.getItem(key); }, set: function(key, val) { - return localStorage.setItem(key, val); + return $window.localStorage.setItem(key, val); }, unset: function(key) { - return localStorage.removeItem(key); + return $window.localStorage.removeItem(key); } }; - }); -angular.module('basyt.angular') - .constant('BasytServer', { - host: window.API_URL, - socket: window.SOCKET_URL, - socketOptions: window.SOCKET_OPTS - }) - .factory('Request', ['$http', 'BasytServer', '$q', '$rootScope', '$urlMatcherFactory', function ($http, BasytServer, $q, $rootScope, $urlMatcherFactory) { + }]); + +angular.module('basyt-angular') + .factory('BasytRequest', ['$http', '$q', '$rootScope', '$urlMatcherFactory', 'BasytServer', function ($http, $q, $rootScope, $urlMatcherFactory, BasytServer) { var endpoints, initialized = false, future, deferred = $q.defer(); $http.get(BasytServer.host) @@ -321,8 +318,9 @@ angular.module('basyt.angular') ); }; }]); -angular.module('basyt.angular') - .factory('Socket', ['BasytServer', 'BasytLocalStore', '$rootScope', '$q', 'Auth', function (BasytServer, BasytLocalStore, $rootScope, $q) { + +angular.module('basyt-angular') + .factory('BasytSocket', ['$rootScope', '$q', 'BasytServer', 'BasytLocalStore', function ($rootScope, $q, BasytServer, BasytLocalStore) { var connection, future = $q.defer(); connect = function () { connection = io.connect(BasytServer.socket, BasytServer.socketOptions); @@ -334,7 +332,8 @@ angular.module('basyt.angular') .emit('authenticate', {token: BasytLocalStore.get('auth_token')}); //send the jwt }; - connect(); + if(angular.isDefined(io)) + connect(); return { on: function (event, cb) { if (connection) { @@ -349,11 +348,11 @@ angular.module('basyt.angular') connection.removeAllListeners(event, cb); } }, - subscribe: function (event, data) { - connection.emit('subscribe', {resource: event, data: data}); + subscribe: function (channel, data) { + connection.emit('subscribe', {resource: channel, data: data}); }, - unsubscribe: function (event, data) { - connection.emit('unsubscribe', {resource: event, data: data}); + unsubscribe: function (channel, data) { + connection.emit('unsubscribe', {resource: channel, data: data}); }, emit: function (label, data) { connection.emit(label, data); @@ -361,8 +360,9 @@ angular.module('basyt.angular') connect: connect }; }]); -angular.module('basyt.angular') - .factory('UserSettings', ['Request', '$rootScope', function (Request, $rootScope) { + +angular.module('basyt-angular') + .factory('BasytUserSettings', ['$rootScope', 'BasytRequest', 'BasytAuth', function ($rootScope, BasytRequest, BasytAuth) { var settings, ready = false, service = { isReady: function () { @@ -372,20 +372,25 @@ angular.module('basyt.angular') return ready ? settings : {}; }, reload: function () { - Request('user_settings:get') - .then( - function (data) { + BasytRequest('user_settings:get') + .then(function (data) { settings = data.result || {}; ready = true; - $rootScope.$broadcast('user:registered'); - }, - function(){ - $rootScope.$broadcast('user:anonymous'); + $rootScope.$broadcast('basyt:user_settings:ready'); }); } }; - - service.reload(); - + if(BasytAuth.isAuthenticated) { + service.reload(); + } + $rootScope.$on('user:login', function(){ + service.reload(); + }); + $rootScope.$on('user:logout', function(){ + ready = false; + }); + $rootScope.$on('user:anonymous', function(){ + ready = false; + }); return service; - }]); \ No newline at end of file + }]); diff --git a/dist/basyt.angular.min.js b/dist/basyt.angular.min.js index be9dcd0..0d2992f 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("AuthInterceptor")}]).value("BasytAnonState","login").value("BasytAuthMessages",{loginRequired:"Login Required",loginSuccess:"Login Successful",loginFailed:"Login Failed",logoutSuccess:"Logout Successful",authFailed:"Authorization Failed"}).run(["$rootScope","Auth","$state","BasytAnonState","$injector","BasytAuthMessages",function(a,b,c,d,e,f){var g=e.get("$alert");a.$on("$stateChangeStart",function(a,e){e.role&&(b.isAuthenticated(e.role)||(g&&g({title:f.authFailed,type:"danger",duration:6}),c.go(d),a.preventDefault()))}),a.$on("user:anonymous",function(){angular.isDefined(c.current.role)&&"ANON"!==c.current.role&&g&&g({title:f.loginRequired,type:"danger",duration:6}),c.go(d)})}]),angular.module("basyt.angular").factory("Auth",["BasytLocalStore","Request","$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={authorize:function(a){return a?this.isAuthenticated(a):!0},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,c){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:authorize").then(function(){},k)}};return d.activeUser=h,m}]),angular.module("basyt.angular").factory("AuthInterceptor",["$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").service("DataSource",["Request","$rootScope","filterFilter","Socket","$q",function(a,b,c,d,e){var f=function(a,b){var c=this;this.endpoint=a+":list",this.socketChannel="entity:"+a,this.map=b,this.isLoaded=!1,this.listeners=[],this.value=[],d.on("entity:update:"+a,function(a){c.reload(!1)}),this.reload(!0)};return f.prototype.bind=function(){var a=e.defer(),b=this,d=a.promise;return this.listeners.push(a),d.list=this.value,d.item=function(a){var d=c(b.value,{id:a});return angular.isArray(d)?d[0]:null},d.unbind=function(){a.resolve(!0),b.listeners.splice(b.listeners.indexOf(a),1)},d.listen=function(b){return a.promise.then(function(){},function(){},b)},d},f.prototype.reload=function(b){var c=this;a(this.endpoint,{params:{deep:!0}}).then(function(a){c.map&&angular.forEach(a.result,c.map),c.value=a.result,c.isLoaded=!0,angular.forEach(c.listeners,function(a){a.notify(c.value)}),b&&d.subscribe(c.socketChannel)},function(){c.value=[],c.isLoaded=!0})},f}]),angular.module("basyt.angular").factory("BasytLocalStore",function(){return{get:function(a){return localStorage.getItem(a)},set:function(a,b){return localStorage.setItem(a,b)},unset:function(a){return localStorage.removeItem(a)}}}),angular.module("basyt.angular").constant("BasytServer",{host:window.API_URL,socket:window.SOCKET_URL,socketOptions:window.SOCKET_OPTS}).factory("Request",["$http","BasytServer","$q","$rootScope","$urlMatcherFactory",function(a,b,c,d,e){var f,g,h=!1,i=c.defer();return a.get(b.host).then(function(a){return f=a.data.routes,h=!0,d.$broadcast("basyt:request:ready"),i.resolve(f),f},function(a,b){i.reject(!1)}),g=i.promise,function(d,h){return g.then(function(g){if(f.hasOwnProperty(d)){var i=angular.copy(h)||{};i.hasOwnProperty("urlParams")?(i.url=b.host+e.compile(f[d].path).format(i.urlParams),delete i.urlParams):i.url=b.host+f[d].path;var j=angular.extend({},f[d],i);return a(j).error(function(a){var b=c.defer();return a.data?b.reject(a.data.err):b.reject(!1),b.promise}).then(function(a){var b=c.defer();return a.data&&a.data.success?b.resolve(a.data):b.reject(a.data?a.data.err:!1),b.promise})}return null},function(){return null})}}]),angular.module("basyt.angular").factory("Socket",["BasytServer","BasytLocalStore","$rootScope","$q","Auth",function(a,b,c,d){var e,f=d.defer();return connect=function(){e=io.connect(a.socket,a.socketOptions),e.on("authenticated",function(){f.resolve(!0),c.$broadcast("basyt:socket:ready")}).emit("authenticate",{token:b.get("auth_token")})},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("UserSettings",["Request","$rootScope",function(a,b){var c,d=!1,e={isReady:function(){return d},getSettings:function(){return d?c:{}},reload:function(){a("user_settings:get").then(function(a){c=a.result||{},d=!0,b.$broadcast("user:registered")},function(){b.$broadcast("user:anonymous")})}};return e.reload(),e}]); \ 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=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 diff --git a/package.json b/package.json index ea6d62c..d3ea263 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "basyt-angular", - "version": "0.2.5", + "version": "0.3.0", "description": "basyt AngularJS Module", "repository": { "type": "git", From 4da58a448bc24589743cff270634627d15731077 Mon Sep 17 00:00:00 2001 From: Mehmet Kamil Sulubulut Date: Thu, 6 Aug 2015 16:51:32 +0300 Subject: [PATCH 2/2] Fixed argument order --- dist/basyt.angular.js | 2 +- dist/basyt.angular.min.js | 2 +- module.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) 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) {