-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.ts
201 lines (161 loc) · 7.23 KB
/
script.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import {MendixSdkClient, Project, Revision, Branch, OnlineWorkingCopy, loadAsPromise} from 'mendixplatformsdk';
import {IModel, domainmodels, security} from 'mendixmodelsdk';
import when = require('when');
import fs = require('fs');
import envvars = require('dotenv');
// If you have a .env file in the project folder, settings are read from that
envvars.load();
// Otherwise, specify them here explicitly
const username = process.env.MXACCOUNT ? process.env.MXACCOUNT : "YOUR MENDIX USERNAME";
const apikey = process.env.APIKEY ? process.env.APIKEY : "YOUR MENDIX APIKEY";
const projectId = process.env.PROJECTID ? process.env.PROJECTID : "MENDIX PROJECT ID";
const projectName = process.env.PROJECTNAME ? process.env.PROJECTNAME : "MENDIX PROJECT NAME";
const branchName = process.env.BRANCHNAME ? process.env.BRANCHNAME : "BRANCH NAME"; // null for mainline
const revNo = process.env.REVISION ? parseInt(process.env.REVISION) : -1; // -1 for latest
const client = new MendixSdkClient(username, apikey);
const project = new Project(client, projectId, projectName);
const revision = new Revision(revNo, new Branch(project, branchName));
var model : IModel = null;
var domainModels : domainmodels.DomainModel[];
var projectSecurity: security.ProjectSecurity;
var _self = this;
// Output will be stored here
fs.mkdirSync("out");
console.log('Make sure that you close all of the output files..')
project.createWorkingCopy(revision)
.then(getProjectModel)
.then(model => this.model=model)
.then(() => loadProjectSecurity(this.model))
.then(projectSecurityResult => {
console.log('Received project security..')
this.projectSecurity = projectSecurityResult;
})
.then(() => loadDomainModels(this.model))
.then(domainModelArray => {
console.log('Received domain models..')
this.domainModels = domainModelArray;
})
.done(processModel)
function processModel(){
console.log(`Everything is loaded..`);
var domainModels: domainmodels.DomainModel[] = _self.domainModels;
var userRoles : security.UserRole[] = _self.projectSecurity.userRoles;
writeModuleRoles();
writeEntities();
console.log('Done, check the out folder..')
}
/* Get the model from the workingcopy as a promise */
function getProjectModel(workingCopy: OnlineWorkingCopy): when.Promise<IModel> {
console.log('Retrieving model..')
var model = workingCopy.model();
return when.promise<IModel>((resolve, reject) => {
if (model){
resolve(model);
}
else{
reject('What model?');
}
});
}
/* Load a projectsecurity from the model as a promise */
function loadProjectSecurity(model: IModel): when.Promise<security.ProjectSecurity> {
console.log('Loading project security..')
var _model = model;
return when.promise<security.ProjectSecurity>((resolve, reject) => {
if(!_model){
reject('model is undefined');
}
var iSecurity = _model.allProjectSecurities()[0];
if (iSecurity) {
iSecurity.load(security => resolve(security));
} else {
reject(`'security' is undefined`);
}
})
}
/* Load all domain models as a promise */
function loadDomainModels(model: IModel): when.Promise<domainmodels.DomainModel[]> {
console.log('Loading domain models..')
var _model = model;
var iDomainmodels = _model.allDomainModels();
return when.all<domainmodels.DomainModel[]>(iDomainmodels.map( iDomainmodel => loadAsPromise(iDomainmodel)));
}
function writeModuleRoles(){
console.log('Writing security roles to csv..');
var userRoles : security.UserRole[] = _self.projectSecurity.userRoles;
var output = '';
output+=['User Role',
'ModuleRole',
"\r\n"].join(";");
userRoles.forEach(function(userRole){
userRole.moduleRolesQualifiedNames.forEach(function(moduleRole){
output+=[userRole.name, moduleRole, "\r\n"].join(";");
})
})
var filename = project.name()+"_roles.csv";
var ws = fs.createWriteStream('./out/'+filename, {flags: "w"});
ws.write(output);
ws.end();
}
function writeEntities(){
console.log('Writing entities to csv..');
var userRoles : security.UserRole[] = _self.projectSecurity.userRoles;
var domainModels: domainmodels.DomainModel[] = _self.domainModels;
var output = '';
output+=[ 'UserRole',
'ModuleRole',
'Module',
'Entity',
'Attribute',
'Description',
'XPath Constraint',
'Accessrights',
'AccessRule ID',
"\r\n"].join(";");
userRoles.forEach(function(userRole){
userRole.moduleRolesQualifiedNames.forEach(function(moduleRoleName){
domainModels.forEach(function(domainModel){
var moduleName = domainModel.qualifiedName;
domainModel.entities.forEach(function(entity){
var entityName = entity.name;
entity.accessRules.filter(function(accessRule){
return accessRule.moduleRolesQualifiedNames.indexOf(moduleRoleName) >= 0;
}).forEach(function(accessRule){
accessRule.memberAccesses.forEach(function(memberAccess){
var memberAccessRights = memberAccess.accessRights;
if(memberAccess.attribute){
var attributeName = memberAccess.attribute.name;
output+=[ userRole.name,
moduleRoleName,
moduleName,
entityName,
attributeName,
accessRule.documentation.trim().replace(/\r?\n|\r/g,''),
accessRule.xPathConstraint.trim().replace(/\r?\n|\r/g,''),
memberAccessRights.name,
accessRule.id,
"\r\n"].join(";");
} else if(memberAccess.association){
var associationName = memberAccess.association.name;
output+=[ userRole.name,
moduleRoleName,
moduleName,
entityName,
associationName,
accessRule.documentation.trim().replace(/\r?\n|\r/g,''),
accessRule.xPathConstraint.trim().replace(/\r?\n|\r/g,''),
memberAccessRights.name,
accessRule.id,
"\r\n"].join(";");
}
})
})
})
})
})
})
var filename = project.name()+"_entities.csv";
var ws = fs.createWriteStream('./out/'+filename, {flags: "w"});
ws.write(output);
ws.end();
}