-
Notifications
You must be signed in to change notification settings - Fork 2
/
user-model.app
173 lines (153 loc) · 5.64 KB
/
user-model.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
module user/user-model
entity User {
name :: String (searchable, validate(name.length() >= 3, "Names need to be at least 3 characters"))
email :: Email (validate(!userEmailTaken(), "Another user already registered using this email address"), validate(email != "", "Enter a valid email address"))
notifications :: Bool
password :: Secret (validate(password.length() >= 8, "Password needs to be at least 8 characters"))
projects -> Set<Project> (inverse = Project.members)
url :: URL
tag :: String (searchable, validate(
tag.length() >= 3 && /[a-z0-9]*/.match(tag),
"User names consist of lowercase characters and numbers. Their minimum length is 3 characters."),
validate(!userTagTaken(), "Another user already registered this user name"))
isAdmin :: Bool (default=false)
function isAdmin() : Bool {
return isAdmin != null && isAdmin;
}
}
section queries
extend entity User {
function reportedIssues() : List<Issue> {
var ids := select i.id
from Issue as i
where _reporter = ~this and _project._private=false //not( project.private)
order by _submitted desc
limit 15;
if(ids.length > 0){
return [i | i in (from Issue where id in ~ids) order by i.submitted desc ];
} else {
return List<Issue>();
}
}
function projects() : List<Project> {
return
select p
from Project as p
left join p.members as m
where _private=false and m = ~this
limit 30;
}
function recentIssues(): List<Issue> {
//Use 2 separate queries to obtain the entities, because selecting the whole ent (including wikitext props)
//in this group-by query causes a temp disk table to be created.
var ids := select i.id from Issue as i
left join i._project as p
where (~this in elements(i._project._members)) or
((i._reporter != null) and (i._reporter._name = ~this.name)) or
(i._email = ~this.email)
order by i._submitted desc
limit 15;
if(ids.length > 0){
return from Issue where id in ~ids;
} else {
return List<Issue>();
}
}
function recentProjects(): List<Project> {
//Use 2 separate queries to obtain the entities, because selecting the whole ent (including wikitext props)
//in this group-by query causes a temp disk table to be created.
var ids := select p.id from Issue as i
left join i._project as p
where i._reporter=~this
group by p
order by max(i._submitted) desc
limit 25;
if(ids.length > 0){
return [p | p in (from Project where id in ~ids) order by p.modified];
} else {
return List<Project>();
}
}
function userEmailTaken() : Bool {
var users := findUserByEmail(email);
return users.length > 1 || (users.length == 1 && users[0] != this); // Database view may be inconsistent, as queries in transaction are take into account
}
function userTagTaken() : Bool {
var users := findUserByTag(tag);
return users.length > 1; // Database view may be inconsistent, as queries in transaction are take into account
}
}
function getUser(u : String) : User {
var users : List<User> :=
from User
where _tag = ~u;
if(users.length == 0) {
return User {};
} else {
return users.get(0);
}
}
section queries
extend entity User {
function toJSON() : JSONObject {
var jsonobject := JSONObject();
jsonobject.put("id", id);
jsonobject.put("name", name);
jsonobject.put("tag", tag);
jsonobject.put("url", url);
jsonobject.put("version", version);
var jsonarray:= JSONArray();
for (project : Project in projects) {
if(project.toSimpleJSON() != null) {
jsonarray.put(project.toJSONRef());
}
}
jsonobject.put("projects", jsonarray);
return jsonobject;
}
function toSimpleJSON() : JSONObject{
var jsonobject := JSONObject();
jsonobject.put("id", id);
jsonobject.put("name", name);
jsonobject.put("version", version);
return jsonobject;
}
}
function abandonUser( u : User, makeProjectsPrivate : Bool, clearComments : Bool ){
var timeStr := ""+now().format("yyyyMMddHHmmss");
var newName := "deleteduser" + timeStr;
message("User ~u.tag has been renamed to ~newName");
u.tag := newName;
u.name := newName;
var pass : Secret := randomUUID().toString();
u.password := pass.digest();
u.notifications := false;
u.url := "";
message("Changed password for ~newName");
if(clearComments){
var comments := from Comment where author = ~u;
for(c in comments){
c.text := "-removed comment-";
}
message("removed text from ~comments.length comments posted by ~newName");
}
u.email := u.email.replace("@", "-at-") + "@" + u.tag + ".yellowgrass.org";
if(makeProjectsPrivate){
for(p in u.projects){
if(p.private == false){
if(p.members.length == 1 && u in p.members){
p.private := true;
message("project ~p.name, which was owned by ~newName, is now set to private");
} else {
message("~newName was also member of a public project: ~p.name");
}
} else {
message("~newName was also member of a private project: ~p.name");
}
}
}
for(s in from SessionManager where securityContext.principal = ~u){
s.securityContext.principal := null;
message("Logged off all sessions for ~newName");
}
}