@steedos/standard-permission 2.2.55-beta.16

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.
Files changed (39) hide show
  1. package/main/default/applications/.gitkeep +0 -0
  2. package/main/default/client/permission_set.client.js +38 -0
  3. package/main/default/objectTranslations/permission_objects.en/permission_objects.en.objectTranslation.yml +108 -0
  4. package/main/default/objectTranslations/permission_objects.zh-CN/permission_objects.zh-CN.objectTranslation.yml +108 -0
  5. package/main/default/objectTranslations/permission_set.en/permission_set.en.objectTranslation.yml +87 -0
  6. package/main/default/objectTranslations/permission_set.zh-CN/permission_set.zh-CN.objectTranslation.yml +87 -0
  7. package/main/default/objectTranslations/permission_shares.en/permission_shares.en.objectTranslation.yml +27 -0
  8. package/main/default/objectTranslations/permission_shares.zh-CN/permission_shares.zh-CN.objectTranslation.yml +27 -0
  9. package/main/default/objects/permission_fields.object.yml +184 -0
  10. package/main/default/objects/permission_objects.action.js +57 -0
  11. package/main/default/objects/permission_objects.function.js +23 -0
  12. package/main/default/objects/permission_objects.object.js +20 -0
  13. package/main/default/objects/permission_objects.object.yml +394 -0
  14. package/main/default/objects/permission_set/buttons/permission_set.action.js +17 -0
  15. package/main/default/objects/permission_set/permission_set.object.yml +268 -0
  16. package/main/default/objects/permission_shares.object.js +57 -0
  17. package/main/default/objects/permission_shares.object.yml +63 -0
  18. package/main/default/objects/restriction_rules.action.js +25 -0
  19. package/main/default/objects/restriction_rules.object.yml +82 -0
  20. package/main/default/objects/share_rules.action.js +25 -0
  21. package/main/default/objects/share_rules.object.yml +82 -0
  22. package/main/default/permissionsets/.gitkeep +0 -0
  23. package/main/default/profiles/.gitkeep +0 -0
  24. package/main/default/routes/copyProfile.router.js +113 -0
  25. package/main/default/server/permission_set.object.js +130 -0
  26. package/main/default/tabs/.gitkeep +0 -0
  27. package/main/default/triggers/.gitkeep +0 -0
  28. package/main/default/triggers/permission_fields.trigger.js +94 -0
  29. package/main/default/triggers/permission_objects.trigger.js +159 -0
  30. package/main/default/triggers/permission_set.trigger.js +89 -0
  31. package/main/default/triggers/restriction_rules.trigger.js +67 -0
  32. package/main/default/triggers/restriction_rules_api_name.trigger.js +30 -0
  33. package/main/default/triggers/share_rules.trigger.js +68 -0
  34. package/main/default/triggers/share_rules_api_name.trigger.js +30 -0
  35. package/package.json +16 -0
  36. package/package.service.js +75 -0
  37. package/public/.md +3 -0
  38. package/src/.md +3 -0
  39. package/webapp/.md +1 -0
@@ -0,0 +1,113 @@
1
+ /*
2
+ * @Author: sunhaolin@hotoa.com
3
+ * @Date: 2022-05-26 16:56:54
4
+ * @LastEditors: sunhaolin@hotoa.com
5
+ * @LastEditTime: 2022-05-26 19:28:09
6
+ * @Description: 复制已有简档来创建新简档
7
+ */
8
+ 'use strict';
9
+ // @ts-check
10
+
11
+ const express = require("express");
12
+ const router = express.Router();
13
+ const core = require('@steedos/core');
14
+ const objectql = require('@steedos/objectql');
15
+
16
+ /**
17
+ * body {
18
+ * originalPermissionSetId: "", // 已有简档的id
19
+ * name: "", // 新简档的名称
20
+ * label: "", // 新简档的标签
21
+ * }
22
+ */
23
+ router.post('/api/permission/permission_set/copy', core.requireAuthentication, async function (req, res) {
24
+ try {
25
+ const userSession = req.user;
26
+ const { userId, spaceId, company_id } = userSession;
27
+
28
+ const { originalPermissionSetId, name, label } = req.body;
29
+ if (!originalPermissionSetId) {
30
+ throw new Error("originalPermissionSetId is required");
31
+ }
32
+ if (!name) {
33
+ throw new Error("name is required");
34
+ }
35
+ if (!label) {
36
+ throw new Error("label is required");
37
+ }
38
+
39
+ const psObj = objectql.getObject('permission_set');
40
+ const poObj = objectql.getObject('permission_objects');
41
+ const pfObj = objectql.getObject('permission_fields');
42
+
43
+ const originalPermissionSet = await psObj.findOne(originalPermissionSetId, {}, userSession);
44
+ if (!originalPermissionSet) {
45
+ throw new Error("permission_set not found");
46
+ }
47
+ // 校验简档是否是简档profile类型
48
+ if (originalPermissionSet.type !== 'profile') {
49
+ throw new Error("permission_set is not profile type");
50
+ }
51
+
52
+ // 基础信息
53
+ const baseInfo = {
54
+ created: new Date(),
55
+ created_by: userId,
56
+ modified: new Date(),
57
+ modified_by: userId,
58
+ space: spaceId,
59
+ company_id: company_id
60
+ };
61
+
62
+ // 创建新简档
63
+ delete originalPermissionSet._id;
64
+ const newPermissionSet = await psObj.insert({
65
+ ...originalPermissionSet,
66
+ ...baseInfo,
67
+ name,
68
+ label
69
+ });
70
+
71
+ // 创建对象权限
72
+ if (newPermissionSet) {
73
+ const orininalPermissionSetName = originalPermissionSet.name;
74
+ const { _id: newPermissionSetId, name: newPermissionSetName } = newPermissionSet;
75
+ const originalPermissionObjects = await poObj.find({ filters: [['space', '=', spaceId], ['permission_set_id', '=', originalPermissionSetId]] }, userSession);
76
+ // 遍历原有的对象权限
77
+ for (const poDoc of originalPermissionObjects) {
78
+ // 创建新的对象权限
79
+ delete poDoc._id;
80
+ const newPermissionObject = await poObj.insert({
81
+ ...poDoc,
82
+ ...baseInfo,
83
+ permission_set_id: newPermissionSetId,
84
+ });
85
+ // 创建新的字段权限
86
+ // 由于 15.permission_objects.observe.object.js 中的 _change 函数中,调用了 permission_fields.resetFieldPermissions 方法,会自动创建新的字段权限,为防止重复创建故这里不手动创建
87
+ // if (newPermissionObject) {
88
+ // const newPermissionObjectName = newPermissionObject.name;
89
+ // const originalPermissionFields = await pfObj.find({ filters: [['space', '=', spaceId], ['permission_set_id', '=', orininalPermissionSetName], ['permission_object', '=', poDoc.name]] }, userSession);
90
+ // // 遍历原有的字段权限
91
+ // for (const pfDoc of originalPermissionFields) {
92
+ // // 创建新的字段权限
93
+ // delete pfDoc._id;
94
+ // const newPermissionField = await pfObj.insert({
95
+ // ...pfDoc,
96
+ // ...baseInfo,
97
+ // permission_set_id: newPermissionSetName,
98
+ // permission_object_id: newPermissionObjectName
99
+ // });
100
+ // }
101
+ // }
102
+ }
103
+
104
+ }
105
+
106
+ res.status(200).send({ message: 'success' });
107
+ } catch (error) {
108
+ console.error(error);
109
+ res.status(500).send({ error: error.message });
110
+ }
111
+
112
+ });
113
+ exports.default = router;
@@ -0,0 +1,130 @@
1
+ var _ = require("underscore");
2
+ function checkType(name, type){
3
+ if(_.include(['admin','user','supplier','customer'], name) && type != 'profile'){
4
+ throw new Meteor.Error(500, "API名称为admin,user,supplier,customer时,类别必须为简档");
5
+ }
6
+ }
7
+
8
+ Creator.Objects['permission_set'].triggers = Object.assign({}, Creator.Objects['permission_set'].triggers,{
9
+ "before.insert.server.check": {
10
+ on: "server",
11
+ when: "before.insert",
12
+ todo: function (userId, doc) {
13
+ var newName;
14
+ // console.log "before.insert.server.check,doc:", doc
15
+ newName = doc != null ? doc.name : void 0;
16
+ if (newName && Creator.getCollection("permission_set").findOne({
17
+ space: doc.space,
18
+ name: newName
19
+ }, {
20
+ fields: {
21
+ name: 1
22
+ }
23
+ })) {
24
+ throw new Meteor.Error(500, "API名称不能重复");
25
+ }
26
+ checkType(doc.name, doc.type);
27
+ if(doc.type === 'profile'){
28
+ if(!doc.license){
29
+ // throw new Meteor.Error(500, "请指定许可证");
30
+ }else{
31
+ if(_.indexOf(_.pluck(Steedos.getLicenseOptionsSync(doc.space), 'value'), doc.license) < 0){
32
+ throw new Meteor.Error(500, "无效的许可证");
33
+ }
34
+ }
35
+ }
36
+
37
+ if(doc.license){
38
+ if(_.indexOf(_.pluck(Steedos.getLicenseOptionsSync(doc.space), 'value'), doc.license) < 0){
39
+ throw new Meteor.Error(500, "无效的许可证");
40
+ }
41
+ }
42
+
43
+ if(doc.type === 'profile'){
44
+ delete doc.users
45
+ }
46
+ }
47
+ },
48
+ "before.update.server.check": {
49
+ on: "server",
50
+ when: "before.update",
51
+ todo: function (userId, doc, fieldNames, modifier, options) {
52
+ var newName, ref;
53
+ newName = (ref = modifier.$set) != null ? ref.name : void 0;
54
+ if (newName && Creator.getCollection("permission_set").findOne({
55
+ space: doc.space,
56
+ name: newName,
57
+ _id: {
58
+ $ne: doc._id
59
+ }
60
+ }, {
61
+ fields: {
62
+ name: 1
63
+ }
64
+ })) {
65
+ throw new Meteor.Error(500, "API名称不能重复");
66
+ }
67
+
68
+ var set = modifier.$set || {}
69
+ if(_.has(set, 'name') || _.has(set, 'type')){
70
+ checkType(set.name || doc.name, set.type || doc.type);
71
+ }
72
+
73
+ if(_.has(set, 'type') || _.has(set, 'users')){
74
+ var type = set.type || doc.type;
75
+ var users = set.users || doc.users
76
+ if(type === 'profile'){
77
+ if(_.has(set, 'users')){
78
+ modifier.$set.users = []
79
+ }else{
80
+ if(!modifier.$unset){
81
+ modifier.$unset = {}
82
+ }
83
+ modifier.$unset.users = 1
84
+ }
85
+ }
86
+ }
87
+ var unset = modifier.$unset || {}
88
+ if((_.has(set, 'license') && set.license != doc.license)){
89
+ throw new Meteor.Error(500, '禁止修改许可证');
90
+ // let _type = set.type || doc.type;
91
+ // if(_type === 'profile'){
92
+ // if(!set.license){
93
+ // throw new Meteor.Error(500, "请指定许可证");
94
+ // }else{
95
+
96
+ // if(_.indexOf(_.pluck(Steedos.getLicenseOptionsSync(doc.space), 'value'), set.license) < 0){
97
+ // throw new Meteor.Error(500, "无效的许可证");
98
+ // }
99
+ // }
100
+ // }
101
+ }
102
+ }
103
+ },
104
+ "after.update.server.syncSpaceUserProfile": {
105
+ on: "server",
106
+ when: "after.update",
107
+ todo: function(userId, doc, fieldNames, modifier, options){
108
+ modifier.$set = modifier.$set || {}
109
+ if(doc.type === 'profile' && _.has(modifier.$set, 'name') && modifier.$set.name != this.previous.name){
110
+ if(doc.space){
111
+ db.space_users.update({space: doc.space, profile: this.previous.name}, {$set: {profile: doc.name}}, {
112
+ multi: true
113
+ });
114
+ }
115
+ }
116
+ }
117
+ },
118
+ "before.remove.server.check": {
119
+ on: "server",
120
+ when: "before.remove",
121
+ todo: function (userId, doc) {
122
+ var documents, object_collections;
123
+ console.log('permission_objects', {permission_set_id: doc._id, space: doc.space});
124
+ var pero= Creator.getCollection("permission_objects").find({permission_set_id: doc._id, space: doc.space}, {fields:{_id:1}});
125
+ if (pero.count() > 0) {
126
+ throw new Meteor.Error(500, `此记录已在对象权限中引用`);
127
+ }
128
+ }
129
+ }
130
+ })
File without changes
File without changes
@@ -0,0 +1,94 @@
1
+ const objectql = require('@steedos/objectql');
2
+ const auth = require('@steedos/auth');
3
+ const clone = require('clone');
4
+ const _ = require('underscore');
5
+ const readonlyFields = ['created', 'created_by', 'modified', 'modified_by'];
6
+
7
+ async function getAll() {
8
+ const schema = objectql.getSteedosSchema();
9
+ const configs = await objectql.registerPermissionFields.getAll(schema.broker)
10
+ const dataList = _.pluck(configs, 'metadata');
11
+
12
+ _.each(dataList, function (item) {
13
+ if (!item._id) {
14
+ item._id = `${item.name}`
15
+ }
16
+ })
17
+ return dataList;
18
+ }
19
+
20
+ async function get(apiName) {
21
+ const schema = objectql.getSteedosSchema();
22
+ const config = await objectql.registerPermissionFields.get(schema.broker, apiName)
23
+ return config ? config.metadata : null;
24
+ }
25
+
26
+ module.exports = {
27
+ listenTo: 'permission_fields',
28
+ beforeInsert: async function(){
29
+ const { doc } = this;
30
+ if(_.include(readonlyFields, doc.field)){
31
+ doc.editable = false;
32
+ }
33
+ if(doc.editable){
34
+ doc.readable = true;
35
+ }
36
+ },
37
+ beforeUpdate: async function(){
38
+ const { doc } = this;
39
+ if(_.include(readonlyFields, doc.field)){
40
+ doc.editable = false;
41
+ }
42
+ if(doc.editable){
43
+ doc.readable = true;
44
+ }
45
+ },
46
+ beforeFind: async function () {
47
+ delete this.query.fields;
48
+ },
49
+
50
+ beforeAggregate: async function () {
51
+ delete this.query.fields;
52
+ },
53
+ afterFind: async function () {
54
+ let spaceId = this.spaceId;
55
+ let dataList = await getAll();
56
+ const values = clone(this.data.values);
57
+ _.each(dataList, (item) => {
58
+ if (!_.find(this.data.values, (value) => {
59
+ return value._id === item._id || item.name === value.name
60
+ })) {
61
+ values.push(item)
62
+ }
63
+ })
64
+ this.data.values = objectql.getSteedosSchema().metadataDriver.find(values, this.query, spaceId);
65
+
66
+ },
67
+ afterAggregate: async function () {
68
+ let spaceId = this.spaceId;
69
+ let dataList = await getAll();
70
+ const values = clone(this.data.values);
71
+ _.each(dataList, (item) => {
72
+ if (!_.find(this.data.values, (value) => {
73
+ return value._id === item._id || item.name === value.name
74
+ })) {
75
+ values.push(item)
76
+ }
77
+ })
78
+ this.data.values = objectql.getSteedosSchema().metadataDriver.find(values, this.query, spaceId);
79
+
80
+ },
81
+ afterCount: async function () {
82
+ let result = await objectql.getObject(this.object_name).find(this.query, await auth.getSessionByUserId(this.userId, this.spaceId))
83
+ this.data.values = result.length;
84
+ },
85
+ afterFindOne: async function () {
86
+ if (_.isEmpty(this.data.values)) {
87
+ let id = this.id
88
+ let data = await get(id);
89
+ if (data) {
90
+ this.data.values = data;
91
+ }
92
+ }
93
+ }
94
+ }
@@ -0,0 +1,159 @@
1
+ const _ = require('underscore');
2
+ const objectql = require("@steedos/objectql");
3
+ const InternalData = require('@steedos/standard-objects').internalData;
4
+ const auth = require("@steedos/auth");
5
+
6
+ const permissions = {
7
+ allowEdit: false,
8
+ allowDelete: false,
9
+ allowRead: true,
10
+ }
11
+
12
+ const baseRecord = {
13
+ is_system:true,
14
+ record_permissions:permissions
15
+ }
16
+
17
+ const getInternalPermissionObjects = async function(){
18
+ let objectsPermissions = [];
19
+ const datasources = objectql.getSteedosSchema().getDataSources();
20
+ for (const datasourceName in datasources) {
21
+ let datasource = datasources[datasourceName];
22
+ let datasourceObjects = await datasource.getObjects();
23
+ _.each(datasourceObjects, function(object) {
24
+ const objectJSON = object.metadata;
25
+ const objectName = objectJSON.name;
26
+ if(!objectJSON._id && !objectJSON.hidden && !_.include(InternalData.hiddenObjects, objectName)){
27
+ let permission_set = objectJSON.permission_set
28
+ _.each(permission_set, function(v, code){
29
+ objectsPermissions.push(Object.assign({}, v, {_id: `${code}_${objectName}`, name: `${code}_${objectName}`, permission_set_id: code, object_name: objectName}, baseRecord))
30
+ })
31
+ }
32
+ });
33
+ }
34
+ return objectsPermissions;
35
+ }
36
+
37
+
38
+ const initPermissionSet = async function(doc, userId, spaceId){
39
+ const userSession = await auth.getSessionByUserId(userId, spaceId);
40
+ if(!doc.permission_set_id){
41
+ throw new Error('权限集不能为空')
42
+ }
43
+
44
+ const permissionSet = await objectql.getObject('permission_set').findOne(doc.permission_set_id)
45
+
46
+ if(!permissionSet){
47
+ throw new Error('无效的权限集')
48
+ }
49
+
50
+ if(permissionSet._id === permissionSet.name){
51
+ const now = new Date();
52
+ const dbPermissionSet = await objectql.getObject('permission_set').insert({
53
+ name: permissionSet.name,
54
+ label: permissionSet.label,
55
+ type: permissionSet.type,
56
+ license: permissionSet.license,
57
+ space: spaceId,
58
+ owner: userId,
59
+ created_by: userId,
60
+ created: now,
61
+ modified_by: userId,
62
+ modified: now,
63
+ company_id: userSession.company_id,
64
+ company_ids: userSession.company_ids
65
+ })
66
+ doc.permission_set_id = dbPermissionSet._id;
67
+ }
68
+ }
69
+
70
+ module.exports = {
71
+ beforeFind: async function () {
72
+ delete this.query.fields;
73
+ },
74
+
75
+ beforeAggregate: async function () {
76
+ delete this.query.fields;
77
+ },
78
+
79
+ afterFind: async function(){
80
+ const { spaceId } = this;
81
+ let dataList = await getInternalPermissionObjects();
82
+ if (!_.isEmpty(dataList)) {
83
+ dataList.forEach((doc) => {
84
+ if (!_.find(this.data.values, (value) => {
85
+ return value.name === doc.name
86
+ })) {
87
+ this.data.values.push(doc);
88
+ }
89
+ })
90
+ const records = objectql.getSteedosSchema().metadataDriver.find(this.data.values, this.query, spaceId);
91
+ if (records.length > 0) {
92
+ this.data.values = records;
93
+ } else {
94
+ this.data.values.length = 0;
95
+ }
96
+ }
97
+
98
+ },
99
+ afterAggregate: async function(){
100
+ const { spaceId } = this;
101
+ let dataList = await getInternalPermissionObjects();
102
+ if (!_.isEmpty(dataList)) {
103
+ dataList.forEach((doc) => {
104
+ if (!_.find(this.data.values, (value) => {
105
+ return value.name === doc.name
106
+ })) {
107
+ this.data.values.push(doc);
108
+ }
109
+ })
110
+ const records = objectql.getSteedosSchema().metadataDriver.find(this.data.values, this.query, spaceId);
111
+ if (records.length > 0) {
112
+ this.data.values = records;
113
+ } else {
114
+ this.data.values.length = 0;
115
+ }
116
+ }
117
+ },
118
+ afterCount: async function(){
119
+ delete this.query.fields;
120
+ let result = await objectql.getObject(this.object_name).find(this.query, await auth.getSessionByUserId(this.userId, this.spaceId))
121
+ this.data.values = result.length;
122
+ },
123
+ afterFindOne: async function(){
124
+ if(_.isEmpty(this.data.values)){
125
+ const all = await getInternalPermissionObjects();
126
+ const id = this.id;
127
+ this.data.values = _.find(all, function(item){
128
+ return item._id === id
129
+ });
130
+ }
131
+ },
132
+ beforeInsert: async function(){
133
+ let doc = this.doc;
134
+ let existedCount = Creator.getCollection("permission_objects").direct.find({permission_set_id: doc.permission_set_id, object_name: doc.object_name, space: doc.space}).count()
135
+ if(existedCount > 0){
136
+ throw new Error("此对象已有权限对象记录")
137
+ }
138
+
139
+ await initPermissionSet(doc, this.userId, this.spaceId);
140
+
141
+ const permissionSet = await objectql.getObject('permission_set').findOne(doc.permission_set_id)
142
+ if(_.isEmpty(doc.name)){
143
+ doc.name = `${doc.object_name}.${permissionSet.name}`
144
+ }
145
+ },
146
+ beforeUpdate: async function () {
147
+ let oldDoc = Creator.getCollection("permission_objects").direct.findOne({_id: this.id})
148
+ let doc = this.doc;
149
+ let permission_set_id = doc.permission_set_id || oldDoc.permission_set_id
150
+ let object_name = doc.object_name || oldDoc.object_name
151
+ let space = oldDoc.space
152
+ let existedCount = Creator.getCollection("permission_objects").direct.find({permission_set_id: permission_set_id, object_name: object_name, space: space, _id: {$ne: this.id}}).count()
153
+ if(existedCount > 0){
154
+ throw new Error("此对象已有权限对象记录")
155
+ }
156
+
157
+ await initPermissionSet(Object.assign({permission_set_id: permission_set_id}, doc), this.userId, this.spaceId);
158
+ }
159
+ }
@@ -0,0 +1,89 @@
1
+ const _ = require('underscore');
2
+ const objectql = require('@steedos/objectql');
3
+ const auth = require("@steedos/auth");
4
+
5
+
6
+ const getSourcePermissionSets = async function(type){
7
+ switch (type) {
8
+ case 'permission_set':
9
+ return await objectql.getSourcePermissionsets();
10
+ case 'profile':
11
+ return await objectql.getSourceProfiles();
12
+ default:
13
+ return (await objectql.getSourceProfiles()).concat((await objectql.getSourcePermissionsets()));
14
+ }
15
+
16
+ }
17
+
18
+ module.exports = {
19
+ beforeFind: async function () {
20
+ delete this.query.fields;
21
+ },
22
+
23
+ beforeAggregate: async function () {
24
+ delete this.query.fields;
25
+ },
26
+
27
+ afterFind: async function(){
28
+ let { spaceId } = this;
29
+ if(!spaceId){
30
+ spaceId = process.env.STEEDOS_CLOUD_SPACE_ID;
31
+ }
32
+ if(!spaceId){
33
+ const spaces = await objectql.getObject('spaces').find({});
34
+ if(spaces.length > 0 ){
35
+ spaceId = spaces[0]._id;
36
+ }
37
+ }
38
+ let dataList = await getSourcePermissionSets();
39
+ if (!_.isEmpty(dataList)) {
40
+ dataList.forEach((doc) => {
41
+ if (!_.find(this.data.values, (value) => {
42
+ return value.name === doc.name
43
+ })) {
44
+ this.data.values.push(doc);
45
+ }
46
+ })
47
+ const records = objectql.getSteedosSchema().metadataDriver.find(this.data.values, this.query, spaceId);
48
+ if (records.length > 0) {
49
+ this.data.values = records;
50
+ } else {
51
+ this.data.values.length = 0;
52
+ }
53
+ }
54
+
55
+ },
56
+ afterAggregate: async function(){
57
+ const { spaceId } = this;
58
+ let dataList = await getSourcePermissionSets();
59
+ if (!_.isEmpty(dataList)) {
60
+ dataList.forEach((doc) => {
61
+ if (!_.find(this.data.values, (value) => {
62
+ return value.name === doc.name
63
+ })) {
64
+ this.data.values.push(doc);
65
+ }
66
+ })
67
+ const records = objectql.getSteedosSchema().metadataDriver.find(this.data.values, this.query, spaceId);
68
+ if (records.length > 0) {
69
+ this.data.values = records;
70
+ } else {
71
+ this.data.values.length = 0;
72
+ }
73
+ }
74
+ },
75
+ afterCount: async function(){
76
+ delete this.query.fields;
77
+ let result = await objectql.getObject(this.object_name).find(this.query, await auth.getSessionByUserId(this.userId, this.spaceId))
78
+ this.data.values = result.length;
79
+ },
80
+ afterFindOne: async function(){
81
+ if(_.isEmpty(this.data.values)){
82
+ const all = await getSourcePermissionSets();
83
+ const id = this.id;
84
+ this.data.values = _.find(all, function(item){
85
+ return item._id === id
86
+ });
87
+ }
88
+ }
89
+ }
@@ -0,0 +1,67 @@
1
+ const objectql = require('@steedos/objectql');
2
+ const auth = require('@steedos/auth');
3
+ const _ = require('underscore');
4
+ const clone = require('clone');
5
+ async function getAll() {
6
+ const schema = objectql.getSteedosSchema();
7
+ const configs = await objectql.registerRestrictionRules.getAll(schema.broker)
8
+ const dataList = _.pluck(configs, 'metadata');
9
+
10
+ _.each(dataList, function (item) {
11
+ if (!item._id) {
12
+ item._id = `${item.object_name}.${item.name}`
13
+ }
14
+ })
15
+ return dataList;
16
+ }
17
+
18
+ async function get(apiName) {
19
+ const schema = objectql.getSteedosSchema();
20
+ const config = await objectql.registerRestrictionRules.get(schema.broker, apiName)
21
+ return config ? Object.assign(config.metadata, { _id: config.metadata._id || `${config.metadata.object_name}.${config.metadata.name}` }) : null;
22
+ }
23
+
24
+ module.exports = {
25
+ listenTo: 'restriction_rules',
26
+
27
+ afterFind: async function () {
28
+ let spaceId = this.spaceId;
29
+ let dataList = await getAll();
30
+ const values = clone(this.data.values);
31
+ _.each(dataList, (item) => {
32
+ if (!_.find(this.data.values, (value) => {
33
+ return value._id === item._id
34
+ })) {
35
+ values.push(item)
36
+ }
37
+ })
38
+ this.data.values = objectql.getSteedosSchema().metadataDriver.find(values, this.query, spaceId);
39
+ },
40
+ afterAggregate: async function () {
41
+ let spaceId = this.spaceId;
42
+ let dataList = await getAll();
43
+ const values = clone(this.data.values);
44
+ _.each(dataList, (item) => {
45
+ if (!_.find(this.data.values, (value) => {
46
+ return value._id === item._id
47
+ })) {
48
+ values.push(item)
49
+ }
50
+ })
51
+ this.data.values = objectql.getSteedosSchema().metadataDriver.find(values, this.query, spaceId);
52
+
53
+ },
54
+ afterCount: async function () {
55
+ let result = await objectql.getObject(this.object_name).find(this.query, await auth.getSessionByUserId(this.userId, this.spaceId))
56
+ this.data.values = result.length;
57
+ },
58
+ afterFindOne: async function () {
59
+ if (_.isEmpty(this.data.values)) {
60
+ let id = this.id
61
+ let data = await get(id);
62
+ if (data) {
63
+ this.data.values = data;
64
+ }
65
+ }
66
+ }
67
+ }
@@ -0,0 +1,30 @@
1
+ /*
2
+ * @Author: sunhaolin@hotoa.com
3
+ * @Date: 2021-12-27 10:49:33
4
+ * @LastEditors: sunhaolin@hotoa.com
5
+ * @LastEditTime: 2022-07-30 13:22:03
6
+ * @Description:
7
+ */
8
+ const util = require('@steedos/standard-objects').util;
9
+ const _ = require('lodash');
10
+ const objectql = require('@steedos/objectql');
11
+ module.exports = {
12
+ listenTo: 'restriction_rules',
13
+
14
+ beforeInsert: async function () {
15
+ const { object_name, doc } = this;
16
+ await util.checkAPIName(object_name, 'name', doc.name, undefined, [['is_system', '!=', true], ['object_name', '=', doc.object_name]]);
17
+ },
18
+
19
+ beforeUpdate: async function () {
20
+ const oldDoc = await objectql.getObject(this.object_name).findOne(this.id)
21
+ let name = oldDoc.name, object_name = oldDoc.object_name;
22
+ if (_.has(this.doc, 'name')) {
23
+ name = this.doc.name
24
+ }
25
+ if (_.has(this.doc, 'object_name')) {
26
+ object_name = this.doc.object_name
27
+ }
28
+ await util.checkAPIName(this.object_name, 'name', name, this.id, [['is_system', '!=', true], ['object_name', '=', object_name]]);
29
+ }
30
+ }