2013-02-23 04:41:12 +08:00
|
|
|
/**
|
|
|
|
Our data model for dealing with users from the admin section.
|
|
|
|
|
2013-03-06 03:52:35 +08:00
|
|
|
@class AdminUser
|
2013-02-23 04:41:12 +08:00
|
|
|
@extends Discourse.Model
|
|
|
|
@namespace Discourse
|
|
|
|
@module Discourse
|
2013-03-06 03:52:35 +08:00
|
|
|
**/
|
2013-05-08 01:30:12 +08:00
|
|
|
Discourse.AdminUser = Discourse.User.extend({
|
2013-03-06 03:52:35 +08:00
|
|
|
|
2013-02-23 04:41:12 +08:00
|
|
|
deleteAllPosts: function() {
|
2013-04-12 04:04:20 +08:00
|
|
|
var user = this;
|
2013-02-23 04:41:12 +08:00
|
|
|
this.set('can_delete_all_posts', false);
|
2013-05-08 01:30:12 +08:00
|
|
|
Discourse.ajax("/admin/users/" + (this.get('id')) + "/delete_all_posts", {type: 'PUT'}).then(function(result){
|
2013-04-12 04:04:20 +08:00
|
|
|
user.set('post_count', 0);
|
|
|
|
});
|
2013-02-23 04:41:12 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
// Revoke the user's admin access
|
|
|
|
revokeAdmin: function() {
|
|
|
|
this.set('admin', false);
|
|
|
|
this.set('can_grant_admin', true);
|
|
|
|
this.set('can_revoke_admin', false);
|
2013-05-08 01:30:12 +08:00
|
|
|
return Discourse.ajax("/admin/users/" + (this.get('id')) + "/revoke_admin", {type: 'PUT'});
|
2013-02-23 04:41:12 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
grantAdmin: function() {
|
|
|
|
this.set('admin', true);
|
|
|
|
this.set('can_grant_admin', false);
|
|
|
|
this.set('can_revoke_admin', true);
|
2013-05-08 01:30:12 +08:00
|
|
|
Discourse.ajax("/admin/users/" + (this.get('id')) + "/grant_admin", {type: 'PUT'});
|
2013-02-23 04:41:12 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
// Revoke the user's moderation access
|
|
|
|
revokeModeration: function() {
|
|
|
|
this.set('moderator', false);
|
|
|
|
this.set('can_grant_moderation', true);
|
|
|
|
this.set('can_revoke_moderation', false);
|
2013-05-08 01:30:12 +08:00
|
|
|
return Discourse.ajax("/admin/users/" + (this.get('id')) + "/revoke_moderation", {type: 'PUT'});
|
2013-02-23 04:41:12 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
grantModeration: function() {
|
|
|
|
this.set('moderator', true);
|
|
|
|
this.set('can_grant_moderation', false);
|
|
|
|
this.set('can_revoke_moderation', true);
|
2013-05-08 01:30:12 +08:00
|
|
|
Discourse.ajax("/admin/users/" + (this.get('id')) + "/grant_moderation", {type: 'PUT'});
|
2013-02-23 04:41:12 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
refreshBrowsers: function() {
|
2013-05-08 01:30:12 +08:00
|
|
|
Discourse.ajax("/admin/users/" + (this.get('id')) + "/refresh_browsers", {type: 'POST'});
|
2013-02-23 04:41:12 +08:00
|
|
|
bootbox.alert("Message sent to all clients!");
|
|
|
|
},
|
|
|
|
|
|
|
|
approve: function() {
|
|
|
|
this.set('can_approve', false);
|
|
|
|
this.set('approved', true);
|
2013-05-28 23:08:32 +08:00
|
|
|
this.set('approved_by', Discourse.User.current());
|
2013-05-08 01:30:12 +08:00
|
|
|
Discourse.ajax("/admin/users/" + (this.get('id')) + "/approve", {type: 'PUT'});
|
2013-02-23 04:41:12 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
username_lower: (function() {
|
|
|
|
return this.get('username').toLowerCase();
|
|
|
|
}).property('username'),
|
|
|
|
|
2013-05-24 23:02:17 +08:00
|
|
|
trustLevel: function() {
|
|
|
|
var site = Discourse.Site.instance();
|
|
|
|
return site.get('trust_levels').findProperty('id', this.get('trust_level'));
|
|
|
|
}.property('trust_level'),
|
2013-02-23 04:41:12 +08:00
|
|
|
|
2013-03-21 08:25:41 +08:00
|
|
|
isBanned: (function() {
|
|
|
|
return this.get('is_banned') === true;
|
|
|
|
}).property('is_banned'),
|
|
|
|
|
2013-02-23 04:41:12 +08:00
|
|
|
canBan: (function() {
|
2013-03-21 08:25:41 +08:00
|
|
|
return !this.get('admin') && !this.get('moderator');
|
2013-02-23 04:41:12 +08:00
|
|
|
}).property('admin', 'moderator'),
|
|
|
|
|
|
|
|
banDuration: (function() {
|
2013-03-21 08:25:41 +08:00
|
|
|
var banned_at = Date.create(this.banned_at);
|
|
|
|
var banned_till = Date.create(this.banned_till);
|
|
|
|
return banned_at.short() + " - " + banned_till.short();
|
2013-02-23 04:41:12 +08:00
|
|
|
}).property('banned_till', 'banned_at'),
|
|
|
|
|
|
|
|
ban: function() {
|
2013-04-04 04:06:55 +08:00
|
|
|
var duration = parseInt(window.prompt(Em.String.i18n('admin.user.ban_duration')), 10);
|
|
|
|
if (duration > 0) {
|
2013-05-08 01:30:12 +08:00
|
|
|
Discourse.ajax("/admin/users/" + this.id + "/ban", {
|
2013-04-04 04:06:55 +08:00
|
|
|
type: 'PUT',
|
|
|
|
data: {duration: duration}
|
|
|
|
}).then(function () {
|
|
|
|
// succeeded
|
|
|
|
window.location.reload();
|
|
|
|
}, function(e) {
|
|
|
|
// failure
|
|
|
|
var error = Em.String.i18n('admin.user.ban_failed', { error: "http: " + e.status + " - " + e.body });
|
|
|
|
bootbox.alert(error);
|
|
|
|
});
|
2013-02-21 02:15:50 +08:00
|
|
|
}
|
2013-02-23 04:41:12 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
unban: function() {
|
2013-05-08 01:30:12 +08:00
|
|
|
Discourse.ajax("/admin/users/" + this.id + "/unban", {
|
2013-04-04 04:06:55 +08:00
|
|
|
type: 'PUT'
|
|
|
|
}).then(function() {
|
|
|
|
// succeeded
|
|
|
|
window.location.reload();
|
|
|
|
}, function(e) {
|
|
|
|
// failed
|
|
|
|
var error = Em.String.i18n('admin.user.unban_failed', { error: "http: " + e.status + " - " + e.body });
|
|
|
|
bootbox.alert(error);
|
2013-02-23 04:41:12 +08:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
impersonate: function() {
|
2013-05-08 01:30:12 +08:00
|
|
|
Discourse.ajax("/admin/impersonate", {
|
2013-02-23 04:41:12 +08:00
|
|
|
type: 'POST',
|
2013-04-04 04:06:55 +08:00
|
|
|
data: { username_or_email: this.get('username') }
|
|
|
|
}).then(function() {
|
|
|
|
// succeeded
|
|
|
|
document.location = "/";
|
|
|
|
}, function(e) {
|
|
|
|
// failed
|
|
|
|
if (e.status === 404) {
|
|
|
|
bootbox.alert(Em.String.i18n('admin.impersonate.not_found'));
|
|
|
|
} else {
|
|
|
|
bootbox.alert(Em.String.i18n('admin.impersonate.invalid'));
|
2013-02-23 04:41:12 +08:00
|
|
|
}
|
|
|
|
});
|
2013-04-12 04:04:20 +08:00
|
|
|
},
|
|
|
|
|
2013-05-08 09:58:34 +08:00
|
|
|
activate: function() {
|
|
|
|
Discourse.ajax('/admin/users/' + this.id + '/activate', {type: 'PUT'}).then(function() {
|
|
|
|
// succeeded
|
|
|
|
window.location.reload();
|
|
|
|
}, function(e) {
|
|
|
|
// failed
|
|
|
|
var error = Em.String.i18n('admin.user.activate_failed', { error: "http: " + e.status + " - " + e.body });
|
|
|
|
bootbox.alert(error);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
deactivate: function() {
|
|
|
|
Discourse.ajax('/admin/users/' + this.id + '/deactivate', {type: 'PUT'}).then(function() {
|
|
|
|
// succeeded
|
|
|
|
window.location.reload();
|
|
|
|
}, function(e) {
|
|
|
|
// failed
|
|
|
|
var error = Em.String.i18n('admin.user.deactivate_failed', { error: "http: " + e.status + " - " + e.body });
|
|
|
|
bootbox.alert(error);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
sendActivationEmail: function() {
|
|
|
|
Discourse.ajax('/users/' + this.get('username') + '/send_activation_email').then(function() {
|
|
|
|
// succeeded
|
|
|
|
bootbox.alert( Em.String.i18n('admin.user.activation_email_sent') );
|
|
|
|
}, function(e) {
|
|
|
|
// failed
|
|
|
|
var error = Em.String.i18n('admin.user.send_activation_email_failed', { error: "http: " + e.status + " - " + e.body });
|
|
|
|
bootbox.alert(error);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-04-12 04:04:20 +08:00
|
|
|
deleteForbidden: function() {
|
|
|
|
return (this.get('post_count') > 0);
|
|
|
|
}.property('post_count'),
|
|
|
|
|
|
|
|
deleteButtonTitle: function() {
|
|
|
|
if (this.get('deleteForbidden')) {
|
|
|
|
return Em.String.i18n('admin.user.delete_forbidden');
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}.property('deleteForbidden'),
|
|
|
|
|
|
|
|
destroy: function() {
|
|
|
|
var user = this;
|
|
|
|
bootbox.confirm(Em.String.i18n("admin.user.delete_confirm"), Em.String.i18n("no_value"), Em.String.i18n("yes_value"), function(result) {
|
|
|
|
if(result) {
|
2013-05-08 01:30:12 +08:00
|
|
|
Discourse.ajax("/admin/users/" + user.get('id') + '.json', { type: 'DELETE' }).then(function(data) {
|
2013-04-12 04:04:20 +08:00
|
|
|
if (data.deleted) {
|
|
|
|
bootbox.alert(Em.String.i18n("admin.user.deleted"), function() {
|
|
|
|
document.location = "/admin/users/list/active";
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
bootbox.alert(Em.String.i18n("admin.user.delete_failed"));
|
|
|
|
if (data.user) {
|
|
|
|
user.mergeAttributes(data.user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, function(jqXHR, status, error) {
|
|
|
|
Discourse.AdminUser.find( user.get('username') ).then(function(u){ user.mergeAttributes(u); });
|
|
|
|
bootbox.alert(Em.String.i18n("admin.user.delete_failed"));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2013-02-23 04:41:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-03-06 03:52:35 +08:00
|
|
|
Discourse.AdminUser.reopenClass({
|
2013-02-23 04:41:12 +08:00
|
|
|
|
|
|
|
bulkApprove: function(users) {
|
|
|
|
users.each(function(user) {
|
|
|
|
user.set('approved', true);
|
|
|
|
user.set('can_approve', false);
|
|
|
|
return user.set('selected', false);
|
|
|
|
});
|
2013-05-08 01:30:12 +08:00
|
|
|
return Discourse.ajax("/admin/users/approve-bulk", {
|
2013-02-23 04:41:12 +08:00
|
|
|
type: 'PUT',
|
|
|
|
data: {
|
|
|
|
users: users.map(function(u) {
|
|
|
|
return u.id;
|
|
|
|
})
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
find: function(username) {
|
2013-05-08 01:30:12 +08:00
|
|
|
return Discourse.ajax("/admin/users/" + username).then(function (result) {
|
2013-03-15 02:45:29 +08:00
|
|
|
return Discourse.AdminUser.create(result);
|
2013-04-12 04:04:20 +08:00
|
|
|
});
|
2013-02-23 04:41:12 +08:00
|
|
|
},
|
|
|
|
|
2013-05-07 23:15:28 +08:00
|
|
|
findAll: function(query, filter) {
|
2013-05-08 01:30:12 +08:00
|
|
|
return Discourse.ajax("/admin/users/list/" + query + ".json", {
|
2013-04-04 04:06:55 +08:00
|
|
|
data: { filter: filter }
|
|
|
|
}).then(function(users) {
|
2013-05-07 23:15:28 +08:00
|
|
|
return users.map(function(u) {
|
|
|
|
return Discourse.AdminUser.create(u);
|
2013-04-04 04:06:55 +08:00
|
|
|
});
|
2013-02-23 04:41:12 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|