hola-server 0.4.9 → 0.5.1
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/core/meta.js +5 -1
- package/http/express.js +21 -21
- package/http/session.js +6 -1
- package/package.json +1 -1
- package/router/read.js +6 -5
package/core/meta.js
CHANGED
|
@@ -10,12 +10,16 @@ const meta_manager = {};
|
|
|
10
10
|
* delete: delete is only used for ref field, it decide when the ref entity will be deleted, how to handle this entity,no value, will not let the refered entity be deleted, keep: keep this entity(no data consistency), cascade: also delete this entity also,
|
|
11
11
|
* sys: this field is used to control the user can set the value or not. sys field can only be set in the server side(before callback is good place to do this)
|
|
12
12
|
* create is false, this attribute can be shown in property list but sys property can't be shown in property list
|
|
13
|
+
* secure: secure properties will not be read by client, this is useful for password
|
|
14
|
+
* group: this is used to control user sharing entities, this means the entity is shared by user group, this is only valid for user field
|
|
13
15
|
*
|
|
14
16
|
* routes: configure customer defined routes
|
|
15
17
|
* link property: field link property link to entity field and the field should ref to an entity.
|
|
16
18
|
* and the field name should be the same with the ref entity field name and shouldn't make as required and no other property
|
|
19
|
+
*
|
|
20
|
+
*
|
|
17
21
|
*/
|
|
18
|
-
const field_attrs = ["name", "type", "required", "ref", "link", "delete", "create", "list", "search", "update", "clone", "sys", "secure"];
|
|
22
|
+
const field_attrs = ["name", "type", "required", "ref", "link", "delete", "create", "list", "search", "update", "clone", "sys", "secure", "group"];
|
|
19
23
|
const meta_attrs = ["collection", "primary_keys", "fields", "creatable", "readable", "updatable", "deleteable", "cloneable", "after_read",
|
|
20
24
|
"before_create", "after_create", "before_clone", "after_clone", "before_update", "after_update", "before_delete", "after_delete", "create", "clone", "update", "batch_update", "after_batch_update", "delete",
|
|
21
25
|
"ref_label", "ref_filter", "route", "user_field"];
|
package/http/express.js
CHANGED
|
@@ -11,6 +11,18 @@ const { asyncLocalStorage, set_context_value } = require('./context');
|
|
|
11
11
|
const app = express();
|
|
12
12
|
let server_initialized = false;
|
|
13
13
|
|
|
14
|
+
const is_excluded_url = (server, req) => {
|
|
15
|
+
const exclude_urls = server.exclude_urls;
|
|
16
|
+
for (let i = 0; i < exclude_urls.length; i++) {
|
|
17
|
+
const re = new RegExp(exclude_urls[i], "gim");
|
|
18
|
+
if (re.test(req.originalUrl)) {
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
|
|
14
26
|
const init_express_server = (base_dir, callback) => {
|
|
15
27
|
if (server_initialized === true) {
|
|
16
28
|
return app;
|
|
@@ -25,27 +37,15 @@ const init_express_server = (base_dir, callback) => {
|
|
|
25
37
|
init_session(app);
|
|
26
38
|
|
|
27
39
|
app.use((req, res, next) => {
|
|
28
|
-
if (server.check_user) {
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
if (!excluded) {
|
|
40
|
-
const user_id = get_session_userid(req);
|
|
41
|
-
if (user_id == null) {
|
|
42
|
-
res.json({ code: NO_SESSION, err: "no session found" });
|
|
43
|
-
} else {
|
|
44
|
-
asyncLocalStorage.run({}, () => {
|
|
45
|
-
set_context_value("req", req);
|
|
46
|
-
next();
|
|
47
|
-
});
|
|
48
|
-
}
|
|
40
|
+
if (server.check_user && !is_excluded_url(server, req)) {
|
|
41
|
+
const user_id = get_session_userid(req);
|
|
42
|
+
if (user_id == null) {
|
|
43
|
+
res.json({ code: NO_SESSION, err: "no session found" });
|
|
44
|
+
} else {
|
|
45
|
+
asyncLocalStorage.run({}, () => {
|
|
46
|
+
set_context_value("req", req);
|
|
47
|
+
next();
|
|
48
|
+
});
|
|
49
49
|
}
|
|
50
50
|
} else {
|
|
51
51
|
asyncLocalStorage.run({}, () => {
|
package/http/session.js
CHANGED
|
@@ -24,4 +24,9 @@ const get_session_userid = (req) => {
|
|
|
24
24
|
return user ? user.id : null;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
const get_session_user_groups = (req) => {
|
|
28
|
+
const group = req && req.session ? req.session.group : null;
|
|
29
|
+
return group && Array.isArray(group) ? group : null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
module.exports = { init_session, get_session_userid, get_session_user_groups };
|
package/package.json
CHANGED
package/router/read.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const { required_post_params, get_params } = require('../http/params');
|
|
2
2
|
const { has_value } = require('../core/validate');
|
|
3
3
|
const { NO_PARAMS, SUCCESS } = require('../http/code');
|
|
4
|
-
const { get_session_userid } = require('../http/session');
|
|
4
|
+
const { get_session_userid, get_session_user_groups } = require('../http/session');
|
|
5
5
|
const { wrap_http } = require('../http/error');
|
|
6
6
|
const { Entity } = require('../db/entity');
|
|
7
7
|
|
|
@@ -45,11 +45,12 @@ const init_read_router = function (router, meta) {
|
|
|
45
45
|
|
|
46
46
|
const param_obj = req.body;
|
|
47
47
|
if (meta.user_field) {
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
|
|
48
|
+
const [user_field] = meta.fields.filter(f => f.name == meta.user_field);
|
|
49
|
+
const user_ids = user_field && user_field.group == true ? get_session_user_groups(req) : [get_session_userid(req)];
|
|
50
|
+
if (user_ids == null) {
|
|
51
|
+
throw new Error("no user or user group is found in session");
|
|
51
52
|
}
|
|
52
|
-
param_obj[meta.user_field] =
|
|
53
|
+
param_obj[meta.user_field] = { "$in": user_ids };
|
|
53
54
|
}
|
|
54
55
|
|
|
55
56
|
const { code, err, total, data } = await entity.list_entity(query_params["_query"], null, param_obj);
|