cronapp-framework-js 1.0.4 → 1.0.5

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,68 +5,28 @@ if (!window.hostApp) {
5
5
  angular.module('custom.controllers', []);
6
6
 
7
7
  // refresh token
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
- });
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;
69
19
  }
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();
70
30
  });
71
31
  };
72
32
 
@@ -114,11 +74,9 @@ if (!window.hostApp) {
114
74
  }
115
75
  });
116
76
 
117
- app.controller('SignupController' , function($controller, $scope, $stateParams, $location, $http, $rootScope, $translate, Notification, UploadService, $timeout, $state, ReportService, FrontBoostCommandService, $log) {
77
+ app.controller('SignupController' , function($controller, $scope, $stateParams, $location, $http, $rootScope, $translate, Notification, UploadService, $timeout, $state, ReportService) {
118
78
 
119
79
  app.registerEventsCronapi($scope, $translate);
120
- app.registerFrontBoostFunctions($scope, FrontBoostCommandService, $log);
121
-
122
80
 
123
81
  $rootScope.Notification = Notification; // Não remover, usado pelo cronapi.js para notificar erros de auto-cadastro.
124
82
 
@@ -129,16 +87,26 @@ if (!window.hostApp) {
129
87
 
130
88
  });
131
89
 
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) {
90
+ app.controller('LoginController', function($controller, $scope, $http, $rootScope, $window, $state, $translate, Notification, ReportService, UploadService, $location, $stateParams, $timeout, $cookies, $templateCache, DashboardService, SyncService) {
133
91
 
134
92
  $http.get(window.NotificationProviderOptions.templateUrl, {cache: true})
135
- .then((response) => $templateCache.put(window.NotificationProviderOptions.templateUrl, response.data));
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
+ };
136
105
 
137
106
  $scope.$http = $http;
138
107
  $scope.params = $stateParams;
139
108
  $scope.$state = $state;
140
109
  app.registerEventsCronapi($scope, $translate, $location);
141
- app.registerFrontBoostFunctions($scope, FrontBoostCommandService, $log);
142
110
 
143
111
  $rootScope.http = $http;
144
112
  $rootScope.Notification = Notification;
@@ -153,13 +121,7 @@ if (!window.hostApp) {
153
121
  };
154
122
 
155
123
  $scope.redirectToLogin = function() {
156
- FrontBoostAuthenticationService.isAvailable().then(available => {
157
- if (!available) {
158
- $scope.cronapi.social.ssoLogin();
159
- } else {
160
- FrontBoostAuthenticationService.signIn();
161
- }
162
- });
124
+ $scope.cronapi.social.ssoLogin();
163
125
  };
164
126
 
165
127
  if (localStorage.getItem('redir_mob')) {
@@ -173,46 +135,48 @@ if (!window.hostApp) {
173
135
  window.grecaptcha.reset();
174
136
  };
175
137
 
138
+ function checkSession() {
139
+ $http({
140
+ method : 'GET',
141
+ url : window.hostApp + 'me'
142
+ }).then(handleSuccess);
143
+ }
144
+
176
145
  $scope.login = function(username, password) {
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();
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;
211
151
  }
212
- });
213
- }
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();
159
+ }
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
+ };
214
170
 
215
- SessionService.checkSession($scope);
171
+ $http({
172
+ method : 'POST',
173
+ url : window.hostApp + 'login',
174
+ data : $.param(user),
175
+ headers : headerValues
176
+ }).then(checkSession).catch(handleError);
177
+ };
178
+
179
+ checkSession();
216
180
 
217
181
  $scope.forgotPassword = function () {
218
182
  if (forgotPasswordEmail.value === '') {
@@ -239,6 +203,31 @@ if (!window.hostApp) {
239
203
  }).catch(data => Notification.error(data));
240
204
  };
241
205
 
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
+
242
231
  function handleError(data, status, headers, config) {
243
232
  status = status || data.status;
244
233
  data = getRequestData(data);
@@ -275,13 +264,12 @@ if (!window.hostApp) {
275
264
 
276
265
  });
277
266
 
278
- app.controller('HomeController', function($controller, $scope, $http, $rootScope, $state, $translate, Notification, ReportService, UploadService, $location, $stateParams, $timeout, DashboardService, SyncService, FrontBoostAuthenticationService, SessionService, FrontBoostCommandService, $log) {
267
+ app.controller('HomeController', function($controller, $scope, $http, $rootScope, $state, $translate, Notification, ReportService, UploadService, $location, $stateParams, $timeout, DashboardService, SyncService) {
279
268
 
280
269
  $scope.$http = $http;
281
270
  $scope.params = $stateParams;
282
271
  $scope.$state = $state;
283
272
  app.registerEventsCronapi($scope, $translate, $location);
284
- app.registerFrontBoostFunctions($scope, FrontBoostCommandService, $log);
285
273
 
286
274
  $rootScope.http = $http;
287
275
  $rootScope.Notification = Notification;
@@ -296,49 +284,49 @@ if (!window.hostApp) {
296
284
  };
297
285
 
298
286
  var idleMonitor = function(timer){
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
- }
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
+ }
342
330
 
343
331
  cronapp.ioc.getInstance(cronapp.configuration.IConfigurationService).getValue("cronapp.framework.auth.idletime").then(idleMonitor);
344
332
 
@@ -355,68 +343,60 @@ if (!window.hostApp) {
355
343
  $scope.ignoreAuth = true;
356
344
  }
357
345
 
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);
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");
383
358
  }
384
- } else {
385
- if (!$scope.ignoreAuth) {
386
- localStorage.removeItem("_u");
359
+ });
360
+ if (localStorage.getItem('_u')){
361
+ refreshToken($http, function() {},function() {
362
+ localStorage.removeItem('_u');
387
363
  if (window.location.hash) {
388
- $state.go('loginReturnUrl', {"returnUrl": window.location.hash});
364
+ $state.go('loginReturnUrl', { "returnUrl" : window.location.hash });
389
365
  } else {
390
- window.location.href = "";
366
+ $state.go('login');
391
367
  }
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 = "";
392
378
  }
393
379
  }
394
- });
380
+ }
395
381
 
396
382
  $rootScope.logout = function logout() {
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);
383
+ $http({
384
+ method : 'POST',
385
+ url : window.hostApp + 'logout',
386
+ headers : {
387
+ 'Content-Type' : 'application/json'
408
388
  }
389
+ }).then(clean).catch(clean);
409
390
 
410
- function clean(result) {
411
- result = getRequestData(result);
412
- $rootScope.session = {};
413
- if (typeof (Storage) !== "undefined") {
414
- localStorage.removeItem("_u");
415
- }
416
-
417
- $scope.cronapi.util.changeViewLogout();
391
+ function clean(result) {
392
+ result = getRequestData(result);
393
+ $rootScope.session = {};
394
+ if(typeof (Storage) !== "undefined") {
395
+ localStorage.removeItem("_u");
418
396
  }
419
- });
397
+
398
+ $scope.cronapi.util.changeViewLogout();
399
+ }
420
400
  };
421
401
 
422
402
  $scope.changePassword = function() {
@@ -595,7 +575,7 @@ if (!window.hostApp) {
595
575
  }(app));
596
576
 
597
577
  window.safeApply = function(fn) {
598
- const phase = this.$root?.$$phase;
578
+ var phase = this.$root.$$phase;
599
579
  if(phase === '$apply' || phase === '$digest') {
600
580
  if(fn && (typeof (fn) === 'function')) {
601
581
  fn();
@@ -605,3 +585,4 @@ window.safeApply = function(fn) {
605
585
  this.$apply(fn);
606
586
  }
607
587
  };
588
+