1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
|
/* jshint -W041 */
/* jshint -W083 */
/*This is for the loop closure I am using in line 143 */
/* jslint browser: true*/
/* global vis,cordova,StatusBar,angular,console,moment */
angular.module('zmApp.controllers').controller('zmApp.PortalLoginCtrl', ['$ionicPlatform', '$scope', 'zm', 'NVRDataModel', '$ionicSideMenuDelegate', '$rootScope', '$http', '$q', '$state', '$ionicLoading', '$ionicPopover', '$ionicScrollDelegate', '$ionicModal', '$timeout', 'zmAutoLogin', '$ionicHistory', 'EventServer', '$translate', '$ionicPopup', function ($ionicPlatform, $scope, zm, NVRDataModel, $ionicSideMenuDelegate, $rootScope, $http, $q, $state, $ionicLoading, $ionicPopover, $ionicScrollDelegate, $ionicModal, $timeout, zmAutoLogin, $ionicHistory, EventServer, $translate, $ionicPopup) {
var broadcastHandles = [];
var processPush = false;
$scope.$on('$ionicView.beforeLeave', function () {
processPush = false;
});
$scope.$on('$ionicView.beforeLeave', function () {
//NVRDataModel.debug("Portal: Deregistering broadcast handles");
for (var i = 0; i < broadcastHandles.length; i++) {
//broadcastHandles[i]();
}
broadcastHandles = [];
});
$scope.$on('$ionicView.enter',
function () {
$scope.$on ( "process-push", function () {
processPush = true;
NVRDataModel.debug (">> PortalLogin: push handler, marking to resolve later");
});
NVRDataModel.setJustResumed(false);
NVRDataModel.debug("Inside Portal login Enter handler");
loginData = NVRDataModel.getLogin();
$ionicHistory.nextViewOptions({
disableAnimate:true,
disableBack: true
});
$scope.pindata = {};
if ($ionicSideMenuDelegate.isOpen()) {
$ionicSideMenuDelegate.toggleLeft();
NVRDataModel.debug("Sliding menu close");
}
$scope.pinPrompt = false; // if true, then PIN is displayed else skip
if (NVRDataModel.hasLoginInfo()) {
NVRDataModel.log("User credentials are provided");
// You can login either via touch ID or typing in your code
var ld = NVRDataModel.getLogin();
if (ld.reloadInMontage == true) {
// we are in montage reload, so don't re-auth
NVRDataModel.log("skipping validation, as this is montage reload");
ld.reloadInMontage = false;
NVRDataModel.setLogin(ld);
unlock(true);
} else if ($ionicPlatform.is('android') && loginData.usePin) {
FingerprintAuth.isAvailable(function (result) {
NVRDataModel.debug("FingerprintAuth available: " + JSON.stringify(result));
if (result.isAvailable == true && result.hasEnrolledFingerprints == true) {
var encryptConfig = {
clientId: "zmNinja",
username: "doesntmatter",
password: "doesntmatter",
maxAttempts: 5,
locale: "en_US",
dialogTitle: $translate.instant('kPleaseAuthenticate'),
dialogMessage: $translate.instant('kPleaseAuthenticate'),
dialogHint: "",
}; // See config object for required parameters
FingerprintAuth.encrypt(encryptConfig, function (succ) {
NVRDataModel.log("Touch success");
unlock(true);
}, function (err) {
NVRDataModel.log("Touch Failed " + JSON.stringify(msg));
});
} // if available
},
function (err) {
NVRDataModel.log("Fingerprint auth not available or not compatible with Android specs: " + JSON.stringify(err));
}
); //isAvailable
} else if ($ionicPlatform.is('ios') && loginData.usePin) {
window.plugins.touchid.isAvailable(
function () {
window.plugins.touchid.verifyFingerprint(
$translate.instant('kPleaseAuthenticate'), // this will be shown in the native scanner popup
function (msg) {
NVRDataModel.log("Touch success");
unlock(true);
}, // success handler: fingerprint accepted
function (msg) {
NVRDataModel.log("Touch Failed " + JSON.stringify(msg));
} // error handler with errorcode and localised reason
);
},
function (err) {});
/* $cordovaTouchID.checkSupport()
.then(function()
{
// success, TouchID supported
$cordovaTouchID.authenticate("")
.then(function()
{
NVRDataModel.log("Touch Success");
// Don't assign pin as it may be alphanum
unlock(true);
},
function()
{
NVRDataModel.log("Touch Failed");
});
}, function(error)
{
NVRDataModel.log("TouchID not supported");
});*/
} else // touch was not used
{
NVRDataModel.log("not checking for touchID");
}
if (loginData.usePin) {
// this shows the pin prompt on screen
$scope.pinPrompt = true;
// dont call unlock, let the user type in code
} else // no PIN Code so go directly to auth
{
unlock(true);
}
} else // login creds are not present
{
NVRDataModel.debug("PortalLogin: Not logged in, so going to login");
if (NVRDataModel.isFirstUse()) {
NVRDataModel.debug("First use, showing warm and fuzzy...");
$ionicHistory.nextViewOptions({
disableAnimate: true,
disableBack: true
});
$state.go('app.first-use');
return;
} else {
if (!$rootScope.userCancelledAuth) {
$ionicHistory.nextViewOptions({
disableAnimate: true,
disableBack: true
});
$state.go("app.login", {
"wizard": false
});
return;
} else {
// do this only once - rest for next time
$rootScope.userCancelledAuth = false;
}
}
}
});
//-------------------------------------------------------------------------------
// remove status is pin is empty
//-------------------------------------------------------------------------------
$scope.pinChange = function () {
if ($scope.pindata.pin == null) {
$scope.pindata.status = "";
}
};
//-------------------------------------------------------------------------------
// unlock app if PIN is correct
//-------------------------------------------------------------------------------
$scope.unlock = function () {
// call with false meaning check for pin
unlock(false);
};
//------------------------------------------------------------------------
// Aaron Lager hack - can't figure out why he gets a 401 after
// successful login and then it works after resaving
//------------------------------------------------------------------------
function tryLoggingSecondTimeHack() {
var d = $q.defer();
zmAutoLogin.doLogin("<button class='button button-clear' style='line-height: normal; min-height: 0; min-width: 0;color:#fff;' ng-click='$root.cancelAuth()'><i class='ion-close-circled'></i> " + $translate.instant('kAuthenticating') + "...</button>")
.then(function (data) // success
{
NVRDataModel.debug("2nd auth login worked");
NVRDataModel.getAPIversion()
.then(function (data) {
NVRDataModel.getKeyConfigParams(1);
NVRDataModel.log("2nd auth:Got API version: " + data);
$rootScope.apiVersion = data;
var ld = NVRDataModel.getLogin();
if (NVRDataModel.versionCompare(data, zm.minAppVersion) == -1 && data != "0.0.0") {
$state.go('app.lowversion', {
"ver": data
});
return;
}
if (NVRDataModel.versionCompare(data, zm.recommendedAppVersion) == -1 && data != "0.0.0") {
NVRDataModel.hrsSinceChecked("zmVersion")
.then(function (val) {
NVRDataModel.debug("ZM Version nag: Checking " + val + " with " + zm.zmVersionCheckNag);
if (val >= zm.zmVersionCheckNag && 0) {
NVRDataModel.updateHrsSinceChecked("zmVersion");
$state.go('app.importantmessage', {
"ver": data
});
return;
}
});
}
/*if (data == "0.0.0")
{
NVRDataModel.log("2nd Auth:API getVersion succeeded but returned 0.0.0 " + JSON.stringify(data));
NVRDataModel.displayBanner('error', ['ZoneMinder authentication failed']);
$state.go("login",
{
"wizard": false
});
return;
}*/
// coming here means continue
//EventServer.init();
var statetoGo = $rootScope.lastState ? $rootScope.lastState : 'app.montage';
//NVRDataModel.debug ("logging state transition");
NVRDataModel.debug("2nd Auth: Transitioning state to: " +
statetoGo + " with param " + JSON.stringify($rootScope.lastStateParam));
$state.go(statetoGo, $rootScope.lastStateParam);
return;
},
function (error) {
NVRDataModel.debug("2nd auth API failed, going to login");
d.reject("failed 2nd auth");
return (d.promise);
});
},
function (error) {
NVRDataModel.debug("2nd auth hack failed, going to login");
d.reject("failed 2nd auth");
return (d.promise);
});
return (d.promise);
}
//broadcastHandles.push(pp);
function unlock(idVerified) {
/*
idVerified == true means no pin check needed
== false means check PIN
*/
NVRDataModel.debug("unlock called with check PIN=" + idVerified);
if (idVerified || ($scope.pindata.pin == loginData.pinCode)) {
NVRDataModel.debug("PIN code entered is correct, or there is no PIN set");
$rootScope.rand = Math.floor((Math.random() * 100000) + 1);
zmAutoLogin.stop(); //safety
zmAutoLogin.start();
// PIN is fine, or not set so lets login
zmAutoLogin.doLogin("<button class='button button-clear' style='line-height: normal; min-height: 0; min-width: 0;color:#fff;' ng-click='$root.cancelAuth()'><i class='ion-close-circled'></i> " + $translate.instant('kAuthenticating') + "...</button>")
.then(function (data) // success
{
NVRDataModel.debug("PortalLogin: auth success");
// $state.go("login" ,{"wizard": false});
//login was ok, so get API details
NVRDataModel.getAPIversion()
.then(function (data) {
NVRDataModel.log("Got API version: " + data);
$rootScope.apiVersion = data;
var ld = NVRDataModel.getLogin();
if (NVRDataModel.versionCompare(data, zm.minAppVersion) == -1 && data != "0.0.0") {
$state.go('app.lowversion', {
"ver": data
});
return;
}
if (NVRDataModel.versionCompare(data, zm.recommendedAppVersion) == -1 && data != "0.0.0") {
NVRDataModel.hrsSinceChecked("zmVersion")
.then(function (val) {
NVRDataModel.debug("ZM Version nag: Checking " + val + " with " + zm.zmVersionCheckNag);
if (val >= zm.zmVersionCheckNag && 0) {
//https://api.github.com/repos/zoneminder/zoneminder/releases/latest
//tag_name
NVRDataModel.updateHrsSinceChecked("zmVersion");
$state.go('app.importantmessage', {
"ver": data
});
return;
}
});
}
/*if (data == "0.0.0")
{
NVRDataModel.log("API getVersion succeeded but returned 0.0.0 " + JSON.stringify(data));
NVRDataModel.displayBanner('error', ['ZoneMinder authentication failed']);
$state.go("login",
{
"wizard": false
});
return;
}*/
// coming here means continue
// console.log (">>>>>>>>>>>>>>>>>>>>>>>>>NEVER");
NVRDataModel.getKeyConfigParams(1);
NVRDataModel.getTimeZone();
EventServer.init();
NVRDataModel.zmPrivacyProcessed()
.then(function (val) {
// console.log(">>>>>>>>>>>>>>>>>>> PRIVACY PROCESSED:" + val);
if (!val) {
var alertPopup = $ionicPopup.alert({
title: $translate.instant('kNote'),
template: $translate.instant('kDataPrivacyZM'),
okText: $translate.instant('kButtonOk'),
cancelText: $translate.instant('kButtonCancel'),
});
}
});
// if push broadcast happens BEFORE this, then no
// state change will occur here which is good
// if push happens AFTER this, then while going to
// lastState, it will interrupt and go to onTap
// (I HOPE...)
//console.log ("NOTIFICATION TAPPED INSIDE CHECK IS "+$rootScope.tappedNotification);
var statetoGo = $rootScope.lastState ? $rootScope.lastState : 'app.montage';
// NVRDataModel.debug("logging state transition");
if (!processPush) {
NVRDataModel.debug("Transitioning state to: " +
statetoGo + " with param " + JSON.stringify($rootScope.lastStateParam));
$state.go(statetoGo, $rootScope.lastStateParam);
return;
}
else {
NVRDataModel.debug ("Deferred handling of push:");
processPush = false;
var s = NVRDataModel.evaluateTappedNotification();
NVRDataModel.debug("tapped Notification evaluation:"+ JSON.stringify(s));
$ionicHistory.nextViewOptions({
disableAnimate:true,
disableBack: true
});
$state.go(s[0],s[1],s[2]);
return;
}
},
function (error) { // API Error
NVRDataModel.log("API Error handler: going to login getAPI returned error: " + JSON.stringify(error));
//NVRDataModel.displayBanner('error', ['ZoneMinder authentication failed']);
NVRDataModel.debug("Doing the Aaron Hack after 1 sec....");
$timeout(function () {
tryLoggingSecondTimeHack()
.then(function success(s) {
NVRDataModel.log("2nd time login hack worked!, nothing to do");
NVRDataModel.getTimeZone();
},
function error(e) {
if ($rootScope.apiValid == true) {
$state.go("app.login", {
"wizard": false
});
return;
} else {
$state.go("app.invalidapi");
return;
}
});
return;
}, 1000);
});
},
// coming here means auth error
// so go back to login
function (error) {
NVRDataModel.debug("PortalLogin: error authenticating " +
JSON.stringify(error));
if (!$rootScope.userCancelledAuth) {
NVRDataModel.displayBanner('error', ['ZoneMinder authentication failed', 'Please check API settings']);
$ionicHistory.nextViewOptions({
disableAnimate: true,
disableBack: true
});
$state.go("app.login", {
"wizard": false
});
return;
} else {
// if user cancelled auth I guess we go to login
$rootScope.userCancelledAuth = false;
$state.go("app.login", {
"wizard": false
});
return;
}
});
} else {
$scope.pindata.status = "Invalid PIN";
// wobble the input box on error
var element = angular.element(document.getElementById("pin-box"));
element.addClass("animated shake")
.one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend',
function () {
element.removeClass("animated shake");
});
}
}
//-------------------------------------------------------------------------------
// Controller Main
//-------------------------------------------------------------------------------
// console.log("************* ENTERING PORTAL MAIN ");
NVRDataModel.log("Entering Portal Main");
var loginData;
$ionicSideMenuDelegate.canDragContent(true);
}]);
|