-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrpc-endpoints.js
175 lines (132 loc) · 4.65 KB
/
rpc-endpoints.js
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
/*******************************************
These RPC methods are available on both the
server and the client. When called on the
server, they are authoritative. When called
on the client, authorization is skipped
where the client doesn't have enough info
to make a judgement call. The server will
take care of it. Meteor handles this for us.
*******************************************/
Meteor.methods({
/*******************************************
* Users
*******************************************/
createUser: function (sessionToken, name, username, password) {
var result;
var user = this.is_simulation ? Users.findOne() : Auth.getUserBySessionToken(sessionToken);
if (!user) {
throw new Meteor.Error(403, "Not authorized to add a user. You must be logged-in to create one.");
}
if (!(result = createUser(name, username, password))) {
throw new Meteor.Error(500, "Unknown error when adding a user.");
}
return result;
},
updateUser: function (sessionToken, user_id, properties) {
var result, existingUser;
var user = this.is_simulation ? Users.findOne() : Auth.getUserBySessionToken(sessionToken);
if (!user) {
throw new Meteor.Error(403, "Not authorized to update a user.");
}
if (user._id !== user_id) {
throw new Meteor.Error(403, "Not authorized to update this particular user (" + user_id + ").");
}
if (properties.username !== undefined) {
existingUser = Users.findOne({username: properties.username});
if (existingUser && existingUser._id !== user_id) {
throw new Meteor.Error(409, "Sorry, that username is unavailable!");
}
}
if (!(result = updateUser(user_id, properties))) {
throw new Meteor.Error(500, "Unknown error when updating a user.");
}
return result;
},
deleteUser: function (sessionToken, user_id) {
var result;
var user = this.is_simulation ? Users.findOne() : Auth.getUserBySessionToken(sessionToken);
if (!user) {
throw new Meteor.Error(403, "Not authorized to remove a user.");
}
if (user._id !== user_id) {
throw new Meteor.Error(403, "Not authorized to remove this particular user (" + user_id + ").");
}
if (!(result = deleteUser(user_id))) {
throw new Meteor.Error(500, "Unknown error when removing a user.");
}
return result;
},
/*******************************************
* Notes
*******************************************/
createNote: function (sessionToken, title, is_private) {
var result;
var user = this.is_simulation ? Users.findOne() : Auth.getUserBySessionToken(sessionToken);
if (!user) {
throw new Meteor.Error(403, "Not authorized to add a note.");
}
if (!(result = createNote(title, user._id, is_private))) {
throw new Meteor.Error(500, "Unknown error when adding a note.");
}
return result;
},
updateNote: function (sessionToken, note_id, properties) {
var result;
var user = this.is_simulation ? Users.findOne() : Auth.getUserBySessionToken(sessionToken);
var note = Notes.findOne({_id: note_id});
if (!user) {
throw new Meteor.Error(403, "Not authorized to update this note.");
}
if (!note) {
throw new Meteor.Error(404, "Note not found.");
}
if (user._id !== note.user_id) {
throw new Meteor.Error(403, "Not authorized to update this note.");
}
if (!(result = updateNote(note_id, properties))) {
throw new Meteor.Error(500, "Unknown error when updating this note.");
}
return result;
},
deleteNote: function (sessionToken, note_id) {
var result;
var user = this.is_simulation ? Users.findOne() : Auth.getUserBySessionToken(sessionToken);
var note = Notes.findOne({_id: note_id});
if (!user) {
throw new Meteor.Error(403, "Not authorized to remove a note.");
}
if (!note) {
throw new Meteor.Error(404, "Note not found.");
}
if (note.user_id !== user._id) {
throw new Meteor.Error(403, "Not authorized to remove this particular note (" + note_id + ").")
}
if (!(result = deleteNote(note_id))) {
throw new Meteor.Error(500, "Unknown error when removing a note.");
}
return result;
},
/*******************************************
* Authentication
*******************************************/
login: function (username, password) {
var sessionToken;
if (!this.is_simulation) {
sessionToken = Auth.getSessionTokenForUsernamePassword(username, password);
if (!sessionToken) {
throw new Meteor.Error(401, "Invalid username and password combination.");
}
return sessionToken;
}
},
logout: function (sessionToken) {
var result;
if (!this.is_simulation) {
result = Auth.clearUserSessions(sessionToken);
if (!result) {
throw new Meteor.Error(412, "Unable to logout: session token not matching a user.");
}
return result;
}
}
});