cronapp-framework-js 1.0.5 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/js/controllers.js CHANGED
@@ -5,28 +5,68 @@ if (!window.hostApp) {
5
5
  angular.module('custom.controllers', []);
6
6
 
7
7
  // refresh token
8
- var refreshToken = function ($http, success, error) {
9
- $http({
10
- method: 'GET',
11
- url: window.hostApp + 'auth/refresh'
12
- }).then(function (data, status, headers, config) {
13
- data = getRequestData(data);
14
- //Keeping the user information, the auth/refresh only has name and username info
15
- if (localStorage.getItem("_u")) {
16
- let currentSession = JSON.parse(localStorage.getItem("_u"));
17
- if (currentSession.user.username === data.user.username)
18
- data.user = currentSession.user;
8
+ var refreshToken = function ($http, success, error, FrontBoostAuthenticationService) {
9
+ FrontBoostAuthenticationService.isAvailable().then(available => {
10
+ if (!available) {
11
+ $http({
12
+ method: 'GET',
13
+ url: window.hostApp + 'auth/refresh'
14
+ }).then(function (data, status, headers, config) {
15
+ data = getRequestData(data);
16
+ //Keeping the user information, the auth/refresh only has name and username info
17
+ if (localStorage.getItem("_u")) {
18
+ let currentSession = JSON.parse(localStorage.getItem("_u"));
19
+ if (currentSession.user.username === data.user.username)
20
+ data.user = currentSession.user;
21
+ }
22
+ // Store data response on local storage
23
+ localStorage.setItem("_u", JSON.stringify(data));
24
+ // Recussive
25
+ setTimeout(function () {
26
+ refreshToken($http, success, error);
27
+ // refresh time
28
+ }, (1800 * 1000));
29
+ success();
30
+ }).catch(function () {
31
+ error();
32
+ });
33
+ } else {
34
+ // For FrontBoost authentication
35
+ FrontBoostAuthenticationService.getSessionInfo().then(info => {
36
+ if (info.isLoggedIn) {
37
+ // Check if token will expire in the next 5 minutes (300000ms)
38
+ const expiresAt = (info.expires_at || 0) * 1000;
39
+ const expiresInMs = expiresAt - Date.now();
40
+ const needsRefresh = expiresInMs < 300000;
41
+
42
+ if (needsRefresh) {
43
+ // Token needs to be refreshed
44
+ FrontBoostAuthenticationService.refreshToken()
45
+ .then(() => {
46
+ // Schedule the next refresh
47
+ setTimeout(() => {
48
+ refreshToken($http, success, error, FrontBoostAuthenticationService);
49
+ }, (1800 * 1000)); // Same refresh interval as cronapp auth
50
+ success();
51
+ })
52
+ .catch(() => {
53
+ error();
54
+ });
55
+ } else {
56
+ // Token is still valid, schedule next check
57
+ setTimeout(() => {
58
+ refreshToken($http, success, error, FrontBoostAuthenticationService);
59
+ }, (1800 * 1000));
60
+ success();
61
+ }
62
+ } else {
63
+ // Not logged in, call error handler
64
+ error();
65
+ }
66
+ }).catch(() => {
67
+ error();
68
+ });
19
69
  }
20
- // Store data response on local storage
21
- localStorage.setItem("_u", JSON.stringify(data));
22
- // Recussive
23
- setTimeout(function () {
24
- refreshToken($http, success, error);
25
- // refresh time
26
- }, (1800 * 1000));
27
- success();
28
- }).catch(function () {
29
- error();
30
70
  });
31
71
  };
32
72
 
@@ -74,9 +114,11 @@ if (!window.hostApp) {
74
114
  }
75
115
  });
76
116
 
77
- app.controller('SignupController' , function($controller, $scope, $stateParams, $location, $http, $rootScope, $translate, Notification, UploadService, $timeout, $state, ReportService) {
117
+ app.controller('SignupController' , function($controller, $scope, $stateParams, $location, $http, $rootScope, $translate, Notification, UploadService, $timeout, $state, ReportService, FrontBoostCommandService, $log) {
78
118
 
79
119
  app.registerEventsCronapi($scope, $translate);
120
+ app.registerFrontBoostFunctions($scope, FrontBoostCommandService, $log);
121
+
80
122
 
81
123
  $rootScope.Notification = Notification; // Não remover, usado pelo cronapi.js para notificar erros de auto-cadastro.
82
124
 
@@ -87,26 +129,16 @@ if (!window.hostApp) {
87
129
 
88
130
  });
89
131
 
90
- app.controller('LoginController', function($controller, $scope, $http, $rootScope, $window, $state, $translate, Notification, ReportService, UploadService, $location, $stateParams, $timeout, $cookies, $templateCache, DashboardService, SyncService) {
132
+ app.controller('LoginController', function($controller, $scope, $http, $rootScope, $window, $state, $translate, Notification, ReportService, UploadService, $location, $stateParams, $timeout, $cookies, $templateCache, DashboardService, SyncService, FrontBoostAuthenticationService, SessionService, FrontBoostCommandService, $log) {
91
133
 
92
134
  $http.get(window.NotificationProviderOptions.templateUrl, {cache: true})
93
- .then((response) => $templateCache.put(window.NotificationProviderOptions.templateUrl, response.data));
94
-
95
- $scope.goHome = () => {
96
- let returnUrl = cronapi.screen.getParam('returnUrl');
97
- if (returnUrl) {
98
- window.location.hash = returnUrl;
99
- }
100
- else {
101
- $state.go('home');
102
- }
103
- $scope.cronapi.forceCloseAllModal();
104
- };
135
+ .then((response) => $templateCache.put(window.NotificationProviderOptions.templateUrl, response.data));
105
136
 
106
137
  $scope.$http = $http;
107
138
  $scope.params = $stateParams;
108
139
  $scope.$state = $state;
109
140
  app.registerEventsCronapi($scope, $translate, $location);
141
+ app.registerFrontBoostFunctions($scope, FrontBoostCommandService, $log);
110
142
 
111
143
  $rootScope.http = $http;
112
144
  $rootScope.Notification = Notification;
@@ -121,7 +153,13 @@ if (!window.hostApp) {
121
153
  };
122
154
 
123
155
  $scope.redirectToLogin = function() {
124
- $scope.cronapi.social.ssoLogin();
156
+ FrontBoostAuthenticationService.isAvailable().then(available => {
157
+ if (!available) {
158
+ $scope.cronapi.social.ssoLogin();
159
+ } else {
160
+ FrontBoostAuthenticationService.signIn();
161
+ }
162
+ });
125
163
  };
126
164
 
127
165
  if (localStorage.getItem('redir_mob')) {
@@ -135,48 +173,46 @@ if (!window.hostApp) {
135
173
  window.grecaptcha.reset();
136
174
  };
137
175
 
138
- function checkSession() {
139
- $http({
140
- method : 'GET',
141
- url : window.hostApp + 'me'
142
- }).then(handleSuccess);
143
- }
144
-
145
176
  $scope.login = function(username, password) {
146
- $scope.message.error = undefined;
147
- if($('form').find('*[class=g-recaptcha]').length){
148
- if(!$scope.captcha_token && $('form').find('*[class=g-recaptcha]').attr("data-sitekey")=== ""){
149
- Notification.error($translate.instant('Login.view.EmptySiteKeyCaptcha'));
150
- return;
151
- }
152
- $scope.captcha_token = window.grecaptcha.getResponse();
153
- if(!$scope.captcha_token && $('form').find('*[class=g-recaptcha]').attr("data-size") !== "invisible"){
154
- Notification.error($translate.instant('Login.view.InvalidCaptcha'));
155
- return;
156
- }
157
- else if($('form').find('*[class=g-recaptcha]').attr("data-size") === "invisible"){
158
- window.grecaptcha.execute();
177
+ FrontBoostAuthenticationService.isAvailable().then(available => {
178
+ if (!available) {
179
+ $scope.message.error = undefined;
180
+ if ($('form').find('*[class=g-recaptcha]').length) {
181
+ if (!$scope.captcha_token && $('form').find('*[class=g-recaptcha]').attr("data-sitekey") === "") {
182
+ Notification.error($translate.instant('Login.view.EmptySiteKeyCaptcha'));
183
+ return;
184
+ }
185
+ $scope.captcha_token = window.grecaptcha.getResponse();
186
+ if (!$scope.captcha_token && $('form').find('*[class=g-recaptcha]').attr("data-size") !== "invisible") {
187
+ Notification.error($translate.instant('Login.view.InvalidCaptcha'));
188
+ return;
189
+ } else if ($('form').find('*[class=g-recaptcha]').attr("data-size") === "invisible") {
190
+ window.grecaptcha.execute();
191
+ }
192
+ }
193
+ var user = {
194
+ username: username ? username : $scope.username.value,
195
+ password: password ? password : $scope.password.value,
196
+ recaptchaToken: $scope.captcha_token ? $scope.captcha_token : undefined
197
+ };
198
+
199
+ var headerValues = {
200
+ 'Content-Type': 'application/x-www-form-urlencoded'
201
+ };
202
+
203
+ $http({
204
+ method: 'POST',
205
+ url: window.hostApp + 'login',
206
+ data: $.param(user),
207
+ headers: headerValues
208
+ }).then(() => SessionService.checkSession($scope)).catch(handleError);
209
+ } else {
210
+ FrontBoostAuthenticationService.signIn();
159
211
  }
160
- }
161
- var user = {
162
- username : username?username:$scope.username.value,
163
- password : password?password:$scope.password.value,
164
- recaptchaToken : $scope.captcha_token ? $scope.captcha_token : undefined
165
- };
166
-
167
- var headerValues = {
168
- 'Content-Type' : 'application/x-www-form-urlencoded'
169
- };
170
-
171
- $http({
172
- method : 'POST',
173
- url : window.hostApp + 'login',
174
- data : $.param(user),
175
- headers : headerValues
176
- }).then(checkSession).catch(handleError);
177
- };
212
+ });
213
+ }
178
214
 
179
- checkSession();
215
+ SessionService.checkSession($scope);
180
216
 
181
217
  $scope.forgotPassword = function () {
182
218
  if (forgotPasswordEmail.value === '') {
@@ -203,31 +239,6 @@ if (!window.hostApp) {
203
239
  }).catch(data => Notification.error(data));
204
240
  };
205
241
 
206
- function handleSuccess(data, status, headers, config) {
207
- if (data.status == 200) {
208
- data = getRequestData(data);
209
- // Store data response on session storage
210
- // The local storage will be cleaned when the browser window is closed
211
- if (typeof (Storage) !== "undefined") {
212
- // save the user data on localStorage
213
- localStorage.setItem("_u", JSON.stringify(data));
214
- $rootScope.session = JSON.parse(localStorage._u);
215
- } else {
216
- // Sorry! No Web Storage support.
217
- // The home page may not work if it depends
218
- // on the logged user data
219
- }
220
-
221
- // Redirect to home page
222
- $scope.goHome();
223
-
224
- // Verify if the 'onLogin' event is defined and it is a function (it can be a string pointing to a non project blockly) and run it.
225
- if ($scope.blockly && $scope.blockly.events && $scope.blockly.events.onLogin && $scope.blockly.events.onLogin instanceof Function) {
226
- $scope.blockly.events.onLogin();
227
- }
228
- }
229
- }
230
-
231
242
  function handleError(data, status, headers, config) {
232
243
  status = status || data.status;
233
244
  data = getRequestData(data);
@@ -264,12 +275,13 @@ if (!window.hostApp) {
264
275
 
265
276
  });
266
277
 
267
- app.controller('HomeController', function($controller, $scope, $http, $rootScope, $state, $translate, Notification, ReportService, UploadService, $location, $stateParams, $timeout, DashboardService, SyncService) {
278
+ app.controller('HomeController', function($controller, $scope, $http, $rootScope, $state, $translate, Notification, ReportService, UploadService, $location, $stateParams, $timeout, DashboardService, SyncService, FrontBoostAuthenticationService, SessionService, FrontBoostCommandService, $log) {
268
279
 
269
280
  $scope.$http = $http;
270
281
  $scope.params = $stateParams;
271
282
  $scope.$state = $state;
272
283
  app.registerEventsCronapi($scope, $translate, $location);
284
+ app.registerFrontBoostFunctions($scope, FrontBoostCommandService, $log);
273
285
 
274
286
  $rootScope.http = $http;
275
287
  $rootScope.Notification = Notification;
@@ -284,49 +296,49 @@ if (!window.hostApp) {
284
296
  };
285
297
 
286
298
  var idleMonitor = function(timer){
287
- let userIsIdle = false;
288
- let userIdleTime = new Date().getTime();
289
-
290
- var notifyIdle = function() {
291
- if (!userIsIdle) {
292
- userIsIdle = true;
293
- console.log("User is Idle");
294
- try {
295
- $scope.logout();
296
- } catch (e) {
297
- console.log(e)
298
- }
299
- }
300
- }
301
-
302
- var resetIdleTimer = function() {
303
- userIdleTime = new Date().getTime();
304
- if (userIsIdle) {
305
- console.log("User is Back");
306
- }
307
- userIsIdle = false;
308
- }
309
-
310
- var monitorIdleWindow = function(win) {
311
- win.onload = resetIdleTimer;
312
- win.onmousemove = resetIdleTimer;
313
- win.onmousedown = resetIdleTimer;
314
- win.ontouchstart = resetIdleTimer;
315
- win.onclick = resetIdleTimer;
316
- win.onkeypress = resetIdleTimer;
317
- win.addEventListener('scroll', resetIdleTimer, true);
318
- resetIdleTimer();
319
- }
320
-
321
- if(timer && timer > 0){
322
- monitorIdleWindow(window);
323
- setInterval(() => {
324
- if (new Date().getTime() - userIdleTime > (60000 * timer)) {
325
- notifyIdle();
326
- }
327
- }, 1000);
328
- }
329
- }
299
+ let userIsIdle = false;
300
+ let userIdleTime = new Date().getTime();
301
+
302
+ var notifyIdle = function() {
303
+ if (!userIsIdle) {
304
+ userIsIdle = true;
305
+ console.log("User is Idle");
306
+ try {
307
+ $scope.logout();
308
+ } catch (e) {
309
+ console.log(e)
310
+ }
311
+ }
312
+ }
313
+
314
+ var resetIdleTimer = function() {
315
+ userIdleTime = new Date().getTime();
316
+ if (userIsIdle) {
317
+ console.log("User is Back");
318
+ }
319
+ userIsIdle = false;
320
+ }
321
+
322
+ var monitorIdleWindow = function(win) {
323
+ win.onload = resetIdleTimer;
324
+ win.onmousemove = resetIdleTimer;
325
+ win.onmousedown = resetIdleTimer;
326
+ win.ontouchstart = resetIdleTimer;
327
+ win.onclick = resetIdleTimer;
328
+ win.onkeypress = resetIdleTimer;
329
+ win.addEventListener('scroll', resetIdleTimer, true);
330
+ resetIdleTimer();
331
+ }
332
+
333
+ if(timer && timer > 0){
334
+ monitorIdleWindow(window);
335
+ setInterval(() => {
336
+ if (new Date().getTime() - userIdleTime > (60000 * timer)) {
337
+ notifyIdle();
338
+ }
339
+ }, 1000);
340
+ }
341
+ }
330
342
 
331
343
  cronapp.ioc.getInstance(cronapp.configuration.IConfigurationService).getValue("cronapp.framework.auth.idletime").then(idleMonitor);
332
344
 
@@ -343,60 +355,68 @@ if (!window.hostApp) {
343
355
  $scope.ignoreAuth = true;
344
356
  }
345
357
 
346
- $rootScope.session = (localStorage.getItem('_u') !== undefined) ? JSON.parse(localStorage.getItem('_u')) : null;
347
-
348
- if($rootScope.session) {
349
- // When access home page we have to check
350
- // if the user is authenticated and the userData
351
- // was saved on the browser's localStorage
352
- $rootScope.myTheme = '';
353
- if ($rootScope.session.user)
354
- $rootScope.myTheme = $rootScope.session.user.theme;
355
- $scope.$watch('myTheme', function(value) {
356
- if(value !== undefined && value !== "") {
357
- $('#themeSytleSheet').attr('href', "node_modules/cronapp-framework-js/css/themes/" + value + ".min.css");
358
+ SessionService.checkSession($scope).then(() => {
359
+ $rootScope.session = (localStorage.getItem('_u') !== undefined) ? JSON.parse(localStorage.getItem('_u')) : null;
360
+
361
+ if ($rootScope.session) {
362
+ // When access home page we have to check
363
+ // if the user is authenticated and the userData
364
+ // was saved on the browser's localStorage
365
+ $rootScope.myTheme = '';
366
+ if ($rootScope.session.user)
367
+ $rootScope.myTheme = $rootScope.session.user.theme;
368
+ $scope.$watch('myTheme', function (value) {
369
+ if (value !== undefined && value !== "") {
370
+ $('#themeSytleSheet').attr('href', "node_modules/cronapp-framework-js/css/themes/" + value + ".min.css");
371
+ }
372
+ });
373
+ if (localStorage.getItem('_u')) {
374
+ refreshToken($http, function () {
375
+ }, function () {
376
+ localStorage.removeItem('_u');
377
+ if (window.location.hash) {
378
+ $state.go('loginReturnUrl', {"returnUrl": window.location.hash});
379
+ } else {
380
+ $state.go('login');
381
+ }
382
+ }, FrontBoostAuthenticationService);
358
383
  }
359
- });
360
- if (localStorage.getItem('_u')){
361
- refreshToken($http, function() {},function() {
362
- localStorage.removeItem('_u');
384
+ } else {
385
+ if (!$scope.ignoreAuth) {
386
+ localStorage.removeItem("_u");
363
387
  if (window.location.hash) {
364
- $state.go('loginReturnUrl', { "returnUrl" : window.location.hash });
388
+ $state.go('loginReturnUrl', {"returnUrl": window.location.hash});
365
389
  } else {
366
- $state.go('login');
390
+ window.location.href = "";
367
391
  }
368
- });
369
- }
370
- }
371
- else {
372
- if (!$scope.ignoreAuth) {
373
- localStorage.removeItem("_u");
374
- if (window.location.hash) {
375
- $state.go('loginReturnUrl', { "returnUrl" : window.location.hash });
376
- } else {
377
- window.location.href = "";
378
392
  }
379
393
  }
380
- }
394
+ });
381
395
 
382
396
  $rootScope.logout = function logout() {
383
- $http({
384
- method : 'POST',
385
- url : window.hostApp + 'logout',
386
- headers : {
387
- 'Content-Type' : 'application/json'
397
+ FrontBoostAuthenticationService.isAvailable().then(available => {
398
+ if (available) {
399
+ FrontBoostAuthenticationService.signOut().then(clean).catch(clean);
400
+ } else {
401
+ $http({
402
+ method: 'POST',
403
+ url: window.hostApp + 'logout',
404
+ headers: {
405
+ 'Content-Type': 'application/json'
406
+ }
407
+ }).then(clean).catch(clean);
388
408
  }
389
- }).then(clean).catch(clean);
390
409
 
391
- function clean(result) {
392
- result = getRequestData(result);
393
- $rootScope.session = {};
394
- if(typeof (Storage) !== "undefined") {
395
- localStorage.removeItem("_u");
396
- }
410
+ function clean(result) {
411
+ result = getRequestData(result);
412
+ $rootScope.session = {};
413
+ if (typeof (Storage) !== "undefined") {
414
+ localStorage.removeItem("_u");
415
+ }
397
416
 
398
- $scope.cronapi.util.changeViewLogout();
399
- }
417
+ $scope.cronapi.util.changeViewLogout();
418
+ }
419
+ });
400
420
  };
401
421
 
402
422
  $scope.changePassword = function() {
@@ -575,7 +595,7 @@ if (!window.hostApp) {
575
595
  }(app));
576
596
 
577
597
  window.safeApply = function(fn) {
578
- var phase = this.$root.$$phase;
598
+ const phase = this.$root?.$$phase;
579
599
  if(phase === '$apply' || phase === '$digest') {
580
600
  if(fn && (typeof (fn) === 'function')) {
581
601
  fn();
@@ -585,4 +605,3 @@ window.safeApply = function(fn) {
585
605
  this.$apply(fn);
586
606
  }
587
607
  };
588
-