chg: refactored site deletion with sql

This commit is contained in:
Tobi Schäfer 2018-05-20 13:16:04 +02:00
parent 70d8c67f07
commit 5683b35d12
3 changed files with 54 additions and 32 deletions

View file

@ -108,7 +108,8 @@ HopObject.prototype.onRequest = function() {
User.autoLogin();
res.handlers.membership = User.getMembership();
if (session.user && !session.user.deleted && session.user.status === User.DELETED) {
// Logout persisting session if account has been deleted
if (User.getCurrentStatus() === User.DELETED && !session.user.deleted) {
User.logout();
}
@ -121,20 +122,22 @@ HopObject.prototype.onRequest = function() {
res.stop();
}
// Simulate 404 for sites which are due for deletion by cronjob
if (res.handlers.site.mode === Site.DELETED && !User.require(User.PRIVILEGED) && !Membership.require(Membership.OWNER)) {
res.handlers.site = root;
root.notfound_action();
res.stop();
}
if (!User.require(User.PRIVILEGED)) {
// Simulate 404 for sites which are due for deletion by cronjob
if (res.handlers.site.mode === Site.DELETED) {
res.handlers.site = root;
root.notfound_action();
res.stop();
}
if (res.handlers.site.status === Site.BLOCKED && !User.require(User.PRIVILEGED)) {
res.status = 403;
res.handlers.site = root;
res.data.error = gettext('The site you requested has been blocked.') +
String.SPACE + gettext('Please contact an administrator for further information.');
root.error_action();
res.stop();
if (res.handlers.site.status === Site.BLOCKED) {
res.status = 403;
res.handlers.site = root;
res.data.error = gettext('The site you requested has been blocked.') +
String.SPACE + gettext('Please contact an administrator for further information.');
root.error_action();
res.stop();
}
}
HopObject.confirmConstructor(Layout);
@ -178,8 +181,9 @@ HopObject.prototype.delete_action = function() {
if (req.postParams.proceed) {
try {
var parent = this._parent;
var type = this._prototype;
var url = this.constructor.remove.call(this, req.postParams) || parent.href();
res.message = gettext('{0} was successfully deleted.', gettext(this._prototype));
res.message = gettext('{0} was successfully deleted.', gettext(type));
res.redirect(User.getLocation() || url);
} catch(ex) {
res.message = ex;

View file

@ -45,12 +45,12 @@ HopObject.prototype.handleMetadata = function(name) {
*/
HopObject.prototype.getMetadata = function(name) {
if (!this.metadata) {
throw Error('No metadata collection defined for prototype ' + this.constructor.name);
} else {
this.metadata.prefetchChildren();
app.log('No metadata collection defined for prototype ' + this.constructor.name);
return name ? null : {};
}
var self = this;
this.metadata.prefetchChildren();
if (!name) {
var result = {};

View file

@ -163,20 +163,38 @@ Site.add = function(data, user) {
* @param {Site} site
*/
Site.remove = function() {
if (this.constructor === Site || this === root) {
HopObject.remove.call(this.stories);
HopObject.remove.call(this.images);
HopObject.remove.call(this.files);
HopObject.remove.call(this.polls);
HopObject.remove.call(this.entries);
HopObject.remove.call(this.members, {force: true});
Layout.remove.call(this.layout, {force: true});
this.getStaticFile().removeDirectory();
this.deleteMetadata();
this.remove();
}
return;
}
if (this.constructor !== Site || this === root) return;
const sql = new Sql();
const id = this._id;
const dir = this.getStaticFile();
this.remove();
root.cache.sites = null;
sql.execute('delete from site where id = $0', id);
sql.execute('delete from membership where site_id = $0', id);
sql.execute('delete from content where site_id = $0', id);
sql.execute('delete from file where site_id = $0', id);
sql.execute("delete from image where parent_type = 'Site' and parent_id = $0", id);
sql.execute("delete from log where context_type = 'Site' and context_id = $0", id);
sql.execute("delete from metadata where parent_type = 'Site' and parent_id = $0", id);
sql.execute('delete from skin where layout_id in (select id from layout where site_id = $0)', id);
sql.execute('delete from layout where site_id = $0', id);
sql.execute('delete from tag_hub where tag_id in (select id from tag where site_id = $0)', id);
sql.execute('delete from tag where site_id = $0', id);
let subQuery = 'select id from poll where site_id';
sql.execute('delete from vote where choice_id in (select id from choice where poll_id in ($0 = $1))', subQuery, id);
sql.execute('delete from choice where poll_id in ($0 = $1)', subQuery, id);
sql.execute('delete from poll where site_id = $0', id);
dir.removeDirectory();
};
/**
*