hola-server 0.4.10 → 0.5.2

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 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/db/entity.js CHANGED
@@ -128,7 +128,7 @@ class Entity {
128
128
  const refer_entity = new Entity(get_entity_meta(search_field.ref));
129
129
  const oids = await refer_entity.find_by_ref_value(value, { _id: 1 }, this.meta.collection);
130
130
  if (oids.length > 0) {
131
- and_array.push({ [search_field.name]: { "$all": oids.map(o => o._id + "") } });
131
+ and_array.push({ [search_field.name]: { "$in": oids.map(o => o._id + "") } });
132
132
  }
133
133
  } else {
134
134
  and_array.push(parse_search_value(search_field.name, search_field.type, value));
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
- module.exports = { init_session, get_session_userid };
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hola-server",
3
- "version": "0.4.10",
3
+ "version": "0.5.2",
4
4
  "description": "a meta programming framework used to build nodejs restful api",
5
5
  "main": "index.js",
6
6
  "scripts": {
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
 
@@ -44,12 +44,22 @@ const init_read_router = function (router, meta) {
44
44
  }
45
45
 
46
46
  const param_obj = req.body;
47
+
47
48
  if (meta.user_field) {
48
- const user_id = get_session_userid(req);
49
- if (user_id == null) {
50
- throw new Error("no user is found in session");
49
+ const [user_field] = meta.fields.filter(f => f.name == meta.user_field);
50
+ if (user_field && user_field.group == true) {
51
+ const user_ids = get_session_user_groups(req);
52
+ if (user_ids == null) {
53
+ throw new Error("no user group is found in session");
54
+ }
55
+ param_obj[meta.user_field] = user_ids;
56
+ } else {
57
+ const user_id = get_session_userid(req);
58
+ if (user_id == null) {
59
+ throw new Error("no user id is found in session");
60
+ }
61
+ param_obj[meta.user_field] = user_id;
51
62
  }
52
- param_obj[meta.user_field] = user_id;
53
63
  }
54
64
 
55
65
  const { code, err, total, data } = await entity.list_entity(query_params["_query"], null, param_obj);