-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path00-users.js
138 lines (119 loc) · 4 KB
/
00-users.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
if (Meteor.isServer) {
// Publish no data unless you're logged in.
Jigsaw.publish = function (name, f) {
Meteor.publish(name, function () {
if (this.userId) {
return f.apply(this, arguments);
} else {
this.complete();
return null;
}
});
};
Jigsaw.publish("directory", function () {
return Meteor.users.find({}, {username: 1});
});
var logInAsUsername = function (username) {
var user = Meteor.users.findOne({username: username});
if (user) {
var stampedLoginToken = Accounts._generateStampedLoginToken();
Meteor.users.update(
user._id, {$push: {'services.resume.loginTokens': stampedLoginToken}});
return {token: stampedLoginToken.token, id: user._id};
} else {
user = {username: username};
return Accounts.insertUserDoc({generateLoginToken: true}, user);
}
};
Accounts.registerLoginHandler(function (options) {
if (!options.changeUsername)
return undefined; // don't handle
// This can only be used to CHANGE user, not log in initially.
if (!Meteor.userId())
throw new Meteor.Error('Not already logged in!');
return logInAsUsername(options.changeUsername);
});
Accounts.registerLoginHandler(function (options) {
if (!options.globalPassword)
return undefined;
// This is just used for initial login, not changing username.
if (Meteor.userId())
throw new Meteor.Error('Already logged in!');
if (options.globalPassword !== (Meteor.settings.globalPassword || 'secret'))
throw new Meteor.Error('Wrong password!');
return logInAsUsername('nobody');
});
Facts.setUserIdFilter(function (userId) {
return !!userId;
});
} else {
Meteor.subscribe("directory");
Jigsaw.changeUsername = function (username, callback) {
Accounts.callLoginMethod({
methodArguments: [{changeUsername: username}],
userCallback: callback
});
};
Jigsaw.logInWithGlobalPassword = function (password, callback) {
Accounts.callLoginMethod({
methodArguments: [{globalPassword: password}],
userCallback: callback
});
};
var changeUsername = function (template) {
var username = template.find('#username-other').value ||
DomUtils.getElementValue(template.find('#directory'));
if (username) {
Jigsaw.changeUsername(username, function (err) {
// XXX deal with err
reactivelyShow('userDirectory', false);
});
}
};
Meteor.autorun(function () {
// If logged in as nobody, always show the directory.
if (Meteor.user() && Meteor.user().username === 'nobody')
reactivelyShow('userDirectory', true);
});
Template.userPanel.users = function () {
return Meteor.users.find({}, {sort: ['username']});
};
Template.userPanel.current = function () {
return Meteor.user() && this.username === Meteor.user().username;
};
Handlebars.registerHelper("isNobody", function () {
return Meteor.user().username === 'nobody';
});
Template.userPanel.events({
'click #show-change-username': function () {
reactivelyShow('userDirectory', true);
},
'keydown #username-other': function (event, template) {
if (event.which === 13)
changeUsername(template);
},
'click #change-username': function (event, template) {
changeUsername(template);
}
});
Template.passwordForm.events({
'keydown #globalPassword': function (event, template) {
if (event.which === 13)
Jigsaw.logInWithGlobalPassword(event.target.value);
}
});
Template.body.showFacts = function () {
return Session.get('showFacts');
};
Template.navbar.siteTitle = function () {
return getPublicConfig('siteTitle') || 'Jigsaw';
};
Template.recent.activePuzzles = function () {
var statusFamily = Families.findOne({name: 'Status'});
if (!statusFamily)
return [];
var query = {tags: {$nin: ['deleted']}};
query['families.' + statusFamily._id] = {$ne: 'Solved'};
return Puzzles.find(query, {sort: {pinged: -1}});
};
}