2013-02-22 15:41:12 -05:00
|
|
|
/**
|
|
|
|
Our data model for dealing with users from the admin section.
|
|
|
|
|
2013-03-05 14:52:35 -05:00
|
|
|
@class AdminUser
|
2013-02-22 15:41:12 -05:00
|
|
|
@extends Discourse.Model
|
|
|
|
@namespace Discourse
|
|
|
|
@module Discourse
|
2013-03-05 14:52:35 -05:00
|
|
|
**/
|
2013-05-07 13:30:12 -04:00
|
|
|
Discourse.AdminUser = Discourse.User.extend({
|
2013-03-05 14:52:35 -05:00
|
|
|
|
2013-02-22 15:41:12 -05:00
|
|
|
deleteAllPosts: function() {
|
2013-04-11 16:04:20 -04:00
|
|
|
var user = this;
|
2013-02-22 15:41:12 -05:00
|
|
|
this.set('can_delete_all_posts', false);
|
2013-05-07 13:30:12 -04:00
|
|
|
Discourse.ajax("/admin/users/" + (this.get('id')) + "/delete_all_posts", {type: 'PUT'}).then(function(result){
|
2013-04-11 16:04:20 -04:00
|
|
|
user.set('post_count', 0);
|
|
|
|
});
|
2013-02-22 15:41:12 -05: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-07 13:30:12 -04:00
|
|
|
return Discourse.ajax("/admin/users/" + (this.get('id')) + "/revoke_admin", {type: 'PUT'});
|
2013-02-22 15:41:12 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
grantAdmin: function() {
|
|
|
|
this.set('admin', true);
|
|
|
|
this.set('can_grant_admin', false);
|
|
|
|
this.set('can_revoke_admin', true);
|
2013-05-07 13:30:12 -04:00
|
|
|
Discourse.ajax("/admin/users/" + (this.get('id')) + "/grant_admin", {type: 'PUT'});
|
2013-02-22 15:41:12 -05: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-07 13:30:12 -04:00
|
|
|
return Discourse.ajax("/admin/users/" + (this.get('id')) + "/revoke_moderation", {type: 'PUT'});
|
2013-02-22 15:41:12 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
grantModeration: function() {
|
|
|
|
this.set('moderator', true);
|
|
|
|
this.set('can_grant_moderation', false);
|
|
|
|
this.set('can_revoke_moderation', true);
|
2013-05-07 13:30:12 -04:00
|
|
|
Discourse.ajax("/admin/users/" + (this.get('id')) + "/grant_moderation", {type: 'PUT'});
|
2013-02-22 15:41:12 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
refreshBrowsers: function() {
|
2013-05-07 13:30:12 -04:00
|
|
|
Discourse.ajax("/admin/users/" + (this.get('id')) + "/refresh_browsers", {type: 'POST'});
|
2013-02-22 15:41:12 -05:00
|
|
|
bootbox.alert("Message sent to all clients!");
|
|
|
|
},
|
|
|
|
|
|
|
|
approve: function() {
|
|
|
|
this.set('can_approve', false);
|
|
|
|
this.set('approved', true);
|
2013-05-28 11:08:32 -04:00
|
|
|
this.set('approved_by', Discourse.User.current());
|
2013-05-07 13:30:12 -04:00
|
|
|
Discourse.ajax("/admin/users/" + (this.get('id')) + "/approve", {type: 'PUT'});
|
2013-02-22 15:41:12 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
username_lower: (function() {
|
|
|
|
return this.get('username').toLowerCase();
|
|
|
|
}).property('username'),
|
|
|
|
|
2013-05-24 11:02:17 -04:00
|
|
|
trustLevel: function() {
|
|
|
|
var site = Discourse.Site.instance();
|
|
|
|
return site.get('trust_levels').findProperty('id', this.get('trust_level'));
|
|
|
|
}.property('trust_level'),
|
2013-02-22 15:41:12 -05:00
|
|
|
|
2013-07-04 07:01:01 -04:00
|
|
|
setOriginalTrustLevel: function() {
|
|
|
|
this.set('originalTrustLevel', this.get('trust_level'));
|
|
|
|
},
|
|
|
|
|
2013-07-01 10:22:21 -04:00
|
|
|
trustLevels: function() {
|
|
|
|
var site = Discourse.Site.instance();
|
2013-07-03 04:27:40 -04:00
|
|
|
return site.get('trust_levels');
|
2013-07-01 10:22:21 -04:00
|
|
|
}.property('trust_level'),
|
|
|
|
|
|
|
|
dirty: function() {
|
2013-07-04 03:32:12 -04:00
|
|
|
return this.get('originalTrustLevel') !== parseInt(this.get('trustLevel.id'), 10);
|
|
|
|
}.property('originalTrustLevel', 'trustLevel.id'),
|
2013-07-01 10:22:21 -04:00
|
|
|
|
|
|
|
saveTrustLevel: function() {
|
|
|
|
Discourse.ajax("/admin/users/" + this.id + "/trust_level", {
|
|
|
|
type: 'PUT',
|
|
|
|
data: {level: this.get('trustLevel.id')}
|
|
|
|
}).then(function () {
|
|
|
|
// succeeded
|
|
|
|
window.location.reload();
|
|
|
|
}, function(e) {
|
|
|
|
// failure
|
2013-07-08 19:32:16 -04:00
|
|
|
var error = I18n.t('admin.user.trust_level_change_failed', { error: "http: " + e.status + " - " + e.body });
|
2013-07-01 10:22:21 -04:00
|
|
|
bootbox.alert(error);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
restoreTrustLevel: function() {
|
2013-07-04 03:32:12 -04:00
|
|
|
this.set('trustLevel.id', this.get('originalTrustLevel'));
|
2013-07-01 10:22:21 -04:00
|
|
|
},
|
|
|
|
|
2013-07-11 19:35:52 -04:00
|
|
|
isBanned: Em.computed.equal('is_banned', true),
|
|
|
|
canBan: Em.computed.not('staff'),
|
2013-03-20 20:25:41 -04:00
|
|
|
|
2013-07-11 19:35:52 -04:00
|
|
|
banDuration: function() {
|
2013-06-10 16:48:50 -04:00
|
|
|
var banned_at = moment(this.banned_at);
|
|
|
|
var banned_till = moment(this.banned_till);
|
|
|
|
return banned_at.format('L') + " - " + banned_till.format('L');
|
2013-07-11 19:35:52 -04:00
|
|
|
}.property('banned_till', 'banned_at'),
|
2013-02-22 15:41:12 -05:00
|
|
|
|
|
|
|
ban: function() {
|
2013-07-08 19:32:16 -04:00
|
|
|
var duration = parseInt(window.prompt(I18n.t('admin.user.ban_duration')), 10);
|
2013-04-03 16:06:55 -04:00
|
|
|
if (duration > 0) {
|
2013-05-07 13:30:12 -04:00
|
|
|
Discourse.ajax("/admin/users/" + this.id + "/ban", {
|
2013-04-03 16:06:55 -04:00
|
|
|
type: 'PUT',
|
|
|
|
data: {duration: duration}
|
|
|
|
}).then(function () {
|
|
|
|
// succeeded
|
|
|
|
window.location.reload();
|
|
|
|
}, function(e) {
|
|
|
|
// failure
|
2013-07-08 19:32:16 -04:00
|
|
|
var error = I18n.t('admin.user.ban_failed', { error: "http: " + e.status + " - " + e.body });
|
2013-04-03 16:06:55 -04:00
|
|
|
bootbox.alert(error);
|
|
|
|
});
|
2013-02-20 13:15:50 -05:00
|
|
|
}
|
2013-02-22 15:41:12 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
unban: function() {
|
2013-05-07 13:30:12 -04:00
|
|
|
Discourse.ajax("/admin/users/" + this.id + "/unban", {
|
2013-04-03 16:06:55 -04:00
|
|
|
type: 'PUT'
|
|
|
|
}).then(function() {
|
|
|
|
// succeeded
|
|
|
|
window.location.reload();
|
|
|
|
}, function(e) {
|
|
|
|
// failed
|
2013-07-08 19:32:16 -04:00
|
|
|
var error = I18n.t('admin.user.unban_failed', { error: "http: " + e.status + " - " + e.body });
|
2013-04-03 16:06:55 -04:00
|
|
|
bootbox.alert(error);
|
2013-02-22 15:41:12 -05:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
impersonate: function() {
|
2013-05-07 13:30:12 -04:00
|
|
|
Discourse.ajax("/admin/impersonate", {
|
2013-02-22 15:41:12 -05:00
|
|
|
type: 'POST',
|
2013-04-03 16:06:55 -04:00
|
|
|
data: { username_or_email: this.get('username') }
|
|
|
|
}).then(function() {
|
|
|
|
// succeeded
|
|
|
|
document.location = "/";
|
|
|
|
}, function(e) {
|
|
|
|
// failed
|
|
|
|
if (e.status === 404) {
|
2013-07-08 19:32:16 -04:00
|
|
|
bootbox.alert(I18n.t('admin.impersonate.not_found'));
|
2013-04-03 16:06:55 -04:00
|
|
|
} else {
|
2013-07-08 19:32:16 -04:00
|
|
|
bootbox.alert(I18n.t('admin.impersonate.invalid'));
|
2013-02-22 15:41:12 -05:00
|
|
|
}
|
|
|
|
});
|
2013-04-11 16:04:20 -04:00
|
|
|
},
|
|
|
|
|
2013-05-07 21:58:34 -04:00
|
|
|
activate: function() {
|
|
|
|
Discourse.ajax('/admin/users/' + this.id + '/activate', {type: 'PUT'}).then(function() {
|
|
|
|
// succeeded
|
|
|
|
window.location.reload();
|
|
|
|
}, function(e) {
|
|
|
|
// failed
|
2013-07-08 19:32:16 -04:00
|
|
|
var error = I18n.t('admin.user.activate_failed', { error: "http: " + e.status + " - " + e.body });
|
2013-05-07 21:58:34 -04:00
|
|
|
bootbox.alert(error);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
deactivate: function() {
|
|
|
|
Discourse.ajax('/admin/users/' + this.id + '/deactivate', {type: 'PUT'}).then(function() {
|
|
|
|
// succeeded
|
|
|
|
window.location.reload();
|
|
|
|
}, function(e) {
|
|
|
|
// failed
|
2013-07-08 19:32:16 -04:00
|
|
|
var error = I18n.t('admin.user.deactivate_failed', { error: "http: " + e.status + " - " + e.body });
|
2013-05-07 21:58:34 -04:00
|
|
|
bootbox.alert(error);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-05-31 11:41:40 -04:00
|
|
|
unblock: function() {
|
|
|
|
Discourse.ajax('/admin/users/' + this.id + '/unblock', {type: 'PUT'}).then(function() {
|
|
|
|
// succeeded
|
|
|
|
window.location.reload();
|
|
|
|
}, function(e) {
|
|
|
|
// failed
|
2013-07-08 19:32:16 -04:00
|
|
|
var error = I18n.t('admin.user.unblock_failed', { error: "http: " + e.status + " - " + e.body });
|
2013-05-31 11:41:40 -04:00
|
|
|
bootbox.alert(error);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
block: function() {
|
|
|
|
Discourse.ajax('/admin/users/' + this.id + '/block', {type: 'PUT'}).then(function() {
|
|
|
|
// succeeded
|
|
|
|
window.location.reload();
|
|
|
|
}, function(e) {
|
|
|
|
// failed
|
2013-07-08 19:32:16 -04:00
|
|
|
var error = I18n.t('admin.user.block_failed', { error: "http: " + e.status + " - " + e.body });
|
2013-05-31 11:41:40 -04:00
|
|
|
bootbox.alert(error);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-05-07 21:58:34 -04:00
|
|
|
sendActivationEmail: function() {
|
2013-07-05 12:26:46 -04:00
|
|
|
Discourse.ajax('/users/' + this.get('username') + '/send_activation_email', {type: 'POST'}).then(function() {
|
2013-05-07 21:58:34 -04:00
|
|
|
// succeeded
|
2013-07-08 19:32:16 -04:00
|
|
|
bootbox.alert( I18n.t('admin.user.activation_email_sent') );
|
2013-05-07 21:58:34 -04:00
|
|
|
}, function(e) {
|
|
|
|
// failed
|
2013-07-08 19:32:16 -04:00
|
|
|
var error = I18n.t('admin.user.send_activation_email_failed', { error: "http: " + e.status + " - " + e.body });
|
2013-05-07 21:58:34 -04:00
|
|
|
bootbox.alert(error);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-04-11 16:04:20 -04:00
|
|
|
deleteForbidden: function() {
|
|
|
|
return (this.get('post_count') > 0);
|
|
|
|
}.property('post_count'),
|
|
|
|
|
|
|
|
deleteButtonTitle: function() {
|
|
|
|
if (this.get('deleteForbidden')) {
|
2013-07-08 19:32:16 -04:00
|
|
|
return I18n.t('admin.user.delete_forbidden');
|
2013-04-11 16:04:20 -04:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}.property('deleteForbidden'),
|
|
|
|
|
|
|
|
destroy: function() {
|
|
|
|
var user = this;
|
2013-07-08 19:32:16 -04:00
|
|
|
bootbox.confirm(I18n.t("admin.user.delete_confirm"), I18n.t("no_value"), I18n.t("yes_value"), function(result) {
|
2013-04-11 16:04:20 -04:00
|
|
|
if(result) {
|
2013-05-07 13:30:12 -04:00
|
|
|
Discourse.ajax("/admin/users/" + user.get('id') + '.json', { type: 'DELETE' }).then(function(data) {
|
2013-04-11 16:04:20 -04:00
|
|
|
if (data.deleted) {
|
2013-07-08 19:32:16 -04:00
|
|
|
bootbox.alert(I18n.t("admin.user.deleted"), function() {
|
2013-04-11 16:04:20 -04:00
|
|
|
document.location = "/admin/users/list/active";
|
|
|
|
});
|
|
|
|
} else {
|
2013-07-08 19:32:16 -04:00
|
|
|
bootbox.alert(I18n.t("admin.user.delete_failed"));
|
2013-04-11 16:04:20 -04:00
|
|
|
if (data.user) {
|
|
|
|
user.mergeAttributes(data.user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, function(jqXHR, status, error) {
|
|
|
|
Discourse.AdminUser.find( user.get('username') ).then(function(u){ user.mergeAttributes(u); });
|
2013-07-08 19:32:16 -04:00
|
|
|
bootbox.alert(I18n.t("admin.user.delete_failed"));
|
2013-04-11 16:04:20 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2013-06-13 13:46:50 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
loadDetails: function() {
|
|
|
|
var model = this;
|
|
|
|
if (model.get('loadedDetails')) { return; }
|
|
|
|
|
|
|
|
Discourse.AdminUser.find(model.get('username_lower')).then(function (result) {
|
|
|
|
model.setProperties(result);
|
|
|
|
model.set('loadedDetails', true);
|
|
|
|
});
|
2013-02-22 15:41:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-03-05 14:52:35 -05:00
|
|
|
Discourse.AdminUser.reopenClass({
|
2013-02-22 15:41:12 -05:00
|
|
|
|
|
|
|
bulkApprove: function(users) {
|
2013-06-26 13:24:30 -04:00
|
|
|
_.each(users, function(user) {
|
2013-02-22 15:41:12 -05:00
|
|
|
user.set('approved', true);
|
|
|
|
user.set('can_approve', false);
|
|
|
|
return user.set('selected', false);
|
|
|
|
});
|
2013-06-06 00:18:22 -04:00
|
|
|
|
2013-07-08 19:32:16 -04:00
|
|
|
bootbox.alert(I18n.t("admin.user.approve_bulk_success"));
|
2013-06-06 00:18:22 -04:00
|
|
|
|
2013-05-07 13:30:12 -04:00
|
|
|
return Discourse.ajax("/admin/users/approve-bulk", {
|
2013-02-22 15:41:12 -05:00
|
|
|
type: 'PUT',
|
|
|
|
data: {
|
|
|
|
users: users.map(function(u) {
|
|
|
|
return u.id;
|
|
|
|
})
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
find: function(username) {
|
2013-05-07 13:30:12 -04:00
|
|
|
return Discourse.ajax("/admin/users/" + username).then(function (result) {
|
2013-06-13 13:46:50 -04:00
|
|
|
result.loadedDetails = true;
|
2013-03-14 14:45:29 -04:00
|
|
|
return Discourse.AdminUser.create(result);
|
2013-04-11 16:04:20 -04:00
|
|
|
});
|
2013-02-22 15:41:12 -05:00
|
|
|
},
|
|
|
|
|
2013-05-07 11:15:28 -04:00
|
|
|
findAll: function(query, filter) {
|
2013-05-07 13:30:12 -04:00
|
|
|
return Discourse.ajax("/admin/users/list/" + query + ".json", {
|
2013-04-03 16:06:55 -04:00
|
|
|
data: { filter: filter }
|
|
|
|
}).then(function(users) {
|
2013-05-07 11:15:28 -04:00
|
|
|
return users.map(function(u) {
|
|
|
|
return Discourse.AdminUser.create(u);
|
2013-04-03 16:06:55 -04:00
|
|
|
});
|
2013-02-22 15:41:12 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|