diff options
| author | ARC <arjunrc@gmail.com> | 2015-04-26 09:53:50 -0400 |
|---|---|---|
| committer | ARC <arjunrc@gmail.com> | 2015-04-26 09:53:50 -0400 |
| commit | b08473490be9d1571ebbd893ce5564043b3950d3 (patch) | |
| tree | 0910b93dce5bfb1d30afbae3491a03e944ba7cc8 /www | |
| parent | fca3641d17b111fe0fdea0bef05243e173e125c4 (diff) | |
Integrated login check for views
Diffstat (limited to 'www')
| -rw-r--r-- | www/js/DataModel.js | 41 | ||||
| -rw-r--r-- | www/js/LoginCtrl.js | 3 | ||||
| -rw-r--r-- | www/js/app.js | 55 | ||||
| -rw-r--r-- | www/templates/login.html | 53 |
4 files changed, 96 insertions, 56 deletions
diff --git a/www/js/DataModel.js b/www/js/DataModel.js index 3935de06..f42a8bcf 100644 --- a/www/js/DataModel.js +++ b/www/js/DataModel.js @@ -5,7 +5,6 @@ angular.module('zmApp.controllers').service('ZMDataModel', ['$http', '$q', '$ionicLoading', function ($http, $q, $ionicLoading) { // var deferred=''; var monitorsLoaded = 0; - var simulationMode = 0; // make 1 for simulation var simSize = 30; // how many monitors to simulate var montageSize = 3; var monitors = []; @@ -14,7 +13,8 @@ angular.module('zmApp.controllers').service('ZMDataModel', ['$http', '$q', '$ion 'username': '', 'password': '', 'url': '', // This is ZM portal API (Don't add /zm) - 'apiurl': '' // This is the API path + 'apiurl': '', // This is the API path + 'simulationMode':false // if true, data will be simulated }; // This is really a test mode. This is how I am validating @@ -89,12 +89,41 @@ angular.module('zmApp.controllers').service('ZMDataModel', ['$http', '$q', '$ion } + if (window.localStorage.getItem("simulationMode") != undefined) { + // Remember to convert back to Boolean! + var tvar = window.localStorage.getItem("simulationMode"); + loginData.simulationMode = ( tvar ==="true"); + console.log ("***** STORED SIMULATION IS "+tvar); + console.log ("******* BOOLEAN VALUE IS " + loginData.simulationMode); + + } + monitorsLoaded = 0; console.log("Getting out of ZMDataModel init"); }, + isLoggedIn: function() + { + if (loginData.username != "" && loginData.password !="" && loginData.url !="" && loginData.apiurl !="") + { + return 1; + } + else + return 0; + { + } + }, + isSimulated: function() + { + return loginData.simulationMode; + }, + + setSimulated: function(mode) + { + loginData.simulationMode = mode; + }, getLogin: function () { return loginData; @@ -105,6 +134,8 @@ angular.module('zmApp.controllers').service('ZMDataModel', ['$http', '$q', '$ion window.localStorage.setItem("password", loginData.password); window.localStorage.setItem("url", loginData.url); window.localStorage.setItem("apiurl", loginData.apiurl); + window.localStorage.setItem("simulationMode", loginData.simulationMode); + console.log ("********** SIMULATION IS " + loginData.simulationMode); }, @@ -120,14 +151,14 @@ angular.module('zmApp.controllers').service('ZMDataModel', ['$http', '$q', '$ion getMonitors: function (forceReload) { console.log("** Inside ZMData getMonitors with forceReload=" + forceReload); var d = $q.defer(); - if (((monitorsLoaded == 0) || (forceReload == 1)) && (simulationMode != 1)) // monitors are empty or force reload + if (((monitorsLoaded == 0) || (forceReload == 1)) && (loginData.simulationMode != true)) // monitors are empty or force reload { console.log("ZMDataModel: Invoking HTTP get to load monitors"); var apiurl = loginData.apiurl; var myurl = apiurl + "/monitors.json"; $http.get(myurl) .success(function (data) { - console.log("HTTP success got " + JSON.stringify(data.monitors)); + //console.log("HTTP success got " + JSON.stringify(data.monitors)); monitors = data.monitors; console.log("promise resolved inside HTTP success"); monitorsLoaded = 1; @@ -145,7 +176,7 @@ angular.module('zmApp.controllers').service('ZMDataModel', ['$http', '$q', '$ion } else // monitors are loaded { - if (simulationMode == 1) { + if (loginData.simulationMode == true) { monitors = simulation.fillSimulatedMonitors(simSize); //fillSimulatedMonitors } diff --git a/www/js/LoginCtrl.js b/www/js/LoginCtrl.js index 2e428c18..e734fef2 100644 --- a/www/js/LoginCtrl.js +++ b/www/js/LoginCtrl.js @@ -6,6 +6,7 @@ $scope.openMenu = function () { $scope.loginData = ZMDataModel.getLogin(); + // Perform the login action when the user submits the login form $scope.login = function () { console.log('Saving login'); @@ -14,4 +15,4 @@ $scope.openMenu = function () { }; -})
\ No newline at end of file +}) diff --git a/www/js/app.js b/www/js/app.js index d6d1012b..a35abcd1 100644 --- a/www/js/app.js +++ b/www/js/app.js @@ -10,7 +10,7 @@ angular.module('zmApp', [ ZMDataModel.init(); var loginData = ZMDataModel.getLogin(); - if (loginData.username && loginData.password && loginData.url && loginData.apiurl) { + if (ZMDataModel.isLoggedIn()) { console.log("VALID CREDENTIALS. Grabbing Monitors"); ZMDataModel.getMonitors(0); @@ -28,19 +28,30 @@ angular.module('zmApp', [ console.log("********Computed Dev Width & Height as" + $rootScope.devWidth + "*" + $rootScope.devHeight); } - // This is a skeleton for now. Eventually I am going to prohibit - // certain views to load unless you've logged in + $rootScope.$on('$stateChangeStart', function (event, toState, toParams) { // console.log ("***** STATE CHANGE CHECK ****"); var requireLogin = toState.data.requireLogin; - // console.log ("STATE REQUIRE LOGIN: "+requireLogin); + + if (ZMDataModel.isLoggedIn() || ZMDataModel.isSimulated()) + { + console.log ("State transition is authorized"); + return; + } + if (requireLogin) { + + // alert ("Not logged in"); + console.log ("**** STATE from "+ "**** STATE TO " + toState.name); + + $ionicPopup.alert ({title: "Credentials Required", + template:"Please provide your ZoneMinder credentials or switch to simulation mode"}); + // for whatever reason, .go was resulting in digest loops. + // if you don't prevent, states will stack event.preventDefault(); - //$state.go('app'); - //$ionicPopup.alert ({title: "Error", template:"You are not logged in."}); - //alert ("Not logged in"); - // get me a login modal! + $state.transitionTo('login'); } + }); @@ -83,19 +94,6 @@ angular.module('zmApp', [ $stateProvider - - /*.state('app', { - data: { - requireLogin: false - }, - url: "/app", - abstract: true, - templateUrl: "templates/intro.html", - controller: 'zmApp.AppCtrl', - - - })*/ - .state('login', { data: { requireLogin: false @@ -109,7 +107,7 @@ angular.module('zmApp', [ .state('monitors', { data: { - requireLogin: false + requireLogin: true }, resolve: { message: function (ZMDataModel) { @@ -126,7 +124,7 @@ angular.module('zmApp', [ .state('events', { data: { - requireLogin: false + requireLogin: true }, resolve: { message: function (ZMDataModel) { @@ -143,7 +141,7 @@ angular.module('zmApp', [ //n .state('events-graphs', { data: { - requireLogin: false + requireLogin: true }, url: "/events-graphs", templateUrl: "templates/events-graphs.html", @@ -153,7 +151,7 @@ angular.module('zmApp', [ .state('montage', { data: { - requireLogin: false + requireLogin: true }, resolve: { message: function (ZMDataModel) { @@ -168,8 +166,13 @@ angular.module('zmApp', [ }); + // if none of the above states are matched, use this as the fallback var defaultState = "/monitors"; - //var defaultState = "/app/montage"; + // as it turns out I can't really inject a factory in config the normal way + // FIXME: In future, read up http://stackoverflow.com/questions/15937267/inject-service-in-app-config + //var defaultState = (ZMDataModel.isLoggedIn())? "/monitors":"/login"; $urlRouterProvider.otherwise(defaultState); + + }); diff --git a/www/templates/login.html b/www/templates/login.html index 9bc7df88..05d28848 100644 --- a/www/templates/login.html +++ b/www/templates/login.html @@ -1,25 +1,30 @@ -<ion-view view-title="Login" > - +<ion-view view-title="Settings"> + <ion-nav-buttons side="left"> - <button class="button button-icon button-clear ion-navicon" ng-click="openMenu()"></button> - </ion-nav-buttons> - - - <ion-content class="padding"> - <div class="list list-inset"> - <label class="item item-input"> - <input autocapitalize="none" type="text" placeholder="Username" ng-model="loginData.username"> - </label> - <label class="item item-input"> - <input type="password" placeholder="Password" ng-model="loginData.password"> - </label> - <label class="item item-input"> - <input autocapitalize="none" type="text" placeholder="Zoneminder base URL" ng-model="loginData.url"> - </label> - <label class="item item-input"> - <input autocapitalize="none" type="text" placeholder="API base URL" ng-model="loginData.apiurl"> - </label> - </div> - <button class="button button-block button-balanced icon ion-locked" ng-click="login()"> Save</button> - </ion-content> -</ion-view>
\ No newline at end of file + <button class="button button-icon button-clear ion-navicon" ng-click="openMenu()"></button> + </ion-nav-buttons> + + + <ion-content class="padding"> + <div class="list list-inset"> + <label class="item item-input"> + <input autocapitalize="none" type="text" placeholder="Username" ng-model="loginData.username"> + </label> + <label class="item item-input"> + <input type="password" placeholder="Password" ng-model="loginData.password"> + </label> + <label class="item item-input"> + <input autocapitalize="none" type="text" placeholder="Zoneminder base URL" ng-model="loginData.url"> + </label> + <label class="item item-input"> + <input autocapitalize="none" type="text" placeholder="API base URL" ng-model="loginData.apiurl"> + </label> + </div> + + <ion-toggle ng-model="loginData.simulationMode" ng-checked={{loginData.simulationMode}} toggle-class="toggle-energized">Simulation Mode</ion-toggle> + + <button class="button button-block button-balanced icon ion-locked" ng-click="login()"> Save</button> + + + </ion-content> +</ion-view> |
