@osimatic/helpers-js 1.0.63 → 1.0.64

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.
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="WEB_MODULE" version="4">
3
+ <component name="NewModuleRootManager">
4
+ <content url="file://$MODULE_DIR$" />
5
+ <orderEntry type="inheritedJdk" />
6
+ <orderEntry type="sourceFolder" forTests="false" />
7
+ </component>
8
+ </module>
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/helpers-js.iml" filepath="$PROJECT_DIR$/.idea/helpers-js.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
package/.idea/vcs.xml ADDED
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VcsDirectoryMappings">
4
+ <mapping directory="$PROJECT_DIR$" vcs="Git" />
5
+ </component>
6
+ </project>
package/index.js CHANGED
@@ -32,7 +32,7 @@ const { ShoppingCart } = require('./shopping_cart');
32
32
  const { FlashMessage } = require('./flash_message');
33
33
  const { CountDown } = require('./count_down');
34
34
  const { ImportFromCsv } = require('./import_from_csv');
35
- const { JwtToken, JwtSession } = require('./jwt');
35
+ const { JwtToken, JwtSession, ApiTokenSession } = require('./jwt');
36
36
  const { ListBox } = require('./list_box');
37
37
  const { WebRTC } = require('./web_rtc');
38
38
  const { EventBus } = require('./event_bus');
@@ -47,7 +47,7 @@ const { WebSocket } = require('./web_socket');
47
47
  module.exports = {
48
48
  Array, Object, Number, String,
49
49
  HTTPRequest, Cookie, UrlAndQueryString, IBAN, BankCard, AudioMedia, UserMedia, PersonName, Email, TelephoneNumber, DateTime, TimestampUnix, SqlDate, SqlTime, SqlDateTime, Duration, File, CSV, Img, FormHelper, Country, PostalAddress, GeographicCoordinates, SocialNetwork,
50
- Browser, DataTable, Pagination, Navigation, DetailsSubArray, SelectAll, MultipleActionInTable, FormDate, InputPeriod, ShoppingCart, FlashMessage, CountDown, ImportFromCsv, JwtToken, JwtSession, ListBox, WebRTC, WebSocket, EventBus,
50
+ Browser, DataTable, Pagination, Navigation, DetailsSubArray, SelectAll, MultipleActionInTable, FormDate, InputPeriod, ShoppingCart, FlashMessage, CountDown, ImportFromCsv, JwtToken, JwtSession, ApiTokenSession, ListBox, WebRTC, WebSocket, EventBus,
51
51
  sleep, refresh, chr, ord, trim, empty,
52
52
  GoogleCharts, GoogleRecaptcha, GoogleMap, OpenStreetMap
53
53
  };
package/jwt.js CHANGED
@@ -93,4 +93,74 @@ class JwtSession {
93
93
  }
94
94
  }
95
95
 
96
- module.exports = { JwtToken, JwtSession };
96
+ class ApiTokenSession {
97
+ static denyAccessUnlessGranted(roles) {
98
+ let hasRole = false;
99
+
100
+ roles.forEach(role => {
101
+ if (ApiTokenSession.isGranted(role)) {
102
+ hasRole = true;
103
+ }
104
+ });
105
+
106
+ return hasRole;
107
+ }
108
+
109
+ static getToken() {
110
+ return localStorage.getItem('api_token');
111
+ }
112
+ static setToken(token) {
113
+ localStorage.setItem('api_token', token);
114
+ }
115
+
116
+ static getTokenData() {
117
+ let tokenData = localStorage.getItem('token_data');
118
+ if (null == tokenData) {
119
+ return null;
120
+ }
121
+ return JSON.parse(tokenData);
122
+ }
123
+ static setTokenData(data) {
124
+ localStorage.setItem('token_data', JSON.stringify(data));
125
+ }
126
+
127
+ static logout() {
128
+ localStorage.removeItem('api_token');
129
+ localStorage.removeItem('token_data');
130
+ }
131
+
132
+ static getData(key) {
133
+ let tokenData = ApiTokenSession.getTokenData();
134
+ if (tokenData == null) {
135
+ return null;
136
+ }
137
+
138
+ if (typeof tokenData[key] != 'undefined') {
139
+ return tokenData[key];
140
+ }
141
+ return null;
142
+ }
143
+
144
+ static isAnonymous() {
145
+ return ApiTokenSession.getToken() == null;
146
+ }
147
+
148
+ static isGranted(role) {
149
+ if (ApiTokenSession.getToken() == null) {
150
+ return false;
151
+ }
152
+
153
+ let roles = [];
154
+ if (null !== ApiTokenSession.getData('role')) {
155
+ roles = ApiTokenSession.getData('role');
156
+ }
157
+ if (null !== ApiTokenSession.getData('roles')) {
158
+ roles = ApiTokenSession.getData('roles');
159
+ }
160
+ roles = Array.isArray(roles) ? roles : [roles];
161
+
162
+ return roles.indexOf(role) !== -1;
163
+ }
164
+ }
165
+
166
+ module.exports = { JwtToken, JwtSession, ApiTokenSession };
package/network.js CHANGED
@@ -1,5 +1,3 @@
1
- const { JwtSession } = require('@osimatic/helpers-js/jwt');
2
-
3
1
  class HTTPRequest {
4
2
  static init() {
5
3
  require('whatwg-fetch'); //fetch polyfill loaded in window.fetch
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@osimatic/helpers-js",
3
- "version": "1.0.63",
3
+ "version": "1.0.64",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"