var app = angular.module('DeltaPIApp', ['ngRoute', 'ngSanitize', 'sba-crud-table', 'sba-git', 'sba-auth']); app.config(function($httpProvider, $routeProvider, $locationProvider, $httpProvider) { $routeProvider .when('/', { controller: 'IndexCtrl', templateUrl: '/file/load/index.html', resolve: { access: function(Access) { return Access.isLoggedIn(); }, }, activetab: 'dashboard', }) .when('/page/products', { controller: 'ProductsCtrl', templateUrl: '/file/load/products.html', resolve: { access: function(Access) { return Access.isLoggedIn(); }, }, activetab: 'products', }) .when('/page/licenses', { controller: 'LicensesCtrl', templateUrl: '/file/load/licenses.html', resolve: { access: function(Access) { return Access.isLoggedIn(); }, }, activetab: 'licenses', }) .when('/page/license_computers', { controller: 'LicenseComputersCtrl', templateUrl: '/file/load/license_computers.html', resolve: { access: function(Access) { return Access.isLoggedIn(); }, }, activetab: 'license_computers', }) .when('/page/license_products', { controller: 'LicenseProductsCtrl', templateUrl: '/file/load/license_products.html', resolve: { access: function(Access) { return Access.isLoggedIn(); }, }, activetab: 'license_products', }) .when('/page/license_pool', { controller: 'LicensePoolCtrl', templateUrl: '/file/load/license_pool.html', resolve: { access: function(Access) { return Access.isLoggedIn(); }, }, activetab: 'license_pool', }) .when('/page/license_history', { controller: 'LicenseHistoryCtrl', templateUrl: '/file/load/license_history.html', resolve: { access: function(Access) { return Access.isLoggedIn(); }, }, activetab: 'license_history', }) .when('/page/admin', { controller: 'AdminCtrl', templateUrl: '/file/load/admin.html', resolve: { access: function(Access) { return Access.isLoggedIn(); }, }, activetab: 'admin', }) .when('/page/git', { controller: 'GitCtrl', templateUrl: '/file/load/git.html', resolve: { access: function(Access) { return Access.isLoggedIn(); }, }, activetab: 'git', }) .when('/page/profile', { controller: 'ProfileCtrl', templateUrl: '/file/load/profile.html', resolve: { access: function(Access) { return Access.isLoggedIn(); }, user: function(User) { return User; } }, }) .when('/page/login', { controller: 'LoginCtrl', templateUrl: '/file/load/login.html', resolve: { user: function(User) { return User; } }, }) .otherwise('/'); $httpProvider.interceptors.push('myHttpInterceptor'); $locationProvider.html5Mode(true); }); app.factory('myHttpInterceptor', function($q, $injector, $window) { return { 'response': function(response) { if (response.data.message && (response.data.message == 'messages.user.expired' || response.data.message == 'messages.auth.expired')) { var Auth = $injector.get('Auth'); Auth.logout().then(function(response) { $window.location.reload(); }); } return response; }, }; }); app.run(function($rootScope, Access, $location) { $rootScope.$on('$routeChangeStart', function(event, current, previous) { if (current && current.$$route && current.$$route.originalPath && current.$$route.originalPath == '/page/login') { Access.isLoggedIn().then(function(response) { // User is logged in $location.url('/'); }, function(response) { // User needs to login }); } }); $rootScope.$on('$routeChangeError', function(event, current, previous, error) { switch (error) { case Access.UNAUTHORIZED: $location.url('/page/login'); break; case Access.FORBIDDEN: $location.url('/page/login'); break; default: console.log('Forbidden'); break; } }); }); app.controller('IndexCtrl', function($scope, $http, $location) { $scope.statistics = []; //fetch Statistics $scope.getStatistics = function(){ var req = { method: 'GET', url: '/statistic/get', } $http(req).then(function(response) { if(response.data.status){ $scope.statistics = response.data.data; } }); } $scope.getStatistics(); $scope.toLink = function(url){ $location.url(url); } });app.controller('GitCtrl', function($scope) { });app.controller('MainCtrl', function(Auth, $scope, User, $window, $route, $http) { User.then(function(user) { $scope.user = user; }); $scope.$route = $route; $scope.doLogout = function() { Auth.logout().then(function(response) { $scope.user.refresh().then(function(user) { $window.location.reload(); }); }); } var req = { method: 'GET', url: '/menu/getAll', } $http(req).then(function(response) { $scope.menu = response.data.data; }); });app.controller('ProductsCtrl', function($scope) { $scope.products = { name: 'Products', controller: 'product', fields: [ 'code', 'name', 'claim_time', 'auto_release', ], fieldDetails: { 'auto_release': { 'type': 'boolean', 'booleanOff': 0, 'booleanOn': 1, }, }, /*buttons: { 'M':{ 'id': 'm', 'name': 'Modules', 'confirmName': 'Accept', 'confirmClass': 'success', 'icon': 'fa-server', 'action': 'btnGotoModules', 'header': false, 'inline': true, 'parentScope': true, }, },*/ } $scope.btnGotoModules = function(btn, row, blnSubmit){ console.log(row); } });app.controller('ProfileCtrl', function($scope, user, $http, $timeout) { $scope.password = {}; $scope.saveProfile = function() { var req = { method: 'POST', url: '/user/update/' + $scope.user.id, data: { name: $scope.user.name, username: $scope.user.username, email: $scope.user.email, }, } $http(req).then(function(response) { if ($scope.user.status = response.data.status) { $scope.user.message = 'success'; } else { $scope.user.message = 'failure'; } $timeout(function() { delete $scope.user.status; delete $scope.user.message; }, 2000); }); } $scope.isSamePassword = function() { return $scope.password && ($scope.password.new == $scope.password.retype); } $scope.savePassword = function() { if (!$scope.isSamePassword()) { return; } var req = { method: 'POST', url: '/user/update/' + $scope.user.id, data: { password: $scope.password.new, }, } $http(req).then(function(response) { $scope.password = {}; if ($scope.password.status = response.data.status) { $scope.password.message = 'success'; } else { $scope.password.message = 'failure'; } $timeout(function() { delete $scope.password.status; delete $scope.password.message; }, 2000); }); } });app.controller('AdminCtrl', function($scope, $location, $http) { $scope.organizations = { name: 'Organizations', controller: 'organization', listUndefinedFields: true, pageLimit: 10, fieldDetails: { id: { hideEdit: true, }, } } $scope.users = { name: 'Users', controller: 'user', listUndefinedFields: true, pageLimit: 10, fieldDetails: { password: { hideList: true, type: 'password' }, id: { hideEdit: true, }, }, autoChangeTo: ['organization_id', 'user_role_id'], } $scope.switch = function(){ var req = { method: 'GET', url: '/user/userSwitch/' + $scope.usernameForSwitch, } $http(req).then(function(response) { if(response.data.status){ $location.url('/'); } }); } });app.controller('LicensesCtrl', function($scope) { $scope.licenses = { name: 'Licenses', controller: 'license', fields: [ 'organization_id', 'number', 'name', 'start_date', 'end_date', 'claim_time', 'auto_release', 'auto_validate', 'auto_validate_terminal', 'products', ], subQueries: { 'organization': { 'lfield': 'organization_id', 'ffield': 'id', 'controller': 'organization', }, 'product': { 'lfield': 'id', 'ffield': 'product_id', 'controller': 'product', }, 'license_product': { 'lfield': 'id', 'ffield': 'license_id', 'controller': 'license_product', }, }, fieldDetails: { 'number': { 'required': true, }, 'name': { 'required': true, }, 'organization_id': { 'label': 'Client', 'changeTo': ['organization.name'], 'type': 'combobox', 'required': true, }, 'auto_release': { 'type': 'boolean', 'booleanOff': 0, 'booleanOn': 1, }, 'auto_validate': { 'type': 'boolean', 'booleanOff': 0, 'booleanOn': 1, }, 'auto_validate_terminal': { 'type': 'boolean', 'booleanOff': 0, 'booleanOn': 1, }, 'products': { 'type': 'multiselect', 'link': 'license_product', 'changeTo': 'product.name', //'hideEdit': true, } } } });app.controller('LicenseComputersCtrl', function($scope) { $scope.license_computers = { name: 'License Computers', controller: 'license_computer', pageLimit: 20, predefinedFilters: { 'is_terminal': 'false', }, fields: [ 'organization_id', 'license_id', 'code', 'name', 'start_date', 'end_date', 'is_terminal', 'auto_release', 'claim_time', 'validated', ], subQueries: { 'license': { 'lfield': 'license_id', 'ffield': 'id', 'controller': 'license', }, 'organization': { 'lfield': 'organization_id', 'ffield': 'id', 'controller': 'organization', }, }, fieldDetails: { 'license_id': { 'label': 'License', 'changeTo': ['license.number', ' - ', 'license.name'], 'type': 'combobox', 'required': true, }, 'organization_id': { 'label': 'Client', 'changeTo': ['organization.name'], 'type': 'combobox', 'required': true, }, 'is_terminal': { 'type': 'boolean', 'booleanOff': '0', 'booleanOn': '1', }, 'code': { 'hideList': true, } } } });app.controller('LicenseProductsCtrl', function($scope) { $scope.license_products = { name: 'License Products', controller: 'license_product', pageLimit: 20, fields: [ 'organization_id', 'license_id', 'product_id', 'concurrent_users', 'start_date', 'end_date', ], subQueries: { 'organization': { 'lfield': 'organization_id', 'ffield': 'id', 'controller': 'organization', }, 'license': { 'lfield': 'license_id', 'ffield': 'id', 'controller': 'license', }, 'product': { 'lfield': 'product_id', 'ffield': 'id', 'controller': 'product', }, }, fieldDetails: { 'organization_id': { 'label': 'Client', 'changeTo': ['organization.name'], 'type': 'combobox', 'required': true, }, 'license_id': { 'label': 'License', 'changeTo': ['license.number', ' - ', 'license.name'], 'type': 'combobox', 'required': true, }, 'product_id': { 'label': 'Product', 'changeTo': ['product.code', ' - ', 'product.name'], 'type': 'combobox', 'required': true, }, } } });app.controller('LicensePoolCtrl', function($scope) { $scope.license_pool = { name: 'License Pool', controller: 'license_pool', pageLimit: 20, fields: [ 'organization_id', 'license_id', 'license_product_id', 'license_computer_id', 'username', 'claim_time', 'auto_release', 'valid_from', 'valid_till', ], subQueries: { 'organization': { 'lfield': 'organization_id', 'ffield': 'id', 'controller': 'organization', }, 'license': { 'lfield': 'license_id', 'ffield': 'id', 'controller': 'license', }, 'license_product': { 'lfield': 'license_product_id', 'ffield': 'id', 'controller': 'license_product', 'function': 'getWith', }, 'license_computer': { 'lfield': 'license_computer_id', 'ffield': 'id', 'controller': 'license_computer', }, }, fieldDetails: { 'organization_id': { 'label': 'Client', 'changeTo': ['organization.name'], 'type': 'combobox', 'required': true, }, 'license_id': { 'label': 'License', 'changeTo': ['license.number', ' - ', 'license.name'], 'type': 'combobox', 'required': true, }, 'license_product_id': { 'label': 'License Product', 'changeTo': ['license_product.product'], 'type': 'combobox', 'required': true, }, 'license_computer_id': { 'label': 'License Computer', 'changeTo': ['license_computer.name'], 'type': 'combobox', 'required': true, }, 'auto_release': { 'type': 'boolean', 'booleanOff': 0, 'booleanOn': 1, }, 'valid_from': { 'hideEdit': true, }, 'valid_till': { 'hideEdit': true, } } } });app.controller('LicenseHistoryCtrl', function($scope) { $scope.license_history = { name: 'License History', controller: 'license_history', readOnly: true, pageLimit: 20, listUndefinedFields: true, getCSV: true, } });app.controller('LoginCtrl', function(Auth, user, $scope, $window) { $scope.doLogin = function() { Auth.login($scope.username, $scope.password).then(function(response) { user.refresh().then(function(user) { $window.location.reload(); }); }); } }); app.factory('User', function(Auth, $q) { var user = {}; var clearUser = function() { user = {}; }; var getUser = function() { return Auth.getUser().then(function(response) { clearUser(); angular.extend(user, response.data.status ? response.data.data : {}, { refresh: getUser, }); return user; }); } return getUser(); }); app.factory('Access', function(User, $q) { var Access = { OK: 200, UNAUTHORIZED: 401, FORBIDDEN: 403, isAnonymous: function() { return User.then(function(user) { return !user.id ? true : $q.reject(Access.FORBIDDEN); }); }, isLoggedIn: function() { return User.then(function(user) { return user.id ? true : $q.reject(Access.UNAUTHORIZED); }); } }; return Access; }); app.service('Auth', function($http) { this.getUser = function() { return $http.get('user/check'); } this.login = function(username, password) { var req = { method: 'POST', url: '/user/login', data: { username: username, password: password, } } return $http(req); } this.logout = function() { return $http.get('/user/logout'); } });