forked from c9/cloud9
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.js
More file actions
148 lines (123 loc) · 3.91 KB
/
Copy pathuser.js
File metadata and controls
148 lines (123 loc) · 3.91 KB
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
var sys = require("sys");
var lang = require("pilot/lang");
var EventEmitter = require("events").EventEmitter;
var User = function (uid, permissions, data) {
EventEmitter.call(this);
// TODO deprecated
Object.defineProperty(this, "name",
{get: function() { console.log("DEPRECATED: name use uid"); console.trace(); return uid; }}
);
this.uid = uid;
this.permissions = permissions;
this.data = data;
this.clients = [];
this.last_message_time = new Date().getTime();
this.$server_exclude = {};
};
sys.inherits(User, EventEmitter);
User.OWNER_PERMISSIONS = {
client_exclude: "",
server_exclude: "",
dav: "rw"
};
User.COLLABORATOR_PERMISSIONS = {
client_exclude: "",
server_exclude: "git",
dav: "rw"
};
User.VISITOR_PERMISSIONS = {
client_exclude: [
"ext/save/save",
"ext/newresource/newresource",
"ext/undo/undo",
"ext/searchreplace/searchreplace",
"ext/quickwatch/quickwatch",
"ext/extmgr/extmgr",
"ext/run/run", //Add location rule
"ext/debugger/debugger", //Add location rule
"ext/noderunner/noderunner", //Add location rule
"ext/watcher/watcher",
"c9/ext/projectinfo/projectinfo",
"ext/tabbehaviors/tabbehaviors"
].join("|"),
server_exclude: [
"git",
"debugger",
"shell",
"runvm"
].join("|"),
dav: "ro"
};
(function() {
this.setPermissions = function(permissions) {
this.$server_exclude = lang.arrayToMap(permissions.server_exclude.split("|"));
this.permissions = permissions;
this.emit("changePermissions", this);
};
this.getPermissions = function(permissions) {
return this.permissions;
};
this.addClientConnection = function(client, message) {
var id = client.sessionId;
if (this.clients[id] === client)
return;
this.clients[id] = client;
this.onClientCountChange();
var _self = this;
client.on("message", function(message) {
_self.onClientMessage(message, client);
});
client.on("disconnect", function() {
_self.emit("disconnectClient", {
user: _self,
client: client
});
delete _self.clients[client.sessionId];
_self.onClientCountChange();
});
if (message)
_self.onClientMessage(message, client);
};
this.onClientMessage = function(message, client) {
try {
if (typeof message == "string")
message = JSON.parse(message);
} catch (e) {
return this.error("Error parsing message: " + e + "\nmessage: " + message, 8);
}
this.emit("message", {
message: message,
user: this,
client: client
});
};
this.onClientCountChange = function() {
var count = Object.keys(this.clients).length;
this.emit("clientCountChange", count);
if (count == 0) {
this.dconn_time = new Date().getTime();
this.emit("disconnectUser", this);
}
};
this.error = function(description, code, message, client) {
//console.log("Socket error: " + description, new Error().stack);
var sid = (message || {}).sid || -1;
var error = JSON.stringify({
"type": "error",
"sid": sid,
"code": code,
"message": description
});
if (client)
client.send(error);
else
this.broadcast(error);
};
this.broadcast = function(msg, scope) {
if (scope && this.$server_exclude[scope])
return;
for (var id in this.clients)
this.clients[id].send(msg);
};
}).call(User.prototype);
module.exports = User;