-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebservices.app
263 lines (244 loc) · 7.71 KB
/
webservices.app
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
module webservices
imports
webservice/model
/**
@ param username: valid email of excisting user
@ param password: valid password of the user asociated to the username
@ param deviceDescription: string for descriptionDevice
*/
service authenticate() {
var jsonErrors := JSONArray();
var jsonResult := JSONObject();
var json := JSONObject(readRequestBody());
var email := json.getString("username");
var password := json.getString("password");
var deviceDescription := json.getString("deviceDescription");
log("called service: athenticate " + email + ":" + deviceDescription);
if(!authenticate(email, password)) {
jsonErrors.put("The login credentials are not valid.");
}
if(jsonErrors.length() > 0) {
jsonResult.put("errors", jsonErrors);
} else {
jsonResult.put("key", securityContext.principal.generateAuthenticationKey(deviceDescription));
jsonResult.put("name", securityContext.principal.name );
jsonResult.put("person", securityContext.principal.toJSON());
}
return jsonResult;
}
service checkAuthenticate() {
var jsonErrors := JSONArray();
var jsonResult := JSONObject();
var json := JSONObject(readRequestBody());
var email := json.getString("username");
var key := json.getString("key").parseUUID();
var deviceDescription := json.getString("deviceDescription");
var users := findUserByEmail(email);
log("called service: check authenticate " + email + ":" + deviceDescription);
if(users.length != 1) {
jsonErrors.put("The user does not exists");
} else {
var user := users[0];
if(user.getDeviceKey(deviceDescription) != key) {
jsonErrors.put("The key is unvalid");
} else {
securityContext.principal := user;
}
}
if(jsonErrors.length() > 0) {
jsonResult.put("errors", jsonErrors);
jsonResult.put("answer", false);
} else {
jsonResult.put("answer", true);
}
return jsonResult;
}
service logoutDevice() {
log("called service: logoutDevice");
securityContext.principal := null;
var jsonResult := JSONObject();
jsonResult.put("answer", true);
return jsonResult;
}
function tryAuthenticate(email : String, password : String) {
auth:validate(authenticate(email, password), "The login credentials are not valid.");
}
service getProjects() {
log("called service: getProjects: ");
var pr := securityContext.principal;
var projectList : List<Project> :=
select distinct p from Project as p join p.members as m where p.private=false or (~pr) = m;
var jsonArray := JSONArray();
for(project : Project in projectList ) {
jsonArray.put(project.toSimpleJSON());
}
return jsonArray;
}
service getMyProjects() {
log("called service: getMyProjects: ");
var pr := securityContext.principal;
var projectList : List<Project> :=
select distinct p from Project as p join p.members as m where (~pr) = m;
var jsonArray := JSONArray();
for(project : Project in projectList ) {
jsonArray.put(project.toSimpleJSON());
}
return jsonArray;
}
/*
@ param number: expecting max number of projecs
*/
service getPopularProjects() {
var json := JSONObject(readRequestBody());
var number := json.getInt("number");
log("called service: getPopularProjects:" + number);
var activeProjects : List<Project> :=
select p
from Project as p
join p.issues as i
where _private=false
group by p
order by max(i._submitted) desc
limit 10;
var jsonArray := JSONArray();
for(project : Project in activeProjects) {
jsonArray.put(project.toSimpleJSON());
}
return jsonArray;
}
service getProject() {
var json := JSONObject(readRequestBody());
var name := json.getString("name");
var versions := json.getJSONObject("versions");
log("called service: getProject:" + name);
var project := loadProject(name);
if(project.private && !(securityContext.principal in project.members) ){
var errors := JSONArray();
errors.put("you are not authenticated for this project");
var result := JSONObject();
result.put("errors", errors);
return result;
}
return project.toJSON(versions);
}
service getIssues() {
var json := JSONObject(readRequestBody());
var name := json.getString("project");
log("called service: getIssues:" + name);
var project := loadProject(name);
if(project.private && !(securityContext.principal in project.members) ){
var errors := JSONArray();
errors.put("you are not authenticated for this project");
var result := JSONObject();
result.put("errors",errors);
return result;
}
var jsonArrayIssues := JSONArray();
for (issue : Issue in project.issues) {
jsonArrayIssues.put(issue.toJSON());
}
return jsonArrayIssues;
}
service getIssuesDetails() {
var json := JSONObject(readRequestBody());
var name := json.getString("project");
var versions := json.getJSONObject("versions").getJSONArray("issues");
log("called service: getIssuesDetails:" + name);
var project := loadProject(name);
if(project.private && !(securityContext.principal in project.members)) {
var errors := JSONArray();
errors.put("you are not authenticated for this project");
var result := JSONObject();
result.put("errors",errors);
return result;
}
var versionobjects := toVersionObejcts(versions);
checkNewIssueObjects(versions);
checkDirtyIssueObjects(versions);
var jsonArrayIssues := JSONArray();
for (issue : Issue in project.issues) {
var vobject := VersionObject{id:=issue.id};
var index := versionobjects.indexOf(vobject);
if(index == -1 || versionobjects.get(index).version != issue.version) {
jsonArrayIssues.put(issue.toExtendedJSON());
}else{
jsonArrayIssues.put(issue.toSimpleJSON());
}
}
return jsonArrayIssues;
}
service getRoadmap() {
var json := JSONObject(readRequestBody());
var name := json.getString("project");
log("called service: getRoadmap:" + name);
var project := loadProject(name);
if(project.private && !(securityContext.principal in project.members)) {
var errors := JSONArray();
errors.put("you are not authenticated for this project");
var result := JSONObject();
result.put("errors",errors);
return result;
}
var releases := generateRoadmap(project);
var jsonArrayReleases := JSONArray();
for(release : Release in releases){
jsonArrayReleases.put(release.toJSON());
}
return jsonArrayReleases;
}
service createIssueService() {
var jsonobject := JSONObject();
var errors := JSONArray();
var json := JSONObject(readRequestBody());
var project := loadProject(json.getString("project"));
var title := json.getString("title");
var description := json.getString("description") as WikiText;
var tags := Set<Tag>();
if(json.getString("tag").length() > 0) {
tags.add(loadTag(json.getString("tag").parseUUID()));
}
var email := json.getString("email") as Email;
log ("createIssue: project: " + project.name + " title: " + title);
if (project == null) {
errors.put("Non excisting project selected");
}
if (json.getString("tag").length() > 0 && tags.length == 0) {
errors.put("Non excisting category selected");
}if(!email.isValid()) {
errors.put("email is not valid");
}
var ig := IssueGhost{
alive := false
project := project
email := email
description := description
tags := tags
title := title
};
if(ig.validateSave().exceptions.length != 0) {
for(e : ValidationException in ig.validateSave().exceptions) {
errors.put(e.message);
}
}
if (errors.length() == 0) {
ig.save();
if(ig.project.notifications){ email(issueConfirmationEmail(ig)); }
jsonobject.put("answer", true);
} else {
jsonobject.put("errors", errors);
jsonobject.put("id", "CreateIssue: " + title);
jsonobject.put("answer", false);
}
return jsonobject;
}
define page testing() {
var project := loadProject("WebDSL")
// output(project.toJSON().toString())
}
access control rules
rule page testing() {
true
}
rule logsql {
true
}